Skip to content

Commit

Permalink
fixes #36 adding support for ADC res other than 10
Browse files Browse the repository at this point in the history
  • Loading branch information
rlogiacco committed Jan 20, 2024
1 parent 8fe6e73 commit 45a557a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
11 changes: 1 addition & 10 deletions Battery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ Battery::Battery(uint16_t minVoltage, uint16_t maxVoltage, uint8_t sensePin) {
this->activationPin = 0xFF;
this->minVoltage = minVoltage;
this->maxVoltage = maxVoltage;
#if defined(ESP32)
this->adc = 4096;
#endif
}

void Battery::begin(uint16_t refVoltage, float dividerRatio, mapFn_t mapFunction) {
Expand Down Expand Up @@ -74,10 +71,4 @@ uint16_t Battery::voltage(uint8_t delay) {
digitalWrite(activationPin, !activationMode);
}
return reading;
}

#if defined(ESP32)
void Battery::setADCResolution(uint8_t bits) {
adc = 0x01 << bits;
}
#endif
}
20 changes: 12 additions & 8 deletions Battery.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@
#ifndef BATTERY_H_
#define BATTERY_H_

#ifndef ADC_RESOLUTION
#ifdef (ESP32)
#define ADC_RESOLUTION 12
#endif // ESP32
#endif // ADC_RESOLUTION

#ifndef ADC_RESOLUTION
#define ADC_RESOLUTION 10
#endif // ADC_RESOLUTION

#include <Arduino.h>

typedef uint8_t(*mapFn_t)(uint16_t, uint16_t, uint16_t);
Expand Down Expand Up @@ -73,11 +83,8 @@ class Battery {
*/
uint16_t voltage(uint8_t delay = 2);

#if defined(ESP32)
void Battery::setADCResolution(uint8_t bits);
#else
static const uint16_t adc = 1024;
#endif
static const uint16_t adc = 0x01 << ADC_RESOLUTION;

private:
uint16_t refVoltage;
uint16_t minVoltage;
Expand All @@ -87,9 +94,6 @@ class Battery {
uint8_t activationPin;
uint8_t activationMode;
mapFn_t mapFunction;
#if defined(ESP32)
uint16_t adc;
#else
};

//
Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ In reality, the relation between battery capacity and its voltage is better repr
- [Higher than 5V, with external voltage regulator](#higher-than-5v-with-external-voltage-regulator)
- [Higher than 5V, activated on demand](#higher-than-5v-activated-on-demand)
- [Voltage divider ratio](#voltage-divider-ratio)
- [ADC Resolution](#adc-resolution)
- [Variable Resolution](#variable-resolution)
- [Remaining capacity approximation](#remaining-capacity-approximation)
- [Improvable](#improvable)
- [Good enough](#good-enough)
Expand Down Expand Up @@ -173,6 +175,23 @@ The *voltage divider total resistance*, made of `R1 + R2`, will determine the cu

When determining the *ratio* don't stick with the resistors nominal values, instead, if possible, use a multimeter to actually measure their resistance so to improve your results: a `4.7kΩ` resistor could easily be a `4.75kΩ` in reality!

## ADC Resolution
Starting from version `1.2.0` the library supports ADC resolutions other than the standard `10bits` of classic Arduino boards.
The library attempts to determine if the board you are building for does support a different ADC resolution and it adopts the default value for such boards, but in case your board is not supported, please:
1. verify you are using the latest version of this library
2. open an issue specifing the board you are building for and the expected ADC resolution
3. temporarily define a macro variable `ADC_RESOLUTION` in your code with the number of bits your ADC uses (eq. `#define ADC_RESOLUTION 12`) **before** importing the library header file

```cpp
#define ADC_RESOLUTION 12
#include 'Battery.h'
```
The currently maximum supported value for the ADC resolution is `15`: specifying a value greater than that will break the library behaviour.
### Variable Resolution
Some boards support the ability to change the ADC resolution programmatically: if this is your case and your code specifies a non default value, you **must** specify the `ADC_RESOLUTION` macro variable accordingly.
## Remaining capacity approximation
The `level` available functions aim at providing an approximation of the remaining battery capacity in percentage. This is not an easy task when you want to achieve reliable values and it is something the industry of mobile devices invests a decent amount of resources.
When an accurate estimate is desireable the battery voltage is not the sole parameter you want to take into consideration:
Expand Down

0 comments on commit 45a557a

Please sign in to comment.