I would like to use the threading module in a micropython programme running on a Plasma 2350W controller to implement a simple, continuously running background task but Thoony simply reports" no module named threading" in response to the programme line “import threading”. Should this be possible? I am trying to flash the colon on a 4 digit Sparkfun LED display, independently of whatever else is going on. The programme I am using to see if this could work is:-
import time
import threading
def colon_flashing():
while True:
led.set_colon(True).draw()
time.sleep(1)
led.set_colon(False).draw()
time.sleep(1)
#led.set_colon(True).draw()
time.sleep(2)
for x in range (12):
led.set_brightness(x)
led.set_character(str(1),0).set_character(str(2),1).draw()
led.set_character(str(3),2).set_character(str(4),3).draw()
print(x)
time.sleep(1)
#led.set_colon(False).draw()
for y in range (16, 0, -1):
led.set_brightness(y)
led.set_character(str(1),0).set_character(str(2),1).draw()
led.set_character(str(3),2).set_character(str(4),3).draw()
time.sleep(1)
time.sleep(5)
I feel sure there are other problems with this code but at the moment I can’t get past the “no module named threading”. From what I have gleaned so far micropython should support a threading function. Any help gratefully received.
Yes, don’t waste your time with threading, follow the advice of @SirFico. asyncio is the way to go. I have done LED animations, music playing, motors all at the “same time” with asyncio and once you understand the concept it is not much different to what you are used to from (traditional) threading.
If you look around this forum, you will find a link to an asyncio introduction that somebody had posted some time ago.
I would concur that asyncio is the way to go for this.
I’ve got a script running on my Plasma which has one thread handling the update loop for the LEDs, and another one listening on port 80 for commands via HTTP. The latter updates a variable telling the LEDs what pattern to run, with the former loop doing the work.
I got a bit of help from Gemini to get the basic structure how to do it and built on it from there.
Future expansion is to be able to send actual LED colours via the HTTP command too but not done that yet.
Google Gemini the AI bot, asked for advice on how to implement and it suggested asyncio or pinning tasks to specific CPU cores. It recommended the former method though.
For good reason. Threading is very limited and marked as experimental. It is explicitly not documented in the official MicroPython docs. So do yourself a favor and don’t use it, unless you are a wizard. In contrast, asyncio is very well documented and you will find many examples, tutorials and docs about it.
Yeah much better and works really well, you’ve just got to make sure to yield back properly. For simple loops like running LEDs it’s perfectly fine.
Maybe when it matures a bit it might be viable for some workloads but realistically, if asyncio is not sufficient for your needs, the Pico may not be the best choice either.
In any case, the Pico is a tremendous little device!