Arduino - Measure Voltage

In this tutorial, we will explore how to use an Arduino to measure voltage from 0V to 25V using a voltage sensor. In detail, we will cover:

Arduino voltage sensor

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×Voltage Sensor
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Recommended) Breadboard Shield For Arduino Uno
1×(Recommended) Enclosure For Arduino Uno

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

About Voltage Sensor

A Voltage Sensor is a pre-assembled voltage divider circuit that employs precision resistors for simplified voltage measurement. It consists of two resistors: 30 KΩ and 7.5 KΩ. With a 5V reference voltage for the ADC, the sensor can measure voltages ranging from 0 to 25V DC. When the ADC's reference voltage is 3.3V, the sensor can measure voltages from 0 to 16.5V DC.

Pinout

A voltage sensor has two sets of pins:

  • Input Interface (connected to the points where you want to measure voltage):
    • VCC pin: This is the positive pin. Connect it to the point with higher voltage.
    • GND pin: This is the negative pin. Connect it to the point with lower voltage.
  • Output Interface (connected to the Arduino):
    • Vout pin (S): This is the signal pin. Connect it to an analog pin on the Arduino.
    • NC pin (+): This is not used. Let it not connected.
    • GND pin (-): This is the ground pin. Connect it to the GND (0V) on the Arduino.
    Voltage Pinout
    image source: diyables.io

Wiring Diagram

Arduino voltage sensor Wiring Diagram

This image is created using Fritzing. Click to enlarge image

Arduino Code

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-measure-voltage */ // define analog input #define ANALOG_IN_PIN A0 #define REF_VOLTAGE 5.0 #define ADC_RESOLUTION 1024.0 #define R1 30000.0 // resistor values in voltage sensor (in ohms) #define R2 7500.0 // resistor values in voltage sensor (in ohms) void setup() { Serial.begin(9600); } void loop() { // read the analog input int adc_value = analogRead(ANALOG_IN_PIN); // determine voltage at adc input float voltage_adc = ((float)adc_value * REF_VOLTAGE) / ADC_RESOLUTION; // calculate voltage at the sensor input float voltage_in = voltage_adc * (R1 + R2) / R2; // print results to serial monitor to 2 decimal places Serial.print("Measured Voltage = "); Serial.println(voltage_in, 2); delay(500); }

Quick Steps

  • Connect Arduino to the voltage sensor
  • Connect Arduino to PC via USB cable
  • Open Arduino IDE, select the right board and port
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Testing by measure 5V and 3.3V on Arduino
  • See the result on Serial Monitor.
COM6
Send
Measured Voltage = 4.96 Measured Voltage = 4.96 Measured Voltage = 4.96 Measured Voltage = 4.96 Measured Voltage = 3.39 Measured Voltage = 3.39 Measured Voltage = 3.39 Measured Voltage = 3.39
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

You might notice that the measurement result is incorrect or significantly different from the actual value. Do not blame the voltage sensor module for this. The measured value may show drift because the default voltage reference is 5V, which can be unstable and dependent on the power supply. Here are some solutions to this issue:

  • Use a power supply that provides sufficient voltage for the Arduino. You can verify this by using a voltmeter to check if the 5V pin on the Arduino outputs 5V.
  • Use an external voltage reference of 3.3V. However, With this method, you can only measure voltages from 0 to 16.5V DC.

Measuring Voltage with a 3.3V Reference

To use this method, you need to set up both the hardware and the code. For the hardware, connect the AREF pin of the Arduino to 3.3V as shown in the diagram below.

Arduino measures voltage Wiring Diagram

This image is created using Fritzing. Click to enlarge image

Then, use the following code:

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-measure-voltage */ #define ANALOG_IN_PIN A0 #define REF_VOLTAGE 3.3 #define ADC_RESOLUTION 1024.0 #define R1 30000.0 // resistor values in voltage sensor (in ohms) #define R2 7500.0 // resistor values in voltage sensor (in ohms) void setup() { Serial.begin(9600); analogReference(EXTERNAL); } void loop() { // read the analog input int adc_value = analogRead(ANALOG_IN_PIN); // determine voltage at adc input float voltage_adc = ((float)adc_value * REF_VOLTAGE) / ADC_RESOLUTION; // calculate voltage at the sensor input float voltage_in = voltage_adc * (R1 + R2) / R2; // print results to serial monitor to 2 decimal places Serial.print("Measured Voltage = "); Serial.println(voltage_in, 2); delay(500); }

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

Function References

The Best Arduino Starter Kit

See Also

※ OUR MESSAGES