HELP Display Battery Volt on Oled

im doing a simple code to show battery full/medium/minimum.

board is arduino nano powered by 3.7v lipo battery directly on 5v pin & GND. is my battVolt correct? the "370 - 340 - 310" value?

i copy this code somewhere but the original said for 2x AA battery (1.5v each) and the value is "270 - 240 - 210".

anyway here is my sketch

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <avr/sleep.h>


#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);


int battVolts;
#define batteryInterval 10000
double lastBatteryTime = 0;


void setup () {

  battVolts = getBandgap(); //Determine what actual Vcc is, (X 100), based om known bandgap voltage.

}


// Returns actual value of Vcc (X 100)
int getBandgap(void) {
  const long InternalReferenceVoltage = 1056L;  // Adjust this value to your boards specific internal BG voltage x1000
  // REFS1 REFS0          --> 0 1, AVcc internal ref. -Selects AVcc external reference
  // MUX3 MUX2 MUX1 MUX0  --> 1110 1.1V (VBG)         -Selects channel 14, bandgap voltage, to measure
  ADMUX = (0 << REFS1) | (1 << REFS0) | (0 << ADLAR) | (1 << MUX3) | (1 << MUX2) | (1 << MUX1) | (0 << MUX0);
  // Start a conversion
  ADCSRA |= _BV( ADSC );
  // Wait for it to complete
  while ( ( (ADCSRA & (1 << ADSC)) != 0 ) );
  // Scale the value
  int results = (((InternalReferenceVoltage * 1024L) / ADC) + 5L) / 10L; // calculates for straight line value
  return results;
}



void loop () {

  if (millis() >= lastBatteryTime + batteryInterval) {
    lastBatteryTime = millis();
    battVolts = getBandgap();
  }


   display.clearDisplay();

   // battery indicator
  display.drawRect(119, 5, 6, 8, WHITE);

  // battery indicator for 3.7v LiPo.
  if (battVolts > 370) {
    // full
    display.fillRect(120, 5, 4, 7, WHITE);
  } else if (battVolts > 340) {
    // medium
    display.fillRect(120, 8, 4, 5, WHITE);
  } else if (battVolts > 310) {
    // minimum
    display.fillRect(120, 10, 4, 3, WHITE);
  } else {
    // empty
  }
 
   display.display();

  }

That line doesn't do what you intend. shifting a zero any number of bits and then or'ing it with anything is the same as doing nothing. It does not set the bit to 0. To do that, you have to AND it with the inverse.

ADMUX &= ~(0 << REFS1);

I find it is usually easier to start with 0 and then just set the bits you need.

byte temp = 0;
ADMUX =  (1 << REFS0) |  (1 << MUX3) | (1 << MUX2) | (1 << MUX1);
ADMUX = temp;

This should be unsigned long

Not the correct way to do unsigned math

if (millis() - lastBatteryTime >= batteryInterval) {

Can't say about your code, but lipos like I think you have are

nominally full @ 4.2 volts
nominally half charged @ 3.75 (ppl use 3.7 to 3.8)
and empty-ish @ 3.2 volts

all subject to details of the use case and strong opinions.

If you can possibly, don't charge above 4.15 volts, and don't discharge under load to less than 3.2 volts.

So you get to pick you own indicators. I would prefer to have more bars and a better resolution. Lipo chemistry is nice in one sense, that is that battery votrage can be used directly to determine state of charge.

You'll get less run time per charge cycle, but increase the battery life more than enough to make the difference.

Read about Lipo chemistry for other caveats and warnings, no better place than

HTH

a7

Hi @fyrus,

If you wish to monitor the battery status correctly, you could try using Adafruit MAX17048 LiPoly / LiIon Fuel Gauge and Battery Monitor :wink:

HTH?

Not all AA batteries are created equal

Carbon-zinc batteries deliver 1.55V
Alkaline batteries deliver 1.5V
NiMH rechargeable batteries deliver 1.2V

So check what your batteries nominal values are

Thanks fpr reply, can i use your help to provide maybe the correct code/line?

Is there anyway without external parts? My project "box" wont fit to add more paets

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.