analogRead()
[Analog I/O]
Description
analogRead () function is used to read the value from the
specified analog pin. Arduino boards contain a multichannel, 10-bit analog to
digital converter. This means that it will map input voltages between 0 and the
operating voltage(5V or 3.3V) into integer values between 0 and 1023. On an
Arduino UNO, for example, this yields a resolution between readings of: 5 volts
/ 1024 units or, 0.0049 volts (4.9 mV) per unit. See the table below for the
usable pins, operating voltage and maximum resolution for some Arduino boards.
The input range can be changed using analogReference(),
while the resolution can be changed (only for Zero, Due and MKR boards) using
analogReadResolution().
On ATmega based boards (UNO, Nano, Mini, Mega), it takes
about 100 microseconds (0.0001 s) to read an analog input, so the maximum
reading rate is about 10,000 times a second.
**The default analogRead() resolution for these boards is 10
bits, for compatibility. You need to use analogReadResolution() to change it to
12 bits.
Syntax
analogRead(pin)
Parameters
pin: the name of the analog input pin to read from (A0 to A5 on most boards, A0 to A6 on MKR boards, A0 to A7 on the Mini and Nano, A0 to A15 on the Mega)
Returns
The analog reading on the pin. Although it is limited to the resolution of the analog to digital converter (0-1023 for 10 bits or 0-4095 for 12 bits). Data type: int.
Example Code
The code reads the voltage on analogPin and displays it.
int analogPin = A3; // potentiometer wiper (middle terminal)
connected to analog pin 3
// outside leads to ground and +5V
int val = 0; // variable to store the value read
void setup() {
Serial.begin(9600); //
setup serial
}
void loop() {
val =
analogRead(analogPin); // read the input
pin
Serial.println(val); //
debug value
}
Notes and Warnings
If the analog input pin is not connected to anything, the
value returned by analogRead() will fluctuate based on a number of factors
(e.g. the values of the other analog inputs, how close your hand is to the
board, etc.).
0 Comments