ESP32 Tutorial 55/55 - How to measure 12V, 24V or 100V DC voltage | SunFounder's ESP32 IoT Learning kit

ESP32 Tutorial 55/55 - How to measure 12V, 24V or 100V DC voltage | SunFounder's ESP32 IoT Learning kit

In this tutorial, we will learn how to use the ESP32 to measure various DC voltages, including 12V, 24V, and even up to 100V. By utilizing a voltage divider circuit made from two resistors, we can safely measure higher voltages without risking damage to the ESP32 microcontroller. This project will demonstrate how to read these voltages and display them on the serial monitor, providing valuable insights into your power sources.

Watch the video for a comprehensive explanation (in video at 00:00). We will be using a simple voltage divider configuration to achieve this, ensuring that we can measure voltages that exceed the ESP32's maximum input levels safely. This tutorial is perfect for those looking to expand their knowledge of the ESP32 and its capabilities.

Hardware Explained

The key components for this project include the ESP32 microcontroller, two resistors forming a voltage divider, and the power source you wish to measure. The ESP32 is equipped with built-in Wi-Fi and Bluetooth capabilities, making it a versatile choice for IoT projects. The voltage divider, made up of two resistors, allows us to scale down the voltage to a safe level that the ESP32 can process.

In this setup, one resistor, R1, is fixed at 10kΩ, while the second resistor, R2, can vary depending on the maximum voltage you wish to measure. The voltage across R1 is what we will read using the ESP32's analog input pin, allowing us to calculate the original voltage from the power source.


  • Ensure resistors are 1% tolerance for accurate measurements.
  • Use a voltage divider to prevent exceeding ESP32's input voltage ratings.
  • Keep R1 at 10kΩ and adjust R2 based on the voltage range.
  • Check connections to avoid floating inputs that can lead to inaccurate readings.
  • Use a stable power source for consistent voltage measurements.

Wiring Instructions

To wire the circuit, start by connecting one end of the R1 resistor (10kΩ) to your voltage source, and the other end to the ESP32's GPIO pin 35. This pin will read the voltage across R1. Next, connect the R2 resistor (which can be 100kΩ or another value depending on your needs) between the junction point of R1 and the ground. Make sure that the ground of the power source is also connected to the ESP32's ground.

For example, if measuring 24V, connect the power supply's positive terminal to R1, then connect the other end of R1 to pin 35 and the junction of the two resistors. The free end of R2 should go to ground. This configuration will allow the ESP32 to read a scaled-down voltage safely.

Code Examples & Walkthrough

const int R1 = 10000; // Resistor 1 value
const int R2 = 100000; // Resistor 2 value
const int VinPin = 35; // Voltage input pin

In this excerpt, we define the values for our resistors, R1 and R2, as well as the analog pin VinPin that we will use to read the voltage. These constants are crucial for our voltage calculations.

void readVoltage() {
  uint32_t voltage_mV = analogReadMilliVolts(VinPin); // Read in millivolts
  VB1 = (((float) voltage_mV) / 1000.0) * (1 + (float)R2/(float)R1);
}

This function reads the voltage in millivolts from the specified pin and calculates the actual voltage VB1 using the voltage divider formula. This is important for translating the scaled-down voltage back to the original value.

void maxVoltage() {
  float maxVoltage = (3.1) * (1 + (float)R2/(float)R1);
}

Here, we define a function to calculate and print the maximum voltage that can be safely measured based on the resistor values. This function is critical for ensuring we do not exceed the ESP32's voltage limits.

Demonstration / What to Expect

When you run the code, you should see the measured voltage displayed on the serial monitor. As you adjust the input voltage, the readings should reflect these changes in real-time, demonstrating the ESP32's ability to measure various DC voltages accurately. If you experience fluctuations in the readings, consider averaging multiple samples to achieve a more stable result (in video at 12:30).

Video Timestamps

  • 00:00 Start
  • 1:59 Introduction to the project
  • 5:45 Voltage divider
  • 7:33 Wiring explained
  • 9:14 Arduino Code explained
  • 14:45 Selecting ESP32 board and COM Port on Arduino IDE
  • 16:27 Measuring 30V using ESP32 Demonstration
  • 21:36 Changed the R2 to 330k ohm
  • 22:33 Minimum voltage measurement
805-ESP32 Tutorial 55 - Arduino code to measure 12V, 24V or 100V DC voltage
Language: C++
Copied!

Resources & references

No resources yet.

Files📁

No files available.