Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

ESP8266 and INA219 Battery Monitoring System with Wi-Fi Connectivity

View and Edit Project In Cirkit DesignerImage of ESP8266 and INA219 Battery Monitoring System with Wi-Fi Connectivity

Circuit Documentation

Summary

This circuit is designed to monitor the voltage and current of a battery using an ESP8266 NodeMCU microcontroller and an INA219 current sensor. The circuit also includes a TP4056 battery charger module and a 18650 Li-ion battery. The ESP8266 NodeMCU reads the battery voltage and calculates the battery percentage, while the INA219 sensor measures the current and voltage of the battery. The TP4056 module is used to charge the 18650 Li-ion battery.

Component List

ESP8266 NodeMCU

18650 Li-ion Battery

TP4056

Resistor

5V Battery

INA219

Wiring Details

ESP8266 NodeMCU

18650 Li-ion Battery

TP4056

Resistor

5V Battery

INA219

Documented Code

ESP8266 NodeMCU Code

// Define constants for voltage divider and battery
const float R1 = 100000.0; // Resistor R1 value (in ohms)
const float R2 = 100000.0; // Resistor R2 value (in ohms)
const float ADC_MAX = 1023.0; // Maximum ADC value for NodeMCU (10-bit ADC)
const float VREF = 3.3; // Reference voltage for NodeMCU ADC (in volts)
const float MAX_BATTERY_VOLTAGE = 4.2; // Fully charged battery voltage (in volts)
const float MIN_BATTERY_VOLTAGE = 3.0; // Minimum battery voltage (in volts)

// Variables for calculations
float adcValue = 0; // ADC reading
float batteryVoltage = 0; // Calculated battery voltage
float batteryPercentage = 0; // Calculated battery percentage

void setup() {
  Serial.begin(115200); // Start serial communication
}

void loop() {
  // Read ADC value from pin A0
  adcValue = analogRead(A0);

  // Calculate battery voltage based on voltage divider ratio
  batteryVoltage = (adcValue / ADC_MAX) * VREF * ((R1 + R2) / R2);

  // Calculate battery percentage
  batteryPercentage = ((batteryVoltage - MIN_BATTERY_VOLTAGE) / 
                       (MAX_BATTERY_VOLTAGE - MIN_BATTERY_VOLTAGE)) * 100;

  // Limit percentage to 0-100%
  if (batteryPercentage > 100) {
    batteryPercentage = 100;
  } else if (batteryPercentage < 0) {
    batteryPercentage = 0;
  }

  // Print results to serial monitor
  Serial.print("ADC Value: ");
  Serial.print(adcValue);
  Serial.print(" | Battery Voltage: ");
  Serial.print(batteryVoltage, 2); // Print with 2 decimal places
  Serial.print(" V | Battery Percentage: ");
  Serial.print(batteryPercentage, 1); // Print with 1 decimal place
  Serial.println(" %");

  // Delay for readability (1 second)
  delay(1000);
}

INA219 Code

#include <Wire.h>
#include <Adafruit_INA219.h>

Adafruit_INA219 ina219;

void setup() {
  Serial.begin(115200);
  if (!ina219.begin()) {
    Serial.println("Failed to find INA219 chip");
    while (1);
  }
  Serial.println("INA219 Initialized");
}

void loop() {
  float current_mA = ina219.getCurrent_mA(); // Read current in mA
  float voltage = ina219.getBusVoltage_V() + (ina219.getShuntVoltage_mV() / 1000); // Battery voltage

  Serial.print("Current: ");
  Serial.print(current_mA);
  Serial.print(" mA | Voltage: ");
  Serial.print(voltage);
  Serial.println(" V");

  delay(1000);
}