Giter VIP home page Giter VIP logo

batterysense's Introduction

IMPORTANT

Please, before submitting a support request read carefully this README and check if an answer already exists among previously answered questions: do not abuse of the Github issue tracker.

Battery Sense GitHub stars GitHub forks Tweet

GitHub version GitHub download GitHub stars GitHub issues Build Status License

This is a simple Arduino library to monitor the battery consumption of your battery-powered projects, being LiPo, LiIon, NiCd or any other battery type, single or multiple cells: if it can power your Arduino you can monitor it!

The principle is simple: we are going to measure our battery capacity by measuring the voltage across the battery terminals.

The big assumption here is that battery capacity is linearly correlated to its voltage: the assumption itself is wrong, but in most cases, it's close enough to reality, especially when it comes to the battery's higher capacity side.

In reality, the relation between battery capacity and its voltage is better represented by a curve and there are many factors affecting it: current drawn, temperature, age, etc...

Battery discharge curves at 750 mA

How to

The library requires at least 1 analog pin (we will call this the sense pin) and no less than 2 pieces of info on your battery: the voltage you will consider the minimum acceptable level, below which your project/product becomes unreliable and should be shut down, and the maximum voltage you can expect when the battery is fully charged.

Additionally, you can provide a second pin (either analog or digital) to activate the battery measurement circuit (we call it the activation pin), useful in all those situations where you can sacrifice a pin to further increase your battery duration.

If you want your readings to be more accurate we strongly suggest calibrating the library by providing your board reference voltage: most of the time you assume your board has exactly 5V between Vcc and GND, but this is rarely the case. To improve this we suggest using the VoltageReference library to obtain a better calibration value for all analog readings.

The sense pin wiring can vary depending on your battery configuration, but here are a few examples based on the assumption you are using a 5V board: in the case of a 3.3V board you should be performing the necessary adjustments.

Lesser than 5V, with voltage booster

Voltage sources made of single-cell LiPo or LiIon, along with some single or multi-cell NiCd configurations (like up to 3 AA or AAA), are not able to provide the suggested 5.0 volts input to your board and a voltage booster can solve your problem. What does that mean when it comes to measuring your battery level? We need to measure the battery voltage before it gets boosted, which means your sense pin must be connected between the battery positive terminal and the booster positive input and we don't need any additional components as the voltage is already in the acceptable range:

                                               +---------+
          +----------------------------- SENSE |         |
          |        +---------+                 |         |
          |        |         |                 |         |
  BAT+ ---+--- IN+ |   5V    | OUT+ ------- 5V | Arduino |
                   | BOOSTER |                 |         |
  BAT- ------- IN- |         | OUT- ------ GND |         |
                   +---------+                 |         |
                                               +---------+

Higher than 5V, with internal voltage regulator

Voltage sources made of multiple cells LiPo or LiIon, along with some single or multi-cell NiCd configurations (like up the classic 9V battery or 4+ AA or AAA), provide voltages above the 5.0 volts input: most of the Arduino boards are equipped with voltage regulators able to dissipate into heat all the excess. To measure such batteries we need to hook our sense pin before it gets regulated, between the battery positive terminal and the Arduino unregulated input VIN or RAW, but we require two resistors to reduce the voltage to acceptable values:

                         +---------+
  BAT+ ---+--------- VIN |         |
          |              |         |
         R1              |         |
          |              |         |
          +------- SENSE | Arduino |
          |              |         |
         R2              |         |
          |              |         |
  BAT- ------------- GND |         |
                         +---------+

The values of R1 and R2 determine the voltage ratio parameter for this library: for information about this value refer to the section below.

Because the resistors in this configuration will constantly draw power out of your battery, you shouldn't pick values under 1k Ohm, or you'll deplete your batteries much faster than normal. On the other end, going too high on the resistor values will impede the library from getting accurate readings.

Higher than 5V, with external voltage regulator

Whenever your battery's maximum voltage exceeds the onboard regulator (if there is any) an external voltage regulator is required. Once again, to measure such batteries we need to hook our sense pin before it gets regulated, between the battery positive terminal and the voltage regulator positive input VIN or RAW and, as before, we require two resistors to reduce the voltage to acceptable values:

