#372 How to use the two Cores of the Pi Pico? And how fast are Interrupts?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the new pie pico has two cores how can we use them and if you want to play with the pico this video can save you a lot of time because we will discover the main differences between programming and arduino and the pi pico after this video you will know more than 99 of all pi pico users and you know how to avoid some quirks gritty youtubers here is the guy with the swiss accent with a new episode and fresh ideas around sensors and microcontrollers remember if you subscribe you will always sit in the first row in this video we will gain processing power by distributing the load to two cores use one core to supervise a sensor at full speed and use the second core to act accordingly synchronize two tasks running on two cores using a barefoot and a professional method just because we can we also learn how to use interrupts on the pickle and measure how fast they are compared with our other boards with this knowledge you should be able to create all sorts of cool projects using the pico and as a benefit this knowledge can be used for the esp32 which also has two cores i assume you already know how to use python on the pi pico otherwise watch my video number 370 or any other introductory video i assume that you know that python uses indents and not curly braces to create program structures if you use the tab key in tony to create indents you will get it right to show our program's effects i connect three leds a button and a buzzer with a because respective pins as with arduino we have to start with declaring or importing the needed libraries machine because we want to use pins you time because we want to add delays to our programs and threat is needed when we want to use both cores next comes the setup section as with arduino we define the different pins an interesting thing here we use pull down for the button in arduino ide we usually use pull up because not all of our mcus offer pull down pull down is easier to understand because we get a one or true if we press the button but keep in mind we have to connect the second pin of the button to 3.3 volts and not to ground in python we can use variables without declaring some people like it others not but pay attention the scope of variables is very different than in the arduino ide when we define a variable in c plus plus it automatically can be used and changed in its block python only has global and local variables if we define a variable outside functions it is global and can only be used inside functions if we try to change a global variable inside a function python creates a new local variable with the same name which can be very frustrating if we want to change its value we must declare it as global inside the function later we need this knowledge in our examples let's continue next comes what we call the loop in the arduino ide in python it is a simple while true statement we switch the led on delay switch it off and delay again no difference to arduino in micropython we do not need to upload the program we just started the green led flashes as expected so we translated our first arduino program from c plus plus to python not very hard but of course it only runs on one core of the pico now we want to use the second core how do we do that we define a second loop for the red led fortunately the thread library is already available in micropython by the way this is a significant difference from circuit python it currently does not support multi-threading if you run the program only the green led blinks not what we expected only if we start the thread both leds blink the second thread automatically runs on the second core cool that's all we need to run two threads on two different cores suppose we would try to create a third thread it crashes so if you need more threats you have to wait for my video about rtos running two independent threats on two cores is okay if we need raw power but usually we want that one task can influence the other task to show you this scenario i use an example taken from the pi foundation's fantastic cookbook i leave a link to where you can download it in the description the main thread simulates a standard traffic light for cars the second thread checks if the button for pedestrian crossing is pressed if so an acoustic signal is created when the next red phase starts to signal also blind people that they can cross the road the example uses a barefoot synchronization technique using a global variable the pedestrian thread sets button pressed to true and the main thread acts upon it theoretically this could create problems but i assume damien george the creator of micropython has taken care that changing a global variable cannot be interrupted by a second thread anyway the example works fine if we press the button the buzzer waits till the traffic light becomes red and starts to bus as expected an excellent example of using the second core to create a fast reaction to the button press but also a waste of processor capacity of course as usual on this channel we want more we want to see the professional way to synchronize threats our professional example is simple we go back to our example with two leds we want now that the second led waits till the first finished its cycle we do this by creating a lock called button button because of the relay race where a button is passed from one runner to another the same here each led thread has to acquire the button before it can run if the button is with the other runner or threat it has to wait as soon as it acquired the button it runs or blinks in our case and after that releases the button the next runner can take it and run simple but efficient the effect is visible the green and the red led blink one after the other cool now you know how to use both cores and how to synchronize threads you already know more than 95 of all pico users if we want to rise to the 99 level we have to learn how to use interrupts with interrupts a signal any time can stop a running task for a moment and do its thing in our traffic light scenario we dedicated a whole core to monitor one pin as said before probably a slight overkill the interrupt routine runs on the same core as the main program and leaves the second one for another job interrupt functions by the way have to be as short as possible never include a print statement for example we will create a frequency counter using an interrupt the main loop calculates the frequency from time and sends it to the console the counter function has two steps it measures the elapsed time from the last call and stores the current time to be used for the next call to be fast all times are measured in microseconds now we need something to check our input pin and to call the counter function as soon as it changes this statement does the trick every time the input pin is rising from 0 to 1 the handler is immediately cold using irq falling would create the same result because the time between two rising edges and two falling edges is the same now we can check how fast our pico can measure it works fine at 100 hertz and it works up to around 6 kilohertz but here its values fluctuate a lot frequent viewers remember video number 345 where we did the same test with other processors and c plus plus to find out what happens we can use a simple trick we set a pin high as soon as the interrupt is triggered and display the signals on our oscilloscope green is the input signal and yellow the signal from the pin we see that the reaction time of the pico is between 55 and 60 microseconds this variance is probably the reason for the fluctuating readings for comparison the esp32 needed three microseconds for the same thing and the stm32 had 1.3 microseconds we see micropython on the pi pico is not made for high speed applications it is 20 to 50 times slower than c plus plus in this respect and now you belong to the 99 club but everybody talks about the famous pio function of the pi pico so here is the challenge who can use the pio to create a much much faster counter anyway we got all we need to use the two cores and do some fancy stuff i can also assure you that if you start to play with micro python you will love its ease of use and development speed as you might know for the swiss time is money and now we come to the quirks the first micro python is very new on the pico this is why you always have to download the newest firmware from the home page for me it was not enough to update the raspberry pi os and download the firmware through tony threats did not work i had to use the version from the home page i assume this will change in the future the second not having a reset button on an mcu board is an interesting decision one could say this is a beginner's mistake or the engineers thought that this product is perfect and does not need a reset button unfortunately this is not true in my case the two core scenarios needed lots of resets because the picot did no more respond to the rebel commands for example without the reset button you have to unplug the micro usb connector every time the processor blocks and we know that this is the achilles heel of these connectors i fear my pico will not survive many such resets so please james lyon or luke if you watch this video please have mercy with us mere mortals we screw it up from time to time and need a reset button we also accept the 5 cents uplift needed for that so what should we remember from this video already now we can use threads on both cores of the pico if needed we can use global variables or locks for the synchronization of tasks because micropython is not rtos we have to stop at two threads also interrupts work currently they are much slower than in c plus plus the programming of the two core functionality is not finished yet so always download the newest firmware that's all for today as always you find the relevant links in the description i hope this video was useful or at least interesting for you if true please consider supporting the channel to secure its future existence thank you bye um um
Info
Channel: Andreas Spiess
Views: 125,837
Rating: undefined out of 5
Keywords: arduino, arduino project, diy, do-it-yourself, eevblog, electronics, esp32, esp32 datasheet, esp32 project, esp32 tutorial, esp8266, esp8266 datasheet, wifi, pi pico, raspberry pi pico projects, raspberry pico, greatscott, guide, hack, hobby, how to, iot, lorawan, nodemcu, project, simple, ttgo, wemos, FET, FET tutorial, lora, n-channel, npn, p-channel, pnp, tutorial, Raspberry Pi, Zoom, Microsoft, Teams, Home Assistant, Docker, Aliexpress, Amazon, Banggood, raspberry pi pico, Pi pico projects
Id: 9vvobRfFOwk
Channel Id: undefined
Length: 14min 24sec (864 seconds)
Published: Sun Feb 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.