Arduino UNO R4 - Measure Voltage

In this guide, we will learn how to measure voltage ranging from 0V to 25V using a voltage sensor with an Arduino UNO R4. We will discuss:

Arduino UNO R4 voltage sensor

Hardware Preparation

1×Arduino UNO R4 WiFi or Arduino UNO R4 Minima
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×Voltage Sensor
1×Jumper Wires
1×Recommended: Screw Terminal Block Shield for Arduino UNO R4
1×Recommended: Breadboard Shield for Arduino UNO R4
1×Recommended: Enclosure for Arduino UNO R4
1×Recommended: Power Splitter for Arduino UNO R4
1×Recommended: Prototyping Base Plate & Breadboard Kit 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)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of Voltage Sensor

A Voltage Sensor is a ready-made circuit called a voltage divider, using accurate resistors to make measuring voltage easy. It has two resistors, 30 KΩ and 7.5 KΩ. With a 5V reference voltage for the ADC, this sensor can measure voltages between 0 and 25V DC. If the ADC's reference voltage is 3.3V, it can measure voltages from 0 to 16.5V DC.

Pinout

A voltage sensor has two types of pins:

  • Input Interface (connect it where you need to measure voltage):
    • VCC pin: This is the positive pin. Connect it to where the voltage is higher.
    • GND pin: This is the negative pin. Connect it to where the voltage is lower.
  • Output Interface (connect to the Arduino UNO R4):
    • Vout pin (S): This is the signal pin. Connect it to an analog pin on the Arduino UNO R4.
    • NC pin (+): Do not connect this; it is not used.
    • G& pin (-): This is the ground pin. Connect it to the GND (0V) on the Arduino UNO R4.
    Voltage Pinout
    image source: diyables.io

Wiring Diagram

The wiring diagram between Arduino UNO R4 voltage sensor

This image is created using Fritzing. Click to enlarge image

Arduino UNO R4 Code

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-measure-voltage */ #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); }

Detailed Instructions

Follow these instructions step by step:

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Connect the Arduino UNO R4 to the voltage sensor.
  • Connect the Arduino Uno R4 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
  • Copy the code provided and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to transfer the code to the Arduino UNO R4.
  • Test by measuring 5V and 3.3V on the Arduino UNO R4.
  • Check the results on the 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  

The measurement result might be wrong or very different from the real value. This is not the fault of the voltage sensor module. The issue could be because the standard voltage reference is 5V, which might be unstable and varies with the power source. Here are some ways to fix this problem:

  • Make sure to use a power supply that gives the right amount of voltage for the Arduino UNO R4. Check if the 5V pin on the Arduino UNO R4 is really giving out 5V by using a voltmeter.
  • Use an external 3.3V voltage reference. Remember, this way you can only measure voltages between 0 and 16.5V DC.

Measuring Voltage with a 3.3V Reference

To use this method, first prepare the hardware and the code. For the hardware, connect the AREF pin on the Arduino UNO R4 to 3.3V as the diagram shows.

The wiring diagram between Arduino UNO R4 measures voltage

This image is created using Fritzing. Click to enlarge image

See The best way to supply power to the Arduino Uno R4 and other components.

Next, use this code:

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-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

Function References

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!