# How to use AnalogRead in Arduino with example

In this tutorial, you will learn how to use the 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, then you need to use an analog to digital converter of Arduino. AnalogRead and AnalogWrite 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.

Analog pins in the Arduino board are marked with the letter ‘A’ e.g.  a0, A1, A2, A3, A4. That means it has 5 built-in analogs to digital converter channels.

Only these analog pins of Arduino can be used to measure analog signals. But If you want to use more channels, you can interface external ADC with Arduino. Check these guides:

Each analog channel is a 10-bit ADC. Therefore, each ADC can measure 1024 (2^10) voltage levels and magnitude of each voltage level or step can be determined by this fomula:

`Discrete Step =  Vref / total steps = 5 / 1023 = 0.0048874 = 4.88mv`

In short, for every 4.88 milivolts of voltage value of on Arduino analog pin, digital value will be incremented by one and so on for every multiple voltage of 4.88mV, respective digital value will be produced.

To understand, ADC resolution lets take an example of ADC with a 2-bit resolution and a reference voltage of 4 volts, it can only represent the voltage with four possible resulting values that are 2^2. The input voltage and digital output will be according to this table:

As you can see from the above table for 2-bit ADC, the one discrete step is equal to one volt analog input and so on. You can also depict from the above table that small bits ADC causes a lot of error. This 2-bit ADC can not measure voltage level between 0-1, 1-2, 2-3, and 3-4. Any voltage value between these numbers will be considered as an error. This error can be minimized by using higher bits ADC.

As we mentioned earlier, Each analog channel of Arduino is 10-bit long. That means the Arduino ADC range is between 0 to 1023, so have 1024 possible values or 2 to the power of 10. So Arduino has 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 you 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).

Arduino AnalogRead function is used to measure the voltage between 0 to 5 volts and converts it into a digital value between 0 to 1023. The reason for value 1023 is because the analog to digital converters is 10-bit long.

For example, if we apply 0 volts on the ADC pin, an analogRead() output will provide zero digital value. Similarly, if we apply 5 volts on the ADC pin, an analogRead() output will provide 1023 digital values. The voltage in between (0-5) volts will give numbers between those values.

• “analogRead(pinNumber);” pinNumber represents the pins where the analog components are 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.

### How to use Meausre Analog Signal?

Let’s connect a potentiometer or variable resistor to the analog pin A0 into the Arduino board and provide 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(A0) ;``

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 on Arduino serial monitor by using serial communication library functions such as Serial.print().

``````Serial.print ( “Analog value : “);
Serial.println (x);``````

When the analog value changes, then x should also change. Analog voltage never is readable if the pin is connected to any other digital pin. The analog voltages only are readable 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 the analog voltage is not readable in any digital pin.

### Schematic Diagram

Make schematic diagram according to these connections.

Upload this code to Arduino. This code measures analog signal from A0 pin of Arduino every one second and prints value on serial monitor.

``````//let potentiometer is connected at analog pin 4:
int analogPin = A0;
int val = 0;
//variable to store the value read
void setup ()
{
Serial.begin (9600);
//Setup serial
}

Void loop ()
{
//Used to read the input pin
Serial . Println (val);
delay(1000);
}``````

### How Code Works?

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.

Check these Arduino programming tutorials:

### 4 thoughts on “How to use AnalogRead in Arduino with example”

1. analogRead() not works in proteus always shows value 1023

• there might be a problem with your code. Share your code here.

2. I have taken code from standard example of Arduino library analogRead function from basic examples
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0: