rlogiacco / batterysense Goto Github PK
View Code? Open in Web Editor NEWArduino library to monitor battery consumption
License: GNU Lesser General Public License v3.0
Arduino library to monitor battery consumption
License: GNU Lesser General Public License v3.0
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>;
}
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.
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
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()
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);
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!
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
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
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.
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
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.
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.
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)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.