Measure Analog voltage using Arduino ADC

In this Arduino guide, we will learn how to use analog to digital converter channels of Arduino UNO R3 and read analog voltage using this board.

The embedded system world has enormous applications of ADC. In your home appliances, there are many applications of embedded system which use analog to digital converters. For example, in oven, temperature sensor is used to measure temperature. Temperature sensor gives output in the form of analog voltage. So a analog to digital converter is used in oven embedded system to interface temperature sensor with the microcontroller. Some applications of ADC are given below :

  • Analog sensors interfacing with Arduino like temperature, humidity, moisture, light, pressure and velocity sensors.
  • Analog voltage and current measurements
  • and many other applications

Arduino UNO ADC

Arduino is a small microcomputer system which don’t understand analog values. Every microcomputer system deals with digital binary numbers in the form of 0’s and 1’s. So Arduino can not understand analog values without the use of analog to digital converters. Many microcontrollers have built in analog to digital converters. Arduino UNO R3 also have five built in analog to digital converter channels. It means that it can read up to five analog voltage values or can be interfaced with five analog output sensors.

Arduino Uno analog pins

Arduino ADC Pins

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 this guide:

Arduino Analog pins

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

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

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

Arduino ADC Resolution

To understand, ADC resolution let’s 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:

Analog VoltageDigital Output
0 to 0.99 volt0
1 to1.99 volt1
2 2.99 volt2
3 3.99 volt3
4 volts4

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

Unlike other microcontrollers, to use Analog to digital converter channels of Arduino you only need to use one function to read analog voltage. But in other microcontrollers, you have to write complete code by using ADC control registers. However Arduino provides ADC library that can be easily used to read analog readings.

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.

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 and can be used for analog input.

Function analogRead(channel number) is used to read analog voltage where channel number is used to specified which channel you are using to read analog voltage.

Measure Analog Signal and Voltage with Arduino

To understand analog to digital converter of Arduino R3, construct the following circuit. You will require the following components.

Required Components

  • Arduino UNO
  • One 5mm LED
  • Potentiometer
  • 220 ohm resistor
  • Connecting Wires
  • Breadboard
Arduino UNO with Potentiometer and LED ADC Channel connection diagram
Arduino UNO with Potentiometer and LED

In the above circuit diagram, a variable resistor is used. One terminal of the variable resistor is connected with the ground, and another terminal with 5 volts from Arduino. The Center terminal of the variable resistor is connected with A0 which is ADC Channel 0. The anode pin of the LED is connected with Arduino digital pin 3 through a 220 ohm current limiting resistor. Moreover, the cathode pin of the LED is commonly grounded with the potentiometer and Arduino GND pins.

We will program this circuit shown above in such a way that when voltage read by channel A0 becomes greater than 3 volt, the LED connected to pin 3 glows and when voltage is less than 3 volt, the LED turns off.

Arduino Sketch

Open your Arduino IDE and go to File > New to open a new file. Copy the code given below in that file.

int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 3; // 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);
  sensorValue = sensorValue * 5 / 1023 ; //its convert digital value back into voltage
  // turn the ledPin on
  if (sensorValue >= 3 )
    digitalWrite(ledPin, HIGH);
  else
    digitalWrite(ledPin, LOW);
}

How the Code Works?

Start by declaring the Arduino analog pin connected with the center pin of the potentiometer. It is pin A0 in our case. We have saved it in the int variable ‘sensorPin.’

int sensorPin = A0;

Next, declare the Arduino digital pin connected with the LED’s anode pin. It is pin 3 in our case.

int ledPin = 3;

Moreover, create an int variable called ‘sensorValue’ to hold the sensor value.


int sensorValue = 0; 

Inside the setup() function, configure the ledPin as an output pin using the pinMode() function. It takes in the pin number as the first parameter and the mode as the second parameter.

void setup() {

  pinMode(ledPin, OUTPUT);
}

Inside the loop() function, we first read the analog signal using the analogRead() function. The sensorPin is taken as a parameter inside it. The analog input reading gets stored in the variable ‘sensorValue.’ It will be in between 0 to 1023 because the Arduino has 10-bit ADC (2*10 = 1023).

sensorValue = analogRead(sensorPin);

Next, will convert the digital value into voltage. sensorValue is multiplied with a resolution of Arduino ADC which is 5 / 1023. Resolution is also know as a minimum step of ADC for each corresponding value of voltage. For example, if the ADC value counts 10, its mean it has taken 10 steps or value at the input pin is 10 times the resolution of ADC. So to convert the digital value back into voltage we simply multiply it with resolution as we done in the first line.

sensorValue = sensorValue * 5 / 1023 ;

We will use if-else statement to check if the voltage is >= 3. If it is, then turn the LED ON. Otherwise in the case if the voltage reading is less than 3, then turn the LED OFF.


  if (sensorValue >= 3 )
    digitalWrite(ledPin, HIGH);
  else
    digitalWrite(ledPin, LOW);

Demonstration

To see the demonstration of the above code, upload the code to Arduino. But, before uploading code, make sure to select the Arduino board from Tools > Board and also select the correct COM port to which the Arduino board is connected from Tools > Port.

select Arduino uno

When the voltage is greater than 3 volt, the LED glows. The voltmeter is connected with variable resistor for checking the voltage.

Analog voltage reading with Arduino

Watch the video demonstration below:

You may also like to read:

5 thoughts on “Measure Analog voltage using Arduino ADC”

  1. CAn I use this same method but with 2 output so If it reads below 3 it sends out a signal through output1 and if it reads more than 3v it sends out a signal through output2 ?

    Reply
    • Hi Marc,
      You can use two outputs using same method but you need one LED for another pin.
      the program is then modified as.,
      at beginning of the program, you need to define one more pin for second LED like
      int ledpin2 = 4; // it uses pin 4 for second LED.
      in setup, you need to add
      pinMode (ledpin2, OUTPUT);
      in loop, you need to replace ledpin with ledpin2 in else case.
      that’s it you are done.

      Reply
  2. Hello!
    I want to modify this code to stay on once it sees 3v and only goes off when the Sensor vale is = 3 )
    {
    digitalWrite(ledPin, HIGH);
    }

    if(sensorValue <= 2.5 )
    {
    digitalWrite(ledPin, LOW);
    }
    }

    Reply

Leave a Comment