0% found this document useful (0 votes)
23 views6 pages

Arduino mppt4.2

This document contains code for an Arduino sketch that measures battery voltage, current and power using an LCD display. It allows the user to select the battery type through button inputs to set the appropriate cutoff voltage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views6 pages

Arduino mppt4.2

This document contains code for an Arduino sketch that measures battery voltage, current and power using an LCD display. It allows the user to select the battery type through button inputs to set the appropriate cutoff voltage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

#include <EEPROM.

h>

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int dut,pwm,v,j,tmp;

bool st,m;

float vo,vi,current,current1,cutoff,cv,p,p1,ef;

void setup() {

lcd.begin();
lcd.backlight();

TCCR1B = TCCR1B & B11111000 | B00000001; // Set PWM frequency for D9 & D10:

Serial.begin(115200);
//analogReference(INTERNAL);
pinMode(9,OUTPUT);

pinMode(10,INPUT_PULLUP);

pinMode(11,INPUT_PULLUP);

pinMode(12,INPUT_PULLUP);

v=EEPROM.read(0);
if(v>2)v=0;
switch(v){
case 0:
cutoff=15.4; // flooded
break;
case 1:
cutoff=14.8; //agm
break;
case 2:
cutoff=14.0; //Gel
break;
case 3:
cutoff=14.6; //LiFePO4
break;

case 4:
cutoff=30.8; //24 flooded
break;
case 5:
cutoff=29.6; //24agm
break;
case 6:
cutoff=28.0; //24Gel
break;
case 7:
cutoff=29.2; //24 LiFePO4
break;
}
}

void loop(){

j++;

if(m==0){

vo=vi= current=current1=0;

for(int i = 0; i < 100; i++) {

current = current + (.049 * analogRead(A2) -25);// for 20A mode


current1 = current1 + (.049 * analogRead(A3) -25);// for 20A mode
vo+= analogRead(A0);
vi+=analogRead(A1);
delayMicroseconds(10);

current = current/60;
current1 = current1/70;
if(current<0.1)current=0;
if(current1<0.1)current1=current=0;

vo=((vo*0.06)/100);
vi=(((vi)*0.6)/80);
//if(v)
vi=(vo*10)-vi;

p=current*vo;
p1=current1*vi;
ef=(p/p1)*100;

analogWrite(9,pwm);

if(vo<cutoff+1)pwm++;
else if(vo>cutoff)pwm--;

if(pwm<0)pwm=0;
if(pwm>255)pwm=255;

if(j>30){

j=0;
lcd.setCursor(0,0);
lcd.print("P ");
tmp=vi;
if(tmp<10)lcd.print(" ");
lcd.print(tmp);
lcd.print("V ");
if(current1<10){tmp=current1*10;
lcd.print(tmp/10);
lcd.print(".");
lcd.print(tmp%10);}
else{ tmp=current1;
lcd.print(" ");
lcd.print(tmp);
}
lcd.print("A ");
tmp= p1;
lcd.print(tmp);
lcd.print("W ");
lcd.setCursor(0,1);
lcd.print("B ");
tmp=vo;
if(tmp<10)lcd.print(" ");
lcd.print(tmp);
lcd.print("V ");
if(current<10){tmp=current*10;
lcd.print(tmp/10);
lcd.print(".");
lcd.print(tmp%10);}
else{ tmp=current;
lcd.print(" ");
lcd.print(tmp);
}
lcd.print("A ");
tmp= p;
lcd.print(tmp);
lcd.print("W ");
//-----------------------------

/*

tmp=vi;
if(tmp<10)Serial.print(0);
Serial.print(tmp);

tmp=vo;
if(tmp<10)Serial.print(0);
Serial.print(tmp);

tmp=current1*10;
if(current1<100)Serial.print(0);
if(current1<10)Serial.print(0);

Serial.print(tmp);

tmp=current*10;
if(current<100)Serial.print(0);
if(current<10)Serial.print(0);
Serial.println(tmp);
*/

//-------------------------

}
if(digitalRead(10)&digitalRead(11)&digitalRead(12))st=0;

if(!digitalRead(11)&st==0){

st=1;

lcd.clear();

if(m==1){

EEPROM.update(0, v);
switch(v){
case 0:
cutoff=15.4; // flooded
break;
case 1:
cutoff=14.8; //agm
break;
case 2:
cutoff=14.0; //Gel
break;
case 3:
cutoff=14.6; //LiFePO4
break;

case 4:
cutoff=30.8; //24 flooded
break;
case 5:
cutoff=29.6; //24agm
break;
case 6:
cutoff=28.0; //24Gel
break;
case 7:
cutoff=29.2; //24 LiFePO4
break;
}
}

m=!m;

if(m==1){

if(!digitalRead(10)&st==0){

st=1;

v=v+1;
if(v>7)v=0;
}

if(!digitalRead(12)&st==0){

st=1;

v=v-1;
if(v<0)v=7;
}

lcd.setCursor(0,0);

lcd.print(" Battery Type ");

lcd.setCursor(0,1);

switch(v){
case 0:
lcd.print(" 12V Flooded ");
break;
case 1:
lcd.print(" 12V AGM ");
break;
case 2:
lcd.print(" 12V Gel ");
break;
case 3:
lcd.print(" 12V LiFePO4 ");
break;
case 4:
lcd.print(" 24V Flooded ");
break;
case 5:
lcd.print(" 24V AGM ");
break;
case 6:
lcd.print(" 24V Gel ");
break;
case 7:
lcd.print(" 24V LiFePO4 ");
break;

delay(10);

You might also like