Importing Libraries | Raspberry Pi Pico Workshop: Chapter 2.8

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
libraries are an incredibly powerful tool that can make life a lot easier because they're kind of like just 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 dos 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 dos sleep and it does it all for you all right 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 sign tan and logs and Bluetooth Bluetooth will lets 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 mindboggling 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 PE code they don't come standard with micropython 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 so servos are a little device which let you control the exact angle of this arm with PW M but they are a little complex as they aren't controlled directly with Judy 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 so 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 thny ensure that your pico is plugged in and and that you have the correct comport 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 index or pipe 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 micro python Sero 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 microp python 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 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 pipy page which hopefully has some documentation for us like it does sometimes you may have a specific web page 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 so if you can find working example code to copy and paste and adapt that's usually a bit easier libraries might have really Terri 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 a park and I would usually select a library that has good documentation just to help us make just just for it to all be a lot easier and nicer to use so this looks really straightforward I'm just going to copy and paste it into thony just as a starting point to see what happens all right let's plug in our nine G Servo so we can see what's up now there is two common color 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 Vols 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 gp0 at the the top there this is just for our example any pin that can create a pwm is one that we can use for a Sero 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 zero which we've plugged it in with import time import the library so from Sero 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 wild true Loop and then I've changed the sleep to 1 seconds and then I could change this to 0° 90° and 180° and if I run that we can now see that it is swinging 90° 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 of 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 piy 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 thony 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 PCO 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 you might have seen two ways that we've been importing Library 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 ROM time import sleep and what this does is it only Imports the sleep function from the time libr when I do this I have to change how I use sleep in my code because we don't need the time do 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 Dot pin with the prefix and also in here machine. pin and then I'd also have to do it oh 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 dot or the machine dot 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 okay so three key takeaways one libraries are pre-written pieces of code that can make life much easier for us two there are standard libraries that come with micropython and external libraries that you can go out and find and install onto your p and three when using external libraries you will need to look for documentation in order to figure out how to use it and you often have to rely on the creator of that to make good documentation
Info
Channel: Core Electronics
Views: 2,919
Rating: undefined out of 5
Keywords: tutorial, DIY, circuit, learn, diy projects, upload, project, beginner, beginner friendly, how to, explanation, guide, help, STEM, Pico, MicroPython, Arduino, Microcontroller, Pico Projects, Raspberry Pi, Raspberry Pi Pico, Course, Learn, Pico Course, Microcontroller Course, Learn Microcontrollers, Pico WH, Pico LED Blink, Pico Beginner, Beginner course, Beginner friendly course, Starter course, Libraries, Thonny Libraries, Thonny Import Library
Id: GwMXniz4KgE
Channel Id: undefined
Length: 10min 6sec (606 seconds)
Published: Wed Feb 07 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.