How to use AnalogRead in Arduino with example

In this tutorial you will learn how to use AnalogRead function of Arduino. If you are working on a project, where you need to measure the analog voltage or analog sensor value which gives you output in the form of voltage, them you need to use a analog to digital converter of Arduino. Analog pins in Arduino board represents from ‘A’ e.g.  A1, A2, A3 etc. Analog Read and Analog write are the two function which is used to deal with the variable value like voltages as same as sensors values. An Arduino has a number of analog inputs, which enables us to measure parameters in the analog domain. This could be a voltage, current, resistance, temperature, light and so on.AnalogRead Arduino

AnalogRead function in Arduino

Analog Read is just measuring the voltage between 0 to 5 volts and turning it into a value between 0 to 1023. The reason of value 1023 is because the analog to digital converters in Arduino works on a 10 bit binary signal (Which means the maximum number is 1023).

For example:

 If puts 0 volts into an analog read so the value would be 0.

   If puts 5 volts into an analog read so the value would be 1023.

The voltage in between (0-5) volts will give numbers between those values.

To receive analog input the Arduino uses analog pins 0 to 5 on most of the boards. These pins are designed to use with the components that output analog information can be used for analog input. The command used to read

  • “analogRead(pinNumber);” pinNumber represents the pins where the analog components is connected in Arduino.
  • For example: int sensorValue = analogRead(A2);. The function is used to program and address analog pins on the Arduino board and will also return the analog input reading which is between 0 to 1023.

Arduino Detect analog voltages by using Analog Read command

All Analog pins in Arduino also act like a digital pins. Within the analog pins ADC is used which converts all analog signals into the digital signals and within the program the analogRead() command is used to read the received analog voltage through analog pins. An example describe that how analogRead command is used to read analog voltages which is given below:

Example 1 of using AnalogRead of Arduino

Lets connect a light sensor to the analog pin A0 into the Arduino board and provide a supply to power on the Arduino Board. At a very start, need to define the pin as an input:

pinMode ( A0, INPUT ) ;

And then reading the analog voltages through the analog pin A0 into the x:

int x = analogRead (A3) ;

That value which is stored in x is the value in between 0 to 1023 because the Arduino has 10-bit ADC (2*10 = 1023) than store this value into the int because int x size is bigger than 10-bits. At the end print the analog value to watch, for this the command use is given below:

Serial.print ( “Analog value : “);

Serial.println (x);

When the analog value changes than x should also changes. Analog voltage never be read able if the pin is connected to any other digital pin. The analog voltages only be read able on the analog pins into the board. If there is a voltage of 2.5 V applied to pin number 0, analogRead(0) returns 512 this means analog voltage is not be read able in any digital pin.

Example 2 of using AnalogRead of Arduino 

//let potentiometer is connected at analog pin 4:

int analogPin = 4;

int val = 0;

//variable to store the value read

void setup () {

       Serial.begin (9600);

//Setup serial


Void loop () {

     Val = analogRead (analogPin);

//Used to read the input pin

Serial . Println (val);

//Debug value


The analogRead() returns the value from 0 to1023, this is because of the resolution of the ADC. The resolution is the degree to which something can be represented numerically. The higher the resolution, the greater accuracy with which something can be represented. Measure the resolution in the terms of the number of bits of resolution. For example, a 1-bit resolution would only allow two (two to the power of one) values zero and one. A 2-bit resolution would allow four (two to the power of two) values zero, one, two and three. If we tried to measure a five volt range with a two bit resolution, and the measured voltage was four volts, our ADC would return a numerical value of 3 as four volts falls between 3.75 and 5V.

So ADC with 2-bit resolution, it can only represent the voltage with four possible resulting values. If the input voltage falls between 0 and 1.25 the ADC returns numerical 0, if the voltage falls between 1.25 and 2.5 the ADC returns a numerical value of 1. And so on. With Arduino ADC range of 0 to 1023, so have 1024 possible values or 2 to the power of 10. So Arduino have an ADC with a 10 bit resolution. In normal analogRead use the reference voltage is the operating voltage of the board. For the more popular Arduino boards such as the Uno, Mega boards, the operating voltage of 5V. So if have a reference voltage of 5V, each unit returned by analogRead() is valued at 0.00488 V. (This is calculated by dividing 1024 into 5V). Check these Arduino programming tutorials:

Leave a Comment