micropythonraspberry-pi-pico

Why does R-Pi Pico not read pin status when running from battery


Simple code: read the status (high or low) of a pin. The pin is either left unconnected, or connected to a ground pin on the Pico. It works when the Pico is powered from USB from a Windows PC, but not when powered by a 5v power block into the Pico micro-usb connector, then the pin is never seen "low"

Tried on several pins of the Pico, all the same. Using Micropython v 20240602-v1.23.0 My code: Copied to Pico as main.py:

from machine import Pin
import uos
import sys 
import utime

# assign Pico pins NB pin numbers are GPIO numbers
En = Pin(3, Pin.IN, Pin.PULL_UP)

LED = Pin(25, Pin.OUT)          # internal LED

#MAIN
if En.value() == 0:
    LED.value(1)
    sys.exit()

if En.value() == 1:
    i = 0
    while i < 50:
        utime.sleep(0.1)
        LED.toggle()
        i += 1

LED.value(0)
sys.exit()

Solution

  • I’m going to guess that when the board is connected to your PC you run the code from Thonny with the board already powered up, whereas with the power block you are plugging the board into the power block and observing the LED.

    I’d start by seeing if this is related to the board’s behaviour on powering up. I’d see what happens if you add utime.sleep(1) after the imports, or put a loop around the #main code.

    Once the board is stable, the PULL_UP should mean the pin isn’t floating.