Menu

BatterySense

Perrotti

Class BatterySense

Class to drive a simple battery level sensor.
To use this class you need to do the circuit describe in pBattSense.png.
Includes a warning led.

Public declaration:

#define BS_VOUT_REF      5.0
#define BS_NO_BATT_LED   255

class BatterySense
{
public:
  float lastVin;
  boolean battHalf= false;
  boolean battLow = false;

  BatterySense(){}
  void begin(float maxVin, float maxVout, float minVin, byte sensePin, byte ledPin= BS_NO_BATT_LED, float vOutRef= BS_VOUT_REF);
  void run();
  float readVIn();
};

Lately Li-Ion batteries are quite popular because they are rechargeable, have low cost and high power. They are easily found in 18650 and 14500 standard sizes. These batteries have nominal voltage of 3.7 volts, but when are fully charged provide up to 4.1 volts. They are quite practical and a good option for feeding robots. The problem with this technology is that the batteries can not be completely discharged. If the voltage reaches below 2.7 volts they may lose the capacity of recharge.

After losing a good amount of batteries, I decided to create a way to allow the robot to monitor the batteries level and to warning when it is time to recharge. The simplest way to monitor the level of batteries is with a voltage divider that reduces the total voltage of battery pack to the 5 volts of the Arduino's analog inputs. As the voltage divider has the output proportional to input, as the load goes down, the output of the divider goes down in the same ratio.

CAUTION: A voltage divider is an extremely simple circuit to do, it involves only two resistors, but take care of use only high-value resistors, the more the better. The analog inputs of the Arduino do not need and dont support high current. The higher the resistors values, less current they let pass and lower impact on energy consumption.

In addition to the voltage divider, the circuit described in pBattSense.png provides a warning led. It is configured for 12.3 volt input, which corresponds to the maximum voltage supplied by 3 batteries in series. For other input voltages, simply replace the resistors R1 and R2 like suggested on circuit image.


How to use

To make the initial measurements use fully charged batteries. Once the circuit is ready and energized, measure the voltage coming from the batteries at J1 (maxVin) and the voltage output on J2's sensor pin (maxVout). Measure Arduino's 5v output as well, maybe it's not exactly 5 volts (vOutRef). Also decide the minimum input voltage for the use of batteries (minVin). In the case of Li-Ion batteries, 2.8 volts per battery is a reasonable value.

Call begin() before any other method informing the values above and the pins for sensor (analog input) and led (digital output).
Call run() at least once per loop.
At any time youn can get the last voltage readed checking public field lastVin or make a new read calling readVIn().
The led blinks in three different patterns to indicate the charge level. The heart beat pattern means batteries need recharging.


void begin(float maxVin, float maxVout, float minVin, byte sensePin, byte ledPin= BS_NO_BATT_LED, float vOutRef= BS_VOUT_REF);

Call before any other method.

  • maxVin: Voltage of battery pack fully charged.
  • maxVout: Voltage on Sensor out to battery pack fully charged.
  • minVin: Minimal voltage to battery pack.
  • sensePin: Analog pin of Arduino to read sensor.
  • ledPin: Digital pin of Arduino to control warning led.
  • vOutRef: Reference voltage of Arduino outputs (5v).

void run();

Call once per loop. Read vIn voltage and control the led pattern. Update the public fields lastVin, battHalf and battLow. If batteries level is ok, led is turned on a few milliseconds per second. If vIn voltage is below the average between maxVin and minVin, led flashes regularly in cycles of 0.5 second (0.5 second on, 0.5 second off) and battHalf flag is setted true. If vIn is below minVin, led blinks in heart beat pattern and the flag battLow is setted true.


float readVIn();

Read and return vIn voltage. Also update the public fields lastVin, battHalf and battLow. See run() method for more details.


Related

Wiki: Home

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.