Inky Frame - distinguishing between power sources

Is there a way on the Inky Frame (in MicroPython) to find out if it’s being USB powered or battery powered?

I’ve been trying to find this information for a while now. I finally found something which found I to be of great help. Although this is C code (which I was specifically looking for), it may give a clue as to how it can be done in micropython.

Interesting, thanks. I haven’t gone back to looking at this. I previously found the discussion [Raspberry Pi Forum: Pico W (MicroPython) Bug. Sounds like anything in this space on Pi Pico W needs lots of testing with Wi-Fi active.

@hel
provided a code snippet in Inky Frame - Deep Sleep explanation - #9 by hel but the comment in post and comments I left in my code suggests something is seriously amiss with reading the VBUS digital input on a Pi Pico W which is half of VBUS voltage provided by a a pair of 10k resistors.

It looks like my earlier optimism regarding the code examples provided has turned to disappointment. Although the examples do give a clue as to how it is possible to distinguish between power sources (USB vs battery) when using just the Pico W, they do not work when using the Inky Frame; presumably because of the way that the various pins on the Pico W interact with pins used by the Inky Frame.

I was hoping to include some code in my project to distinguish between power sources so that I could then decide whether to simply loop with a delay (when on USB power) or call the sleep function of the InkyFrame (when on battery power).

I might need to rethink my approach :-(.

If you know the battery voltage then you could use something like voltage < 4.75 to indicate battery power?

Using the voltage of VSYS is an easy workaround as @kjw states. But the PicoW has a dedicated pin to detect VBUS (WL_GPIO2 IP VBUS sense).

So from the hardware it is possible to distinguish power sources.

I don’t know if MicroPython (or the variant from Pimoroni) exposes this pin, but in CircuitPython builds for the PicoW, I always have a pin call VBUS_SENSE and another pin called VOLTAGE_MONITOR. The latter gives me ADC-readings of VSYS, the former tells me if VBUS is available.

I’ve been using the following micropython code to determine if USB power is present.
I only have PICO W but I believe the code will work on both PICO and PICO W.
I am using this code on Badger2040W. Inky Frame should be the same.

from machine import Pin
# If VBUS pin is low then the PICO is not connected to USB
try :
    # Try PICO_W pin first, note: 
    # This will power up the cyw43 wifi chip if it is not powered up already.
    usb_present=Pin("WL_GPIO2").value()
except ValueError :
    # Not PICO_W access the PICO Pin 
    usb_present=Pin("GPIO24").value()

My application assumes it is running on battery when usb_present is False.
When running on battery it takes action to reduce power consumption.
For example, I reduce the RP2040 cpu clock speed, and turn off hardware block I don’t need (off topic).

1 Like

I did a bit more testing and from a few minutes playing with a 5.7" Inky Frame running pimoroni-picow_inky_frame-v1.19.16-micropython.uf2 the VBUS sensing now seems to work. I had strange results and it was taking about a second to give those strange results last time. It’s now sub 1 ms and looks ok.

The program display the voltage on buttons LEDs and turns the busy led on if USB power is in use. The code is micropython-examples/inkybuspower.py at master · kevinjwalters/micropython-examples · GitHub . WiFi isn’t used in that code, would be interesting to see how things behave with that turned up and actively used.

The one oddity with that is if I take an Inky Frame and put it on battery power (only) and then wake it up with button A then the first reading of the voltage is 0.087V. After that first reading it’s fine. Tut.