Tutorial.AnalogInput History

Show minor edits - Show changes to markup

August 27, 2015, at 03:06 PM by Simone Maiocchi -
Changed line 89 from:
  • AnalogWriteMega - Fade 12 LEDs on and o¬, one by one, using an Arduino Mega board.
to:
  • AnalogWriteMega - Fade 12 LEDs on and off, one by one, using an Arduino or Genuino Mega board.
August 27, 2015, at 03:04 PM by Simone Maiocchi -
Changed lines 1-6 from:
to:
Added lines 21-22:
Changed line 28 from:
to:
Added lines 30-31:
Changed line 65 from:
to:
Changed lines 88-92 from:
  • AnalogReadSerial - Read a potentiometer, print it's state to the serial monitor
  • AnalogInOutSerial - Read an analog input, map its values, and then use that information to dim or brighten an LED.
  • Fade - Use an analog input to fade an LED
  • Calibration - Calibrating analog sensor readings
to:
  • AnalogInOutSerial - Read an analog input pin, map the result, and then use that data to dim or brighten an LED.
  • AnalogWriteMega - Fade 12 LEDs on and o¬, one by one, using an Arduino Mega board.
  • Calibration - Define a maximum and minimum for expected analog sensor values.
  • Fading - Use an analog output (PWM pin) to fade an LED.
  • Smoothing - Smooth multiple readings of an analog input.
July 28, 2015, at 03:13 PM by Simone Maiocchi -
Added lines 80-81:


Last revision 2015/07/28 by SM

July 28, 2015, at 03:11 PM by Simone Maiocchi -
Changed lines 57-60 from:

The analogRead() command converts the input voltage range, 0 to 5 volts, to a digital value between 0 and 1023. This is done by a circuit inside the microcontroller called an analog-to-digital converter or ADC.

By turning the shaft of the potentiometer, you change the amount of resistance on either side of the center pin (or wiper) of the potentiometer. This changes the relative resistances between the center pin and the two outside pins, giving you a different voltage at the analog input. When the shaft is turned all the way in one direction, there is no resistance between the center pin and the pin connected to ground. The voltage at the center pin then is 0 volts, and analogRead() returns 0. When the shaft is turned all the way in the other direction, there is no resistance between the center pin and the pin connected to +5 volts. The voltage at the center pin then is 5 volts, and analogRead() returns 1023. In between, analogRead() returns a number between 0 and 1023 that is proportional to the amount of voltage being applied to the pin.

to:

The analogRead() command converts the input voltage range, 0 to 5 volts, to a digital value between 0 and 1023. This is done by a circuit inside the microcontroller called an analog-to-digital converter or ADC.

By turning the shaft of the potentiometer, you change the amount of resistance on either side of the center pin (or wiper) of the potentiometer. This changes the relative resistances between the center pin and the two outside pins, giving you a different voltage at the analog input. When the shaft is turned all the way in one direction, there is no resistance between the center pin and the pin connected to ground. The voltage at the center pin then is 0 volts, and analogRead() returns 0. When the shaft is turned all the way in the other direction, there is no resistance between the center pin and the pin connected to +5 volts. The voltage at the center pin then is 5 volts, and analogRead() returns 1023. In between, analogRead() returns a number between 0 and 1023 that is proportional to the amount of voltage being applied to the pin.

July 28, 2015, at 03:03 PM by Simone Maiocchi -
Changed lines 31-32 from:

Connect three wires to the Arduino board. The first goes to ground from one of the outer pins of the potentiometer. The second goes from 5 volts to the other outer pin of the potentiometer. The third goes from analog input 0 to the middle pin of the potentiometer.

to:

Connect three wires to the Arduino or Genuino board. The first goes to ground from one of the outer pins of the potentiometer. The second goes from 5 volts to the other outer pin of the potentiometer. The third goes from analog input 0 to the middle pin of the potentiometer.

July 10, 2015, at 10:39 AM by Simone Maiocchi -
Changed lines 21-22 from:
to:
July 09, 2015, at 01:36 PM by Simone Maiocchi -
Changed line 45 from:

click the image to enlarge

to:

click the image to enlarge\\

Changed line 49 from:

click the image to enlarge

to:

click the image to enlarge\\

