Oct
9

How to measure battery voltage on Adafruit nRF52840 Feather

10/09/2023 10:05 PM by Admin in Iot


So in this tutorial we are going to measure the LiPo battery voltage connected to Adafruit nRF52 Feather Bluefruit board and display it in the serial console. First upload this code to the board through arduino IDE.

#define VBAT_PIN          (A7)
#define VBAT_MV_PER_LSB   (0.73242188F)
#define VBAT_DIVIDER      (0.71275837F)
#define VBAT_DIVIDER_COMP (1.403F)

int vbat_raw;
uint8_t vbat_per;
float vbat_mv,vbat_v;

void setup(void) {
  Serial.begin(115200);

  analogReference(AR_INTERNAL_3_0);
  analogReadResolution(12);  // Can be 8, 10, 12 or 14
  delay(1);
}

void loop(void) {
  vbat_raw = analogRead(VBAT_PIN);
  vbat_per = mv_to_percent(vbat_raw * VBAT_MV_PER_LSB);
  
  vbat_mv = (float)vbat_raw * VBAT_MV_PER_LSB * VBAT_DIVIDER_COMP;
  vbat_v = vbat_mv/1000;
  
  Serial.print("ADC = ");
  Serial.print(vbat_raw * VBAT_MV_PER_LSB);
  Serial.print(" mV (");
  Serial.print(vbat_raw);
  Serial.print(") ");
  Serial.print("LIPO = ");
  Serial.print(vbat_v);
  Serial.print(" V (");
  Serial.print(vbat_per);
  Serial.println("%)");

  delay(4000);
}

uint8_t mv_to_percent(float mvolts) {
  uint8_t battery_level;

  if (mvolts >= 3000) {
    battery_level = 100;
  } else if (mvolts > 2900) {
    battery_level = 100 - ((3000 - mvolts) * 58) / 100;
  } else if (mvolts > 2740) {
    battery_level = 42 - ((2900 - mvolts) * 24) / 160;
  } else if (mvolts > 2440) {
    battery_level = 18 - ((2740 - mvolts) * 12) / 300;
  } else if (mvolts > 2100) {
    battery_level = 6 - ((2440 - mvolts) * 6) / 340;
  } else {
    battery_level = 0;
  }

  return battery_level;
}

Next attach a LiPo battery with JST connector to Adafruit nRF52 board connector. Then use the USB cable to charge the board. Ensure yellow CHG LED is ON during charging. Now open serial console to see the battery charging status , once yellow CHG LED is OFF that mean battery charging is completed.


Your Thoughts

Search
SPONSOR
CRYPTOWATCH
FOLLOW US
ANNOUNCEMENTS

New tool added : SVG Zoom Dimension Calculator.

SPONSOR