Multi Thread Coding on the Raspberry Pi Pico in MicroPython - Threads, Locks and problems!

preview_player
Показать описание
The Raspberry Pi Pico uses the RP2040 microcontroller chip which has two ARM Cortex M0+ cores. Usually when we code we only use a single core. This leaves half of the processing power idle.

In this tutorial I'll show you how to run code in parallel on the second core and how to correctly manage and share resources between the two threads.

I'll also show you how I got around some of the bugs in the threading package, which is still in the experimental stage.

Don't forget to check out my project page for more details and links to all the code used in the video.

You can also access the code at the project GitHub repository at

0:00 Introduction
1:56 Multi thread coding
3:26 Threading Package
6:06 Simple dual core example
8:50 Using global variables to share data
12:03 Using classes to share data
15:56 Using Locks or Semaphores
19:02 Lock example code
23:07 Polling the Lock
26:32 SPI LCD example
28:59 Bug in thread package
30:27 Running second thread on demand
36:00 Conclusion
Рекомендации по теме
Комментарии
Автор

Really useful, going to use it to control two independent motors, one steering, one driving... Nice

seanswales
Автор

Liked your tutorial, with a stream of examples from understanding the basics to a real case usage.
I was searching threading related info for Raspberry Pi Pico, and this tutorial nicely appeared: I'm sure I'll give it a try.
Thank you very much. Andrea

andreafavero
Автор

Excellent explanation and great video. Thank you for helping us to better understand threading on Python.

manuelcarriedo
Автор

I'm Finally getting the time to play with many of these Microcoller Modules "Pi's / Arduino's / ESP32's".. I appreciate all the Tips and Tricks Videos like these.. (For Newbies like ME) 👍👍😊

x_CrossHair_x
Автор

Have followed your tutorials with great interest; excellent work, very well explained. Thank you very much for sharing your insights. Rob

robdriessen
Автор

It would be interesting to compare the maximum frame rates of each method to determine how much more efficient each was compared to using only one core. There must be some overhead involved in switching cores that could be quite significant.

jeffschroeder
Автор

Thanks a lot, really good explanation. My automatic plants watering system soon will be working

FranciscoJavier
Автор

You're awesome. Thank you for what you do

anispinner
Автор

THANK YOU SIR FOR THIS DETAILED EXPLANATION
Greetings from Morocco <3 <3 <3

zakarialbouhmadi
Автор

Great explanation for beginners. Really loved the video.

mandelbro
Автор

This was on point! Thanks so much. Worth every second of it!!

GeekEver
Автор

thanks! what micropython fw version do you use? so main question is does _thread module can be used in production code?

bobtntcnl
Автор

Will there be any issue when apply the same method to all the other cores available ? Assumming earch cores handle one box redenering around the LCD and running around. The use case is appilcable for max. 8 application running concuerrently or different input running curently ?

billyheng
Автор

Thank you for the time and insight you provide with your channel.
If each thread waits for the other one to finish its access to the frame buffer, what is the advantage of using two cores? The renderer can only do a little work (some Trig maybe) before its main job of accessing the frame buffer. The two cores would indeed run in parallel if the frame buffer were double-buffered, but of course this is not possible with the small Pico RAM size.

lanehauck
Автор

Great thorough explanation, thanks for that. One point of critique, I'm not a big fan of the 'live drawing' animation style found at the beginning of the video. It adds a lot of visual noise and especially distracts from your speaking. This however is a personal preference but imo worth mentioning

sambeard
Автор

Wondering if you have any more info on how to use the REPL tool in PyCharm - for example, flashing a program to it causes it to run immediately, but when opening REPL the keyboard interrupt ends it. Using the soft reboot is enough to reset it, but it's an issue using the socket library for my web sever as I get the EADDRINUSE OSError, essentially that the static IP address I want to bind to is already being used, but by the instance that was interrupted.

You seem to be able to just upload and start it in a few examples here? Plus the terminal window you use just says 'Local', mine has a path to And obviously I can't flash a new version of the file unless I close the instance of REPL terminal as the COM port is in use...

If you know of any sources online about how to use REPL to debug beyond the official documentation (which is not great) I would appreciate it a ton :)

awesomegamer
Автор

What is that IDE that you are using?
As well could this MicroPython setup you are using guarantee timing precision during execution?

HitAndMissLab
Автор

I’m not sure about the fact that the second thread is running on the second core. I’ve already used threads in my projects but with an ESP32-C3, so a single core microcontroller and it runs without problems.

enricorossini
Автор

Hello, wanna try your example, but only got a 1.8 tft spi (128x260) (the red one with sd card reader) and no MISO nor MISI but only A0 and SDA ... how can i adapt teh code?

VPRSlab
Автор

then theres the PIO stuff. they basically function as 'event computers' that generate CPU interrupts based on the interaction between a state machine and the input stream. This makes it easy for drivers to be efficient because there is no impedance gap to cross converting from a raw data stream to contextually meaningful IO events. While the PIO is figuring out what events it should tell the CPU about happening on the IO pins, the CPU is free to do whatever it likes, and is not occupied doing this work itself like it would be in many other computing environments.

homelessrobot