Itsy Bitsy express - reading battery voltage

Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
alistair_walterhartley
 
Posts: 3
Joined: Wed Mar 09, 2022 7:00 pm

Itsy Bitsy express - reading battery voltage

Post by alistair_walterhartley »

I am interested in being the battery voltage via Bluetooth to an app, so I can monitor the charge while using the app. Does anyone know how to read the input voltage on the batt pin?

Thanks

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: Itsy Bitsy express - reading battery voltage

Post by mikeysklar »

This is an example for the ItsyBitsy nRF52840 Express:

https://learn.adafruit.com/adafruit-its ... -2865012-8

Code: Select all

#include <Arduino.h>
#include <Adafruit_TinyUSB.h> // for Serial

#if defined ARDUINO_NRF52840_CIRCUITPLAY
#define  PIN_VBAT          A8   // this is just a mock read, we'll use the light sensor, so we can run the test
#endif

uint32_t vbat_pin = PIN_VBAT;             // A7 for feather nRF52832, A6 for nRF52840

#define VBAT_MV_PER_LSB   (0.73242188F)   // 3.0V ADC range and 12-bit ADC resolution = 3000mV/4096

#ifdef NRF52840_XXAA
#define VBAT_DIVIDER      (0.5F)          // 150K + 150K voltage divider on VBAT
#define VBAT_DIVIDER_COMP (2.0F)          // Compensation factor for the VBAT divider
#else
#define VBAT_DIVIDER      (0.71275837F)   // 2M + 0.806M voltage divider on VBAT = (2M / (0.806M + 2M))
#define VBAT_DIVIDER_COMP (1.403F)        // Compensation factor for the VBAT divider
#endif

#define REAL_VBAT_MV_PER_LSB (VBAT_DIVIDER_COMP * VBAT_MV_PER_LSB)


float readVBAT(void) {
  float raw;

  // Set the analog reference to 3.0V (default = 3.6V)
  analogReference(AR_INTERNAL_3_0);

  // Set the resolution to 12-bit (0..4095)
  analogReadResolution(12); // Can be 8, 10, 12 or 14

  // 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);
}


User avatar
alistair_walterhartley
 
Posts: 3
Joined: Wed Mar 09, 2022 7:00 pm

Re: Itsy Bitsy express - reading battery voltage

Post by alistair_walterhartley »

Hi, thank you. I missed this code as I have been looking for Python codes.

I will try it and see if I can convert it to Python.

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: Itsy Bitsy express - reading battery voltage

Post by mikeysklar »

Use get_voltage() with CircuitPython to read a BAT pin.

Code: Select all

# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import board
import analogio

vbat_voltage = analogio.AnalogIn(board.D9)


def get_voltage(pin):
    return (pin.value * 3.3) / 65536 * 2


battery_voltage = get_voltage(vbat_voltage)
print("VBat voltage: {:.2f}".format(battery_voltage))

https://learn.adafruit.com/adafruit-fea ... management

User avatar
alistair_walterhartley
 
Posts: 3
Joined: Wed Mar 09, 2022 7:00 pm

Re: Itsy Bitsy express - reading battery voltage

Post by alistair_walterhartley »

If I want to use UART.Write to send the voltage reading into an app, is there a specific format for example byte or string? I tried it with the variable in the brackets and get a Len() error?

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: Itsy Bitsy express - reading battery voltage

Post by mikeysklar »

Since CircuitPython 4.0, 'buf' being passed to uart.write(buf) must be in bytes.

Code: Select all

write(self, buf: circuitpython_typing.WriteableBuffer)→ Optional[int]
Write the buffer of bytes to the bus.

New in CircuitPython 4.0: buf must be bytes, not a string.

return
the number of bytes written

rtype
int or None

reset_input_buffer(self)→ None
Discard any unread characters in the input buffer.
Here is some example syntax from the UART Communication Between Two CircuitPython Boards guide.

Code: Select all

# Messages are of the form:
# "<TYPE,value,value,value,...>"
# We send and receive two types of messages:
#
# Message contains a light sensor value (float):
# <L,light>
#
# Message contains statuses of two buttons. Increment NeoPixel brightness by 0.1 if the second
# button is pressed, and reduce brightness by 0.1 if the first button is pressed.
# <B,btn_A,btn_B>

uart.write(bytes(f"<L,{light}>", "ascii"))

uart.write(bytes(f"<B,{btn_A.value},{btn_B.value}>", "ascii"))

https://learn.adafruit.com/uart-communi ... -3101783-2

Locked
Please be positive and constructive with your questions and comments.

Return to “Itsy Bitsy Boards”