July 09, 2015, at 01:06 PM by Simone Maiocchi -
Changed lines 76-79 from:
  • AnalogReadSerial - read a potentiometer, print it's state to the serial monitor
  • AnalogInOutSerial - read an analog input, map its values, and then use that information to dim or brighten an LED.
  • Fade - use an analog input to fade an LED
  • Calibration - calibrating analog sensor readings
to:
  • AnalogReadSerial - Read a potentiometer, print it's state to the serial monitor
  • AnalogInOutSerial - Read an analog input, map its values, and then use that information to dim or brighten an LED.
  • Fade - Use an analog input to fade an LED
  • Calibration - Calibrating analog sensor readings
July 07, 2015, at 05:29 PM by Simone Maiocchi -
Changed line 1 from:
to:
July 07, 2015, at 01:14 PM by Simone Maiocchi -
Changed lines 1-4 from:

Examples > Analog I/O

Analog Input

to:
July 05, 2015, at 07:40 PM by Simone Maiocchi -
Changed line 10 from:
  • 10K photoresistor and 10K resistor
to:
  • 10K ohm photoresistor and 10K ohm resistor
July 05, 2015, at 07:37 PM by Simone Maiocchi -
Changed lines 11-12 from:
  • built-in LED on pin 13
to:
  • built-in LED on pin 13 or
  • 220 ohm resistor and red LED
Changed lines 29-30 from:

For this example, it is possible to use the board's built in LED attached to pin 13. To use an additional LED, attach its longer leg (the positive leg, or anode), to digital pin 13, and it's shorter leg (the negative leg, or cathode) to the ground (GND) pin next to pin 13. Because of the low amount of current coming from digital pin 13, it is not necessary to use a current limiting resistor in this particular case.

to:

For this example, it is possible to use the board's built in LED attached to pin 13. To use an additional LED, attach its longer leg (the positive leg, or anode), to digital pin 13 in series with the 220 ohm resistor, and it's shorter leg (the negative leg, or cathode) to the ground (GND) pin next to pin 13.

July 04, 2015, at 07:52 PM by Simone Maiocchi -
Changed line 60 from:
to:
July 04, 2015, at 05:37 PM by Simone Maiocchi -
Changed lines 33-34 from:

Vout=Vin*(R2/(R1+R2))

to:

Vout=Vin*(R2/(R1+R2))

July 04, 2015, at 05:36 PM by Simone Maiocchi -
Changed lines 31-32 from:

This circuit uses a variable resistor, a fixed resistor and the measurement point is in the middle of the resistors. The voltage available follows this formula:\\

to:

This circuit uses a variable resistor, a fixed resistor and the measurement point is in the middle of the resistors. The voltage measured (Vout) follows this formula:

Added line 34:
July 04, 2015, at 05:34 PM by Simone Maiocchi -
Changed lines 31-33 from:

This circuit uses a variable resistor, a vixed resistor and the measurement point is in the middle of the resistors. The voltage available follows this formula: Vout=Vin*(R2/(R1+R2))

to:

This circuit uses a variable resistor, a fixed resistor and the measurement point is in the middle of the resistors. The voltage available follows this formula:
Vout=Vin*(R2/(R1+R2))
where Vin is 5V, R2 is 10k ohm and R1 is the photoresistor value that ranges from 1M ohm in darkness to 10k ohm in daylight (10 lumen) and less than 1k ohm in bright light or sunlight (>100 lumen).

July 04, 2015, at 05:30 PM by Simone Maiocchi -
Added lines 31-33:

This circuit uses a variable resistor, a vixed resistor and the measurement point is in the middle of the resistors. The voltage available follows this formula: Vout=Vin*(R2/(R1+R2))

July 04, 2015, at 05:28 PM by Simone Maiocchi -
Added line 30:

The circuit based on a photoresistor uses a resistor divider to allow the high impedence Analog input to measure the voltage. These inputs do not draw almost any current, therefore by Ohm's law the voltage measured on the other end of a resistor connected to 5V is always 5V, regardless the resistor's value. To get a voltage proportional to the photoresistor value, a resistor divider is necessary.

July 04, 2015, at 03:25 PM by Simone Maiocchi -
Changed lines 5-6 from:

In this example we use a variable resistor (a potentiometer or a photoresistor), we read its value using one analog input of an Arduino or Genuino board and we change the blink rate of the built-in LED accordingly. The analog value is read as a voltage because this is how the analog input works.

