Back to Tutorial

ESP32 with DC Voltage Sensor

If you want to measure DC voltage using ESP32, this tutorial will guide you step-by-step. The ESP32 voltage sensor project is useful for monitoring battery voltage, power supply voltage, solar panels, and IoT power systems.

In this guide, you will learn how to connect a DC voltage sensor to ESP32, understand the voltage divider principle, and run Arduino code to measure voltage accurately.


What is an ESP32 Voltage Sensor?

A DC voltage sensor module allows the ESP32 to measure voltages higher than 3.3V, which is the maximum safe input for the ESP32 ADC pins.

Since ESP32 analog pins cannot read high voltage directly, a voltage divider circuit using resistors reduces the input voltage to a safe range.

Typical uses include:

  • Battery monitoring systems
  • Solar panel voltage measurement
  • Power supply monitoring
  • Smart energy meters
  • IoT voltage monitoring projects

Components Required

To build this project, you will need:

  • ESP32 Development Board
  • DC Voltage Sensor Module
  • Jumper Wires
  • Breadboard
  • DC Power Source (Battery or Adapter)

ESP32 Voltage Sensor Pin Configuration

Voltage SensorESP32
VCCExternal Voltage
GNDGND
Signal (S)GPIO32

The sensor output connects to GPIO32, which reads the analog voltage using the ESP32 ADC.


Voltage Divider Working Principle

The voltage sensor uses a voltage divider circuit.

A voltage divider reduces high voltage into a smaller measurable voltage.

Voltage divider formula:Vout=Vin×R2R1+R2V_{out} = V_{in} \times \frac{R2}{R1 + R2}Vout​=Vin​×R1+R2R2​

Where:

  • Vin = Input voltage
  • Vout = Output voltage to ESP32 ADC
  • R1 and R2 = Resistor values

In this example:

  • R1 = 30kΩ
  • R2 = 7.5kΩ

This allows measurement of higher voltages safely.


HOW TO CONNECT


ESP32 DC Voltage Sensor Arduino Code

Upload the following code using Arduino IDE.

/*
 * 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  32 // ESP32 pin GPIO32 connected to voltage sensor
#define REF_VOLTAGE    3.3
#define ADC_RESOLUTION 4096.0
#define R1             30000.0 // resistor values in voltage sensor (ohms)
#define R2             7500.0  // resistor values in voltage sensor (ohms)

void setup() {
  Serial.begin(9600);

  // set ADC attenuation
  analogSetAttenuation(ADC_11db);
}

void loop() {

  int adc_value = analogRead(ANALOG_IN_PIN);

  float voltage_adc = ((float)adc_value * REF_VOLTAGE) / ADC_RESOLUTION;

  float voltage_in = voltage_adc * (R1 + R2) / R2;

  Serial.print("Measured Voltage = ");
  Serial.println(voltage_in, 2);

  delay(500);
}

How the Code Works

1. Reading Analog Voltage

The ESP32 reads the analog value using:

analogRead(GPIO32)

This returns a value between 0 and 4095 because ESP32 uses a 12-bit ADC.


2. Converting ADC Value to Voltage

The code converts ADC reading into voltage using:

voltage_adc = (adc_value * 3.3) / 4096

This calculates the voltage at the ADC pin.


3. Calculating Actual Input Voltage

Because of the voltage divider, the actual voltage is calculated using:

voltage_in = voltage_adc * (R1 + R2) / R2

This gives the real input voltage applied to the sensor.


Example Serial Monitor Output

Measured Voltage = 5.02
Measured Voltage = 5.01
Measured Voltage = 5.03

This means the ESP32 is successfully measuring the DC voltage.


Applications of ESP32 Voltage Measurement

This project can be used in many IoT and electronics applications, including:

  • Battery monitoring system
  • Solar power monitoring
  • Smart energy meter
  • Power supply diagnostics
  • IoT voltage monitoring dashboard

Tips for Accurate Voltage Measurement

To improve accuracy:

  • Use precision resistors (1% tolerance)
  • Add a capacitor for noise filtering
  • Calibrate readings using a multimeter
  • Avoid input voltage above sensor limit

Conclusion

Measuring voltage with ESP32 and a DC voltage sensor module is simple and useful for many IoT projects. By using the ESP32 ADC and voltage divider circuit, you can safely measure higher voltages and monitor power systems in real time.

This project is ideal for beginners learning ESP32 analog sensors and voltage monitoring.

Share this post

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Tutorial