In this video of the Pico Workshop, we are taking a look at some helpful MicroPython Code that could be vital to your next project. We will take a look at functions to measure the passing of time, multiple ways of running parallel code and more.

Transcript

Alright, we're on the home stretch now. No intro to this chapter, we're just going to get straight into it. In this video, we're just going to look at a collection of helpful bits and bobs. There's lots of helpful things that we've tried to weave in along the way throughout this course, but there's still a big list of things remaining. So let's take a look at them. And these things you might not use in every project, but they are really helpful tools to have in your toolbox as a maker.

So let's start with some helpful MicroPython features. We're just going to look at some simple demo codes, which you can find on our course page linked below. Let's start with Tick-MS. Now we did look at it in the while loops video, but it's definitely worth another look at because we didn't really explain it too well. Tick-MS just measures the time since the Pico has turned on. And it's great because it gives you a reference point that you can use to measure the time between two events happening. Here's our sample code, super easy. We need to import the time library. We're going to start by assigning the current time to a variable called start. Then we're just going to put the Pico to sleep for a random time between zero and one seconds. Random.random just gets a number between zero and one. And then after that, we're going to say the current time minus the time we measured up here is the elapsed time. And with this, we're going to be able to measure the time between this line here and this line here. And if we run it, we can see that that sleep was 535 milliseconds long. And all we're doing here is measuring the time between event A and event B, which is a really helpful thing in your project.

Let's talk about interrupts. And the best way is with an analogy. Let's say that you wanted to detect a button being pressed. This is kind of like you sitting at home and waiting for a package to be delivered. So far, the way that we've been checking for button presses is using time.sleep and .value to essentially go and check the door every minute to see if the package has arrived. But this is terribly inefficient. In the real world, we would just instead wait for the delivery person to come knock on the door. And then when we hear that, we would go and check for the package, right? This knock on the door is an interrupt.

So starting off, I've got some random code in my while true loop here. And this is just obviously going to print hello every two seconds. No checking of a button in here whatsoever. I've gone ahead and set up an interrupt request on a button that we've set up here. And now whenever we press the button, it's going to call a function and run what's inside that function. So the handler here is the function that's going to be called. And all that's in the function is led.toggle. And if I run this code, you can see that hello is being printed every two seconds. But if we go over to the Pico and press the button, you can see that it toggles the LED. Now, something you might notice here is that when I press the button, it doesn't always turn off and on. You can see that sometimes it stays on and sometimes it's acting very strangely. And that's because buttons aren't perfect. And when we press them, they might actually trigger a few times. And to prevent this, we need to create something called a debounce, which you can actually use TickMS to create. I will have some sample code for that in the course page if you want.

Timers are another form of interrupt, but instead of being triggered by a GPIO PIM, they are instead triggered by time. So here I've got a very similar script. Every two seconds in my while true, I'm going to print hello. But then I've gone ahead and created this timer here. It's periodic. And every 1,000 milliseconds or one second, it's going to call the function toggleLED. And in that toggleLED function, we can put whatever we want, but I'm just going to call led.toggle. And if we go ahead and run that, we can see that hello is going to be printed every two seconds and our LED is going to flash on and off every second. There's also another type of timer we can use. The last one was periodic, meaning that it keeps looping over and over. But this one is a one-shot, which means it only activates once from when we call it. So I had to go ahead and create another instance of a timer. And then I've initialized it, but I've initialized it as a one-shot that will activate in 5,000 seconds and call the function five seconds, which is just going to print out five seconds has passed. And if we run that, hello is printing, lights are flashing, and after five seconds, the Pico is going to print five seconds has passed. And this is super helpful because it kind of lets us run two lots of code at once, one in your while true loop and another lot of code on a timer. And these timer interrupts are really accurate and precise. And an example of its application might be checking a sensor at an exact and fixed time interval.

Asyncio might just be one of the coolest things to learn because it allows you to run multiple while true loops at the same time. Now, this code does exactly what the last one did, but using Asyncio instead. So starting off, we define two functions here, and in them is a while true loop. One prints hello and then sleeps for two seconds, and the other one toggles the LED and sleeps for one second. And very importantly, see that we're using awaitAsyncio.sleep. That allows us to run these two while true loops with their sleeps without conflicting with each other. And then all we do is we get the event loop and assign it to an instance of some variable. We're going to call it loop. And then we create the Asyncio tasks and tell it what functions we want it to run at the same time. And then we just start running it. And if I run this code, you can see that it's printing hello and the LED is flashing at the same time. Super powerful stuff. And just remember, you can have more than two while true loops. You could have a dozen if you really wanted to.

