Pi Pico W with the Arduino IDE | Using WiFi

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today in the workshop we'll program the new raspberry pi pico w without using micro python you'll see how we can use the arduino ide to work with the wi-fi features of this new microcontroller we'll also convert some esp32 code to run on the picow we're working without a wire today so welcome to the workshop [Music] [Applause] well hello and welcome to the workshop and today we're going to be working with a microcontroller that i'm pretty certain you've been hearing a lot about lately the raspberry pi pico w now the pico w was released on the 30th of june 2022 and on that day there were a wealth of youtube videos that were also released by makers who were more fortunate than myself and actually managed to get an advanced copy of the board so i had to order a board that day and on the first day it was released they were a little hard to get but now they're very easy to get unlike the other raspberry pi micro computers the microcontrollers the pico and now the pico w are quite easy to get and they're available at all the common raspberry pi distributors now the videos that were released that day were great they covered pretty well every aspect of the pico w and they really didn't leave a lot for me to cover however there was one thing i noticed that they all had in common and that was that they all used micro python now this makes perfect sense of course the raspberry pi released a new uf2 file along with the board and they released a number of code examples with micro python but you can also use c plus plus in the arduino ide with this board and that's what i wanted to cover today there are a number of reasons why you might want to use the arduino ide instead of micro python micropython is a great language and it makes it very easy to write software but there are limitations in terms of speed and c plus plus can be faster because it's a compiled language so you might want to use it for that reason you might want to use the arduino ide just because you're more familiar with the arduino ide or perhaps you want to reuse some old code some code from some arduinos some code you wrote for the regular pico or maybe even esp32 or esp8266 code that can take advantage of the wi-fi capability and you can do that with the arduino ide finally there's even a security aspect to it if you're writing an iot application and you have network connection information passwords api keys in your code when you put them into micropython they're just going into a text file and they can be easily read whereas with c plus plus they're compiled they're a bunch of ones and zeros they're pretty well safe from prying eyes so for whatever reason you can use the arduino ide to program the pico w and that's exactly what we're going to do today now one thing we're not going to do today is do an extensive coverage of the pico w because this is basically the same as the pico with wi-fi capabilities so we're going to focus just on the wi-fi aspect of things so we're going to begin our story today by looking at a picture of a cow in early june of 2022 raspberry pie tweeted a picture of a cow with a raspberry pie logo on it the picture had the caption pie cow and those who were clever enough to rearrange the letters realized that they were actually introducing a new product the pico w this new microcontroller was introduced at the end of june 2022 the pico w is a raspberry pi pico with the addition of a wi-fi module it uses the same rp24 microcontroller as the original pico and has the same specifications its wi-fi capabilities are provided by an infinon cyw43439 wireless module this module is capable of both wi-fi and bluetooth however on the raspberry pi picow only the wi-fi has been currently enabled as with its predecessor the pico w is quite inexpensive and it retails for six dollars us per unit the pico w has the same form factor and pin out as the original pico but there are a few subtle differences the onboard led is in the same location but on the pico w it has been driven by the wi-fi chip and it uses port wl underscore gpio1 on the original pico this used gpio pin 25 on the rp2040 pin 25 is now in use for the wi-fi the debug connector pinout on both boards is identical however on the pico w the debug has been moved from its original location on the edge of the board to further inward of course the most notable feature of the pico w is the wi-fi module which is encased in a metal can at the end of the board for those programming in micro python there is a new uf2 file for the pico w that has all of the wi-fi libraries on it note that raspberry pi is still maintaining a uf2 file for the original pico and you should use the correct file for the board that you're using when they release the pico w raspberry pi also released the pico w data sheet and it has all the details about the connections and capabilities of this new module the pico w has the same pin out as the original raspberry pi pico therefore all of the development accessories that you already have for the pico will work fine with the pico w and it's a plug-in replacement for its predecessor so let's get working with the raspberry pi pico w now i've got the pico w here actually i've got two pico w's one that i have soldered some pins on too and one that is the way that you purchase it without any pins on it although you can buy pico w's now as well as regular picos with connectors already soldered onto them now i've also got one of the original raspberry pi picos over here and the obvious difference between the two of them is that the raspberry pi logo has been replaced by this metal can which is of course the wi-fi section of the pico w and an antenna section below it now we'll take a closer look at that i'll try to get my thumb out of the way uh you can see that the antenna section is etched into the board right beside the wi-fi module and you're not supposed to try to get anything metal around that so that's got some significance when you're building an enclosure however i understand that you actually can put metal on the side of it if you want to make the antenna more directional but as you can see they've kept exactly the same form factor for the pico it's the same pin outs uh everything else is in the same place the boot cell push button is in the same place the debug has moved though here's the debug on the original one and the debug on the new pico w the led is in the same place although it is not driven the same way it was on gpio pin 25 here and of course it's now being driven by the wi-fi module over here but otherwise it's an identical board it's got the same micro usb connector on the end that can provide both power and data the mounting holes appear to be pretty well in the same place and so any design that you had or any prototyping gear that you had for the original pico will work with the new pico w now before we start working with the pico w i thought it was only fair that we should talk about the elephant in the room or in this case actually two elephants in the room because there are two aspects of the pico w that have come up immediately that we really should discuss now the first elephant is of course bluetooth what happened to bluetooth after all the wi-fi kit that they're using on the pico w is also capable of bluetooth and yet bluetooth has not been released with the pico w it only has wi-fi capability even upton actually addressed this when he announced the pico w on his blog post in the raspberry pi blog and he said that bluetooth was something that they may consider adding to the pico w in the future now that might imply that this is just a hardware or a firmware update however there are a number of bloggers and participants on forums who have pointed out that there may actually be a hardware reason why we cannot use bluetooth with the picow now in order to understand this hardware concern you need to take a look at the block diagram of the cyw43439 chip the one that is used to power the wi-fi section of the pico w you will notice that there's a wi-fi lan subsystem and a bluetooth subsystem in the chip and they both use a common radio and this is a very common method of doing this the esp32 does the same thing there is a separate input for the bluetooth section and for the wireless lan section and the wireless lan section uses the sdio 2.0 spi interface whereas you can use a uart or pcm for the bluetooth now the concern comes from looking at the schematic for the raspberry pi pico w which raspberry pi released on the day they released the microcontroller if you look at the schematic you can see the sdio connections for the wi-fi then they're clearly being used however if you take a look at the bluetooth connections you will notice that they don't appear to be connected and this was the reason that many people were concerned that bluetooth simply cannot work with this board however i don't know if this is necessarily true let's take a look at this diagram again the block diagram now if you go down in the bluetooth subsystem you'll notice there's also an sdio 2.0 input on there and perhaps that implies that they can both use the same input and if that's the case there should be no hardware concerns about using bluetooth with the pico w so it remains to be seen whether we can be using bluetooth with the pico w in the near future however there is as i mentioned another elephant in the room and that elephant is simply the esp32 how does this microcontroller compare to the esp32 and why would we want to use a pico w in lieu of an esp32 now when you compare the pico w to the esp-32 the esp-32 certainly seems to come out ahead the pico w has a dual core microcontroller whereas the esp32 is available with either single or dual cores the pico w runs at 48 megahertz with a top speed of 133 megahertz the esp-32s are available from 80 megahertz to a whopping 240 megahertz on some new models the picow is 264 kilobytes of static ram whereas the esp-32 has 520 kilobytes of sram both units are capable of 2.4 gigahertz wi-fi there are now some esp32s that are also capable of 5 gigahertz wi-fi now the pico w of course has no bluetooth as we've just mentioned whereas the esp32 does indeed have bluetooth the pico w has 26 gpio pins and two uarts whereas the esp32 has 36 gpios and three uarts the pico w has three 12-bit analog to digital converters as opposed to 18 analog-to-digital converters in the esp-32 the pico w has a built-in temperature sensor the esp32 also has that and it also has touch sensors it would seem from this comparison that the esp32 is indeed in first place with the pico w being in second place however such comparisons don't tell the entire story and there may be other reasons why you'd want to use a pico w one of the reasons could be the price at six dollars the pico w is cheaper than pretty well every esp32 module that you can possibly find although the price difference isn't that big especially if you're buying from sources like aliexpress or ebay the pico w also will have some advantages in that you can reuse the code that you've used in previous pico designs and so there may be a number of reasons that you want just a wi-fi enabled project and you want to use advantages like the pico w's pio ports and for that case the picow would be a great thing to use at any rate they're both great microcontrollers and today we're going to look at using that pico w with the arduino ide now in order to use the new pico w with your arduino ide you're going to have to add another boards manager and this is basically the same procedure we went through when we added esp32 capability to the ide now the boards manager that we're going to be adding is a really great product not only does it cover the pico and the pico w but it covers pretty well every rp 2040 based microcontroller board there is out there so it's a good addition to your arduino ide now i'm going to show you how to install this boards manager for both the classic arduino ide 1.8 and for the newer arduino ide 2.0 so let's go and take a look at this miraculous boards manager now i'm on the github repository for earl f philhauer iii and he has some amazing code in his repository including this boards manager for the arduino ide for just about every rp2040 board that you can imagine and this is a repository that he maintains very frequently you can see that the last update was nine hours ago now i want to scroll down here and show you the list of supported boards it's pretty impressive we start off of course with the pico and the pico w but you'll also see just about every rp2040 board that you can think of on this repository and since he maintains it so frequently as new boards come out they're sure to have a place on here so he's really created the great resource now we're going to need a json file to install that into our arduino ide and he's got the json file right down over here so i'm just going to copy that and then we'll go into the ide and we'll configure it to use this boards manager now i'm in the classic arduino ide the 1.8 version and i'm going to install that boards manager from that json file that we copied off of the github page so we'll go into file and we'll go down into preferences over here and here's preferences you'll notice down here we have a box that says additional boards manager urls now if this is a new copy the arduino ide and you're just getting started you may just have this field over here blank in this text box and if that's the case you can just paste that json file into it but if you're like me and you've already got some entries here you can click this little icon to the side of the text box and i'll just make this a bit bigger so we can see it and this has two of these json files already in it so i'm just going to add the one that we added today you need to add them each as their own independent line so i've added it over here and i can say ok and now i can say ok over here and now what i need to do is go into the boards manager so i'm going to go over here to where i show the board that i currently have or the one that i last used and i'll go into boards manager and bring that up and there's a number of things you can filter by i just filtered by pico and here it is over here the raspberry pi pico rp 2040 uh by earl f philhauer iii and that's the one that we want to install this is the one that our boards manager was just modified with the json file to include so we're going to install that and we can close this now and we are done we have now installed the boards manager and so we go down to tools over here and go into board you'll look over here and you'll see that we have this new boards manager here and you'll notice that it has a whole group of different uh types of processors including the raspberry pi pico w now you can also install this boards manager into the ide 2.0 and this is the release candidate 9 of ide 2.0 and it's the same procedure you go into file you go down to your preferences and once again you'll see a field for the additional board manager urls since mine is occupied i can go and hit this icon over here and i can see that it can add another line to that so let's just paste my line in there we will say ok and now we'll say okay over here and this has now been included and so now we can go into our boards manager which you do this by clicking on this icon over here and we can filter by pico again and there it is over there and the interesting thing that i found with the arduino ide 2.0 is that once you put the json file in there it gets installed for you you don't need to uninst do an installation it's already there for you and so that's the method that you'll use to add this boards manager to ide 2.0 so now that we've got our boards manager installed it is time to start working with our pico w and as is tradition with all new microcontrollers we're going to start off with the blink sketch now you might argue that this isn't really a new microcontroller after all this is the raspberry pi pico that has the addition of the wi-fi capability and blink has nothing to do with wi-fi but there is a good reason for testing with blink and if you look down in blink you'll see what the reason is it uses the constant led built-in for the on-board led on the microcontroller that you're loading it onto and if you recall on the pico w they've changed the way that that led works it is now connected to a gpio port on the infrared wi-fi chip not on the rp2040 as it used to be on the pico and so the board manager should be able to translate that and do this correctly and so it is actually a good test for the board's manager to run the blink sketch now i've got my pico w hooked up to the computer and really not much to it just basically onto a usb port with a micro usb cable and i'm going to upload and everything should work fine but if it doesn't work fine you can resolve it usually by doing this there's a boot cell key the boot selector key on the picot you can hold that down you can pull out the cable while it's still being held down pull out the usb cable and then plug the cable back in and after you plug the cable in release the button and that'll usually reset it correctly now if you do have to do that if at all possible try unplugging and plugging the cable on the computer end and not on the pico end because this micro usb connector is rather fragile so the less that you have to exercise it the better but i'm just going to hope for a clean upload right now i've got my pico sitting over here and we'll upload the fabulous blink sketch to it and you'll notice that drive that appears briefly at the top when you do an upload that's actually a good indication that your upload is successful and of course another good indication is that our onboard led is indeed blinking on and off once per second as it would do with the blink sketch so now that we've successfully loaded blink let's move on to a few more interesting aspects of coding for the raspberry pi pico w with the arduino ide now while the blink sketch is a great sketch to use to test out a new microcontroller it doesn't test out the feature of the raspberry pi pico w that i think everyone is most interested in and that's of course the wi-fi capability but there are some example sketches that were included with the boards manager to do that and i've loaded one up to my pico already called scan networks and you can find that just by going to examples and then going down to examples for raspberry pi pico w where you'll see a wealth of different code examples these have mostly been just copied off of other microcontroller code samples and made sure or modified to work with the pico w under the wi-fi section you'll see a number of examples and i've got scan networks open right now now scan networks is a pretty simple application it makes use of the wi-fi library which of course is the heart of all the wi-fi activity on the pico w as well as many other microcontrollers so we include that library we're going to use our serial monitor to display the results so we set the baud rate for that up we define a couple of character constants to format our results and then in the loop what we're going to do is we're going to use the wi-fi property scan network stack to get the number of networks back if we don't get a count and that's what we're going to get here is the count of the number of networks will print no networks found but if we do we'll found we find the number of networks we print the number of networks that we found we print a header over here for a table that we're going to create and then we go into a four next loop and we go through the entire count and for every one of them we print out a number of the parameters onto that table and we make it onto a new line then when we finish we just print out sleeping and we delay everything by five seconds and go and do it again and when it starts you'll notice it delays by five seconds as well and that's to give the wi-fi time for it to stabilize there's actually going to be a 10 second delay between readings now as i said i've already got that loaded so let's just open up my serial monitor to see if i've got some scan networks it's beginning our scan and there's a scan of all the various networks that are available to me here in the workshop that are being seen by my raspberry pi pico w so that pretty well proves out that the pico w is capable of wi-fi which of course is its main feature now connecting to a wi-fi network with the pico w or any other wi-fi based microcontroller is a pretty straightforward process you provide an ssid which is a network name and you provide a password and assuming these are accepted your network's dhcp server will kick back an ip address and that's the ip address of your station now in order to accomplish all of this we use the wi-fi library and so what we're going to be doing right now is going through a basic connection example with the wi-fi library and you can use this connection example in any of your wi-fi enabled projects now here's a basic demo of what we need to do in order to run the pico w in station mode and in station mode this is a station on your wi-fi network it'll need to go out and obtain an ip address and the ip address comes from your dhcp server which in most people's network is part of their router now this is a pretty simple demonstration but you can use it as the basis for anything that you build with the pico w that needs wi-fi connectivity now we start off of course by including the wi-fi library which is pretty well the meat and potatoes of everything and then after that we define a couple of character constants for ssid and password and those are the credentials that you're going to need to connect to your wi-fi network and so you're going to substitute your own credentials for your ssid and your password over here now we'll go into the setup and we're going to be using the serial monitor to display our connectivity status and to display the ip address that we get and so we're going to begin our serial monitor and we're going to operate our wi-fi in station mode now in actual fact i haven't been able to figure out how to get the pico w to operate in anything other than station mode i've been experimenting with access point mode but it doesn't really work that well and from what i've been reading on the internet i'm not the only person having problems with that so this line is actually optional but it does set the mode to station mode and if you don't give it the line it'll default the station mode anyway and then we're going to start our wi-fi and we just do a wi-fi dot begin and pass it that ssid and password that we created earlier and so right now we're trying to connect to the wi-fi what we're going to do is we're going to print a series of periods onto our serial monitor while we're establishing a connection so while the established while the status is that we are not connected we're going to delay for a short period we're going to print a period we're going to delay again and then do the whole thing over and over again and so we'll get appeared on our screen and it'll increment until we're actually connected once we are connected we're going to just add a new line and we're going to say the pico w is connected to the wi-fi network and give it the name of the wi-fi network and then we're going to print out the ip address and so we're going to say our assigned ip address is this and it's the local ip that we're getting and in the loop we're going to delay for two seconds and then keep printing that ip address because this activity can go by very quickly and one thing about using the pico w with the arduino ide is you can't have your serial monitor open while you're uploading to it there'll be a conflict and so by the time you get your monitor hooked up you may indeed not see any of the periods or this initial message so i just put this into the loop to keep repeating the address just to see what our address is because we'll need that for our sketch and so this is a very basic sketch but it does show you how to connect the wi-fi let's load it up to the pico w right now which i've actually already done and take a look at what happens and so i've loaded it up let's just open my serial monitor now and my ice p address is 192.168.39.179 now i'm going to open my terminal over here let's just get that smaller so that we can see both screens okay and we're going to ping that address and we'll just do a ping on that i'll make that bigger now and as you can see we're getting data back and so we indeed do have a device connected to it and in order to prove that i'm going to disconnect the power from my device and everything is stopped and so that was indeed the pico w that we're pinging and so as you can see it's quite easy to get the pico w on to the wi-fi network using the wi-fi library i think you'll find for most part it's basically the same as working with the esp32 so now let's move on to a few other things now there are many reasons that you might want to connect the microcontroller up to a wi-fi network and one of them is to make use of data that you can get on the internet through apis or other methods from internet based servers and there's a good example of doing that that's already included with the examples that came with the pico w library you'll find this wi-fi client example which i've already loaded over here to be in the same section as where we found the network scan application now wi-fi client establishes a connection to a server out on the internet and then comes back and prints out a quote of the day and then it'll go to sleep for about five minutes and then do it again and the reason for the long delay is to avoid flooding that server and it would disconnect us if we were doing that now it starts off by including wi-fi and we establish both the ssid and password so you're going to have to replace that with the ssid and password for your network and we also establish a hostname down over here and that's the server we're going to get the quote from and that's the host and that's the port on the host so it's not a regular http call because it is on port 17. now we'll go into the setup we set up the serial monitor and we'll connect to our wi-fi network and setup and in the loop we'll go and we'll go and connect to our host so we connect to our host and if we actually connect successfully we'll send a string out to it and you can see that this example is this one board from the esp8266 because the string we're sending is literally hello from esp8266 and it really can be any string so you can change that to pico w if that bothers you we'll wait for some data to become available and if it doesn't timeout we'll get that data and we will print that data out to our serial monitor and then after that we will close our connection and then we're going to wait and as i said it's going to wait for five minutes so for that reason i've actually been running this for a little while on my serial monitor so i'd get a couple of these quotes of the day and so we'll just open my serial monitor and there you go i've connected the wi-fi and you can see the first one i came back with was from george bernard shaw and another one from shaw over here shaw and dickens seemed to be a lot of the quotes you get over here so here we have two quotes and then it waited for a while we got a dickens quote over here waited for a while we got a repeat of the shaw one which is another thing that i notice if you run it for a while you'll see several repeats because there really are only about a dozen at the most of these quotes of the day at least at the moment when i'm running it but this is a good example again of how a microcontroller can go out to the internet and grab data now one very common task when using microcontrollers that have web capabilities is to build a user interface using a web page and when raspberry pi released the pico w they provided a code sample in micro python for doing exactly that in their code sample you built a web page that had a push button and the push button allowed you to control the pico w's on board led well i've replicated that code in c plus plus and that's what i want to show you right now so let's go and see how we can build a web-based interface using the arduino ide and our pico w now here's the control that we're going to be using to control the built-in led on our raspberry pi pico w and this is basically an adaptation of some code that was written for the esp32 by rui santos over at the random nerd tutorials and i've taken a lot of his html aspects and just modified it a little bit for our purposes over here otherwise the code is actually very similar because we're working with the wi-fi library which is similar in the esp32 and in the picow now we'll start off by including that wi-fi library and then of course we're going to set up our network credentials so you'll have to replace these with the proper ones for your network then we're going to start a wi-fi server and we're going to call the object server and we're going to open it on port 80. port 80 is the http or hypertext transfer protocol port and that's what we use for a web page we'll need a string to store the header that's the response that comes in from the http request and we'll also use a string to store the state of the led now you might think we would use a boolean for that but if we use a string for this we can also write this out to the web page so we can write the state off and we'll initialize this with a value of off now these variables over here are just so that we can create a time delay without using the delay function and we're going to use this for a timeout to make certain that our page hasn't timed out and then we'll go into the setup now in setup we'll start by setting up our serial monitor so we can take a look at everything that's happening we'll initialize the built-in led as an output and again we're using the constant led built-in for that led because remember that led is actually been driven off of the infrared wi-fi chip it's not being driven directly from the rp-2040 then we'll set that led off so we'll set it low and here's where we go and connect to the wi-fi network as we did in our previous wi-fi sketch we'll connect the network and we'll print out our ip address and then we're going to start that server object that we created earlier so we'll start that over here now we go into the loop and in the loop we are listening for any incoming clients and if we find the new client we start a timeout and then we capture what is in the header of the client so that's what all this code is doing over here now in the header it's going to be interesting because what we're going to be doing is sending a different url every time we press the button we'll either append it with led on or led off so we're going to look for that condition inside our header and if we see led on then we're going to print that to our serial monitor we're also going to set that pico led state the string that we defined earlier to on and if we see it led off we're going to do the opposite thing to find the strings off and in both cases we're going to control the led so we're going to set the led built in high if we see it on and we set it low if we see it off now comes the point where we actually display the html web page and you can see the html after all of the print lines over here after the client println because the client println prints what the client is going to see so here's our web page itself back here here's the styling for the buttons so if you want to change some of the colors on the buttons etc if you want to change the size of them this is where you would modify the page then we print the header over here on the page with an h1 tag the pico w led control and then we're going to print the state of the button and that's why we have pico led state as a string so we can just print out whether the led is on or off and then we're going to set the buttons we're going to take a look at pico led state if it's off we're going to display the on button and if it's on we're going to display the off button and that's what we do over here and then this is the end of the http response down over here we'll clear the header variable and we'll stop over here once the client has been disconnected and that of course happens if we get out of this loop over here and so that's basically the code if you break it down it's not that difficult to work with although it does seem like a lot of code let's load it up to our pico w and see if we can control our onboard led okay so i'm ready to test out my pico w led control i've got my pico w city in the experimenter's breadboard still although i could just have it loose right now because honestly it's not connected to anything and we're controlling the onboard led the led is kind of small but it's up over here so hopefully you'll be able to see it go on and off now on the screen i've got my pico w led control web page over here it's at the ip address that the pico w picked up when it connected to the network and it says that the onboard led is off which is indeed true and i have a switch to turn it on i've also got over here my serial monitor and you'll notice every time i click on here i'm going to get data in the serial monitor now when i highlight the button you can see the link it's going to and it's going to send the same url with led on written on it so let's go and click on that and as you can see it does indeed turn the led on and now the link is to go to led off now the display now tells me the onboard led is on it gives me the option of turning it off and i'm going to toggle it again and this time take a look at the serial monitor every time i do i'm going to get data in the serial monitor and this is the data i'm getting back from the client in this case the client being the web browser and of course as you can see it also is toggling the led on and off so this is basically just a replication of the same demonstration that you've already seen for the pico double using micropython except this time we're using c plus plus and the arduino ide now at the beginning of the video i mentioned that one of the reasons that you might want to use the arduino ide to program the pico w is that it would allow you to reuse code that you already had for the esp8266 or esp32 and that's exactly what we're going to do right now i'm going to take some code that i used in an earlier video a video about controlling servo motors with the esp32 and i'm going to use it on the pico w and what we're going to do is build a web-based interface with a slider control on it that allows you to control the position of a servo motor so let's go and take a look at that now for our experiment we will of course be using a raspberry pi pico w we'll also be using a servo motor a small sg90 or equivalent hobby servo motor will be great and you'll need a power supply for that servo motor anything from five to six volts dc will work i'm using a six volt dc power pack that has four double a batteries in it we'll begin by connecting the control lead of the servo motor to gpio pin 2 on the pico w the control lead is usually an orange wire we'll connect the ground of the servo motor which is generally either a brown or a black wire to the negative side of the power supply we'll also connect this to one of the grounds on the pico any of the many grounds on the pico interface will suffice and finally we'll connect the power lead of the servo to the positive side of the power supply and this completes the wiring now let's go and take a look at some code that we can use to drive the servo with our pico w now before we get into our web-based servo control i just want to make certain that our servo motor is hooked up and functioning properly and an easy way to do that is this to run the sweep sketch now this is another sketch that you'll find in the examples for the rp2040 but it's just been ported over from other microcontrollers because i'm certain that you've seen the sweepsketch before you've probably used it with the arduino the sketch is very simple it just includes the servo library creates a my object and then it attaches the servo in the setup to the pin that we're using in this case gpio pin number two and then it goes into a loop and it creates an integer called pause which is the position and it uses a for loop for 0 to 180 it will increment it and so it's going to increment it by 1 and go from 0 to 180 and then every increment it's going to write the position to the servo motor and do a small delay and do it again so our servo should step from one end to the other and then when it reaches the other end it's going to do the exact same thing but do it backwards so it's going to decrement it by one from 180 down to zero and then the loop will just repeat itself so the consequence will be that the servo just goes back and forth so let's go and load that up to our picot and it's loaded and if we look at our servo motor we can see that it is acting accordingly it is going back and forth and being stepped back and forth by the uh the program so it's a very simple little sketch but it does prove out that the servo motor is hooked up correctly and is functioning properly and now we can get into the sketch that we're going to use to create a web based servo control with the pico w now the code that i'm going to adapt came from this article that i did earlier on using servo motors with the esp32 and if we go down here we'll see that i had a web remote controlled servo and so let's go and take a look at the code for that because that's what i'm basically going to be repurposing and here's the code down over here now it's a bit hard to read on this screen right now but you could essentially go over to the dronebotworkshop.com website take a look at this code and then take a look at the code that we're going to be using and compare the two of them now here's the modified version of the code for the pico w there were very few modifications to make we're going to start off by including the two libraries and that's actually where the main modification was the original sketch used the esp servo library because the servo library itself doesn't work with the esp32 but in this case it works here so we're going to be using the servo library otherwise it's pretty standard we're going to create a servo object called my servo and we're going to define the pin that the servo's on which is gpio pin number two here's where we slip in our wi-fi credentials and we're going to start a wi-fi server object on port 80 for http traffic and create a variable to store the header we're also going to be decoding the http get we're going to be using ajax for this function and so we're going to break that down into a string that is an array of five different elements and we're going to have two different position variables inside there as well to measure the position of a couple of specific characters now we're going to once again create a time delay by setting up a current and previous time over here and then we'll run into our setup now in setup we're going to attach the servo motor to the servo pin and these two extra parameters over here allow you to fine tune the operation of the servo motor so if you find that it isn't going from exactly 0 to 180 degrees you can tune these two values the minimum and maximum value over here respectively in order to make it perfect now we're going to start our serial monitor and connect to the wi-fi as we did before and at the bottom over here we're going to begin our server the one we defined on port 80. now in the loop we're going to listen for incoming clients and when the client comes once again we're going to set up a time delay to make certain that they don't time out i'm going to print out the serial monitor and we're going to hold any data that we get from the new client now when the client is connected we're going to look to see if we have any data if we're going to get it we're going to get this data and then we're going to print it out to the serial monitor and now we're going to go and set the actual http connection up over here uh here's the html web page that starts off over here the css for the page so again you can modify it if you want to change the colors this is the modification you need to make if you want to change the appearance of the slider that we're using we're going to be using jquery because we're going to be using ajax to communicate this back and forth between the uh web browser and the server and so we're going to load jquery over here from the google source and we're going to put a page title in over here this is the line in which we're going to display the position of the servo so we're going to display the servo position over here and here's the slider control itself and you'll notice the slider we've defined with a minimum value of 0 and a maximum of 180 and those are the values we'll send back to control the servo motor now here is all the javascript that we are going to be using it's a little hard to read javascript when it's written in code like this but you can break it out if you want to and format it to make it more readable but at any rate what we're doing is we're going to look at the inner html value for the slider value and we're going to get that value and use it in our code now here's the get data over here that we've extracted and we're going to set our position headers to look for the equal and the and sign because that's what separates the data for us and we'll use this to create a string that has the actual position value and then we'll move our servo into that position now since this is a string we need to use the two in function to make it into an integer and we'll print this out on our serial monitor and then we'll close off the http connection down over here so again there's a lot that's happening over here but when you break it down piece by piece it's not that hard to understand so let's load that up and see if we can control our servo motor with it so here's the demonstration of the servo motor now once again i've got the pico w inside the experimenter's breadboard that i built i've also got my servo motor here of course and the batteries that are powering the servo motor and everything's connected together with this small sawdust breadboard and so now if we look up on the web page you can see i've got my server servo control set to 102 degrees i'll move it down and you'll notice that my servo motor is also moving accordingly and so it does seem to work here's 180 on one end of the extreme and zero on the other and we'll move it down to about the middle here to about 90 moves over there now if we're on the web page you can actually see the data that we're sending back and forth it's not in the url but it's buried back there so if we go into inspect and i'm on google chrome right now by the way and we go to the network side over here and we move our slider you'll notice it creates an event every time we move it and i know that text is a bit small to see but basically it says the value equals 119 which is where we went to over here and so if i move it again the value equals 41 and so that's the data that we're exchanging with the web server and so as you can see it wasn't that difficult to take an esp32 sketch for controlling a servo motor and porting it over to the new pico w all right well that brings an end to our look at the arduino ide with the pico w i hope that you found it useful and i hope that it's opened up your eyes to the fact that you don't only need to use micropython to program the pico w now if you want to get the code samples that i used today or if you want some more information about the pico w you will find an article on the dronebotworkshop.com website that accompanies this video and there's a link to that article right below the video and while you're on the website if you haven't yet please consider signing up for my newsletter it's not a sales letter it's just my way of letting you know what goes on in the workshop and of course it's free to sign up for another thing that is free is the dronebot workshop forums which is a great place to get together and discuss pico w's and all sorts of electronics and microcontroller projects with a number of like-minded individuals and you'll find information about joining the forum below the video as well and finally if you have not yet subscribed to the youtube channel please do me the honor of becoming my newest subscriber it's very easy to do all you need to do is click on the red subscribe button below the video and also click on that bell notification so that you'll get notified every time that i make a new video so until we meet the next time please take good care of yourself please stay safe out there and i will see you again very soon here in the dronebot workshop goodbye for now [Music] you
Info
Channel: DroneBot Workshop
Views: 107,846
Rating: undefined out of 5
Keywords: raspberry pi pico w, arduino ide, arduino ide 2.0, raspberry pi pico tutorial, raspberry pi pico
Id: yatxW3tMhRg
Channel Id: undefined
Length: 49min 26sec (2966 seconds)
Published: Sun Aug 07 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.