Tutorial.AnalogInput History

Hide minor edits - Show changes to output

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
%height=400px%[[ Attach:PhotoResistorA0_schem.png | Attach:PhotoResistorA0_schem.png]]
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-]
%width=400px%[[Attach:PhotoCellA0.png |Attach:PhotoCellA0.png]]

[-image developed using [[http://www.fritzing.org |Fritzing]]. For more circuit examples, see the [[http://fritzing.org/projects/|Fritzing project page]] -]
May 02, 2012, at 03:41 AM by Scott Fitzgerald -
Changed line 41 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/3.Analog/AnalogInput/AnalogInput.ino lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/03.Analog/AnalogInput/AnalogInput.ino lang=arduino tabwidth=4:)
November 16, 2011, at 04:10 AM by Scott Fitzgerald -
Changed line 41 from:
(:source http://arduino.cc/en/pub/code/new-extension/build/shared/examples/3.Analog/AnalogInput/AnalogInput.ino lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/3.Analog/AnalogInput/AnalogInput.ino lang=arduino tabwidth=4:)
September 30, 2011, at 03:07 AM by Scott Fitzgerald -
Changed line 41 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/3.Analog/AnalogInput/AnalogInput.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/new-extension/build/shared/examples/3.Analog/AnalogInput/AnalogInput.ino lang=arduino tabwidth=4:)
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:
%width=400px%[[Attach:Graph_schem.png |Attach:Graph_schem.png]]
to:

%height=400px%[[ Attach:AnalogReadSerial_sch.png | Attach:AnalogReadSerial_sch.png]]
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, [[Reference/analogRead | @@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, [[Reference/analogRead | @@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:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/3.Analog/AnalogInput/AnalogInput.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/3.Analog/AnalogInput/AnalogInput.pde lang=arduino tabwidth=4:)
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, [[Reference/analogRead | @@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:
(:divend:)
to:
(:divend:)

!!!See Also:
* [[Reference/pinMode]]()
* [[Reference/analogRead]]()
* [[Reference/digitalWrite]]()
* [[Reference/delay]]()


* [[Tutorial/AnalogReadSerial]] - read a potentiometer, print it's state to the serial monitor
* [[Tutorial/AnalogInOutSerial]] - read an analog input, map its values, and then use that information to dim or brighten an LED.
* [[Tutorial/Fade]] - use an analog input to fade an LED
* [[Tutorial/Calibration]] - calibrating analog sensor readings
August 24, 2010, at 08:18 AM by Christian Cerrito -
Changed line 33 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/Analog/AnalogInput/AnalogInput.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/3.Analog/AnalogInput/AnalogInput.pde lang=arduino tabwidth=4:)
February 23, 2010, at 07:53 PM by Tom Igoe -
Changed lines 33-58 from:
%color=#7e7e7e%/*
%color=#7e7e7e% Analog Input
%color=#7e7e7e% Demonstrates analog input by reading an analog sensor on analog pin 0 and
%color=#7e7e7e% turning on and off a light emitting diode(LED) connected to digital pin 13.
%color=#7e7e7e% The amount of time the LED will be on and off depends on
%color=#7e7e7e% the value obtained by analogRead().
%color=#7e7e7e%
%color=#7e7e7e% The circuit:
%color=#7e7e7e% * Potentiometer attached to analog input 0
%color=#7e7e7e% * center pin of the potentiometer to the analog pin
%color=#7e7e7e% * one side pin (either one) to ground
%color=#7e7e7e% * the other side pin to +5V
%color=#7e7e7e% * LED anode (long leg) attached to digital output 13
%color=#7e7e7e% * LED cathode (short leg) attached to ground
%color=#7e7e7e%
%color=#7e7e7e% * Note: because most Arduinos have a built-in LED attached
%color=#7e7e7e% to pin 13 on the board, the LED is optional.
%color=#7e7e7e%
%color=#7e7e7e%
%color=#7e7e7e% Created by David Cuartielles
%color=#7e7e7e% Modified 16 Jun 2009
%color=#7e7e7e% By Tom Igoe
%color=#7e7e7e%
%color=#7e7e7e% http://arduino.cc/en/Tutorial/AnalogInput
%color=#7e7e7e%
%color=#7e7e7e% */
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/Analog/AnalogInput/AnalogInput.pde lang=arduino tabwidth=4:)
Deleted lines 34-55:
%color=#cc6600%int%% sensorPin = 0; %color=#7e7e7e%// select the input pin for the potentiometer
%color=#cc6600%int%% ledPin = 13; %color=#7e7e7e%// select the pin for the LED
%color=#cc6600%int%% sensorValue = 0; %color=#7e7e7e%// variable to store the value coming from the sensor

%color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
%color=#7e7e7e%// declare the ledPin as an OUTPUT:
%color=#cc6600%pinMode%%(ledPin, %color=#006699%OUTPUT%%);
}

%color=#cc6600%void%% %color=#cc6600%'''loop'''%%() {
%color=#7e7e7e%// read the value from the sensor:
sensorValue = %color=#cc6600%analogRead%%(sensorPin);
%color=#7e7e7e%// turn the ledPin on
%color=#cc6600%digitalWrite%%(ledPin, %color=#006699%HIGH%%);
%color=#7e7e7e%// stop the program for <sensorValue> milliseconds:
%color=#cc6600%delay%%(sensorValue);
%color=#7e7e7e%// turn the ledPin off:
%color=#cc6600%digitalWrite%%(ledPin, %color=#006699%LOW%%);
%color=#7e7e7e%// stop the program for for <sensorValue> milliseconds:
%color=#cc6600%delay%%(sensorValue);
}
August 27, 2009, at 08:46 PM by Tom Igoe -
Added lines 18-20:
[-image developed using [[http://www.fritzing.org |Fritzing]]. For more circuit examples, see the [[http://fritzing.org/projects/|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:
(:div class=code :)

%color=#7e7e7e%/*
%color=#7e7e7e% Analog Input
%color=#7e7e7e% Demonstrates analog input by reading an analog sensor on analog pin 0 and
%color=#7e7e7e% turning on and off a light emitting diode(LED) connected to digital pin 13.
%color=#7e7e7e% The amount of time the LED will be on and off depends on
%color=#7e7e7e% the value obtained by analogRead().
%color=#7e7e7e%
%color=#7e7e7e% The circuit:
%color=#7e7e7e% * Potentiometer attached to analog input 0
%color=#7e7e7e% * center pin of the potentiometer to the analog pin
%color=#7e7e7e% * one side pin (either one) to ground
%color=#7e7e7e% * the other side pin to +5V
%color=#7e7e7e% * LED anode (long leg) attached to digital output 13
%color=#7e7e7e% * LED cathode (short leg) attached to ground
%color=#7e7e7e%
%color=#7e7e7e% * Note: because most Arduinos have a built-in LED attached
%color=#7e7e7e% to pin 13 on the board, the LED is optional.
%color=#7e7e7e%
%color=#7e7e7e%
%color=#7e7e7e% Created by David Cuartielles
%color=#7e7e7e% Modified 16 Jun 2009
%color=#7e7e7e% By Tom Igoe
%color=#7e7e7e%
%color=#7e7e7e% http://arduino.cc/en/Tutorial/AnalogInput
%color=#7e7e7e%
%color=#7e7e7e% */
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:
%color=#cc6600%int%% sensorPin = 0; %color=#7e7e7e%// select the input pin for the potentiometer
%color=#cc6600%int%% ledPin = 13; %color=#7e7e7e%// select the pin for the LED
%color=#cc6600%int%% sensorValue = 0; %color=#7e7e7e%// 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:
%color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
%color=#7e7e7e%// declare the ledPin as an OUTPUT:
%color=#cc6600%pinMode%%(ledPin, %color=#006699%OUTPUT%%);
}
Added lines 66-77:
%color=#cc6600%void%% %color=#cc6600%'''loop'''%%() {
%color=#7e7e7e%// read the value from the sensor:
sensorValue = %color=#cc6600%analogRead%%(sensorPin);
%color=#7e7e7e%// turn the ledPin on
%color=#cc6600%digitalWrite%%(ledPin, %color=#006699%HIGH%%);
%color=#7e7e7e%// stop the program for <sensorValue> milliseconds:
%color=#cc6600%delay%%(sensorValue);
%color=#7e7e7e%// turn the ledPin off:
%color=#cc6600%digitalWrite%%(ledPin, %color=#006699%LOW%%);
%color=#7e7e7e%// stop the program for for <sensorValue> milliseconds:
%color=#cc6600%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:
(:divend:)
June 25, 2009, at 11:55 PM by Tom Igoe -
Changed lines 11-14 from:
!!!Circuit

Attach:potentiometer.jpg
to:

!!! Circuit
An analog input connected to analog input pin 0.

[-click the image to enlarge-]
%width=400px%[[Attach:graph-circuit3.png |Attach:graph-circuit3.png]]

!!!Schematic

[-click the image to enlarge-]
%width=400px%[[Attach:Graph_schem.png |Attach:Graph_schem.png]]



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

Attach:potentiometer.jpg

!!!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