Raspberry Pi Pico - Control the (I/O) World

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today in the workshop we're working with the raspberry pi pico microcontroller we'll hook up a collection of i o devices to the pico and we'll make them work using micro python we've ordered the pico with everything on it so welcome to the workshop [Music] well hello and welcome to the workshop where we are welcoming a brand new microcontroller now i'm sure you've probably heard that near the end of january the folks at the raspberry pi foundation announced a new product and for the first time it was not a microcomputer it was a microcontroller the raspberry pi pico now the pico is built around their own silicon the rp 2040 chip so it's a brand new experience for all of us and in the last couple of weeks people have been scrambling to get their hands on some picots to get their hands on pico accessories and to learn a little bit more about this little microcontroller which goes for all of four us dollars well i've been no different i've managed to get my hands on a couple of picos i've got pico accessories on order i got a few that just arrived here yesterday afternoon and so we'll be doing a lot of work with the pico but what i wanted to do to start off with just see how you could use this thing so what i did is i put this little breadboard together and i built what i guess you could just call a thing and what this thing is is a raspberry pi pico attached to a number of different io devices and we're going to hook all of these things up and then we're going to go through it section by section so you can see how you can use micropython to code for these devices and armed with that knowledge you should be able to hook up just about anything to a raspberry pi pico so before we get into the specifics of the pico let's take a closer look at this little everything breadboard i have here and so here's my breadboard with the pie pico and a number of different components interface to it you can see the pi pico buried underneath all of these jumper wires here and this back in the corner here you can also see the edge of the raspberry pi 4 that i'm using to control everything today now to give you a tour of what's on this breadboard there is an led over here it's actually an rgb led it's a bit difficult to see because of its transparent nature you can see the three dropping resistors for it you've got a potentiometer over here of course it's going to be used to test the analog inputs a couple of push button switches a couple of momentary contact ones a red one and a black one this is an oled display it's an i2c device as well so we can see both how we can work with displays and how we work with i2c and i've got a dc motor over here and i've got an h bridge controller to drive the dc motor and i'm just using one side of the h bridge because it's actually capable of controlling two motors it's just a little six volt dc motor so i've got six volt power supply for it over here and now let's go and learn a bit more about the little picot that's going to control all of these devices the raspberry pi pico was introduced by the raspberry pi foundation in january of 2021 this is the first microcontroller in the raspberry pi family the pico is powered by the rp2040 microcontroller chip it has multiple pwm capable gpio ports i2c buses spi buses analog to digital converters and uarts the raspberry pi pico retails for only four us dollars the raspberry pi designed rp 2040 chip has also been licensed to other manufacturers and so you'll start seeing other boards built around this chip very soon the most predominant feature on the raspberry pi pico board is the micro usb connector this is for both data communications and it will power your pico the onboard led is connected to its own exclusive gpio port it is the only led on the pico which gives no indication whatsoever of when power is applied to it the boot select button allows you to select which boot loader to use when you're starting the pico up the pico supports two-wire serial debug through a debug connector at the bottom of the board in the center of the board you'll see the raspberry pi designed rp2040 microcontroller tip the rp-2040 is a 32-bit dual core arm cortex m0 plus chip it runs at a normal clock speed of 48 megahertz and can be boosted to 133 megahertz in the pico implementation there are two megabytes of onboard external flash memory as well there is also an on-board real-time clock and an on-board temperature sensor the pico has been designed to either use pins or to be surface mount capable the design has eight ground points plus an additional one as part of the debug you'll notice that the ground points are evenly spaced on the pico board they can also be distinguished in the fact that all the ground connections have square pads instead of rounded ones v-bus is an output of the micro usb cable voltage which will usually be 5 volts if there's no micro usb attached there is no output from vbus vsys is the power of input for the system you can run the system on anything from 2 to 5 volts although the pi pico is a 3.3 volt logic machine the 3v3 output on pin 36 is a 3.3 output from the pico's internal voltage regulator you can use this to power external devices but don't exceed 300 milliamps the 3.3 volt enable enables the 3.3 volt power supply on the pico board it can be used to externally turn the pico on or off the run connection is an enable connection for the rp2040 microcontroller it can also be used as a reset for the microcontroller there are 26 exposed gpio pins on the raspberry pi pico there is also a 27th gpio pin that is internal and is used for the onboard led the raspberry pi pico has 12-bit analog to digital converters there are three adcs available plus an internal analog to digital converter the internal adc is used for the temperature sensor the adc ref and the adcgnd connections can be used for an external voltage reference to improve the accuracy of the a to d converter the raspberry pi pico has two i2c buses with a number of connection options it also has two spi buses also with multiple connection options and two uarts as well it supports 16 pwm channels in total again with a number of different connection options the raspberry pi pico can operate as a normal microcontroller it can be powered using the micro usb port it can also be battery powered as it is capable of operating at voltages as low as 2 volts the raspberry pi pico can also operate as a usb device when plugged into a computer's usb port the raspberry pi pico can operate as a usb device this can be useful for building things like keyboard emulators the raspberry pi pico can be programmed with micro python or with c plus plus we'll be using micropython in today's experiments so now let's take a closer look at the raspberry pi pico now here we have a raspberry pi pico actually two raspberry pi picos in the midst of a number of other microcontrollers and a few microcomputers now here is a pie picot with all of its feet soldered onto it ready for plugging into a solderless breadboard and at the back over here you can also see there's a three pin connector and that's an optional debug connector we won't be working with that today but in the future we'll take a look and see what that's all about now this is how you get the pi pico when you order one it doesn't have any feet on it you need to solder that on and we're going to do that in a moment but before we do let's compare the size of our pie pico to a few other very common boards now keeping it in the family going to a micro computer we've got the raspberry pi zero and so you can see the pi pico is reasonably smaller than the pi zero it has the same number of i o ports but of course the pi zero also has a micro sd card reader an hdmi port two micro usb ports and one of them for power and a camera connector at the back so it beats the pico with a lot of capabilities interestingly these are a dollar apart in price to pico being a four dollar u.s board and the pi zero being a five dollar one now over here we've got the head of the uh pi family the raspberry pi 4 and as you can see you could fit quite a few picos onto a pie four so perhaps you can make an expansion hat for the pie four that has multiple picos on it and pretty well control anything now i've got a few other boards here that aren't raspberry pi's now this is an arduino pro mini and as you can see it's a bit smaller than the pico it has less io pins the pro mini is essentially the same as the arduino uno in terms of capability it's an 8-bit avr so it's no match for the pico in terms of capabilities or the number of i o pins and over here we've got an arduino nano 33 iot and this is a very interesting board it's using a sam d21 processor so it's a very fast board so it's probably capable of similar power to what we're going to get in the picot it also has built-in wi-fi and bluetooth it's got an extra module on the board and the pico has no connectivity like that now one board that you'll probably want to compare the 2 uh koi extensively would be this and this is of course the c duino shao and as you can see the shower is a lot smaller than the pico it's about you probably get two and a half if you put them sideways maybe even three shallows in the space of a pico power wise these are likely to be similar would be nice to put these two boards against each other one day but in terms of the number of i o ports of course the pico has you built and cost wise the xiao is a five dollar u.s board and the pico is a four dollar us board so that's really uh cutting it pretty close you're getting an incredible amount of power these days in very tiny packages and so now let's go and take a look and see what we need to do to take this picot and make it look more like this picot now here are the parts and equipment that we're going to need to solder the pins onto the picot and of course the first piece of equipment we're going to need is the pico itself and they come in these packages over here i guess they just come off the pico machine like this and so i've taken one out we can move these over here now you're going to need some pins for it and these are 40 pin male header strips and they're very commonly available and by a great coincidence the pico happens to have 20 pins and so we can cut one of these in half and use this as the pins for it i've got three other pins over here now this is optional but there is a debug connector at the very end of the picot board and if you want to put a connector on that you're going to need a few extra pins for that i've also got a little pair of diagonal cutters and that's just to cut this 40 pin strip into 220 pin strips now of course over here i have a soldering iron which is not plugged in at the moment but i've got a very thin tip on the iron and that's important because you want to be able to get in at those individual pins and of course you're going to need a stand for the iron you're going to need a soldering sponge over here and of course some solder over here there's a couple of other things i've got here which you may not have expected first of all this little stack of post-it notes is this being used as a spacer when i solder this piece and i'll show you what i mean in just a moment here and this is what i used to hold the pins together now there's a couple of ways you can do this some people like to put everything onto a solderless breadboard and then solder it into there i'm not crazy about that idea for a couple of reasons first of all i'd be a little concerned about the heat going down into the pins and melting any of the plastic and the breadboard although if you're quick about it like you should be that isn't going to happen there's also a possibility of a solder splatter getting somewhere into your breadboard so uh you can use builder's putty as well that's another way to hold them down but i like doing it like this and let me show you with the three pins over here what i mean i'm going to put these pins in and now they're held in the board like that and this is where my little post-it note comes in over here actually because unfortunately i can't use these pins to support the rest because although everything is on a 0.1 of an inch grid this way in this way it isn't this way these holes don't match up with here so they don't line up that way but if i turn this upside down and put it on top of the pins i'm all ready to solder these three pins the debug pins onto my picot so let's just solder these three pins at the top here these are the debug pins and the connectors going the opposite direction of the gpio pins and there we go it's as simple as that and now we've got a connector on here for the debug and we can get on to soldering these gpio pins now i've cut my male header strip into two pieces 220 pin pieces and i've put it down into this perf board now if you find the cheap perf board the type that doesn't have the plated through holes works better for this if you use two layers of it it really holds the pins quite tightly and it lets you get them soldered at a good angle so it'll fit into a breadboard later so let's just put the picot down onto its pins there we go and we shall begin to solder this okay we'll flip it around but okay now we can take the picot out of the board and as you can see we've got our header on and it looks pretty nice i'm gonna go and clean up these connections a little bit and then we'll be finished soldering up our pico now you don't have to clean the flux and the resin off of the board it'll work just fine without doing that but if you want to make it look nice and shiny you're going to need a couple of chemicals to do it the most important one is some flux remover and you can get this at your local electronics store in fact they probably have a better price there then you'll find it online because shipping this stuff can be expensive and this is something you can find just at your drugstore and this is isopropyl alcohol now this is not rubbing alcohol that has the methanol in it this is just basically isopropyl and water and you're going to use that just to clean off the residue from this i've also got some essential safety pieces of equipment you need of course you're going to need some goggles to keep from getting this stuff in your eyes you will need a mask because you don't really want to breathe this stuff and naturally you will need some gloves and i think most of us have the safety equipment around the house these days and also for doing the work an old toothbrush comes in very handy and just an old plastic container that you can dump the stuff into and work from there so that you don't pollute your supply now when you're working on this work in a place like a basin or something where it is safe make certain you are wearing appropriate clothing don't wear your best clothes by any means i basically just use the flux remover first and i use a toothbrush to scrub all of the little pins and then after that i clean everything off with the isopropyl alcohol and then you can just let it dry if you have an air dryer you can use that as well do not use a heat gun on it but a hair dryer at low setting would also work or you can just let it sit and dry and then you could have a nice and shiny picot so now our pico is back from its bath and it's looking all clean and shiny there doesn't appear to be any resin or flux around any of the pins and we can flip it over and we can see the same for the debug pins over here now cleaning it and taking the flux off of it is an optional step of course you don't have to do that the flux and resin is not going to harm anything and your pico will work perfectly fine so if you don't have the chemicals or the facilities to do it safely somewhere please don't worry about it but i like to do it especially as in this case our pico is about to be introduced to a bunch of different input and output sensors and we do want it looking at its best now let's go and see what we can do with this clean little picot okay now that we've got our pico all sorted up it's time to go and do something with it and for this we're going to need a computer and we're going to need an ide now the ide i'm going to be using today is a thoni ide now this is the ide that a raspberry pi has recommended and although it's not the most powerful ide it is certainly going to be capable of doing what we need to do with it today i'm also going to use the thoni ide on a raspberry pi on a raspberry pi 4 using the latest version of the raspberry pi operating system you can also get the thawney ide for windows mac and linux so you can use any computer you'd like when you're working with your pico so let's go and see how we do that okay now that we have our pico all soldered up the next step is to put micro python onto it what i've done to it right now is i've taken a micro usb cable i've connected the micro usb end into the picot but the other end is currently not connected to anything in the meantime i've got my raspberry pi 4 all booted up with a fresh copy of the raspberry pi desktop and i'm getting ready to plug the cable into the usb port on the pi now as i do that though there is a button here that says boot cell on it i want to hold that down while i plug the cable in and hold it down for a couple of seconds and then release it and i get this removable medium as inserted menu and it's asking me if i want to open this in the file manager and i do and this is our pico it has in this mode come up as actual removable media off of our file menu and the same thing would happen by the way under windows or linux or mac you would get this is coming up just as if you had plugged in let's say a usb drive or something now there's an index.html file here let's click on that and that of course will open a web browser and it redirects us to the raspberry pi pico page we'll say okay to the cookies warning up over here and go down to where it says getting started with micro python we'll click that and we'll look at this menu over here and it says we can download a file and so let's go and download this file this uf2 file and so we're going to do a download and the file has already downloaded it's a very small file and we'll do show and folder over here and that'll open up the second folder we can close the web browser now and this file over here is the latest greatest version of micropython and all we need to do is drag that file into this pico directory here and once we do the pico will reset itself and you'll notice that the file menu for it went away and it no longer appears as a file now we're going to be using the thawney ide which comes installed on the raspberry pi so you just go into programming and you'll see the thoni python ide we'll click that to open it and at the bottom here where it says python373 we'll click in there and we will see the raspberry pi pico micro python and we'll set it up there and in the bottom over here which is what is our shell we'll see that it came back and it said the micro python version 1.14 on the raspberry pi pico now this at the bottom is our shell and up here at the top is our editor and we can go directly into the shell if we want and work on things over here and let's do a and it came back and printed hello world and so we can work directly in the shell or we can work up here on the editor and i'll do basically the same thing here and then we can run that and first we want to know where we need to save it to let's save it to our pico and we'll give it a name test.py why not and it runs and it says hello world and so we can run programs from the editor and save them either onto the computer or onto the pico or we can run directly down here in the shell at the pico itself and so we're now ready to begin all of our experiments so now it's time to begin our breadboard experiments and we're going to begin with some of the most elementary i o devices there are we're going to be using our rgb led and our two push button switches leds and switches are simple electronic devices that are very useful for illustrating basic digital i o concepts the raspberry pi pico has 26 gpio pins and most of them support pulse width modulation you should remember that the pico is a 3.3 volt logic device when you're interfacing other things to it the pico has internal pull up and pull down resistors which are useful for minimizing the amount of circuitry you need in our experiments i'm going to be using three leds a red green and a blue one however i'm going to have all three of them in the same package this is a basic rgb led you can use this or you can use three discrete leds if you wish in addition to the rgb led you're going to need a couple of push button switches these are momentary contact normally open switches i used a red one and a black one you'll also need three dropping resistors for your rgb led or discrete leds and i use 330 ohm resistors for this we'll begin by connecting pin 14 of the pico which is gpio pin 10 to one of the resistors and then to the anode of a red led segment we'll connect pin 15 which is gpio11 through a resistor to the green segment and we'll connect pin 19 which is gpio pin 14 through a resistor to the blue led segment we'll connect one of the pico's many grounds on pin 23 to the common cathode connection on the led next we'll connect pin 4 which is gpio pin 2 to one side of the black switch we'll connect pico pin 20 which is gpio pin 15 to one side of the red switch we'll connect pin 3 which is a ground to the other side of the black switch and pin 36 on the pico is the 3.3 volt output and we'll connect that to the other side of the red switch note the differences in the wiring between our two switches and this completes our circuit now let's go and take a look at a few code samples we can use with it now the first python script that we're going to look at today to control the rgb led is kind of a take on the blank sketch since we have an rgb led we can do some fancier blinking so this is kind of our hello world now we start off the python script by importing two libraries machine and you time and you're going to see that in pretty well every one of the scripts that we run today the machine library is an essential library for talking to all of the i o ports on the pico and you time is used any time that we need a time delay or any kind of a time function now we'll start by defining the three leds that we have the red the green and the blue one and you'll notice how the machine library comes into play over here now notice with the pin number over here we're talking about the dpio pin and not the physical pin on the pico so make certain you understand that difference you'll also notice that we define all of these as being outputs again using the machine library the well true section of our program is very similar to the loop in an arduino program anything inside this section will discontinue to run and what we're doing is basically just turning on and off leds first of all we start with the red led and we set its value to one and that will illuminate the red section of our rgb led we set the green and the blue to zero and then we go to sleep for two and two is two seconds so what will happen is that led will stay on for two seconds and then we'll go into the next sequence and in this case we're going to do the green one etc etc we go through all the various different combinations then out over here we do a print and that print end of loop is going to appear down over here in our shell at the bottom so that's where any print statements will come out and then we'll do a last one which is all the leds off wait for two and then it's just going to go over and do it over and over again so this is our rgb version of the blink let's go and load that now to our pico and we can do that like this and as you can see our led is going through the color sequences right now as we would expect it would and we've also just printed end of loop up here on the shell and of course it's just going to go to the beginning of that loop again and just repeat itself so here you go the rgb version of the blink sketch running on the raspberry pi pico now when you look at the code to test our switches i want you to keep in mind how the switches were wired they were not wired identically specifically the red button had one side connected to 3.3 volts so turning it on would send the signal high whereas the black button was wired in the opposite fashion it had one side connected to ground so switching it on would send the signal low and neither of them had pull up or pull down resistors we're going to take care of all of that in code now we start off again by importing both the machine and the u time libraries and then we define our red button as being the one defined to gpio pin number 15. again that's a gpio number not a physical pin and it is an input so we define it as an input and we also define a pull down resistor on this now remember the red button has one side connected to 3.3 volts so we want to hold it low all the time and when the switch is on it will activate it and pull it high so we have a pull down resistor over here activated through code and we do the same thing with the black one except we're going to be using a pull up resistor in the case of the black switch because it's wired in the opposite fashion and that is on gpio pin number two and then it's a very simple matter of just doing a test to see if the button has been pressed so we use an if statement and we say if the red button value has been set to 1 which will happen when you press it and connect the 3.3 volts to gpio input we're going to print red and so that's going to print down the shell over here now if the black button is set to zero because remember it's always going to be set and pulled high by its internal pull up but if it goes low because the button has been pressed then we're going to print black down over here in the shell and then we put a very slight delay of a quarter of a second and that's just sort of a crude attempt to doing some debouncing and so let's load that up to the pico and it's loaded and let's go and press the red button and you can see we get red written when i press it now i'll press the black button and as you would expect in the shell it is printing out black so you'll notice that we are defining one with a pull up and one with a pull down and we're defining one as being active when it is a one and the other one is being active when there's a zero so two different ways of accomplishing the same thing and getting the input from a push button switch on the pico now in our next script we're going to make use of a push button and a couple of our leds and we're going to demonstrate a couple of other concepts one of them a fairly important concept and the other one a useful one now we're going to start off by importing the same libraries we have before the machine library for the gpio and the utime for the time functions and then we're going to define our two leds just as basically as being outputs just as we did before and we're going to set the value of both the leds to zero so the red and the green led are what we're dealing with and we're going to initialize them both as being off and then we're going to go and set up the red push button and we're doing that the way we did before now you remember the red one is the one that is wired to the high side on the other side to the 3.3 volts so we have a pull down on the input so normally that input is low unless the button is pushed now what we're going to be demonstrating with this button is the use of interrupts and interrupts are a very important concept for any microcontroller now an interrupt is pretty well exactly what it says the program is going along operating and suddenly an event occurs and it interrupts the program and the program has to go off and service that interrupt before it can get back to doing what it was before and so we've got that set up down here and i'll actually move down to this line first to explain it now you recall we've already defined button red up over here as being an input with a pull down resistor well now that gives it another property and that's irq or interrupt request and we ask it what kind of an interrupt is going to trigger it and we're triggering it on a rising signal in other words going from low to high and that makes perfect sense because this is a push button which will go high whenever the button is pressed and we have to tell it what we're going to do when we're going to get that interrupt and that means we need to pass it to a function called an interrupt handler so we say the handler equals this int handler and int handler is indeed the name of the function we've defined over here to handle our interrupt now the first thing we do in int handler is we turn off the interrupts to this pin so if another one occurs while we're doing this we aren't interrupted by our own interrupt so to speak then we're going to print interrupt detected so that's going to print down the shell over here we're going to turn the red led on at that point we'll keep the green one off and then we're going to go to sleep for four seconds now it's not very customary to go to sleep in the middle of an interrupt handler but we're just doing this as a demonstration and then after we finish sleeping we're going to turn our red led off and then we're going to set the handler back so that it's working again and so this is our interrupt handler that's going to be called every time we get an interrupt generated here by pressing the button now if we go into the true loop we're going to see what our program is going to be doing when it isn't being interrupted and it shows another function the toggle function the toggle function takes the state of an output and reverses it so if the output is high it reverses it to low if it's a one it reverses it to zero etc etc so the led is going to be either off or on and we're going to toggle it to the opposite state then we're going to sleep for two seconds and do it again so basically we've got a two second blink sketch going on here with two lines of code so let's go and load that up right now to our picot and you can see that we are starting our flashing led and this is what this is going to be doing until i interrupt it which i can do by pressing the red push button and i pressed it and now we've got the red signal and that's going to stay there for four seconds and then the green is going to start over and over again and i can interrupt that at any point i can interrupt it right now and it finishes there can interrupt it when the green one is on and it finishes there and if you look up in our shell you can see that every time i press the interrupt button i'll press it again we get an interrupt detected there and so a very basic demonstration of two very important concepts with the raspberry pi pico now this script is essentially an amalgamation of the last two python scripts we looked at and all we're going to be doing is controlling the rgb led with the black push button so we'll start off again by importing both the machine and the u time libraries and then we'll define our red green and blue leds as we did before as being outputs then we'll set the value of those leds to zero so that the led is off and we'll define our black push button with the internal pull-up resistor then we'll go into the true loop and in the true loop we see an if statement and we're checking to see the value of the black push button and we want to see if it goes to zero because that means someone has pressed a button if that's the case we just sequence through the colors we'd put the red one on and we sleep for a second then we put the green one on sleep for a second we'll put the blue one on and then after that we'll turn all the leds off and go to the top again and wait for someone to press the button again so it's very simple we'll send that up to our pico right now and there we go and it's the black button and i'm going to press it and there we're going through our color sequence press it again and it happens again so we've essentially combined the last two scripts that we've looked at in order to control the rgb led with the push buttons through the pico using both inputs and outputs now let's move on to the analog inputs like most modern microcontrollers the raspberry pi pico also has analog to digital converters so let's work with the analog inputs on the raspberry pi pico the pico has three internal analog to digital converters these are 12-bit adcs so they have higher resolution than the 10-bit adc that the arduino avr family has the pico also has an analog reference and an analog ground pin to be used with an external voltage reference for improved performance from the analog to digital converters the simplest component that we can use to demonstrate the a to d converters is a potentiometer and i used a 10k linear pot we begin by connecting one of the pico's many grounds i use the one on pin 23 to one end of the potentiometer pin 31 which is dpio26 and also analog to digital converter 0 is connected to the wiper of the pot and pin 36 which is a 3.3 volt output is connected to the other end of the pot and this simple wiring completes our circuit now let's go and see how we can code for the analog to digital converter now here's the script that we're going to be using to demonstrate our analog input and as you can see it's another very simple script we start off by importing the same two libraries that we've used earlier the machine library for dealing with the gpio inputs and the utime library for dealing with time functions we then define a variable called potentiometer and we define it as being our analog input so we use the machine function and we're using the adc function of it to point the gpio pin number 26 which is also analog to digital converter 0. now this will only work on a pin that has an analog to digital converter and on the pico there are three such pins then we go into our true loop and i want you to notice this print statement here we're basically printing the value of the potentiometer which should print the value of the output of the analog to digital converter but notice what we're doing over here we're using a read unsigned 16 bit output now you'll recall that the analog to digital converter on the pico is a 12 bit adc as opposed to something like a 10 bit adc that an arduino uno would have and so with the uno we would get values back from 0 to 10 23 because our 10 bits would allow for 10 24 different values now with a 12-bit converter something like you would find on an esp-32 or c duino show you would expect values from 0-40-95 because you get 40-96 but remember we're going to get this back as an unsigned 16-bit integer so unsigned being the fact the value is always going to be positive and 16 bits means it's actually going to be from 0 to 65 530 so the value coming back may not be what you expected but you will see that this does have its advantages because when we need to pass this to other operations such as when we use pwm as long as we keep everything as unsigned 16 bit numbers we don't need to worry about things like map commands then after we print this we're just going to go to sleep for two seconds and we're going to do it again so basically we should just see down in the shell the values that's coming back from the potentiometer and so let's load that up and we're getting a value back right now and let's turn the pot all the way to one extreme and 65 535 that is the maximum value you'll get so that's when i'm putting 3.3 volts into the analog to digital converter now let's go down to the other end now on the other end i've tried a number of potentiometers and i cannot get my value down to zero now there is a ground pin on the pico called an analog ground and i thought that may have something to do with it but i've tried different connections exclusively using that ground and having that ground tied to my normal ground as i do right now and it doesn't seem to be affecting it it may be my potentiometers or the fact that it's such a sensitive a to d converter but again i'm going to bring it up right now and you'll notice these values can go up to 65 535 and so otherwise that's a pretty simple demonstration of the analog input on the raspberry pi pico now in this script we're going to read the value from the potentiometer and use it as a control for a pwm signal that we're going to feed to the red segment of the rgb led so we'll start off by importing the machine and the u time libraries again and then we define our led and note how we define it differently than we did before we define it as a pwm output and then we define where it is which is gpio pin number 10. so it's not just an output anymore it's a pwm output and the potentiometer is defined the same way we did earlier and we now have another property that led red can inherit because it's using pwm and that's the frequency that it operates at we're going to set that for a kilohertz now we go into true and there's only one statement in here we're going to set the duty cycle and that's the duty cycle as an unsigned 16-bit integer to the value that the potentiometer takes from the read of the unsigned 16-bit integer so it matches perfectly we're just going to read the pot and use that as the control for the duty cycle and that's a pretty simple way of controlling pwm far less lines of code than you would need for example in an arduino to do the same thing so let's load that up to our pico and let's take a look at the red led and as you can see i can bring it down to next to nothing although it's still slightly there and i can gradually increase the brightness of it and so a very simple light dimmer with just a couple of lines of python code now one very common component that you're going to use in your projects is some form of display and so with that in mind i've attached a small oled display to my raspberry pi pico let's go and see what it takes to get that working the oled display we're using is an i2c device and we're going to run it on the pico's internal i2c bus number zero the pico has two internal i2c buses and there are several options for connecting them up as this is a display device we only need to be able to write to it we are not getting any data back from it now for our oled display we're going to be using a standard type 1306 oled display this is a very common item available in different configurations any one of them will work in this experiment as long as the display is rated for 3.3 volts we'll begin by connecting a pico ground on pin 33 to the oled ground we'll connect the 3.3 volt output on pin 36 to the vcc of the oled display pin 27 which is also gpio21 is connected to the scl input on the oled display pin 26 which is gpio20 is connected to the sda input on the display and this completes our connections now here's the script that we're going to be using to drive our display and in order to use this script we're going to need to add another library so go up into tools and go into manage packages and in the search type in ssd1306 you'll get back a number of results and one of them will be this module that i've already installed the ssd 1306 module this is just a general purpose library for working with the ssd1306 type oled displays which are very common displays once you've installed that you can close it and go back to the script now in the script we import the machine in the utime library like we have in our previous scripts and then we set up a couple of pins one labeled sda and one labeled scl and we use the machine library and define them to gpio pins 20 and 21 respectively then we set up an i2c object using the machine i2c property and we pass it the parameters of the i2c object we want to set up and we need to pass it four parameters the first one is which i2c bus are we using bus zero or one we're going to be using zero then we pass it the pins we're going to be using for sda and scl and we've already defined that over here and then we pass it the frequency that we want to run the i2c bus at because this will run at multiple frequencies now we use the ssd 1306 library that we had installed earlier we import that and we define our oled now note we didn't have to give it the oled i2c address because the ssd1306 is a fixed address and that's already built into the library it does however come by different parameters of display so we have got it to a 128 by 32 and it's using i2c because this library can also use spi so we can get a different model of it and connect it a different way after that we just start printing out to the oled with the oled text and we set the text up first so we're going to the welcome to the pi pico display demo we're going to display that on the first three lines it's a very tiny oled and we'll have some very tiny text then the oled show shows what it is that we've put into the buffer over here we're going to let that sit there for four seconds then we're going to fill the display we're going to show that for about two seconds and then we're going to empty the display a fill of zero does an emptying of the display we'll show that and then while it's true we're going to use the text hello world and we're going to scroll it now the oled scroll function only moves everything over one pixel at a time so we need to do a number of repeat oled scrolls and shows and we do that in this for loop over here with a very slight delay so that you'll see this hello world scroll across our display and so let's load that up to our pico and let's watch the display and see what it looks like and then for filling and there's our scroll and so there you go driving an lcd display with the raspberry pi pico now being able to control a motor is a very popular application for a microcontroller and so i decided to hook up a small dc motor with an h bridge controller to the raspberry pi pico let's go and see what it takes to get that working we're going to be controlling the speed of our dc motor using pulse width modulation from a gpio pin on the pico we'll be using an h-bridge driver that uses a mosfet for improved performance for this experiment you'll be needing a dc motor i used one that is rated at six volts you'll also need a tb6612 fng eight bridge motor driver this is a dual batch bridge but we're only going to be using one side of it and you'll need a 6 volt power supply for your motor we'll begin by connecting pin 9 of the pico which is gpio pin 6 to the pwm a input on the tb6612 we'll connect pin 10 of the pico which is gpio pin 7 to the ai2 input and pin 11 of the pico gpio8 is connected to the ai-1 input pin 36 of the pico is the 3.3 volt out and it will be connected to the vcc connection on the h-bridge module we'll also connect 3.3 volts to the standby input on the module and the ground on pin 3 of the pico will be connected to the ground input on the module next we'll connect one connection of our motor to the a01 output on the motor driver the other motor connection is connected to a02 we'll connect the positive side of our six volt dc power supply to the vm input on the motor driver and we'll also make certain that the negative output of our six volt power supply is connected to our ground and this completes our wiring now here's the script that we're going to be using so that the pico can control both the speed and the direction of our motor using pulse width modulation so we're going to start off by importing the same two libraries we always use the machine and u-time library and then we'll set up the potentiometer as we did before as an analog to digital input and now we're going to set the pins up on the h-bridge controller that's what these three are here now the h-bridge controller has three pins in order to control the motor ai1 and ai2 are used to control the direction the motor spins and these are set up just as outputs and then we have a pwm pin as well and this controls a motor speed and so we set that as a pwm output on the pico so notice the difference between this and this and then the buttons are set up as they were before the red and the black button with their respective pull down and pull up resistors and the leds are set up as well now they're just set up as outputs because we're not going to be controlling their brightness this time and then we set the leds up so the red and green are off and the blue one is on so the blue led will come on at the beginning of this script we'll set the pwm frequency of our pwm input and i kill's 50 hertz but that was just an arbitrary guess and you can experiment that to see if it affects the performance of your motor also going to set the initial value for ai1 and ai2 so that we have the motor spinning in a specific direction then we go into the true loop and we can see in the true loop that we're using the exact same code we used when we were driving the led we basically take the duty cycle of our pwm and match it to the value we're reading from the potentiometer and then we look at the value of the two buttons now if the red one has been pressed then we set the values of ai1 and ai2 like this so the motor will spin in one direction we set the led red to be on and the other two to be off if the black button is pressed we set ai1 and ai2 to the opposite direction and then we turn on the green led and turn off the other two and then we have a small sleep period over here just to debounce to push buttons and so basically we should be able to control the speed of the motor with the pot and the direction with the two buttons so let's load that up to our pico now and we note the led is blue at the moment and we can control the motor now let's hit the red button the led turned red and the motor is spinning the opposite direction we'll hit the black button the led is green that also reverse the motor and so we can control the speed with the pot get it really fast or get it to go really quite slow and we can control the direction with the switches over here all with the help of our pico now the last demonstration with the motor used almost every component on the saddle is breadboard the exception was we didn't use a display so let's not have our display feeling left out let's add the display we'll write some code that ties it all together and we'll run our everything experiment and so here's the code for our everything demonstration and it's very similar to the code that we used for the motor demonstration i just added a few oled functions to it so we start off again by importing the two libraries we always use the machine and the u time library we define our potentiometer connection and we define the connections we're making to the h bridge controller we define our two push buttons and we define our three leds we'll also set up an sda and an scl connection and define an i2c connection over here and this will be for our display we'll import the library for the display and define the display that we're using over here now we'll start off everything by printing on the display so we print the lines pico motor test onto the top of the display and we show it we wait for two seconds and then we go into the leds and we turn the red one on and we wait for two seconds and then we turn the green one on and we wait for another two seconds after that we turn on the blue one and we leave it on and we set the motor frequency and we also set these two values so the motor has a direction to go in and then we go into the true loop which again looks very similar to the one we use controlling the motor one difference is this first variable i'm calculating called speed value it's an integer i'm determining it by the potentiometer position divided by 500 i came up with 500 experimentally it's going to be used to create a bar graph on the oled and this is the same line you saw before where we control the duty cycle of the pwm wave that's going to the motor controller using the potentiometer then when the buttons are pressed we can illuminate the correct led and reverse the motors direction every time we press a button and then we get down to this display which is the part that's a bit different we're going to use the fill rect to make a rectangle on the oled display these parameters tell me the position that it starts off in and this gives me the size of the rectangle now speed value is going to be used to determine the length and that way our rectangle will look like a bar graph the one on the end here means i want this displayed we do an oled show and then we do the same command again with a zero on the end so that we can update this next time then after that we put it to sleep for a quarter of a second and do everything over again so essentially we'll get the same motor demo but this time we could have a bar graph working on our display so let's load that up to the pico and it says pico motor test the leds are cycling through their color and there we go with the fan and i can do this to change the speed and you can see it on the bar graph reverse the direction if i wish so this basically demonstrates that we can combine all of these components together with the pico and control them all in our everything experiment and so now that i've got our everything code working over here perhaps i want to keep this experiment and run it now of course at the moment it's being powered by the raspberry pi but that's just supplying 5 volts of power because the code is obviously on my microcontroller so all it should be able to do right now is disconnect it and connect it to a power supply and have it run right and we'll do that it doesn't appear to be doing anything and when you think about it of course it isn't doing anything because it doesn't know what to do the pico has started up because i've applied power to it but i've got a number of programs i've placed on this pico it has no idea which one of them it would like me to run on boot up now there is a way that i can tell it to run this program when it boots up which of course is going to be essential so that i can take the picot away from the computer i programmed it on and bring it out into the open so to speak bring it out into the wild so let me connect it back up to the computer and i will show you exactly how we do this now first of all i've disconnected it so i'm going to have to go and reconnect and you can see it comes up down here raspberry pi pico so i'm connected again and here is my everything program and what i need to do to make this program run when the pico is booted up is i need to save it under a different name so i'm going to do save as i'm going to say i'm going to save on the pico and i'm going to save it on a special name and that's main dot py and so we'll call that main dot py now send it back up to my pico so that it's running and there it goes we're going we've got our bar graph okay now let's disconnect this from the raspberry pi raspberry pi 4 that is and connect it up over here and it's starting off again now and it's running all by itself without the pi 4 just on its own power and so when you want a program to be the default one you just change its name to main dot py and if i had another program i wanted to change i would just overwrite main.py with the new program and so that brings us to the conclusion of our look at the raspberry pi pico and it is certainly not the last time you're going to be seeing the pico here in the workshop for one thing while i was filming this video my first order from pymeroni of a bunch of pico specific accessories has just arrived and i've made another order from them for some more accessories i've got another pico order coming from some other company over here with just a bunch more picots so i'm pretty well invested in the pico and you'll be seeing a lot more of it here in the dronebot workshop now the best way to find out about these new pico videos of course is to be a subscriber so if you aren't subscribed to the youtube channel please honor me by doing that just click on the subscribe button and when you do also click the little bell notification and as long as you've got notifications enabled on your youtube account you will get notified every time that i make a new video now if you need some more information about the pico or if you'd like the code that i used today you'll find that all in the article that accompanies this video on the dronebot workshop website and you'll find the link to that right below the video you will also find a link to a discussion thread on the dronebot workshop forum that is exclusively for this video so if you want to talk about the video discuss the code it is discuss to pico in general the forum is a great place to do that and finally there's one other thing of course is the newsletter the newsletter is my way of keeping in touch with you to let you know what's going on here in the workshop both with the pico and with all of the other interesting things that we work on so the newsletter is free there's information below the video as well for signing up for that newsletter and it's not a sales letter it's just my way of keeping in touch with you and so with that said i hope you enjoyed this video i hope it's opened your eyes to some of the things you can do with the pico please stay safe and take care of yourself and i'll see you again very soon here in the dronebot workshop goodbye for now [Music] you
Info
Channel: DroneBot Workshop
Views: 293,901
Rating: undefined out of 5
Keywords: Raspberry Pi Pico, raspberry pi pico projects, raspberry pi pico programming, pi pico
Id: Zy64kZEM_bg
Channel Id: undefined
Length: 63min 15sec (3795 seconds)
Published: Sat Feb 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.