Measuring voltage on an Arduino

Arduino with potentiometer

Being able to measure voltages automatically with a micro-controller is a very useful feature. Although the ADC on a micro-controller is limited to measure between 0V and 5V, this range can be extended quite easily using a special type of resistor network called a resistor divider.

In this tutorial I will be explaining how to read a voltage on an Arduino into an integer and how to convert that to a voltage. In future tutorials I will show you how to expand or narrow this measuring range.

Measuring a voltage on an Arduino

Reading a voltage on an Arduino input pin is a relatively simple task. Three actions have to be taken to complete a measurement.

  1. Configure an IO pin for analogue reading
  2. Read an integer from the ADC
  3. Convert this integer to a voltage

Configure an IO pin for analogue reading

Six of the IO pins on the Arduino UNO are capable of reading analog values. These pins are named A0 through A5. In this example I will be using A0. You can change the pins function to input by using the pinMode() function. I will put this inside an empty application in the setup function because it only has to run once.

void setup()
{
    pinMode(A0, INPUT);
}

void loop()
{
}					

Read an integer from the ADC

The ADC in the micro-controller converts the voltage it senses on the input pin into an integer values ranging from 0 to 1023. 0V will be 0 and 5V will be 1023. We read this values by using the function analogRead().

int value;

void setup()
{
    pinMode(A0, INPUT);
}

void loop()
{
    value = analogRead(A0);
}					

Now we have stored the value into the variable called ‘value’. I want to have a look at that value. Because I want to read these values on my computer, I will add Serial.begin() to the setup() function. This will enable the Arduino to communicate to the PC over a serial connection. This serial connection is emulated by the USB driver.

The number 9600 is a speed setting for the communication. Both the computer and Arduino have to use the same speed in order to properly communicate.

The second addition will be in the loop() function. After every ADC reading I will send this value through the serial connection to the PC using the Serial.print() function.

int value;

void setup()
{
    Serial.begin(9600);
    pinMode(A0, INPUT);
}

void loop()
{
    value = analogRead(A0);
    Serial.print("ADC reading : ");
    Serial.print(value);
    Serial.println();
}					

Testing with a potentiometer

Because a potentiometer is a resistor divider, I attached one to the Arduino to use as a variable voltage source. The outer leads go the +5V and ground and the slider lead is attached to A0, the pin I will do a reading on.

Arduino with potentiometer
Arduino with potentiometer

While connected to the computer, I opened the serial monitor windows in the Arduino IDE. This gives me a stream of endless reading from the A0 pin.

Arduino ADC serial monitor
Arduino ADC serial monitor

The same can be done with the serial plotter, also included in the IDE. This gives me a nice graph while turning randomly on the potentiometer.

Arduino ADC serial plotter
Arduino ADC serial plotter

Convert this integer to a voltage

int value;
float voltage;

void setup() 
{ 
  Serial.begin(9600); 
  pinMode(A0, INPUT); 
} 

void loop() 
{ 
  value = analogRead(A0);
  voltage = map(value, 0, 1023, 0, 500) / 100.0;
   
  Serial.print("ADC reading : "); 
  Serial.print(voltage); 
  Serial.println("Volt"); 
} 

A couple of things were added to the code. First I added a float name voltage, so the program can store a decimal voltage value.

In the loop function I converted value into voltage using the map function. This function takes five parameters. The first is the value to convert. The second and third are the ADC range and the last two the range in voltage it represents.

Because the map function only uses integer values, it can not produce a decimal value, this is why I made the range 0 to 500 instead of 0 to 5.00. By dividing the result by 100.0 the result is forced to be a decimal number and also fixed to a maximum to two digits after the dot.

Arduino ADC voltage
Arduino ADC voltage