Getting Started with Raspberry Pi Pico

Updated 16 February 2023

This guide is a little outdated: Follow our updated guide for a much simpler way to get started!

Contents

  • Part 1 - Interacting Coding with the REPL
  • Part 2 - Moving Beyond the REPL (with rshell)

Part 1 - Interactive Coding with the REPL

This guide is a little outdated: Follow our updated guide for a much simpler way to get started!

Let's write our first program for the Raspberry Pi Pico.

To follow along, all you'll need is a Raspberry Pi Pico, and a micro-USB lead to connect the Pico to your computer.

Download software

  • MicroPython for Raspberry Pi Pico - this is software that runs on Pico which allows us to interactively program it. The downloaded file will be called micropython-[DATE].uf2
  • CoolTerm - A program to send commands to the Pico over USB.

The MicroPython download is under the "Getting Started with MicroPython" tab.

Copy MicroPython to your Pico

  • Hold the BOOTSEL button
  • Connect your Pico to your computer using the USB cable (The Pico will appear as a storage device.)
  • Copy + Paste the .uf2 file to your Pico

hold-bootsel-and-plug-in-usb

copy-micropython-to-pico

Run and configure CoolTerm

Navigate to where you downloaded CoolTerm and run CoolTerm.exe

Click Options and select your Pico's port. You can check which port is assigned to your Pico by opening device manager and looking for the USB Serial Device (Windows: Start > Settings > Device Manager)

port-number    terminal-line-mode

In the Terminal menu, set the Terminal Mode to Line Mode

Click Ok

Click Connect

connect-to-pico

Hello World!

Now we can execute Python code interactively on our Pico. Enter the command print("hello world!") and the Pico should execute the print statement, displaying the text back on the next line.

pico-hello-world

Our Pico has just executed its first line of code! Hello, World!

This interactive programming interface is called the REPL (Read Evaluate Print Loop) - it's super useful for testing out commands and quickly prototyping bits of code.

Drive the onboard LED

Now we'll drive the onboard LED connected to pin 25. We'll need to import the Pin package to control the pin, and set the pin to an OUTput so it can drive the LED.

from machine import Pin
led = Pin(25, Pin.OUT)
led.value(1)

The LED should turn on. The value() function sets the state of the LED: 1 is on, 0 is off. We can also use toggle() which will toggle the state of the LED.

drive-led  led-on

Set up a timer - flash the LED

We have manual control of the LED, now let's set it up to blink automatically. We'll import the Timer package, create a timer, and define a function to toggle the LED

Enter the following code (don't include the ... these are just to show what the terminal will read-back)

from machine import Timer
tim = Timer()
def tick(timer):
...    global led
...    led.toggle()

now press enter a few times until the prompt returns to normal >>>
Finally, we initialise the timer with a frequency, and set it to call the tick() function we just defined.

tim.init(freq=3.5, mode=Timer.PERIODIC, callback=tick)

setup-timer-blink-led

And with that, our LED blinks with a regular frequency.

Remix

I wonder what would happen if we declared another timer eg. tim2 = Timer() and initialised it to call tick with a different frequency... have a go yourself, and check the end of the video if you need help!


Part 2: Moving Beyond the REPL (with rshell)

Since the REPL is live-coding, any time you power-cycle your Pico the program will be erased. To flash our code to the Pico, we're going to use a Python tool called rshell.

Install Python & rshell

Next, we'll install rshell using pip: the built-in package manager for Python (comes bundled with Python versions 3.4 and up).
Open a terminal (Windows: PowerShell) and execute the following command:
pip install rshell

Begin an rshell session

Make sure your Pico is plugged in via USB, and that you have disconnected CoolTerm. 

We'll connect to the Pico with the following command:

rshell -p COMx --buffer-size 512

Where COMx is your COM port eg. I'm using COM4.

connect-rshell

Things to note after connecting is there are no entries after "Retrieving root directories" because we have not flashed any code to the Pico yet. The board_name is also pyboard - we'll make use of this later.

Create a script

We'll need a script to flash to the Pico - I'll use similar code to what we saw in Part 1. Copy and paste the following code into your favourite text editor (Notepad, Atom, Notepad++ etc.) and save it as main.py somewhere convenient - I chose a location under My Documents

from machine import Pin, Timer

led = Pin(25, Pin.OUT)
tim = Timer()

def tick(timer):
    global led
    led.toggle()
    print('Toggle LED')

tim.init(freq=1, mode=Timer.PERIODIC, callback=tick)

Flash the script

Navigate to the directory you saved your script using the Change Directory command: cd. I saved mine in My Documents > code > main.py so I execute cd Documents/code/ to change into that directory.

The ls command lists files in a directory, this can be helpful for making sure you're in the right place -  you should see your main.py file.

Finally, we need to copy the script. For this we use the Copy (cp) command, which has the format: cp <from> <to>. Here, we copy to the Pico which has the name /pyboard, so the command will be:

cp main.py /pyboard/main.py

rshell-copy

And with that, our Pico is flashed! Power-cycle your Pico by removing and re-connecting power and the LED should immediately begin blinking. If you connect to the Pico using CoolTerm you should also see the "Toggle LED" message appear.

Exiting rshell

Notice how the terminal prompt turned green after we ran rshell? This tells us we're in an active rshell session. To quit rshell and return to your normal command prompt, press Ctrl+C. You can also use the exit command.

rshell-exit

That concludes our Getting-Started guide. If you have any questions feel free to start the conversation below, or over on our forums.

Workflow tip

You can get away with omitting the board name when flashing your script eg. cp main.py /main.py

A reason for including the board-name is that you can be sure your script is going to the right place. This is a useful habit to get into - especially when executing dangerous commands like "delete everything" ( rm -r * ) Including /pyboard/ in the command guarantees you won't accidentally delete files on your computer. It also allows for having multiple MicroPython boards connected at the same time, and being able to choose the correct target for your script.

Have a question? Ask the Author of this guide today!

Please enter minimum 20 characters

Your comment will be posted (automatically) on our Support Forum which is publicly accessible. Don't enter private information, such as your phone number.

Expect a quick reply during business hours, many of us check-in over the weekend as well.

Comments


Loading...
Feedback

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.