led flash 10 times, not 5?

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
Rissy
Posts: 116
Joined: Sun Aug 14, 2022 8:15 am

led flash 10 times, not 5?

Post by Rissy » Sat Aug 27, 2022 1:09 pm

Why does this script flash the onboard LED on the Pico W, 10 times, instead of 5!?

Code: Select all

import machine
import utime

led = machine.Pin("LED", machine.Pin.OUT)

count = 0
led.off()

while count < 5:
    led.toggle()
    utime.sleep(0.1)
    led.toggle()
    count = count + 1
    utime.sleep(0.5)

Rissy
Posts: 116
Joined: Sun Aug 14, 2022 8:15 am

Re: led flash 10 times, not 5?

Post by Rissy » Sat Aug 27, 2022 2:24 pm

This is my boot.py for the Pico W.

When i run it through Thonny, the LED flashes 10 times.

When i run it outside of Thonny, on boot up, the LED flashes 5 times as programmed.

Has anyone else had something similar this this type of oddity?

beetle
Posts: 51
Joined: Sat Oct 16, 2021 11:35 am

Re: led flash 10 times, not 5?

Post by beetle » Sat Aug 27, 2022 3:23 pm

Rissy, the code you show flashes the Led 5 times. exactly 5 times. As it happens I was putting the latest micropython on to a new rpi PicoW (V1.19.1) so just incase a bug had crept in as theres no stable release showing as yet I put your code on the board and ran it. 5 nice green flashes. :)

Edit: I see you have been putting in more info since I posted this about the code being in a boot.py. I tried to rename my flashing.py to boot.py but got some errors from Thonny. Possibly something to do with Thonny only having a Pico interpreter setting and not a PicoW interpreter. ??

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: led flash 10 times, not 5?

Post by Roberthh » Sat Aug 27, 2022 3:43 pm

Looks like a Thonny thing, executing boot.py twice, once in raw mode for the board set-up magic it does and once for the regular boot.
So a good rule, if something strange happens is go back to barre bone and kick out any "helpful" tools.

Rissy
Posts: 116
Joined: Sun Aug 14, 2022 8:15 am

Re: led flash 10 times, not 5?

Post by Rissy » Sat Aug 27, 2022 3:53 pm

beetle wrote:
Sat Aug 27, 2022 3:23 pm
Rissy, the code you show flashes the Led 5 times. exactly 5 times. As it happens I was putting the latest micropython on to a new rpi PicoW (V1.19.1) so just incase a bug had crept in as theres no stable release showing as yet I put your code on the board and ran it. 5 nice green flashes. :)

Edit: I see you have been putting in more info since I posted this about the code being in a boot.py. I tried to rename my flashing.py to boot.py but got some errors from Thonny. Possibly something to do with Thonny only having a Pico interpreter setting and not a PicoW interpreter. ??
Does that mean the Pico doesn't have a boot.py privilege then?

Rissy
Posts: 116
Joined: Sun Aug 14, 2022 8:15 am

Re: led flash 10 times, not 5?

Post by Rissy » Sat Aug 27, 2022 3:54 pm

Roberthh wrote:
Sat Aug 27, 2022 3:43 pm
Looks like a Thonny thing, executing boot.py twice, once in raw mode for the board set-up magic it does and once for the regular boot.
So a good rule, if something strange happens is go back to barre bone and kick out any "helpful" tools.
Weird one for people to note, that one is!

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: led flash 10 times, not 5?

Post by jimmo » Fri Sep 02, 2022 11:38 am

Remember that boot.py should only be used to configure hardware that needs to happen before main.py runs (or is required to load main.py, for example configuring filesystems). On the pyboard for example, you must configure the USB mode in boot.py if you want to see the output of main.py on the USB serial.

There are very few reasons to use boot.py on the Pico.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: led flash 10 times, not 5?

Post by dhylands » Tue Sep 06, 2022 5:30 pm

boot.py will run every time that the micropython board is soft reset, which included entering the RAW REPL. Programs like Thonny, rshell, mpremote all use the raw repl for every command that they execute.

rshell executes about 5 commands when it connects to a board, so your boot.py will get run 5 times. However, main.py doesn't get run when entering the RAW repl, just boot.py.

Post Reply