Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Jhuman Khan
Published

12V Battery Autocut Charge Controller Using ATmega328P

In this post, we’ll design and build a 12V Battery Autocut Charge Controller using the ATmega328P microcontroller.

IntermediateFull instructions provided294
12V Battery Autocut Charge Controller Using ATmega328P

Things used in this project

Story

Read more

Schematics

sheet_1_HapvUtIRNt.png

Code

12V Battery Autocut Charge Controller Using ATmega328P

Arduino
// Pin configuration
const int relayPin = 12;  // Relay control pin
const int voltageSensePin = A0;  // Voltage sensing pin

// Battery voltage limits
const float batteryFullVoltage = 14.4;  // Full cutoff voltage
const float batteryLowVoltage = 10.8;  // Low charging start voltage

// Voltage divider configuration
const float R1 = 10000.0;  // Resistor R1 value in ohms (10kΩ)
const float R2 = 3300.0;   // Resistor R2 value in ohms (3.3kΩ)

// ADC reference voltage
const float adcRefVoltage = 5.0;  // Reference voltage for the ADC
const int adcResolution = 1023;  // 10-bit ADC resolution

void setup() {
  pinMode(relayPin, OUTPUT);  // Set relay pin as output
  digitalWrite(relayPin, LOW);  // Turn off the relay initially
}

void loop() {
  // Read the ADC value
  int adcValue = analogRead(voltageSensePin);
  
  // Calculate the battery voltage using the voltage divider formula
  float batteryVoltage = (adcValue * adcRefVoltage / adcResolution) * ((R1 + R2) / R2);
  
  // Battery charging logic
  if (batteryVoltage >= batteryFullVoltage) {
    digitalWrite(relayPin, LOW);  // Turn off charging
  } else if (batteryVoltage <= batteryLowVoltage) {
    digitalWrite(relayPin, HIGH);  // Turn on charging
  }

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

Credits

Jhuman Khan
6 projects • 2 followers

Comments