G'day, I'm going to show you how to program your Raspberry Pi Pico using Thonny. We'll install Thonny if you need to, write our first script, and afterwards I'll even show you a couple of neat debugging tricks.
The first step is to download Thonny if you don't already have it. The good news for those following along on a Raspberry Pi at home, you already have Thonny. For me, I'm running Windows today, so I'll need to download Thonny. Navigate to thonny.org and choose the version that looks right for you. And in my downloads folder, I can see that Thonny has downloaded. I'll run the installer. Normal installer stuff.
Great, let's run Thonny. Time to plug our Pico in. My Pico has pins on it, it's okay if yours doesn't, that won't make a difference for now. Hold down the boot cell button and connect your Pico to your computer over the USB cable.
Go to run and select interpreter, and make sure that you select MicroPython Raspberry Pi Pico. It's also a good idea to install or update the firmware first. And that's done.
One last thing that makes life a bit easier is to go to view and files. And this will show us our file tree on the left. So we have the local files on our computer and the files that are on the Pico. And we are ready to go.
I have the REPL down the bottom here. So this is the interactive Python REPL that's running on the Pico. We can actually execute some live code. I could say print, print hello world, and the Pico will take that print command and send it right back. The REPL is great for testing small bits of code to makeSure, let's format the closed-captions transcript into paragraphs:
"Sure that they work as you go. Now let's write a script. This top window here is our empty untitled script.
First thing is to import some packages. From machine import pin. This will give us the ability to control the pins on the Pico. I'll assign LED to pin 25. That's the pin on the microcontroller that the LED is connected to. And call that an output.
Now if we create a loop by saying while true, we can toggle that LED. But if we were to just execute this, the LED would turn on and off so quickly we wouldn't be able to see. So we need to include a delay. We can say from time import sleep. And we can then use the sleep function to create a delay of I'm going to say half a second.
We can save the script and save it to the Raspberry Pi Pico and call it main.py. If all goes well, you should see on your Raspberry Pi Pico, there is a new file called main.py. The file isn't actually executing yet. For that, we need to restart the Pico. From that REPL interface, you can press control D. That's called a soft reboot command. And now, now that the Pico has booted up, it's going to look for that main.py file and run it. And we can see here that the LED is flashing.
It's often useful to print text to the screen when you're programming so that you can do some debugging. So let's do that now. I'm going to create a number N and set that to zero for now. And now after toggling that LED, I can execute a print command and say that 13 times, and now we call upon our variable N is some other variable. Close the quotes and open the format function. So 13 times N, so we put N as our first entry, and then 13 times N as our second entry. 13 times N. Perfect.
So the order that you put arguments in here will matter. The first argument will"Go in this first place and the second argument will go in this second place. And finally, we just need to increment N. N equals N plus one. Save that file again and control D for a soft reboot.
And there we have it. We have a blinking LED and we also have some nice printed commands coming out on our screen. So this is a neat way to print the values of variables to your console so you can see what's going on.
There is a pretty cool feature as well, where if you go to view and plotter, you actually get to see a plot of various things that are happening in the Pico. So down the bottom here, we have the key. Maybe you can see this. Blue times, is that orange, is red. So it's picked up on our print statement and it's actually extracting information from it. We can see that this line here is slowly increasing in value, and right now it's over a thousand. So this is corresponding to the result of our multiplication. At the moment, it's nearly 1500. And we have this orange line here. This would be our N because we have 13 times N. And we can pick that out of the argument as well, because it's actually showing us our print statement. So this is really neat.
Looking back at our file tree, we can see that main.py is on the Pico. So if we disconnect the Pico and then reconnect it, we can see that back in Thonny, the Pico was disconnected and also that file tree window has disappeared. Even though our Pico is running our script, to have it appear back in Thonny, we just need to use the stop restart function. And now we have our REPL again and we can see our file tree. However, this does mean that our Pico has stopped executing its code. But we can just start it again using control D.
So we've been saving our scripts directly.To the Pico so far, as you can see in the file tree here. If you want to create a backup of that on your computer, you can right click and select download to my user home directory, and then you can put it wherever you like.
Also, for scripts that are on your computer already, you can right click them and select upload to, and that's the root directory symbol. So that will upload it to your Pico and you'll get a warning that it's going to overwrite the same file on your Pico.
And with that, we are on our way to programming the Raspberry Pi Pico using MicroPython and Thonny. I hope you learned something. I know I did.
If you have any questions remaining, feel free to open a thread on the Core Electronics forums. We're full-time makers and here to help.
Catch you next time.
Makers love reviews as much as you do, please follow this link to review the products you have purchased.