Tutorial.AnalogInput History

Hide minor edits - Show changes to markup

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