Arduino Nano ESP32 - Measure Voltage

In this guide, we will learn how to use an Arduino Nano ESP32 to measure voltage between 0V and 25V with a voltage sensor. We will explain:

Arduino Nano ESP32 voltage sensor

Hardware Preparation

1×Arduino Nano ESP32
1×USB Cable Type-C
1×Voltage Sensor
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino Nano ESP32
1×(Recommended) Screw Terminal Adapter for Arduino Nano

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. We appreciate your support.

Overview of Voltage Sensor

A Voltage Sensor is a device that already has a voltage divider circuit built into it, using very accurate resistors to help measure voltage easily. It includes two resistors: one is 30 KΩ and the other is 7.5 KΩ. If the ADC uses a 5V reference voltage, this sensor can check voltages from 0 to 25V DC. When the reference voltage is 3.3V for the ADC, the sensor can check voltages from 0 to 16.5V DC.

Pinout

A voltage sensor comes with two groups of pins:

  • Input Interface (connect it where you need to measure voltage):
    • VCC pin: Connect this positive pin to the higher voltage point.
    • GND pin: Connect this negative pin to the lower voltage point.
  • Output Interface (connect it to the Arduino Nano ESP32):
    • Vout pin (S): Connect this pin to an Arduino Nano ESP32 analog pin.
    • NC pin (+): Do not connect this; leave it free.
    • GMS pin (-): Connect this to the Arduino Nano ESP32's GND (0V).
    Voltage Pinout
    image source: diyables.io

Wiring Diagram

The wiring diagram between Arduino Nano ESP32 and voltage sensor

This image is created using Fritzing. Click to enlarge image

Arduino Nano ESP32 Code

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-measure-voltage */ #define ANALOG_IN_PIN A7 // The Arduino Nano ESP32 pin connected to voltage sensor #define REF_VOLTAGE 3.3 #define ADC_RESOLUTION 4096.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

To get started with Arduino Nano ESP32, follow these steps:

  • If you are new to Arduino Nano ESP32, refer to the tutorial on how to set up the environment for Arduino Nano ESP32 in the Arduino IDE.
  • Connect the Arduino Nano ESP32 to the voltage sensor.
  • Connect the Arduino Nano ESP32 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano ESP32 board and its corresponding COM port.
  • Copy the above code and paste the code into the Arduino IDE.
  • Press the Upload button in the Arduino IDE to transfer the code to the Arduino Nano ESP32.
  • Test by measuring 5V and 3.3V outputs on the Arduino Nano ESP32.
  • 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  

※ NOTE THAT:

The value measured can change because the standard voltage reference is 5V. This might not be stable and can vary with the power supply.

Video Tutorial

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!