In this video, we take a look at libraries and how to use them. We take a look at standard libraries in MicroPython, how to import external libraries in Thonny, and we go through an example of finding and using an external servo library to drive a servo.

Transcript

Libraries are an incredibly powerful tool that can make life a lot easier, because they're kind of like pre-written pieces of code that can do a lot of the heavy lifting so you don't have to. We've actually already been using some libraries, mainly for sleep and for the setting up of the pins. When we use time.sleep, we don't need to see what's going on behind the scenes, but there is an incredible amount of things that happen to get the Pico to go into that sleep. What happens on a hardware level is quite complex and very involved, but thankfully you import the time library, just a single line of code, time.sleep, and it does it all for you.

Alright, libraries let us use pre-written code to make our life easier. Now let's get practical. MicroPython has something called standard libraries. When you install MicroPython onto your Pico, these libraries come in-built with it. And to enable it, all you need to do is import and then the name of the library. To name a few of these libraries, we have the machine library, which lets us use ADC, PWM, and PIN, which lets us set up the pins of our Pico. We have the time library, which we've been using for the sleep function. There's also the math library, which contains functions like cos, sin, tan, and logs. And Bluetooth, Bluetooth will let you use Bluetooth functions on your Pico. There are quite a few standard libraries, and a quick Google search will let you know which ones you can use.

So we have standard libraries, but there is also a world of external libraries, and there is a mind-boggling amount of them, often for very, very specific and niche applications. A lot of the time when you buy a module like this really cool OLED display here, you will find links to an external library to help you use it, or if it's a common module, you'll be able to find some with a quick Google search. And when you start using more complex modules like that OLED module, where it's quite difficult to interact with it, you're going to be using a lot of libraries, and a lot of the work you're doing is going to be very library dependent. But these external libraries need to be downloaded and installed onto your Pico.

So let's go through an easy example of a library that you might commonly use. The Servo library. So we're just going to go through the real-world process of looking for a library to help drive a servo, and adapting it into the code and figuring out how to use it. Servos are a little device which let you control the exact angle of this arm with PWM. But they are a little complex, as they aren't controlled directly with duty cycle, more so how long between each pulse. And it's not too difficult, but there is a little bit of calculation required to figure out what PWM at what frequency to send to the servo. So it's usually just a lot easier to just use a library for that.

To do so, we're going to need to start by downloading and installing the library onto our Pico, which is a piece of cake in Thonny. Ensure that your Pico is plugged in, and that you have the correct COM port selected. Then go to tools, and hit manage packages. From this window that popped up, we can search and install libraries from the Python Package Indexed, or PyPI. And this is the largest repository or collection of Python and MicroPython libraries out there. So we're going to be looking for the MicroPython servo library, so we're just going to punch in servo, that might get us it, if not we might need to punch in MicroPython servo. Nope, we're going to need to type in MicroPython servo. And there it is. Click on it, and then just hit install, and that is going to install the library for you. Something worth mentioning that like MicroPython, this is only installed onto this Pico. So if you use another Pico, you'll need to reinstall all the libraries onto it again. From here, we can see that it's actually installed onto our Pico. And if we go back, and then we go into files, and then into our libraries folder, we can see that we now have it in our libraries folder on our Pico.

Okay, cool, we've got it installed, but how do we know what functions this library has? How do we know how to use it? Well, this is where we're going to have to rely on the creator of the library to give us some documentation on how to do so. A good way to check is to go back into manager, and then we can go click on there, wait for it to load a second, and then we're going to be able to open up the PyP page, which hopefully has some documentation for us like it does. Sometimes you may have a specific webpage or guide with documentation on it already, or maybe a quick Google search of the library can give you something, whatever is available is best.

Here, we can see that the creator of this library has already given us some example working code, which is really helpful in this situation, as well as we've got something called an API, which is kind of the how to actually use this, and this can be a little bit technical and complex sometimes to read, especially as a beginner. If you can find working example code to copy and paste and adapt, that's usually a bit easier. Libraries might have really terrible documentation, and you might struggle to figure out how to use that library. Some might have really great documentation, and it's a walk in the park, and I would usually select a library that has good documentation just to help us make, just for it to all be a lot easier and nicer to use. This looks really straightforward. I'm just going to copy and paste it into Thonny, just as a starting point to see what happens.

All right, let's plug in our 9-gram servo so we can see what's up. Now, there is two common colour schemes for the three wires on the end here, so we're just going to go through both of them. Okay, so for our power wires, you're going to have a red and then maybe a black or brown one, so plug ground into ground, and then we're going to be powering it off of VBUS this time, which is the one on the top right here. This is because this servo runs on 5 volts and not 3.3 volts, and it can draw up to 600 milliamps of power. We'll talk about this in a later video. Then we're going to grab our signal wire, which is either going to be orange or white, and plug it into GPIO0 at the top there. This is just for our example. Any pin that can create a PWM is one that we can use for a servo.

Okay, we're going to go back into our code now, and first things first, I can clearly see that that is how they're setting up the pin, so we'll set it to pin 0, which we've plugged it in with. Import time, import the library, so from servo, they're importing the servo library. We just installed, and if we run that sample code, we can see that the servo twists. Now from here, I can modify this code however I want, like here I've put it inside of a while true loop, and then I've changed the sleep to one second, and then I can change this to 0 degrees, 90 degrees, and 180 degrees, and if I run that, we can now see that it is swinging 90-degrees each time. Here I've just found the most important things, which is the command to actually set the angle of the servo, and the actual setting up that's required to get to that point, and this might need a bit more investigation depending on what library you're doing, but it's a process that you're frequently going to do, and the experience is going to be different every time.

There is also another way to import a library. Sometimes you might get the library directly as a .py file. You might be following along with a tutorial, and then it just throws you the library directly as a file like this, so to upload it to the board, you're going to need to obviously plug it in, and then open up the file explorer in Thonny. Then under your computer files, you're going to need to navigate to the location of the library, so here it is for me, and then on your Pico, you want to open up the libraries folder, and then just simply right-click and upload to library, and then that's just going to put it on the board for you to be able to use, and this works not only for libraries, but any MicroPython code, so if you wrote something really cool, you can take it off your board, and then put it on the internet, and then somebody else can take it from the internet and put it on their board exactly just like we did here.

Something important worth touching on here as well, we might have seen two ways that we've been importing libraries so far, so there is just the import, which we're doing here, and then we've also got the from something import something, and they're both importing a library, but they're doing it in slightly different ways. Let's use the time as an example. When I say import time, I'm telling it to import the entire time library with all of the functions that it contains, and if I want to use the sleep function, I have to say in the time library, sleep, use the sleep function. The other way to do it is that I could say from time import sleep, and what this does is it only imports the sleep function from the time library. When I do this, I have to change how I use sleep in my code, because we don't need the time. prefix anymore. I can just use sleep. This also means that if I only just imported machine, which imports pin, ADC, PWM, and every other thing under machine, when I call pin here, I have to specify machine.pin with the prefix, and also in here, machine.pin, and then I'd also have to do it, if I can copy and paste, there as well, which gets really lengthy. So if you import the whole library, when you call a function, you need the time. or the machine. prefix on the front there. If you just import that specific function from the library, you don't. There is no right or wrong way to do this, or how you import libraries, and you should do whatever you are comfortable with, or whatever the project requires. 

Three key takeaways: libraries are pre-written pieces of code that can simplify tasks, there are standard and external libraries in MicroPython, and when using external libraries, you need to refer to documentation for usage instructions, relying on the creator for good documentation.

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.