Go Down

Topic: How to check your arduino's battery pack level? (Read 688 times) previous topic - next topic

8bitbacon

Hi, I wanted to share this script, the battery pack connects to analog A0 for + and negative to gnd

And when the battery reaches a new low it will store the percent in the battery_percent variable from 0-100


Code: [Select]

int lowest_level=1024;
int read_battery_level=0;
int battery_percent=100;
int temp_math=0;
void setup() {
Serial.begin(9600);
}

void loop(){
read_battery_level=analogRead(A0);
if(read_battery_level<lowest_level){
lowest_level=read_battery_level;
temp_math=int(String(read_battery_level/1024.0).indexOf('.'));
if(int(read_battery_level/1024)!=1){
Serial.println(String(read_battery_level/1024.0).substring(2,4) + "%");
battery_percent=String(read_battery_level/1024.0).substring(2,4).toInt();
}else{
Serial.println("100%");
battery_percent=100;
}
}
}
arduino mini woot

ballscrewbob

Connecting batteries directly to an analog pin is a bad idea without a divider network made to suit the particular voltage of the particular battery.
That at least offers some protection to the analog pin.

Simple enough to do as shown here

It may not be the answer you were looking for but its the one I am giving based on either experience, educated guess, google or the fact that you gave nothing to go with in the first place so I used my wonky crystal ball.

8bitbacon

arduino mini woot

Go Up