Pi Pico Debug

While playing with my Pico Pi I was trying to get the Wifi to work, the code worked while being powered by USB but as soon as I only connected it to 3v batterys (X2 AA 1.5v) it just made a humming sound and I wasnt sure what what going on.

Spoiler alert, after using 4.5v (3X AA 1.5V) batterys it worked so my assumption is the wifi module needs more power 🤷

These are just some of the steps I followed and will build upon these (and update here) as I learn.

LED Communication / Boot Sequence

I added a boot sequence to the start-up by quickly flashing the LED on/off 3 times. The value here was I knew my code was executing and could debug from there.

1
2
3
4
5
6
7
8
9
10
11
async def boot_led():   
onboard.off()
boot_show = 3
wait_for = 0.3
while boot_show > 0:
print(boot_show)
onboard.on()
await asyncio.sleep(wait_for)
onboard.off()
await asyncio.sleep(wait_for)
boot_show -= 1

Text Logging

Im not a MicroPython expert and it does look to have existing in logging modules however I like to do things a little more manual first and then once I understand the process use a module. The value I feel is I then 1. appreciate what is abstracted away from me and 2. understand the actual problem its solving.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import time

def log_error(e):
log=open("log.txt","a") # append

try:
print("An exception occurred")
year, month, day, hour, mins, secs, weekday, yearday = time.localtime()
today = "{}-{:02d}-{:02d} {}:{}:{}".format(year, month, day, hour, mins, secs) # YYYY-MM-DD hh:ss:sss
print(today)

log.write(today + " - ")
log.write(str(e)+"\n\n")
finally:
log.flush()

https://github.com/carlpaton/PicoPiHoon/blob/main/Pi%20Pico%20W/SMARSCar/mylog.py#L3

I then added a except in my main code

1
2
3
4
5
6
try:
asyncio.run(main())
except Exception as e:
log_error(e)
finally:
asyncio.new_event_loop()

https://github.com/carlpaton/PicoPiHoon/blob/main/Pi%20Pico%20W/SMARSCar/main.py#L22

This gave me basic logging like the below. Ideally I’d like to include a stack strace but currently it wasnt needed.

1
YYYY-MM-DD hh:ss:sss - The error message that was thrown

Hardward Debug

The Pico has hardward debug ability using debug pins. I figured out my problem with the steps above so this kind of debug is something I will probably explore later. The video linked below shows how to do this with another Pico Pi (Warning it look complicated :D)