to:

In this example we use a variable resistor (a potentiometer or a photoresistor), we read its value using one analog input of an Arduino or Genuino board and we change the blink rate of the built-in LED accordingly. The resistor's analog value is read as a voltage because this is how the analog inputs work.

July 04, 2015, at 12:45 PM by Simone Maiocchi -
Changed lines 5-6 from:

In this example we use a variable resistor (a potentiometer or a photoresistor), we read its value using the analog input of an Arduino or Genuino board and we change the blink rate of the built-in LED accordingly. The analog value is read as a voltage because this is how the analog input works.

to:

In this example we use a variable resistor (a potentiometer or a photoresistor), we read its value using one analog input of an Arduino or Genuino board and we change the blink rate of the built-in LED accordingly. The analog value is read as a voltage because this is how the analog input works.

Changed line 9 from:
  • Potentiometer 'or'
to:
  • Potentiometer or
July 04, 2015, at 12:38 PM by Simone Maiocchi -
Changed lines 5-6 from:

In this example we use a variable resistor - a potentiometer or a photoresistor - and we read its value using the analog input of an Arduino or Genuino board. The analog value is read as a voltage because this is how the analog input works. The read value is used change the blink rate of the built-in LED on pin 13.

to:

In this example we use a variable resistor (a potentiometer or a photoresistor), we read its value using the analog input of an Arduino or Genuino board and we change the blink rate of the built-in LED accordingly. The analog value is read as a voltage because this is how the analog input works.

Changed line 9 from:
  • Potentiometer or
to:
  • Potentiometer 'or'
July 04, 2015, at 12:35 PM by Simone Maiocchi -
Changed lines 9-10 from:
  • Potentiometer or
  • 10K photoresistor and 10K resistor)
to:
  • Potentiometer or
  • 10K photoresistor and 10K resistor
Changed line 14 from:

With potentiometer

to:

With a potentiometer

Changed line 20 from:

With a photosensitive resistor

to:

With a photoresistor

Changed lines 28-29 from:

For this example, it is possible to use the Arduino board's built in LED attached to pin 13. To use an additional LED, attach its longer leg (the positive leg, or anode), to digital pin 13, and it's shorter leg (the negative leg, or cathode) to the ground (gnd) pin next to pin 13. Because of the low amount of current coming from digital pin 13, it is not necessary to use a current limiting resistor in this particular case.

to:

For this example, it is possible to use the board's built in LED attached to pin 13. To use an additional LED, attach its longer leg (the positive leg, or anode), to digital pin 13, and it's shorter leg (the negative leg, or cathode) to the ground (GND) pin next to pin 13. Because of the low amount of current coming from digital pin 13, it is not necessary to use a current limiting resistor in this particular case.

Changed lines 42-45 from:

In the beginning of this program, the variable sensorPin is set to to analog pin 0, where your potentiometer is attached, and ledPin is set to digital pin 13. You'll also create another variable, sensorValue i to store the values read from your sensor.

The analogRead() command converts the input voltage range, 0 to 5 volts, to a digital value between 0 and 1023. This is done by a circuit inside the Arduino called an analog-to-digital converter or ADC.

to:

At the beginning of this sketch, the variable sensorPin is set to to analog pin 0, where your potentiometer is attached, and ledPin is set to digital pin 13. You'll also create another variable, sensorValue to store the values read from your sensor.

The analogRead() command converts the input voltage range, 0 to 5 volts, to a digital value between 0 and 1023. This is done by a circuit inside the microcontroller called an analog-to-digital converter or ADC.

Changed lines 48-49 from:

That value, stored in sensorValue, is used to set a delay() for your blink cycle. The higher the value, the longer the cycle, the smaller the value, the shorter the cycle.

to:

That value, stored in sensorValue, is used to set a delay() for your blink cycle. The higher the value, the longer the cycle, the smaller the value, the shorter the cycle. The value is read at the beginning of the cycle, therefore the on/off time is always equal.

July 04, 2015, at 12:19 PM by Simone Maiocchi -
Changed lines 5-6 from:

A potentiometer is a simple knob that provides a variable resistance, which you can read into the Arduino board as an analog value. In this example, you'll connect a poterntiometer to one of the Arduino's analog inputs to control the rate at which the built-in LED on pin 13 blinks.