Now, this one's a bit special. We're going to sort of look up to the summit of the mountain, and there we will see PIO, Programmable IO. Everything else we've mentioned at this video is around the intermediate level, but PIO is definitely at the more advanced skill level. PIO actually has its own dedicated processing cores in the RP2040 chip. This is separate to the ones that we've been using so far. And this is one of the cool things about it. We have our own dedicated hardware specifically for IO interactions. PIO is unique to the RP2040, and its main use is in making custom communication protocols. The Pico doesn't have CAN bus, which is another common communication protocol, but it instead gives you the building blocks to make your own CAN bus. Let's say you wanted to have four UART channels connected. The Pico only has two peripherals for it. You could make another two with PIO. If you had an ancient piece of hardware with a weird protocol from the 80s, you can use PIO to interface a Pico with it. For us modern day makers, though, the most common application is in driving WS2812 LEDs, or NeoPixels. Now, this isn't code to drive NeoPixels. This is just really simple. Let's take a look at kind of how it works. All we're going to do is we're going to flash the onboard LED at 10 hertz. This here is our main chunk of PIO code, and there's a few different sets of instructions that we can give it, and wrap target and wrap here denote that this is a loop, so anything we put in it is going to be looped over and over, and this set here sets the state of the pin to a 1 or a 0. So we start by turning the pin on, and then we've got this 31 here in the brackets, and this is actually a delay, but not in seconds, in cycles. It gets a bit technical with how the hardware works, but long story short, we can only feed it 32 instructions at a time, and all we're telling it to do is set the pin, which is one instruction, and then 31 instructions of dude, absolutely nothing. And then in the next line, we give it another 32 set of instructions, and this just means no operation, so do nothing for one, delay for another 31, and we keep doing this until we turn the pin off, delay for 31, and on and on and on. And if you add all of these up, this is 256 cycles in this loop. And when we initialize it here, we're going to say we're going to run it at a frequency of 2,560 cycles a second. Divide that by 256, you get 10 hertz. And then we're going to turn on this state machine for 5 seconds, and then turn it off. And that is quite a lot of work just to get a Pico to flash an LED at 10 hertz. But something to keep in mind is that a COM protocol is doing the same thing, just with specific timings instead of a continuous 10 Hz signal. Now that's not typically how you use PIO, and as you see, it's a little bit on the more difficult side, and it might be a bit more niche, but it is definitely worth coming back to in the future, because it opens up a cool new world of possibilities.

Now there are definitely things out there that we haven't covered. Maybe they were too niche, or maybe they didn't even exist at the time that this course was made. MicroPython isn't static, it's a living and breathing language that is constantly getting new additions and updates. So on our course page for this video, you'll find some more things that we think are worthwhile knowing, just those extra few tools in your toolbox.

Another thing worth knowing about is the MicroPython.org documentation site. This place holds all the documentation of how to use pretty much everything we covered in this course, as well as examples. And what we covered in this course might only be scratching the surface of all the setup options and commands that you can use with MicroPython functions. For example, if we go to the documentation page for Asyncio, we can see that if we call loop.stop, we can stop the Asyncio loop in its tracks. And on this site, you'll also find the RP2 reference sheet, which beautifully contains a quick reference list of all the code pieces that you need for the Pico in one place.

One more thing to expand your horizons before we finish, there are other Pico boards and other languages that you can use. MicroPython isn't the only language that the Pico can be programmed with. You can also use C++. And now that you know MicroPython, it's really easy to learn another language. It's just a different set of syntax, and they might do a few things a bit differently. And there are times that you might want to switch over. MicroPython is actually pretty inefficient, and there are times where C++ might make a job easier. For example, when this video was released, MicroPython's low-power saving functions don't work very well, but in C++, they do. But for 99% of projects, MicroPython is going to be perfectly fine. There are also other boards out there that use the RP2040 chip and can be programmed in MicroPython basically the same. You can find boards with cool custom hardware on it. Maybe a Pico that has an Ethernet port, or how about one that's inbuilt into a round display like a smartwatch, or how about even a tiny board that's about the size of a bread clip? You may not use them, but it's definitely worthwhile knowing that there are other boards out there for very unique applications. And that about wraps that up. Again, you don't need to go out and master all these things right away. They're just more so tools in your maker toolbox that you might find really helpful one day.

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.