ESP8266 - Measure Voltage

In this guide, we will learn how to use an ESP8266 and a voltage sensor to measure voltages between 0V and 25V. We will explain in detail:

ESP8266 NodeMCU voltage sensor

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×Voltage Sensor
1×Jumper Wires
1×Breadboard
1×(Optional) Screw Terminal Expansion Board for ESP8266

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.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Voltage Sensor

A Voltage Sensor is a ready-made circuit made of two precise resistors that helps in easy voltage measurement. It has two resistors: 30 KΩ and 7.5 KΩ. When using a 5V reference voltage for the ADC, this sensor can check voltages from 0 to 25V DC. If the reference voltage is 3.3V for the ADC, it can measure voltages from 0 to 16.5V DC.

Pinout

A voltage sensor includes two groups of pins.

  • Input Interface (connect this to the points where you are measuring voltage):
    • VCC pin: This is the positive pin. Connect it to the point that has the higher voltage.
    • GND pin: This is the negative pin. Connect it to the point that has the lower voltage.
  • Output Interface (connect this to the ESP8266):
    • Vout pin (S): This is the signal pin. Connect it to an analog pin on the ESP8266.
    • NC pin (+): Do not connect this pin; it is not used.
    • GND pin (-): This is the ground pin. Connect it to the GND (0V) pin on the ESP8266.
    Voltage Pinout
    image source: diyables.io

Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and voltage sensor

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

ESP8266 Code

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-measure-voltage */ #define ANALOG_IN_PIN A0 // The ESP8266 NodeMCU pin connected to voltage sensor #define REF_VOLTAGE 3.3 #define ADC_RESOLUTION 1024.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 ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Connect the ESP8266 to the voltage sensor.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Copy the code provided and paste it into the Arduino IDE.
  • Click the Upload button in the Arduino IDE to transfer the code to the ESP8266.
  • Test by measuring 5V and 3.3V outputs on the ESP8266.
  • 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 might change over time because the usual voltage reference is 5V. This can be unstable and 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!