Datasheet
// Let the ADC settle
delay(1);
// Get the raw 12-bit, 0..3000mV ADC value
raw = analogRead(vbat_pin);
// Set the ADC back to the default settings
analogReference(AR_DEFAULT);
analogReadResolution(10);
// Convert the raw value to compensated mv, taking the resistor-
// divider into account (providing the actual LIPO voltage)
// ADC range is 0..3000mV and resolution is 12-bit (0..4095)
return raw * REAL_VBAT_MV_PER_LSB;
}
uint8_t mvToPercent(float mvolts) {
if(mvolts<3300)
return 0;
if(mvolts <3600) {
mvolts -= 3300;
return mvolts/30;
}
mvolts -= 3600;
return 10 + (mvolts * 0.15F ); // thats mvolts /6.66666666
}
void setup() {
Serial.begin(115200);
while ( !Serial ) delay(10); // for nrf52840 with native usb
// Get a single ADC sample and throw it away
readVBAT();
}
void loop() {
// Get a raw ADC reading
float vbat_mv = readVBAT();
// Convert from raw mv to percentage (based on LIPO chemistry)
uint8_t vbat_per = mvToPercent(vbat_mv);
// Display the results
Serial.print("LIPO = ");
Serial.print(vbat_mv);
Serial.print(" mV (");
Serial.print(vbat_per);
Serial.println("%)");
delay(1000);
}
© Adafruit Industries https://learn.adafruit.com/adafruit-itsybitsy-nrf52840-express Page 22 of 179