+---------------------------------+
|                  +---------+    |         +---------+
| BAT+ ---+--- IN+ |         |    +-- SENSE |         |
|         |        |         |              |         |
|        R1        |         |              |         |
|         |        |         |              |         |
+---------+        |   REG   | OUT+ ---- 5V | Arduino |
          |        |         |              |         |
         R2        |         |              |         |
          |        |         |              |         |
  BAT- ---+--- IN- |         | OUT- --- GND |         |
                   +---------+              +---------+

The values of R1 and R2 determine the voltage ratio parameter for this library: for information about this value refer to the section below.

Higher than 5V, activated on demand

Batteries are a precious resource and you want to prolong their life as much as you can so, deplete your battery to determine its capacity is not desirable.

As a consequence of connecting the battery terminals through two resistors we are drawing some energy out of the battery: for a 9V battery and 1k Ohm for R1 and R2, you will be adding a constant 4.5mA current consumption to your circuit. Not a huge amount, but definitely not desirable.

If you have an unused pin on your Arduino it will be easy to limit this additional current consumption to be drawn only when needed: during battery measurement. We will be turning the activation pin HIGH during battery measurement so that the voltage divider will be disconnected most of the time:

                         +---------+
  BAT+ ---+--------- VIN |         |
          |              |         |
         SW--------- ACT |         |
          |              |         |
         R1              |         |
          |              | Arduino |
          +------- SENSE |         |
          |              |         |
         R2              |         |
          |              |         |
  BAT- ------------- GND |         |
                         +---------+

In the above schematics, SW is a circuit that can connect or disconnect the sensing circuit depending on the voltage on ACT: the most common and cheap circuit is made of an NPN transistor Q1, a p-channel MOSFET Q2, a 1k-4.7k Ohm resistor R3 and a 5k-20k Ohm resistor R4:

                       BAT+
                        |
                  +-----+
                  |     |
                  R4    |
                  |\    |
   ACT --- R3 ---Q1 \-- Q2
                  |     |
                  |     |
                 GND   VDIV
                       to R1/R2/SENSE

Feel free to refer to this circuit simulation to better understand how the circuit works and how much current draws when in operation.

Simulated Circuit

Voltage divider ratio

Whenever your battery voltage is above your board voltage you need a voltage divider to constrain your readings within the 0-5V range allowed by your Arduino and you will have to provide this library with its ratio.


  BAT+ ---+
          |
         R1
          |
          +------- SENSE
          |
         R2
          |
  BAT- ----

