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

ESP32-Based Dual Battery Voltage Monitoring and Automatic Switching System

View and Edit Project In Cirkit DesignerImage of ESP32-Based Dual Battery Voltage Monitoring and Automatic Switching System

Circuit Documentation

Summary

The circuit is designed to monitor the voltage of two 12v batteries and switch between them based on a voltage threshold using a microcontroller and relay system. The circuit includes voltage sensors to monitor the battery voltages, step-down power converters to provide 5v power to the microcontroller and relays, and a DC motor as the load. The ESP32 microcontroller reads the voltage levels from the sensors and controls the relays to switch the battery connection to the motor.

Component List

12v Battery

Voltage Sensor

12v to 5v Step Down Power Converter

Relay 4 Channel 5v

ESP32 38 PINS

DC Motor

Wiring Details

12v Battery

Voltage Sensor

12v to 5v Step Down Power Converter

Relay 4 Channel 5v

ESP32 38 PINS

DC Motor

Documented Code

// Pin Definitions
const int sensorPin1 = 34; // Voltage Sensor 1 connected to G34
const int sensorPin2 = 35; // Voltage Sensor 2 connected to G35
const int relayPin1 = 23;  // Relay 1 IN1 connected to G23
const int relayPin2 = 22;  // Relay 2 IN2 connected to G22

// Voltage Threshold
const float voltageThreshold = 11.0; // Threshold voltage in volts

// Calibration Constants (to convert ADC value to voltage)
const float referenceVoltage = 3.3;  // Reference voltage of ESP32 ADC
const int adcMaxValue = 4095;        // Maximum ADC value (12-bit ADC)
const float voltageDividerRatio = 5.0; // Voltage divider ratio of the sensor

void setup() {
  // Initialize Serial Monitor for debugging
  Serial.begin(115200);

  // Initialize relay pins as outputs
  pinMode(relayPin1, OUTPUT);
  pinMode(relayPin2, OUTPUT);

  // Initially, connect Battery 1 to the load and disconnect Battery 2
  digitalWrite(relayPin1, LOW); // LOW means Relay 1 is connected
  digitalWrite(relayPin2, HIGH); // HIGH means Relay 2 is disconnected
}

void loop() {
  // Read the voltage from the sensors
  float voltage1 = readVoltage(sensorPin1);
  float voltage2 = readVoltage(sensorPin2);

  // Print the voltage values for debugging
  Serial.print("Battery 1 Voltage: ");
  Serial.print(voltage1);
  Serial.println(" V");
  
  Serial.print("Battery 2 Voltage: ");
  Serial.print(voltage2);
  Serial.println(" V");

  // Check if Battery 1 voltage drops below the threshold
  if (voltage1 < voltageThreshold) {
    // Switch to Battery 2
    digitalWrite(relayPin1, HIGH);  // Disconnect Battery 1
    digitalWrite(relayPin2, LOW);   // Connect Battery 2
    Serial.println("Switching to Battery 2...");
  } else {
    // Keep using Battery 1
    digitalWrite(relayPin1, LOW);   // Connect Battery 1
    digitalWrite(relayPin2, HIGH);  // Disconnect Battery 2
    Serial.println("Using Battery 1...");
  }

  // Delay for stability (adjust as needed)
  delay(1000);
}

// Function to read and convert ADC value to voltage
float readVoltage(int sensorPin) {
  int adcValue = analogRead(sensorPin);
  float voltage = (adcValue * referenceVoltage / adcMaxValue) * voltageDividerRatio;
  return voltage;
}

This code is designed to run on an ESP32 microcontroller. It initializes the relay pins and sets up the serial communication for debugging. The loop function reads the voltage from two sensors and switches the relays based on the voltage threshold. The readVoltage function converts the ADC value to a voltage reading, taking into account the reference voltage, ADC resolution, and voltage divider ratio.