Arduino Analog Input or Read with analogRead Function
Arduino board have build-in Analog To Digital Converter to take analog inputs. Arduino have a function called analogRead.
Analog input features is required to interface analog sensors like, LM35 temperature sensor, LDR light sensor etc.
Arduino UNO have 10 bit analog to digital converter. It means the digital value received from analog input is from 0 to 1023.
The digital value 0 means minimum analog input and 1023 digital value means maximum analog input.
Arduino Uno Analog Pins
Arduino UNO board have six analog input pins names from A0 to A5, as shown in the figure 1 below.
Syntax of AnalogRead function
int x = analogRead(A0); //read analog input from A0 pin.
analogRead() function return a integer data type. In the above code we are using variable x of integer data type to hold the digital value.
Arduino UNO Analog To Digital Converter
Arduino UNO works on the 5 volt and the same voltage is used as analog reference.
Analog reference is the maximum analog input for the conversion. If Analog ref. is set to 5 volt and the analog input is also 5 volt than Arduino reads it as 1023 corresponding digital value.
If input in near 0 volt, Arduino read it as digital (0000000000) which is 0 in decimal. And if the input is near 5 volt, Arduino read it as digital (1111111111) which is 1023 decimal.
In 10 bit resolution (0000000000) is minimum digital value, and (1111111111) is maximum digital value.
Arduino Potentiometer Wiring
To test the Arduino analog input function we can use Potentiometer to supply analog voltage to the A0 pin. Or you can use any other analog input pin of the Arduino.
The Potentiometer is a three terminal device. The center terminal is the connected to the A0 pin of Arduino. and other two pins are connected to ground and VCC ( 5 volt ).
The Fig 2 shows how to connect the Potentiometer to the Arduino UNO on pin A0. Similarly you can connect with any other analog input pins of Arduino.
Analog Input Examples
In this example we take input from Potentiometer on A0 pin, and read this analog input with the help of analogRead function. And send digital reading to the serial port.
You can use serial monitor terminal application to watch the analog digital values.
Example 1 – Analog input test program code on pin A0.
/* analogRead() function test program. Author: Nilesh Chaurasia https://elextutorial.com */ void setup() { Serial.begin(9600); // Set the baud rate to 9600 } void loop() { int x = analogRead(A0);// s1 is String type variable. Serial.print("Received Digital Value => "); Serial.println(x);//display same in the serial monitor. delay(2000);//Do the same thing after 2 second delay. }
In the example you have to rotate the screw of the Potentiometer to see some changes in the serial monitor.
Note: If the Potentiometer is near the 0 volt position the serial monitor shows 0. And if Potentiometer near the 5 volt position the serial monitor shows the 1023 digital value.