BatterySense
Arduino battery sensing library
Public Member Functions | Static Public Attributes | List of all members
Battery Class Reference

Public Member Functions

 Battery (uint16_t minVoltage, uint16_t maxVoltage, uint8_t sensePin, uint8_t adcBits=10)
 
void begin (uint16_t refVoltage, float dividerRatio, mapFn_t=0)
 
void onDemand (uint8_t activationPin, uint8_t activationMode=LOW)
 
uint8_t level ()
 
uint8_t level (uint16_t voltage)
 
uint16_t voltage (uint8_t delay=2)
 

Static Public Attributes

static const uint8_t ON_DEMAND_DISABLE = 0xFF
 

Constructor & Destructor Documentation

◆ Battery()

Battery::Battery ( uint16_t  minVoltage,
uint16_t  maxVoltage,
uint8_t  sensePin,
uint8_t  adcBits = 10 
)

Creates an instance to monitor battery voltage and level. Initialization parameters depend on battery type and configuration.

Parameters
minVoltageis the voltage, expressed in millivolts, corresponding to an empty battery
maxVoltageis the voltage, expressed in millivolts, corresponding to a full battery
sensePinis the analog pin used for sensing the battery voltage
adcBitsis the number of bits the ADC uses (defaults to 10)

Member Function Documentation

◆ begin()

void Battery::begin ( uint16_t  refVoltage,
float  dividerRatio,
mapFn_t  mapFunction = 0 
)

Initializes the library by optionally setting additional parameters. To obtain the best results use a calibrated reference using the VoltageReference library or equivalent.

Parameters
refVoltageis the board reference voltage, expressed in millivolts
dividerRatiois the multiplier used to obtain the real battery voltage
mapFunctionis a pointer to the function used to map the battery voltage to the remaining capacity percentage (defaults to linear mapping)

◆ level()

uint8_t Battery::level ( )

Returns the current battery level as a number between 0 and 100, with 0 indicating an empty battery and 100 a full battery.

◆ onDemand()

void Battery::onDemand ( uint8_t  activationPin,
uint8_t  activationMode = LOW 
)

Enables on-demand activation of the sensing circuit to limit battery consumption.

Parameters
activationPinis the pin which will be turned HIGH or LOW before starting the battery sensing
activationModeis the optional value to set on the activationPin to enable battery sensing, defaults to LOW useful when using a resistor divider to save on battery consumption, but it can be changed to HIGH in case you are using a P-CH MOSFET or a PNP BJT

◆ voltage()

uint16_t Battery::voltage ( uint8_t  delay = 2)

Returns the current battery voltage in millivolts.

Parameters
delayis the amount of milliseconds to wait to allow the ADC input voltage to stabilize (defaults to 2ms)

Member Data Documentation

◆ ON_DEMAND_DISABLE

const uint8_t Battery::ON_DEMAND_DISABLE = 0xFF
static

Activation pin value disabling the on-demand feature.


The documentation for this class was generated from the following files: