Monday, 23 April 2018

microcontroller - Low Battery Indicator


I'm looking to make a battery life indicator that I can use in conjunction with a micro-controller. I'm hoping that I can then read the current battery life remaining and increasingly put the micro-controller to sleep to preserve battery life.



Answer




To the primary question, as to how to measure the voltage of a battery, the most simple method is to use one of the ADC pins on the arduino.


If you're providing 5v from the battery (unlikely), then you can run the + straight to one of the analog in pins, and use analogRead() - each increment in value (from 0-1023) will represent 4.9mV. So multiply the read value by .0049 to get the volts read.


If you're running higher than 5v (more likely), you can use a voltage divider circuit (see: http://en.wikipedia.org/wiki/Voltage_divider) to bring the voltage you're sending to the analog input to <= 5v. If you're running 12V, you'll want to divide the voltage into roughly 1/3. Then, after multiplying the ADC value, multiply by 3 to get actual voltage.


Thus, if using 12V, and a 1/3rd voltage divider:


#define BAT_PIN 14

float read_batt_volts(void) {

int val = analogRead(BAT_PIN);


float volts = (float) val * (float) 0.0049 * (float) 3;

return(volts)
}

!c


No comments:

Post a Comment

arduino - Can I use TI&#39;s cc2541 BLE as micro controller to perform operations/ processing instead of ATmega328P AU to save cost?

I am using arduino pro mini (which contains Atmega328p AU ) along with cc2541(HM-10) to process and transfer data over BLE to smartphone. I...