[SOLVED]Toggling a led on Nucleo F411. Do I need a delay?

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
antwtna
Posts: 3
Joined: Fri Mar 08, 2019 9:38 am

[SOLVED]Toggling a led on Nucleo F411. Do I need a delay?

Post by antwtna » Fri Mar 08, 2019 9:53 am

Hi guys,
I am quite new to micropython, I usually only use C on micros and a bit of Python to test things.

I have a Nucleo F411 and yesterday I compiled micropython and flashed it, and then I did some small test using rshell to change main.py.

When I tried the last little test I saw something that I cannot explain easily, this was the code in main:

[code]
import pyb

print("Hello from Micropython!")
pyb.LED(1).toggle()
[/code]

What I saw is that the Led went always on after pressing the reset button and seeing the print on my serial terminal. Afterward, if I sent the same command through the serial the led effectively toggled.

What is physically happening?
Do I need to insert a delay for any reason?

Thanks :)
Last edited by antwtna on Fri Mar 08, 2019 3:42 pm, edited 1 time in total.

ThomasChr
Posts: 121
Joined: Sat Nov 25, 2017 7:50 am

Re: Toggling a led on Nucleo F411. Do I need a delay?

Post by ThomasChr » Fri Mar 08, 2019 10:39 am

toggle() is ONE command. If the LED is off, it gets switched on, if it's on it gets switched off.
If you want to blink a led you have to call toogle() in a loop - and yes, in this case you need a delay because you won't be able to see the blinking otherwise because it is way too fast for your slow human eyes.

antwtna
Posts: 3
Joined: Fri Mar 08, 2019 9:38 am

Re: Toggling a led on Nucleo F411. Do I need a delay?

Post by antwtna » Fri Mar 08, 2019 10:44 am

Hi, thanks for your answer.
I really wanted to see the Led change at every reset, and I expected that if I pressed the reset when the led was off it would turn on, and the next time, when the led is already on, pressing the reset button the led would turn off.
Anyway, your answer made me think about it, and I realize that the led status may be cleared at every reset, and this means that the led is turned off at every reset and then toggled on by that code. Silly me for not realizing this :P

A better test would have been a callback linked to the USR button.

Thank you!

ThomasChr
Posts: 121
Joined: Sat Nov 25, 2017 7:50 am

Re: Toggling a led on Nucleo F411. Do I need a delay?

Post by ThomasChr » Fri Mar 08, 2019 12:59 pm

Yes, every microcontroller tries a clean reset which sets all periphials to a clean status.
Otherwise programs can't be sure how the requested periphial is configured.

antwtna
Posts: 3
Joined: Fri Mar 08, 2019 9:38 am

Re: [SOLVED]Toggling a led on Nucleo F411. Do I need a delay?

Post by antwtna » Fri Mar 08, 2019 3:47 pm

Yes, that was a very dummy question, sorry :oops:
The fact is that I was only thinking from a "software" perspective("ok, it should toggle"), and my mind was kind of stuck into it.

Post Reply