Back to SunFounder ESP32 Starter Kit Course

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

If you don't like to see ads while video is being played you can purchase YouTube premium Here

In this video we learn how to measure any DC voltage using ESP32 board. we learn how to use only 2 resistors to be able to safely measure the DC voltage using ESP32.

Topics in this lesson

Use Chapters from timeline or click on the time

  • 00:00 Introduction
  • 1:59 Intoduction 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 measuremen

Watch video instruction for this code on YouTube
Resource page for this lesson and code:
Tutoril by

Arduino code for ESP32 to measure any DC voltage
written by Ahmad Shamshiri
Feb 19, 2024
bool debug = false;
const int R1 = 10000;//we read the voltage acrosst this resistor ()
const int R2 = 100000;

const int VinPin = 35; // Potentiometer connected to GPIO14

// PWM settings
const int freq = 5000; // PWM frequency
const int resolution = 12; // PWM resolution (bits)
const int channel = 0; // PWM channel

float VB1;
int mV;

void setup() {
  // Configure PWM
  ledcSetup(channel, freq, resolution);
  //calculatResistor(R1, 24);//10000 ohm and 35.0V


void loop() {

  Serial.print("VB1 Voltage: ");
  Serial.print(VB1); // Convert millivolts to volts
  Serial.println("V ");


void readVoltage()
  uint32_t voltage_mV = analogReadMilliVolts(VinPin); // Read the voltage in millivolts
    Serial.print("PinVoltage: ");
    Serial.println();//this adds a new line
  mV = voltage_mV;
  VB1 = (((float) voltage_mV) / 1000.0)  * (1 + (float)R2/(float)R1);

prints maximum volage
void maxVoltage()
  float maxVoltage = ( 3.1)  * (1 + (float)R2/(float)R1);
    Serial.print("****Maximum Voltage: ");

}//maxVoltage() end

void calculatResistor(int r1, float voltage)
    Serial.print("Calculating R2 when R1 is :");
    Serial.print(" Ohms and Maximum Input Voltage is ");  
    Serial.print("***** R2 Should Be ");  
    float r2 = (voltage - 3.1)/ (3.1/ (float)r1);
    Serial.print(r2 / 1000.0);  
    Serial.println(" kilo Ohms");  


**** Purchase this SunFounder ESP32 Learning kit from Affiliated Stores ***
ESP32 from Amazon USA
ESP32 from Amazon Canada
ESP32 from Amazon Germany
ESP32 from Amazon UK
ESP32 from SunFounder website
ESP32 from Amazon Japan
ESP32 from eBay

The least I expect from you is to thumb up the video and subscribe to my channel. I appriciate that. .I have spent months making these lectures and writing code. You don't lose anything by subscribging to my channel. Your subscription is stamp of approval to my videos and more people can find them and in it turn it helps me. Thank you

If you found this tutorial helpful, please support me so I can continue creating content like this. support me via PayPal

**** AFFILIATE PROGRAM **** We are a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for us to earn fees by linking to and affiliated sites.

Right Side