to:

In this example we use a variable resistor - a potentiometer or a photoresistor - and we read its value using the analog input of an Arduino or Genuino board. The analog value is read as a voltage because this is how the analog input works. The read value is used change the blink rate of the built-in LED on pin 13.

Changed lines 8-9 from:
  • Arduino Board
  • Potentiometer (or another variable resistor like a photosensitive resistor and 10K resistor)
to:
  • Arduino or Genuino Board
  • Potentiometer or
  • 10K photoresistor and 10K resistor)
April 08, 2014, at 07:37 AM by Scott Fitzgerald -
Changed line 32 from:
to:

Potentiometer

Added lines 35-38:

click the image to enlarge Photoresistor

April 08, 2014, at 07:34 AM by Scott Fitzgerald -
Changed line 9 from:
  • Potentiometer
to:
  • Potentiometer (or another variable resistor like a photosensitive resistor and 10K resistor)
Changed line 13 from:
to:

With potentiometer

Added lines 19-24:

With a photosensitive resistor click the image to enlarge

image developed using Fritzing. For more circuit examples, see the Fritzing project page

May 02, 2012, at 03:41 AM by Scott Fitzgerald -
Changed line 41 from:
to:
November 16, 2011, at 04:10 AM by Scott Fitzgerald -
Changed line 41 from:
to:
September 30, 2011, at 03:07 AM by Scott Fitzgerald -
Changed line 41 from:
to:
September 23, 2010, at 10:32 PM by Christian Cerrito -
Changed lines 3-4 from:

Analog Input

to:

Analog Input

September 23, 2010, at 09:13 PM by Christian Cerrito -
Changed lines 26-27 from:
to:
September 17, 2010, at 06:36 PM by Tom Igoe -
Changed lines 21-22 from:

For this example, it is possible to use the Arduino board's built in, small, surface mount LED attached to pin 13. To use an additional LED, attach it's long, positive leg (the anode), to digital pin 13, and it's shorter negative leg (the cathode) to the ground (gnd) pin right near by. Because of the low amount of current coming from digital pin 13, it is not necessary to use a current limiting resistor in this particular case.

to:

For this example, it is possible to use the Arduino board's built in LED attached to pin 13. To use an additional LED, attach its longer leg (the positive leg, or anode), to digital pin 13, and it's shorter leg (the negative leg, or cathode) to the ground (gnd) pin next to pin 13. Because of the low amount of current coming from digital pin 13, it is not necessary to use a current limiting resistor in this particular case.

Changed lines 30-33 from:

In the beginning of this program, the variable sensorPin is assigned to your potentiometer on analog pin 0, while ledPin is set to digital pin 13. Another variable, sensorValue is created to store the data that will be retrieved from your sensor.

By turning the shaft of the potentiometer, we change the amount of resistence on either side of the wiper which is connected to the center pin of the potentiometer. This changes the relative "closeness" of that pin to 5 volts and ground, giving us a different analog input. When the shaft is turned all the way in one direction, there are 0 volts going to the pin, and we read 0. When the shaft is turned all the way in the other direction, there are 5 volts going to the pin and we read 1023. In between, analogRead() returns a number between 0 and 1023 that is proportional to the amount of voltage being applied to the pin.

to:

In the beginning of this program, the variable sensorPin is set to to analog pin 0, where your potentiometer is attached, and ledPin is set to digital pin 13. You'll also create another variable, sensorValue i to store the values read from your sensor.

The analogRead() command converts the input voltage range, 0 to 5 volts, to a digital value between 0 and 1023. This is done by a circuit inside the Arduino called an analog-to-digital converter or ADC.

By turning the shaft of the potentiometer, you change the amount of resistance on either side of the center pin (or wiper) of the potentiometer. This changes the relative resistances between the center pin and the two outside pins, giving you a different voltage at the analog input. When the shaft is turned all the way in one direction, there is no resistance between the center pin and the pin connected to ground. The voltage at the center pin then is 0 volts, and analogRead() returns 0. When the shaft is turned all the way in the other direction, there is no resistance between the center pin and the pin connected to +5 volts. The voltage at the center pin then is 5 volts, and analogRead() returns 1023. In between, analogRead() returns a number between 0 and 1023 that is proportional to the amount of voltage being applied to the pin.

