Fuel Gauge Solution for micro:bit

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
burksfamly
 
Posts: 31
Joined: Fri Jan 06, 2017 10:20 pm

Fuel Gauge Solution for micro:bit

Post by burksfamly »

I have been researchng a fuel gaige solution for micro:bit v2 to monitor battery life. Any support libraries must be micropython for the microbit...not MakeCode or others. I have several external circuit design candidates, but the are elaborate. I have seen code example for the nrf51 using intrinsix hw, but they are in cpp using proprietary libraries not supporting micropython. I have looked at som STEMMA JST solutions on Adafruit, but not supporting micropython. My last resort is an external circuit that uses a digital pin to control logic state to tturn on circuit before I tead voltage via adc analog pin...then turn off via digital pin. There has to be a better way for micro:bit under micropython.

Any suggestions?

User avatar
barshatriplee
 
Posts: 200
Joined: Wed Mar 22, 2023 10:11 am

Re: Fuel Gauge Solution for micro:bit

Post by barshatriplee »

Use the built-in analog-to-digital converter (ADC) to read the battery voltage directly. You can connect the positive side of the battery to one of the analog input pins (e.g. pin 0), and use the ADC module to read the voltage. Here's some sample code to get you started:

Code: Select all

       
from microbit import *
import machine

# Set up ADC on pin 0
adc = machine.ADC(0)

while True:
    # Read voltage and convert to millivolts
    voltage = adc.read_u16() * 3.3 / 65535 * 2

    # Print voltage to serial console
    print("Battery voltage: {:.2f}V".format(voltage))

    # Wait a few seconds before reading again
    sleep(5000)
 
Note that the maximum voltage the ADC can handle is 3.3V, so if your battery voltage is higher than that, you'll need to use a voltage divider circuit to bring it down to a safe level.

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

Return to “Microcontrollers”