ESP32 - Measure Voltage

In this guide, we will learn how to measure voltage ranging from 0V to 16.5V using a voltage sensor with an ESP32. We will explain the steps in detail.

ESP32 voltage sensor

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Voltage Sensor
1×Jumper Wires
1×(Optional) 9V Power Adapter for ESP32
1×(Recommended) ESP32 Screw Terminal Adapter

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 these links are affiliate links. We may earn a commission on your purchase at no extra cost to you. We appreciate it.

Introduction to Voltage Sensor

A Voltage Sensor is a ready-made voltage divider circuit that uses specific resistors to make measuring voltage easy. It has two resistors: one is 30 KΩ and the other is 7.5 KΩ. If the ADC has a reference voltage of 5V, the sensor can measure voltages from 0 to 25V DC. When the ADC's reference voltage is 3.3V, it can measure voltages from 0 to 16.5V DC.

Pinout

A voltage sensor comes with two types of pins:

  • Input Interface (connect where you want 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 to the ESP32):
    • Vout pin (S): Connect this signal pin to an analog pin on the ESP32.
    • NC pin (+): Do not connect this; it is not used.
    • GND pin (-): Connect this ground pin to the GND (0V) on the ESP32.
    Voltage Pinout
    image source: diyables.io

Wiring Diagram

ESP32 voltage sensor Wiring Diagram

This image is created using Fritzing. Click to enlarge image

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.

ESP32 Code

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-measure-voltage */ #define ANALOG_IN_PIN 36 // ESP32 pin GPIO36 (ADC0) 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); }

Quick Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Connect the ESP32 to the voltage sensor.
  • Connect the ESP32 board to your PC via a USB cable
  • Open Arduino IDE on your PC.
  • Select the right ESP32 board (e.g. ESP32 Dev Module) and COM port.
  • Copy and paste the above code into the Arduino IDE.
  • Press the Upload button in Arduino IDE to compile and upload the code to the ESP32.
  • Test by measuring 5V and 3.3V on the ESP32.
  • Check the readings on the Serial Monitor.
COM6
Send
Measured Voltage = 4.93 Measured Voltage = 4.93 Measured Voltage = 4.93 Measured Voltage = 4.93 Measured Voltage = 3.42 Measured Voltage = 3.42 Measured Voltage = 3.42 Measured Voltage = 3.42
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 code uses the analogRead() function to read values from an ADC (Analog-to-Digital Converter) connected to a voltage sensor. The ESP32 ADC is suitable for projects that do not require high accuracy. However, for projects needing precise measurements, please note:

  • The ESP32 ADC is not perfectly accurate and might require calibration for correct results. Each ESP32 board can vary slightly, so calibration is needed for each individual board.
  • Calibration can be challenging, especially for beginners, and might not always provide the exact results you want.

For projects requiring high precision, consider using an external ADC (e.g., ADS1115) with the ESP32 or an Arduino, which has a more reliable ADC. If you still want to calibrate the ESP32 ADC, refer to the ESP32 ADC Calibration Driver.

Video Tutorial

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

Learn More

※ OUR MESSAGES