Let's take our first steps with the Raspberry Pi Pico development board. We'll load MicroPython onto the Pico and then program it using the interactive REPL.


Let's take our first steps with Raspberry Pi Pico. We'll get it running MicroPython and we'll send commands to it over USB Serial that it will execute interactively using what's called the REPL. Let's get started.

Let's start by putting MicroPython onto the Pico. Follow this URL which is also linked below and you'll see this getting started page. If you scroll down there is a getting started with MicroPython tab and a friendly animation of what we're about to do. So download the UF2 file. That's going to go to my downloads directory and you'll see it here.

Next, hold down the button, the boot cell button, on the Pico and then connect it to your computer. And in this PC I can see there's a it looks like a flash drive it's appeared as a device called RPi-RP2. All we need to do is to drag that file into that directory. And as easy as that we now have MicroPython running on the Pico.

To program it we're going to connect to it over serial using a software called CoolTerm also linked below. Follow the link and you'll wind up on Roger Meyer's freeware. If you scroll down we have the application CoolTerm. This is a free terminal program. I'll download that. And in downloads I'll just need to right click extract. Open the directory that was extracted and we can now run CoolTerm. I'm just going to use the default preferences.

To connect CoolTerm to the Pico we need to go to options and select the COM port for the Pico. If I open up this COM port list I'm going to pick COM port 4 because that's probably the Pico. If you are unsure about what COM port to choose you can open your start menu and search for device manager. And under the section ports common lpt you can open that up and I can see two entries. The one that I'm interested in is the entry that says USB serial device and that's listed as COM 4. So that'll be fine. The rest of these settings are fine. Just come over to the terminal tab and use the terminal mode line mode. That'll make things a little bit easier. And finally we can press the connect button and if everything goes well then almost nothing should happen. There's just a small timer here saying that we're connected and we've been connected for a few seconds. 

So the Pico is ready and waiting for our code. So let's write some. Let's start with a very simple print statement. Print open bracket "Hello world" close the quote. Close the bracket. And there we have it. This is pretty cool. What we've just done is sent a command to the Pico and this is the REPL interface. The REPL interface is allowing us to execute code live interactively. So this code that we have just sent the Pico is executed and by executing that it's printed what we asked for. It's printed "Hello world". So it's executed our command on the spot. That's pretty cool.

Okay moving right along. Let's do something physical on the Pico. At the moment the only physical periphery on the Pico is the LED that's on pin 25. So let's blink that. In the terminal we need to import some packages. So I'll import pin. From the machine package we'll import pin functionality. And we need to define the... We need to create our LED object. So let's say LED equals pin 25. And it's an output. Okay so we'll import that. And then we'll import the pin function. And we'll call it an output. 

Okay so far so good. We've imported a package that allows us to control GPIO. And we have declared a GPIO object LED as being connected to pin 25 and we're calling it an output. So now we can drive it simply by executing led.value and we'll set it to a one to turn that GPIO on and I don't know if you can see it but that LED has just come on. Cool. I could also execute led.toggle. So it doesn't matter what state the LED is in but led.toggle will turn that off. So now by using the up arrow I can call the last command that I executed.  So I can press led.toggle, led.toggle. And as I keep doing that you'll see that LED turning on and off. 

But I'm doing that manually that's that's not very gratifying. How about we set up a timer and have that execute automatically.

This terminal is getting a little bit messy but you can always just hit the clear data to erase all that history. So let's create a function that will toggle the LED and a timer that will call that function periodically. All our code is still running so the LED is still set up. I've just cleared the terminal so that it's a little bit easier to see.

Let's import timer from machine. From machine import timer and create a timer object. Tim. I'll call him Tim. Tim equals timer. So we have our timer. Now we just need the function that our timer will call every time it elapses. Every time it ticks let's say. We can we can declare functions in the REPL as well. Let's let's see how this works.

I'll define a function and call it I don't know tick. Pass timer as the argument and enter : to begin the function definition. Now our prompt has changed to these ellipses. These three dots and that's telling us that there's just going to be a tab. So anything we enter here is going to be inside our function declaration.

Now I wish I hadn't cleared the terminal. We declared our LED GPIO object as the variable LED. So I'm going to tell the timer function to look for that global variable. Global LED. And now call LED toggle. So we have our function defined. All it's going to do is to toggle the LED. But we need to press enter a couple of times to leave the function definition. Let's let's see how that works. We still have the three ellipses so we're inside the function. If I hit enter we're still inside it. If I hit enter a couple more times you can see the prompt has changed back to the regular Python prompt. So that means that all of this is inside the function definition and now we're just back to entering live code.

All we need to do now is to set the timer to run. So we have the function that's going to be called. We just need to call it with the timer. So we'll tim.init to initialize the timer. And we'll run it at, I don't know, three Hz let's say. Let's say 3.5. You can enter floating point numbers here too. We'll set the mode as timer periodic. And callback, callback is the function that will be called every time the timer ticks. So 3.5 hertz, this is the function that's going to be called. And that's our tick function that we just wrote. And once I hit enter this LED should begin blinking. And indeed it does. So we have our LED blinking at a constant rate because the timer is calling that callback.

Let's have a bit of an experiment now. I'm going to create tim2 as a timer, just a second timer. And I wonder what would happen if I'm going to press the up arrow key to go back to my tim.init line. What if I initialize tim2 at a frequency of let's just say two Hz and use the same callback. So now I'll set up a second timer to call that toggle function. Now that's quite interesting. So you can see the LED now has a little bit of a different character to it. It's kind of a jerky loping frequency that it's flashing at. And probably what's happening here is that we have two timers running independently, but they're each calling the toggle command. So you have like a bit of a polyrhythm happening with this LED.

Michael from the future here, just to let you know, at the moment we're live coding and that means that when you power cycle or reset the Pico, it's going to drop that code that you've written and you'll have to reprogram it again through the REPL. Stay tuned for a follow-up video on scripting with Pico. And that means we'll write a script that uploads to the Pico. And when you remove power and power it again, or if you reset the Pico, it will still run that same program every time. Stay tuned for more.

There you have it. Uploading code to a Raspberry Pi Pico using the REPL. If you have any questions, head over to the Core Electronics forums and we'll see you there. Thanks for watching.


Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.