| 1 |
/* |
| 2 |
Analog Input |
| 3 |
|
| 4 |
Demonstrates analog input by reading an analog sensor on analog pin 0 and |
| 5 |
turning on and off a light emitting diode(LED) connected to digital pin 13. |
| 6 |
The amount of time the LED will be on and off depends on the value obtained |
| 7 |
by analogRead(). |
| 8 |
|
| 9 |
The circuit: |
| 10 |
- potentiometer |
| 11 |
center pin of the potentiometer to the analog input 0 |
| 12 |
one side pin (either one) to ground |
| 13 |
the other side pin to +5V |
| 14 |
- LED |
| 15 |
anode (long leg) attached to digital output 13 through 220 ohm resistor |
| 16 |
cathode (short leg) attached to ground |
| 17 |
|
| 18 |
- Note: because most Arduinos have a built-in LED attached to pin 13 on the |
| 19 |
board, the LED is optional. |
| 20 |
|
| 21 |
created by David Cuartielles |
| 22 |
modified 30 Aug 2011 |
| 23 |
By Tom Igoe |
| 24 |
|
| 25 |
This example code is in the public domain. |
| 26 |
|
| 27 |
https://www.arduino.cc/en/Tutorial/BuiltInExamples/AnalogInput |
| 28 |
*/ |
| 29 |
|
| 30 |
int sensorPin = A0; // select the input pin for the potentiometer |
| 31 |
int ledPin = 13; // select the pin for the LED |
| 32 |
int sensorValue = 0; // variable to store the value coming from the sensor |
| 33 |
|
| 34 |
void setup() { |
| 35 |
// declare the ledPin as an OUTPUT: |
| 36 |
pinMode(ledPin, OUTPUT); |
| 37 |
} |
| 38 |
|
| 39 |
void loop() { |
| 40 |
// read the value from the sensor: |
| 41 |
sensorValue = analogRead(sensorPin); |
| 42 |
// turn the ledPin on |
| 43 |
digitalWrite(ledPin, HIGH); |
| 44 |
// stop the program for <sensorValue> milliseconds: |
| 45 |
delay(sensorValue); |
| 46 |
// turn the ledPin off: |
| 47 |
digitalWrite(ledPin, LOW); |
| 48 |
// stop the program for for <sensorValue> milliseconds: |
| 49 |
delay(sensorValue); |
| 50 |
} |