September 17, 2010, at 06:24 PM by Tom Igoe -
Changed lines 5-6 from:

A potentiometer is a simple knob that provides a variable resistance, which we can read into the Arduino board as an analog value. In this example, you'll connect a poterntiometer to one of the Arduino's analog inputs to control the rate at which the built-in LED on pin 13 blinks.

to:

A potentiometer is a simple knob that provides a variable resistance, which you can read into the Arduino board as an analog value. In this example, you'll connect a poterntiometer to one of the Arduino's analog inputs to control the rate at which the built-in LED on pin 13 blinks.

September 17, 2010, at 05:51 PM by Tom Igoe -
Changed line 7 from:

Hardware Required

to:

Hardware Required

September 17, 2010, at 05:51 PM by Tom Igoe -
September 17, 2010, at 05:51 PM by Tom Igoe -
Changed lines 5-6 from:

A potentiometer is a simple knob that provides a variable resistance, which we can read into the Arduino board as an analog value. In this example, that value controls the rate at which an LED blinks.

to:

A potentiometer is a simple knob that provides a variable resistance, which we can read into the Arduino board as an analog value. In this example, you'll connect a poterntiometer to one of the Arduino's analog inputs to control the rate at which the built-in LED on pin 13 blinks.

Changed lines 10-11 from:
to:
  • built-in LED on pin 13
September 16, 2010, at 10:08 PM by Tom Igoe -
Changed line 37 from:
to:
August 26, 2010, at 04:45 AM by Christian Cerrito -
Changed lines 7-11 from:

We connect three wires to the Arduino board. The first goes to ground from one of the outer pins of the potentiometer. The second goes from 5 volts to the other outer pin of the potentiometer. The third goes from analog input 2 to the middle pin of the potentiometer.

By turning the shaft of the potentiometer, we change the amount of resistence on either side of the wiper which is connected to the center pin of the potentiometer. This changes the relative "closeness" of that pin to 5 volts and ground, giving us a different analog input. When the shaft is turned all the way in one direction, there are 0 volts going to the pin, and we read 0. When the shaft is turned all the way in the other direction, there are 5 volts going to the pin and we read 1023. In between, analogRead() returns a number between 0 and 1023 that is proportional to the amount of voltage being applied to the pin.

to:

Hardware Required

  • Arduino Board
  • Potentiometer
Changed lines 12-13 from:

An analog input connected to analog input pin 0.

to:
Changed lines 18-21 from:
to:

Connect three wires to the Arduino board. The first goes to ground from one of the outer pins of the potentiometer. The second goes from 5 volts to the other outer pin of the potentiometer. The third goes from analog input 0 to the middle pin of the potentiometer.

For this example, it is possible to use the Arduino board's built in, small, surface mount LED attached to pin 13. To use an additional LED, attach it's long, positive leg (the anode), to digital pin 13, and it's shorter negative leg (the cathode) to the ground (gnd) pin right near by. Because of the low amount of current coming from digital pin 13, it is not necessary to use a current limiting resistor in this particular case.

Deleted lines 26-28:

Added lines 29-34:

In the beginning of this program, the variable sensorPin is assigned to your potentiometer on analog pin 0, while ledPin is set to digital pin 13. Another variable, sensorValue is created to store the data that will be retrieved from your sensor.

By turning the shaft of the potentiometer, we change the amount of resistence on either side of the wiper which is connected to the center pin of the potentiometer. This changes the relative "closeness" of that pin to 5 volts and ground, giving us a different analog input. When the shaft is turned all the way in one direction, there are 0 volts going to the pin, and we read 0. When the shaft is turned all the way in the other direction, there are 5 volts going to the pin and we read 1023. In between, analogRead() returns a number between 0 and 1023 that is proportional to the amount of voltage being applied to the pin.

That value, stored in sensorValue, is used to set a delay() for your blink cycle. The higher the value, the longer the cycle, the smaller the value, the shorter the cycle.

