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?
Fuel Gauge Solution for micro:bit
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- barshatriplee
- Posts: 200
- Joined: Wed Mar 22, 2023 10:11 am
Re: Fuel Gauge Solution for micro:bit
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:
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.
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)
Please be positive and constructive with your questions and comments.