Access the full workshop here: https://core-electronics.com.au/courses/raspberry-pi-pico-workshop/
Alright, let's finally set up our Pico and run our very first bit of code. There are many different coding languages out there, but for this course, we will be using the MicroPython language. It is a very popular language that is a variant of another popular language called Python. MicroPython is just a cut-down and more lightweight version of it designed to run on microcontrollers like our Pico here.
We are going to be using a program called Thonny, which is what we will be using to program our Pico with. So if you don't already have that installed, we have a link in the description or there's a link on the course site, or you can just head over to thonny.org. Once it's finished installing, we're going to open up Thonny and you should be looking at something like this.
Now we're going to install MicroPython on the Pico. So if you've already got MicroPython on your Pico setup, you can skip this phase. You can check if MicroPython is installed by plugging in your Pico and then in Thonny in the bottom right, you should be able to see MicroPython Raspberry Pi Pico as a selectable option. That means it is installed.
To install MicroPython, we'll first start by holding down the boot select button and then we'll plug our USB into the Pico. You can let go of it once it's plugged in. Then we're going to go back into Thonny and we're going to hit run and then select configure interpreter. Under this field here, we're going to make sure that we have MicroPython Raspberry Pi Pico selected, and then we're going to click install or update MicroPython.
Under variant, we're going to select either Pico PicoH or PicoWH, depending on which variant of the board you have. We've got a PicoWH, so we're just going to select this one and then we're going to leave the version alone and just click install. It shouldn't take too long to download and install MicroPython onto the Pico. And once it's done, your Pico is all set up, ready to go.
Once you've finished installing in this previous window, we're going to hit OK, and then we're going to reopen it again. You should now see that this port option here has selected the board. It should automatically do that. If not, you can go down into the bottom here and select MicroPython Raspberry Pi Pico and you'll see which COM port it is connected to. Selecting the right serial port is very important as we are telling the computer which serial connection the Pico is. If you don't select the right comport, you can't run any code you've written in Thonny on your Pico. It should be noted that my Pico is plugged into COM port three, but it might be different on your system and it most likely will.
So if we take a look at Thonny, we have two sections here. We've got this top section here, which is the editor. This is where we will be writing most of our MicroPython code. We also have this bottom section here, which is called the shell. And this is very similar to the editor, but it works in a slightly different way. You can think of the top editor as kind of a piece of paper that you write out all your code onto and then you send it to the Pico and then it executes all that code. The shell is like talking to the Pico directly, line by line. We send it a command, it reads it and it replies immediately. We like to call this REPL or Read Evaluate Print Loop. The shell here is used maybe if you want to try some code on the fly, but most of the time it's the place where we can see the Pico replying back to us when we ask it to. We might ask it to tell us what sensor data it is seeing or what it is currently doing, but we'll get into that later.
This is a good opportunity to introduce our first piece of code, the print function. To try it out, we're going to write in the shell, print, and then some brackets on the end. Now, this print function is going to print whatever we put in the brackets, but to get it to print a normal sentence, we'll need to put quotation marks in like so. In these quotation marks, you can type anything. So we're just going to put in, hello world. And if you hit enter, the Pico should reply back with whatever you put in the quotation marks.
Imagine you have just written your first piece of code. So what's happening here is that when we hit enter, the print command gets sent from Thonny to the Pico, and the Pico reads it, it knows what the print command does, and so it follows that order and prints back whatever was in the print command back to Thonny.
We can also write that same code in the editor, and we can kind of keep it there, and when we want to send it to the Pico, we can hit this big green button at the top here. And as you can see, it replies back exactly the same. But the advantage of the editor here is that you could then put in another line with something else. This is the second line. Wow. And if you run it again, the Pico will return with both lines. And this is why we like using the editor, because it lets us write out a big, long bit of code, and then we can send it to the Pico.
This specific formatting we have to use with the brackets and the quotation marks is called syntax, and it is kind of like grammar, but for coding. While in day-to-day conversations, you don't need to use perfect grammar for somebody to understand you, it is not the same when it comes to coding. You need to use correct grammar or syntax, otherwise the Pico won't understand what you're trying to tell it.
Don't worry too much about that, though. Syntax is easy to learn, and it's one of the many reasons we're using MicroPython, because it is simple, clean, and easy to understand. It's got very simple syntax compared to other languages.
I'll end this video by showing you some sample code. Don't worry about understanding this or how it works, I'm just showing you some important syntax before we begin. First of all, each line of code here is like a command that we're going to send to the Pico. I've got kind of these spaces between some of these lines. These spaces, they don't really do anything. They don't slow down the Pico, it just ignores the spaces that you put in there. The reason we use these spaces is to make the code more readable to humans like us. If I go through and I delete these spaces, this is the exact same code as before, but it might be a bit more hard to read to us humans, so we like to put spacing in there, but it is personal preference.
Another important thing is comments. If you look in the code here, we have some hashtags followed by some grayed-out text, and these are comments. The Pico will also ignore anything that you put after the hashtag, and we can use this as an opportunity to comment on our code to make it more understandable for humans. If you look at this line, you probably have no clue what's going on, but thankfully, somebody has commented what this line does. It sets up pin 16, which is connected to the LED. I'll quickly add another comment here just by putting in hashtag followed by putting in whatever text. So I say, this line puts the Pico to sleep for two seconds, and it is a very good idea to comment your code, as it might help someone else or future you in figuring out how this code works. You might come back to it later and have no idea what is going on, and these comments will be very helpful.
Makers love reviews as much as you do, please follow this link to review the products you have purchased.