Changed lines 39-52 from:
to:
August 24, 2010, at 08:18 AM by Christian Cerrito -
Changed line 33 from:
to:
February 23, 2010, at 07:53 PM by Tom Igoe -
Changed lines 33-58 from:
 /*
   Analog Input
  Demonstrates analog input by reading an analog sensor on analog pin 0 and
  turning on and off a light emitting diode(LED)  connected to digital pin 13. 
  The amount of time the LED will be on and off depends on
  the value obtained by analogRead(). 
  
  The circuit:
  * Potentiometer attached to analog input 0
  * center pin of the potentiometer to the analog pin
  * one side pin (either one) to ground
  * the other side pin to +5V
  * LED anode (long leg) attached to digital output 13
  * LED cathode (short leg) attached to ground
  
  * Note: because most Arduinos have a built-in LED attached 
  to pin 13 on the board, the LED is optional.
  
  
  Created by David Cuartielles
  Modified 16 Jun 2009
  By Tom Igoe
  
  http://arduino.cc/en/Tutorial/AnalogInput
  
  */
to:
Deleted lines 34-55:
 int sensorPin = 0;    // select the input pin for the potentiometer
 int ledPin = 13;      // select the pin for the LED
 int sensorValue = 0;  // variable to store the value coming from the sensor

 void setup() {
   // declare the ledPin as an OUTPUT:
   pinMode(ledPin, OUTPUT);  
 }

 void loop() {
   // read the value from the sensor:
   sensorValue = analogRead(sensorPin);    
   // turn the ledPin on
   digitalWrite(ledPin, HIGH);  
   // stop the program for <sensorValue> milliseconds:
   delay(sensorValue);          
   // turn the ledPin off:        
   digitalWrite(ledPin, LOW);   
   // stop the program for for <sensorValue> milliseconds:
   delay(sensorValue);                  
 }
August 27, 2009, at 08:46 PM by Tom Igoe -
Added lines 18-20:

image developed using Fritzing. For more circuit examples, see the Fritzing project page

July 05, 2009, at 07:19 PM by Tom Igoe -
Changed lines 28-34 from:

[@ /*

  Analog Input
 Demonstrates analog input by reading an analog sensor on analog pin 0 and
 turning on and off a light emitting diode(LED)  connected to digital pin 13. 
 The amount of time the LED will be on and off depends on
 the value obtained by analogRead(). 
to:
Changed lines 57-63 from:
 The circuit:
 * Potentiometer attached to analog input 0
 * center pin of the potentiometer to the analog pin
 * one side pin (either one) to ground
 * the other side pin to +5V
 * LED anode (long leg) attached to digital output 13
 * LED cathode (short leg) attached to ground
to:
 int sensorPin = 0;    // select the input pin for the potentiometer
 int ledPin = 13;      // select the pin for the LED
 int sensorValue = 0;  // variable to store the value coming from the sensor
Changed lines 61-62 from:
 * Note: because most Arduinos have a built-in LED attached 
 to pin 13 on the board, the LED is optional.
to:
 void setup() {
   // declare the ledPin as an OUTPUT:
   pinMode(ledPin, OUTPUT);  
 }
Added lines 66-77:
 void loop() {
   // read the value from the sensor:
   sensorValue = analogRead(sensorPin);    
   // turn the ledPin on
   digitalWrite(ledPin, HIGH);  
   // stop the program for <sensorValue> milliseconds:
   delay(sensorValue);          
   // turn the ledPin off:        
   digitalWrite(ledPin, LOW);   
   // stop the program for for <sensorValue> milliseconds:
   delay(sensorValue);                  
 }
Changed lines 79-108 from:
 Created by David Cuartielles
 Modified 16 Jun 2009
 By Tom Igoe

 http://arduino.cc/en/Tutorial/AnalogInput

 */

int sensorPin = 0; // select the input pin for the potentiometer int ledPin = 13; // select the pin for the LED int sensorValue = 0; // variable to store the value coming from the sensor

void setup() {

  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);  

}

void loop() {

  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);    
  // turn the ledPin on
  digitalWrite(ledPin, HIGH);  
  // stop the program for <sensorValue> milliseconds:
  delay(sensorValue);          
  // turn the ledPin off:        
  digitalWrite(ledPin, LOW);   
  // stop the program for for <sensorValue> milliseconds:
  delay(sensorValue);                  

} @]

to:
June 25, 2009, at 11:55 PM by Tom Igoe -
Changed lines 11-14 from:

Circuit

to:

Circuit

An analog input connected to analog input pin 0.

click the image to enlarge

Schematic

click the image to enlarge