The voltage divider ratio is determined by the formula (R1 + R2) / R2: if you use two resistors of the same value the ratio will be 2, which can be interpreted as whatever value we read it will be half of the actual value. This allows us to sense batteries up to 10V. If you use a 22k Ohm resistor for R1 and a 10k Ohm for R2 then your voltage ratio` will be 3.2 and you will be able to safely monitor a 12-15V battery.

You must select the resistors in order to get a ratio that will produce values between the 0-5V range (or 0-3.3V for 3.3V devices) at all times and to obtain that the process is quite simple: divide your battery maximum voltage by 5V and you'll get the absolute minimum value for the voltage ratio, then pick any two resistors values whose combination produce a ratio equal or higher than the absolute minimum. For a 12V battery, the absolute minimum voltage ratio* is 12/5=2.4, meaning you can't use a split supply divider made of two equal resistors: you need R1 to be a higher value than R2! Get this wrong and you will probably burn your sense pin.

You can use this nice website to find some appropriate values for the resistors setting your battery maximum voltage as Voltage source and aiming at obtaining an Output voltage value lesser than your board voltage (5V or 3.3V) but as close as possible.

The voltage divider total resistance, made of R1 + R2, will determine the current drawn from your battery by the sensing circuit: the lower is the total resistance and the more accurate your readings, the higher the resistance and the less current is drawn from your battery (Ohm's law rulez!). My suggestion is to keep this value within 20k-22k Ohm when using an always-connected circuit and under 10k Ohm if you use an on-demand configuration.

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 through an additional, but optional, constructor parameter named adcBits which obviously defaults to 10.

So, in case your board ADC provides an ADC with a resolution higher than 10 bits, you should provide the additional parameter with the number of bits your ADC provides. In case the board has the ability to change the ADC resolution at runtime, it is your responsibility to ensure the ADC resolution is properly set before each call to any of the measuring functions: voltage or level.

#include <Battery.h>

Battery batt = Battery(3000, 4200, SENSE_PIN, 12);

IMPORTANT

The currently maximum supported value for the ADC resolution is 15: specifying a value greater than that will break the library behaviour.

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 desirable the battery voltage is not the sole parameter you want to take into consideration:

  • cell chemistry has a very high influence, obviously
  • cells based on the same chemistry might produce pretty different results depending on the production process
  • each chemistry has a different ideal operating cell temperature
  • the rate you draw current from the battery influences the remaining capacity
  • batteries are not everlasting: as the cell ages, the battery capacity gets reduced
  • and more

The library itself doesn't aim at providing accurate estimates, but what I consider an improvable but good enough estimate.

Improvable

The library can be configured to use a mapping function of your choice, given the function complies with the mapFn_t interface:

uint8_t mapFunction(uint16_t voltage, uint16_t minVoltage, uint16_t maxVoltage)

To configure your personalized function you only have to provide a pointer to it during initialization:

Battery batt = Battery(3000, 4200, SENSE_PIN);

uint8_t myMapFunction(uint16_t voltage, uint16_t minVoltage, uint16_t maxVoltage) {
  // your code here
}


void setup() {
  batt.begin(3300, 1.47, &myMapFunction);
}

You are not limited in considering only the parameters listed in the function interface, meaning you can take into consideration the cell(s) temperature, current consumption or age: that's open to your requirements and circuitry.

Good enough

After collecting a few data points on battery voltage vs. battery capacity, I've used the https://mycurvefit.com/ and https://www.desmos.com online tools to calculate the math functions best representing the data I've collected.

Mapping functions

In the above plot I represent the battery percentage (Y axis) as a function of the difference between the current battery voltage and the minimum value (X axis): the graph represents a battery with a voltage swing of 1200mV from full to empty, but the functions scale accordingly to the minVoltage and maxVoltage parameters.

The library ships with three different implementations of the mapping function:

  • linear is the default one (dashed red), probably the least accurate but the easiest to understand. Its main drawback is, for most chemistries, it will very quickly go from 25-20% to 0%, meaning you have to select the minVoltage parameter for your battery accordingly. As an example, for a typical Li-Ion battery having a 3V to 4.2V range, you want to specify a 3.3V configuration value as the minimum_ voltage_.
  • sigmoidal (in blue) is a good compromise between computational effort and approximation, modeled after the typical discharge curve of Li-Ion and Li-Poly chemistries. It's more representative of the remaining charge on the lower end of the spectrum, meaning you can set the minimum voltage according to the battery-safe discharge limit (typically 3V for a Li-Ion or Li-Poly).
  • asymmetric sigmoidal (in green) is probably the best approximation when you only look at battery voltage, but it's more computationally expensive compared to sigmoidal function and, in most cases, it doesn't provide a great advantage over its symmetric counterpart.

I strongly encourage you to determine the function that best matches your particular battery chemistry/producer when you want to use this library in your product.

Examples

Here follow a few real case scenarios that can guide you in using this library.

Single-cell Li-Ion on 3.3V MCU

As an example, for a single-cell Li-Ion battery (4.2V - 3.7V) powering a 3.3V MCU, you'll need to use a voltage divider with a ratio of no less than 1.3. Considering only E6 resistors, you can use a 4.7kΩ (R1) and a 10kΩ (R2) to set a ratio of 1.47: this allows you to measure batteries with a maximum voltage of 4.85V, well within the swing of a Li-Ion. It's a little too current-hungry for my tastes in an always-connected configuration, but still ok. Considering the chemistry maps pretty well to our sigmoidal approximation function I'm going to set it accordingly along with the minimum voltage whose lowest safe value clearly is 3.0V (if a Li-Ion is drained below 3.0V the risk of permanent damage is high), so your code should look like:

#define SENSE_PIN A0

Battery batt = Battery(3000, 4200, SENSE_PIN);

void setup() {
  // specify an activationPin & activationMode for on-demand configurations
  //batt.onDemand(3, HIGH);
  batt.begin(3300, 1.47, &sigmoidal);
}

And here you can find the relative circuit.

Double cell Li-Ion (2S) on 5V MCU

For a double-cell Li-Ion battery (8.4V - 7.4V) powering a 5V MCU, you'll need to use a voltage divider with a ratio no less than 1.68: you can use a 6.8kΩ (R1) and a 10kΩ (R2) to set the ratio precisely at 1.68, perfect for our 8.4V battery pack. The circuit will continuously draw 0.5mA in an always-connected configuration, if you can live with that. As we don't want to ruin our battery pack and we don't want to rush from 20% to empty in a few seconds, we'll have to set the minimum voltage to 6.8V (with a linear mapping) to avoid the risk of permanent damage, meaning your code should look like:

#define SENSE_PIN A0

Battery batt = Battery(6800, 8400, SENSE_PIN); 

void setup() {
  // specify an activationPin & activationMode for on-demand configurations
  //batt.onDemand(3, HIGH);
  batt.begin(5000, 1.68);
}

In terms of electrical circuit, here is a simulation.

NOTE: I could have used the sigmoidal approximation, as the chemistry fits pretty well on the curve, in which case a 6V minimum voltage would have been a better configuration value.

9V Alkaline on 5V MCU

Another classic example might be a single 9V Alkaline battery (9V - 6V) powering a 5V MCU. In this case, you'll need to use a voltage divider with a ratio no less than 1.8 and, for the sake of simplicity, we'll go for a nice round 2 ratio. Using a nice 10kΩ both for R1 and R2 we'll be able to measure batteries with a maximum voltage of 10V consuming only 0.45mA. The trick here is to determine when our battery should be considered empty: a 9V Alkaline, being a non-rechargeable one, can potentially go down to 0V, but it's hard for our board can still be alive when this occurs. Assuming we are using a linear regulator to step down the battery voltage to power our board we'll have to account for the regulator voltage drop: assuming it's a 1.2V drop, we might safely consider our battery empty when it reaches 6.2V (5V + 1.2V), leading to the following code:

#define SENSE_PIN A0

Battery batt = Battery(6200, 9000, SENSE_PIN);

void setup() {
  // specify an activationPin & activationMode for on-demand configurations
  //batt.onDemand(3, HIGH);
  batt.begin(5000, 2.0);
}

Excluding resistors and battery values, the circuit is the same as in the first example.

NOTE: Most 5V MCU can actually continue to operate when receiving 4.8V or even less: if you want to squeeze out as much energy as you can you can fine tune the low end, but also consider there is not much juice left when a battery voltage drops that much.

Double cell Li-Ion (2P) on demand for 3.3V MCU with 12 bits ADC

Having two batteries in parallel doesn't change anything in terms of provided voltage, but doubles the capacity (among other things). In this example, we also wish to increase our measurement precision and extend our battery life as much as possible. In terms of resistor values, we will be using 330Ω for R1 and 1kΩ for R2, giving a ratio of 1.33, but we'll use an on-demand configuration to minimize battery consumption. Also, we want maximum precision, thus we will use the more complex and more accurate asigmoidal approximation function, leveraging the increased precision given by the 12 bits ADC provided by the MCU we are going to use, in contrast to the more common 10bits resolution usually available. This is the code we suggest:

#define SENSE_PIN A0
#define ACTIVATION_PIN 3
#define ADC_RESOLUTION 12


Battery batt = Battery(4200, 3300, SENSE_PIN, ADC_RESOLUTION);

void setup() {
 	analogReadResolution(ADC_RESOLUTION);
  batt.onDemand(ACTIVATION_PIN, HIGH);
  batt.begin(3300, 1.47, &asigmoidal);
}

And this is the reference circuit we would implement.

batterysense's People

Contributors

dltj avatar jimjjewett avatar mesmerise avatar nunonunes avatar papylhomme avatar per1234 avatar rlogiacco avatar torsten85 avatar voipminic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

batterysense's Issues

Library assumes 10-bit ADC

uint16_t Battery::voltage() {
	if (activationPin != 0xFF) {
		digitalWrite(activationPin, activationMode);
		delayMicroseconds(10); // copes with slow switching activation circuits
	}
	analogRead(sensePin);
	delay(2); // allow the ADC to stabilize
	uint16_t reading = analogRead(sensePin) * dividerRatio * refVoltage / 1024;
	if (activationPin != 0xFF) {
		digitalWrite(activationPin, !activationMode);
	}
	return reading;
}

The voltage() method assumes a 10-bit ADC based on the code above. (Technically, I think that 1024 should be 1023 which is the full scale value for 10 bits: 0b1111111111 = 0x3FF = 0d1023)

However some boards such as MKR1010 support other resolutions - 12 or 16 bits. Seems like it would be pretty straightforward to add an ADC resolution property to the class and add a setter method for it, with a default of 10 bits. Happy to create a pull request if you agree.

Error in the guide?!

Hey,
Thanks again for your work, i believe you have a mistake in your guide.
image
You say in the description to use a PNP transistor but the circuit uses (and only works as it is) a NPN transistor.

Best Regards

Thinking error.

The "higher-on-demand.png" is wrong.
When the mosfet is 'off', and battery voltage is higher than "MCU VCC +0.5volt", current will still flow through R1 and the pin protection diode of the MCU to VCC of the MCU.

Non linear discharge model

Define a non linear discharge model better approximating the typical battery discharge curve: it doesn't have to be perfect, just accounting for rough variations.
If the model is collapsed into a function, an alternative function can be provided by the user to provide an even better and more accurate model, like via a function pointer uint8_t (*dischargeFunction)(uint16_t)

void Battery::begin(uint16_t refVoltage, float dividerRatio, uint8_t (*dischargeFunction)(uint16_t)) {
  ...
}

uint8_t linearDischargeFunction(uint16_t voltage) {
  return (unsigned long)(voltage - minVoltage) * 100 / (maxVoltage - minVoltage);
}

uint8_t approxDischargeFunction(uint16_t voltage) {
  double norm = (voltage - minVoltage) / (maxVoltage - minVoltage);
  return <some math based on norm>;
}

Which voltage divider for 3.3v board?

The main readme suggest voltage divider like this for 3.3v board

Battery + ----
|
|
|
R1 (4.7kΩ)
|
|
|
|--------- A0
|
|
|
R2 (10kΩ)
|
|
|
Battery - ------

How to connnect this actually? Is it like this?

Battery + ---------- VCC pin
|
|
|
R1 (4.7kΩ)
|
|
|
|--------- A0 pin
|
|
|
R2 (10kΩ)
|
|
|
Battery - ---------- GND pin

Double ADC read when accessing voltage and level

If I read voltage and level from the sensor, the adc is used two times. This could be prevented by reusing the voltage value to compute the battery level.

uint8t level();
uint8t level(uint16_t voltage);

Proposed patch

higher-on-demand circuit

Hi there,

big up & thx for the library, it works very well!

There seems to be a minor issue with the suggested circuit for the higher-on-demand scenario. Correct me if I am wrong, but I believe the n-channel MOSFET in circuit diagram is connected wrongly: Source should be connected to GND and drain should be connected to R2 and not vice versa as shown in higher-on-demand.png.

Best,
Andreas

Higher voltage than expected

Discussed in https://github.com/rlogiacco/BatterySense/discussions/42

Originally posted by Dans-Droids December 6, 2021
Hi, thank you for making this brilliant library!

I am finding that on my STM32F103C8, powered by a single Li-Po through a 5v3a regulator with the sense pin on PA1, the returned voltage is higher than it should be.

I have found that with the value of 5000 in the following, the recorded voltage is around 7680:

battery.begin(5000, 1.47, &sigmoidal);

  • if i change the 5000 to 3300, this causes the recorded voltage to be around 4850 .

Do i need to reference the 1.1v Analog Voltage of the pins?
I am planning to use the ADCTouchSensor library on pin PA6 - this requires the Analog Reference to be floating... Would this interfere with this BatterySense library??

Thank you for your help!

Library conflict

hello
i use batterysence code in my project but it hanged
can you help me
i used this libraries in my project , i think that interfere with another libraries
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
#include <Wire.h>
#include <FHT.h>

Originally posted by @noisecontrol in #31 (comment)

Arduino MKR WiFi 1010 Battery Level

Hey,
Thanks for this library. However, I'm having issues obtaining the battery level. It always reads 100. I have a Li-Po 3.7v 1000mAh battery connected to the Arduino through the on-board JST connector. Any ideas on what issues I may be having? Thanks in advance.

Battery battery = Battery(3000, 4200, ADC_BATTERY);
...
battery.begin(3300, 1.47);
...
battery.level()

Unsteady reading result

Hello,

I'm using the library with Arduino mega to measure 2 of 6AA battery packs wired in parallel. Measured with a digital voltmeter I got 8910 millivolts which is different from the result read by the library. Here is the circuit diagram of my setup based on on-demand circuit configuration: voltmeter_batterySenseLib_0_2_schem and below is the result captured from Arduino serial monitor:

Battery voltage is 4960 (100)
Battery voltage is 60496 (0)
Battery voltage is 60496 (100)
Battery voltage is 60496 (100)
Battery voltage is 4960 (0)
Battery voltage is 14960 (100)
Battery voltage is 50496 (100)
Battery voltage is 60496 (100)
Battery voltage is 4960 (0)
Battery voltage is 60496 (100)
Battery voltage is 14960 (0)
Battery voltage is 57104 (100)
Battery voltage is 4960 (100)
Battery voltage is 60496 (100)
Battery voltage is 4960 (100)
Battery voltage is 4960 (100)
Battery voltage is 4960 (100)
Battery voltage is 4960 (100)
Battery voltage is 14960 (100)
Battery voltage is 40496 (100)
Battery voltage is 14960 (0)
Battery voltage is 14960 (100)
Battery voltage is 4960 (100)
Battery voltage is 4960 (0)
Battery voltage is 4960 (100)
Battery voltage is 4960 (0)
Battery voltage is 60496 (100)
Battery voltage is 14960 (100)
Battery voltage is 4960 (0)
Battery voltage is 60496 (0)
Battery voltage is 14960 (100)
Battery voltage is 50496 (0)
Battery voltage is 14960 (0)
Battery voltage is 4960 (0)
Battery voltage is 14960 (100)
Battery voltage is 4960 (100)
Battery voltage is 60496 (0)
Battery voltage is 4960 (100)
Battery voltage is 14960 (100)
Battery voltage is 4960 (100)
Battery voltage is 14960 (0)
Battery voltage is 60496 (100)
Battery voltage is 14960 (0)
Battery voltage is 4960 (100)

I also did a calibration using EEPROMCalibration code but having no improvement in the reading result.
Any pointers are appreciated.

Thanks.

Battery voltage reporting too high

Hi, I'm surely doing something wrong, but here is my situation in designing a voltage tester for 3S li-ion batteries using a Wemos D1 Mini.
Note the only connection to the arduino is the A0 sense pin and ground. The battery being tested is a separate entity, if that's significant.

#include <Arduino.h>
#include <Battery.h>
// *    +--------+
//  *   |        |
//  *   |       +-+
//  *   |       | | 40k  R1
//  *   |       | |
//  * -----     +-+
//  *  ---       |---------+ A0
//  *   |       +-+
//  *   |       | |
//  *   |       | | 10k  R2
//  *   |       +-+
//  *   |        |
//  *   +--------+---------+ GND
//  **/
const float R1 = 40000.0; // ohms
const float R2 = 10000.0;
const uint16_t battMin = 10500; // millivolts
const uint16_t battMax = 14400;
Battery battery(battMin, battMax, A0);
void setup()
{
  Serial.begin(76800);
  while (!Serial)
    ;
  float ratio = (R1 + R2) / R2;  // 5
  battery.begin(5000, ratio);
}
void loop()
{
  int analogValue = analogRead(A0);
  float batteryVoltage = battery.voltage();
  int batteryLevel = battery.level();
  Serial.print("Analog value: ");
  Serial.print(analogValue);
  Serial.print(", Battery voltage: ");
  Serial.print(batteryVoltage);
  Serial.print(" mV, Battery level: ");
  Serial.print(batteryLevel);
  Serial.println("%");
  delay(200); 
}

At 10.5 volts (the minimum, which should report as 0%), the output is similar to:

Analog value: 675, Battery voltage: 16479.00 mV, Battery level: 100%

Perhaps I'm misunderstanding how the ohms law ratio works here, but the analog value seems to be safely within 1024 as expected but yeah, the battery voltage stated in mV is way too high

Small error in OnDemandDoubleLiIon

It appears that lines 16 and 18 in the example OnDemandDoubleLiIon have a small typo. On line 16, digitalWrite(13, HIGH) should be digitalWrite(3, HIGH). on line 18, digitalWrite(13, LOW) should be digitalWrite(3, LOW). Pin 3 is set as the activation pin earlier in the code.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.