• Have you tried out dark mode?! Scroll to the bottom of any page to find a sun or moon icon to turn dark mode on or off!

diy solar

diy solar

Making your own BMS or Solar/inverter controller (Code included)

DENWA

New Member
Joined
Sep 22, 2019
Messages
146
So I was using an Arduino for some quick measurements of a battery string. I switched it to a Digispark ATTiny85 beacuse it plugs directly into a USB port for power which makes fun and portable. I was using it to build a ON/OFF relay for battery switch or Inverter. it was simple and fun and I left the code comments to add in the serial monitor or USB display if you want to see the voltage you are reading. This got me thinking couldn't you build a really nice custom BMS with Temperature sense and all the stuff you really wanted? BTW-- I do not write code for a living I'm just a dad who likes to fix stuff.


//You'll need R1 connected from voltage to Pin2 & R2 and GND.
//Intent here is to check that 12V battery is greater than ~13.5V
//13.5V indicates battery is being charged by panels after being run down to 12.3V under load.
//If battery is >13.5V then turn on relay that controls the 110V inverter
//Turn off inverter when battery is less than 12.3V
//Need wait for inverter to start and load to stabilize before measuring battery to avoid the start/stop loop!
#include <DigiKeyboard.h>
int lowbatt = 12.3;
int fulbatt = 13.5;
//int ledPin = 1; // LED connected to digital pin 1
int drivePin = 0; //digital write to turn on MOSFET to turn on the Inverter
int nodrivePin = 1; //opposite of drive pin
int value = 0;
float battvolt;
float R1 = 9990.0;
float R2 = 4850;

void setup(){
// Serial.begin(9600);
// pinMode(ledPin, OUTPUT); // sets the digital pin as output
pinMode(drivePin, OUTPUT); // sets the digital pin as output
pinMode(nodrivePin, OUTPUT); // sets the digital pin as output
pinMode(2, INPUT); //sets analong pin to input
// ADMUX = (0x08); // Only line changed to use internal 1.1V Ref as oppose to Vcc.
//ADMUX |= (1<<REFS1);
}
void loop(){
value = analogRead(1); //reads phyisical pin2 aka analog pin1
battvolt = value * (5.0/1024)*((R1 + R2)/R2);
// Serial.print("Battery Voltage =");
// Serial.println(battvolt);
// DigiKeyboard.delay(3000);

// DigiKeyboard.println(battvolt);
// delay(1500);


if (battvolt >= fulbatt){ //test LED and Voltage measurement

// digitalWrite(ledPin, HIGH); // sets the LED on
// delay(1000); // waits for a second
// digitalWrite(ledPin, LOW); // sets the LED off
//delay(1000); // waits for a second
digitalWrite(nodrivePin, LOW);
digitalWrite(drivePin, HIGH); // turns on Mosfet controlling inverter
delay(5000); // waits for 5 seconds to ensure inverter/load stabilize and avoid start/stop oscillation condition
}

if (battvolt <= lowbatt ){

// digitalWrite(ledPin, HIGH); // sets the LED on
// delay(5000); // waits for a second
// digitalWrite(ledPin, LOW); // sets the LED off
// delay(1000); // waits for a second
digitalWrite(nodrivePin, HIGH);
digitalWrite(drivePin, LOW); // turns OFF Mosfet controlling inverter
delay(5000); // waits for 5 seconds to ensure battery voltage stabilizes before reading again

}
}
 
You’ll enjoy this thread::

 

diy solar

diy solar
Back
Top