June 17, 2009, at 11:25 PM by Tom Igoe -
Changed lines 19-25 from:
 * AnalogInput
 * by DojoDave <http://www.0j0.org>
 *
 * Turns on and off a light emitting diode(LED) connected to digital  
 * pin 13. The amount of time the LED will be on and off depends on
 * the value obtained by analogRead(). In the easiest case we connect
 * a potentiometer to analog pin 2.
to:
  Analog Input
 Demonstrates analog input by reading an analog sensor on analog pin 0 and
 turning on and off a light emitting diode(LED)  connected to digital pin 13. 
 The amount of time the LED will be on and off depends on
 the value obtained by analogRead(). 

 The circuit:
 * Potentiometer attached to analog input 0
 * center pin of the potentiometer to the analog pin
 * one side pin (either one) to ground
 * the other side pin to +5V
 * LED anode (long leg) attached to digital output 13
 * LED cathode (short leg) attached to ground

 * Note: because most Arduinos have a built-in LED attached 
 to pin 13 on the board, the LED is optional.

 Created by David Cuartielles
 Modified 16 Jun 2009
 By Tom Igoe

 http://arduino.cc/en/Tutorial/AnalogInput
Changed lines 45-48 from:

int potPin = 2; // select the input pin for the potentiometer int ledPin = 13; // select the pin for the LED int val = 0; // variable to store the value coming from the sensor

to:

int sensorPin = 0; // select the input pin for the potentiometer int ledPin = 13; // select the pin for the LED int sensorValue = 0; // variable to store the value coming from the sensor

Changed lines 50-51 from:
  pinMode(ledPin, OUTPUT);  // declare the ledPin as an OUTPUT
to:
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);  
Changed lines 55-59 from:
  val = analogRead(potPin);    // read the value from the sensor
  digitalWrite(ledPin, HIGH);  // turn the ledPin on
  delay(val);                  // stop the program for some time
  digitalWrite(ledPin, LOW);   // turn the ledPin off
  delay(val);                  // stop the program for some time
to:
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);    
  // turn the ledPin on
  digitalWrite(ledPin, HIGH);  
  // stop the program for <sensorValue> milliseconds:
  delay(sensorValue);          
  // turn the ledPin off:        
  digitalWrite(ledPin, LOW);   
  // stop the program for for <sensorValue> milliseconds:
  delay(sensorValue);                  
March 25, 2007, at 09:49 AM by David A. Mellis -
Added lines 1-43:

Examples > Analog I/O

Analog Input

A potentiometer is a simple knob that provides a variable resistance, which we can read into the Arduino board as an analog value. In this example, that value controls the rate at which an LED blinks.

We connect three wires to the Arduino board. The first goes to ground from one of the outer pins of the potentiometer. The second goes from 5 volts to the other outer pin of the potentiometer. The third goes from analog input 2 to the middle pin of the potentiometer.

By turning the shaft of the potentiometer, we change the amount of resistence on either side of the wiper which is connected to the center pin of the potentiometer. This changes the relative "closeness" of that pin to 5 volts and ground, giving us a different analog input. When the shaft is turned all the way in one direction, there are 0 volts going to the pin, and we read 0. When the shaft is turned all the way in the other direction, there are 5 volts going to the pin and we read 1023. In between, analogRead() returns a number between 0 and 1023 that is proportional to the amount of voltage being applied to the pin.

Circuit

Code

/*
 * AnalogInput
 * by DojoDave <http://www.0j0.org>
 *
 * Turns on and off a light emitting diode(LED) connected to digital  
 * pin 13. The amount of time the LED will be on and off depends on
 * the value obtained by analogRead(). In the easiest case we connect
 * a potentiometer to analog pin 2.
 */

int potPin = 2;    // select the input pin for the potentiometer
int ledPin = 13;   // select the pin for the LED
int val = 0;       // variable to store the value coming from the sensor

void setup() {
  pinMode(ledPin, OUTPUT);  // declare the ledPin as an OUTPUT
}

void loop() {
  val = analogRead(potPin);    // read the value from the sensor
  digitalWrite(ledPin, HIGH);  // turn the ledPin on
  delay(val);                  // stop the program for some time
  digitalWrite(ledPin, LOW);   // turn the ledPin off
  delay(val);                  // stop the program for some time
}

Share