Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Badger 2040 badger_os get_battery_level, strange behaviour #334

Closed
veryalien opened this issue Apr 2, 2022 · 7 comments
Closed

Badger 2040 badger_os get_battery_level, strange behaviour #334

veryalien opened this issue Apr 2, 2022 · 7 comments
Labels
[- badger2040 -] https://shop.pimoroni.com/products/badger-2040 bug Something isn't working

Comments

@veryalien
Copy link

veryalien commented Apr 2, 2022

In the badger 2040 example launcher.py I noticed that the battery level was only shown correctly when the USB cable was also connected. This defeats the purpose of monitoring how much battery power is left.

The ADC and VREF pins don't seem to be getting initialised correctly for badger_os, so the call to:

badger_os.get_battery_level()

doesn't return anything useful when running only on batteries. Which is pretty useless.

The badger 2040 example battery.py works correctly running only on battery power. That example 'initialises' the ADC and VREF pins. I copied the following to the top of launcher.py and now it's all working as I'd expect with or without a connected battery which itself is either switched on or off:

import badger_os
from machine import Pin, ADC

vbat_adc = ADC(badger2040.PIN_BATTERY)
vref_adc = ADC(badger2040.PIN_1V2_REF)
vref_en = Pin(badger2040.PIN_VREF_POWER)
vref_en.init(Pin.OUT)
vref_en.value(0)

# Reduce clock speed to 48MHz

...

MAX_BATTERY_VOLTAGE = 3.2
MIN_BATTERY_VOLTAGE = 2.0 

I completely nuked my badger and started again. I completely replaced the default main.py with the launcher.py code and I got the battery level working just with this additional code which is also in badger_os.py (see my commment below):

from machine import ADC

vbat_adc = ADC(badger2040.PIN_BATTERY)

Sorry for all the edits:
I nuked it all again and did another experiment, if you just add a simple dummy call to badger_os.get_battery_level() right at the 'top' of launcher.py after import badger_os, just ignore the returned value, who cares. It all then works on just battery power (no extra import of ADC and setting vbat_adc is needed). There's something fishy with some kind of initialisation somewhere, but I don't know what's really happening.

I think there is a least one other issue about badger battery level monitoring and which values are correct for various types of batteries. The comments in the code should be corrected and improved to clarify the min/max range for all types of batteries.

@veryalien
Copy link
Author

veryalien commented Apr 2, 2022

When I looked at the code for badger_os here:

https://github.com/pimoroni/pimoroni-pico/blob/main/micropython/examples/badger2040/badger_os.py

It apparantly does exactly the same thing, and the battery.py example with (MAX_BATTERY_VOLTAGE = 3.2 and
MIN_BATTERY_VOLTAGE = 2.0) works fine only on battery power. But it definitely doesn't work properly in launcher.py on my badger, loaded with the latest pimoroni-pico release 1.18.6 from April 1st (hmmm?!)

@veryalien veryalien changed the title Badger 2040 badger_os get_battery_level only works with USB power connected Badger 2040 badger_os get_battery_level, strange behaviour Apr 2, 2022
@helgibbons
Copy link
Contributor

helgibbons commented Apr 4, 2022

With the new improved 'sleep' behaviour the launcher doesn't stay awake for long enough to get a true voltage reading for the battery icon - this was mentioned in the release notes for 1.18.5:

Battery indicator mostly reads as disconnected, since it needs some time for the vref to stabilise.

You could save the battery.py example to your Badger if you wanted a way to read how much battery is left when not plugged in (or mod the launcher to stay awake for longer so it can read the battery correctly I guess, but that seems a bit counterintuitive?)

I wonder if it's worth ditching the battery icon from the launcher and adding battery.py as a built in if we can't think of a pleasing solution to this?

@helgibbons helgibbons added the bug Something isn't working label Apr 4, 2022
@veryalien
Copy link
Author

As far as I can see, it's working when I add the first call. I suppose it might never really update to show the true battery level.
I'll go back to the old-fashioned method of just replacing empty batteries when I notice it doesn't work any more.
Not a major loss if this function gets removed in the future because it doesn't make sense any more. The 'problem' is closed for me.

@1337Reaper
Copy link

1337Reaper commented Apr 25, 2022

Much like the users above, I am having trouble with accurate battery readings. I have to ask what the point is of having a battery meter if it doesn't work properly? I would like to be notified that I am losing power before it actually happens. This is basic functionality IMO.

@TheLogster
Copy link

The addition of a throw-away call to badger_os.get_battery_level() in the laucher.py is a simple fix.

@ZodiusInfuser ZodiusInfuser added the [- badger2040 -] https://shop.pimoroni.com/products/badger-2040 label Jan 20, 2023
@Gadgetoid
Copy link
Member

Badger 2040 and Badger 2040 W now live here: https://github.com/pimoroni/badger2040/

Closing this issue to keep Badger 2040 to its new home. I've made a note to look into this, but please feel free to re-open over at https://github.com/pimoroni/badger2040/issues with a link back here for posterity if it warrants more investigation.

@Arthur-Philip-Dent
Copy link

I stumbled upon this here, since I am wondering, where the battery icon is gone in [v0.0.4](https://github.com/pimoroni/badger2040/releases/tag/v0.0.4 and even )
v0.0.3.

So some issue is already documentet for the 2040W here but here it says, it's not yet implemented in the badger2040.

Did I get that right or is it just me, having no towel and being still dizzy from the improbility drive?

Thanks for any fish, err, hint!

The Hitchhiker

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[- badger2040 -] https://shop.pimoroni.com/products/badger-2040 bug Something isn't working
Projects
None yet
Development

No branches or pull requests

7 participants