Developing for the Raspberry Pi Pico in VS Code — Start Coding!

Chris Wood
All Geek To Me
Published in
4 min readJan 29, 2021

In Part One, we set up your computer so you can make your Raspberry Pi Pico creations in Visual Studio Code. This time, we’ll do the Pico equivalent of Hello World and make the onboard LED flash.

Starting a new project

Open up VS Code and and go to the File menu and then choose Open. Navigate to a suitable place and then create a new folder called LED-Flash. Once you’ve created that folder, select it and open it up in Code.

If you can’t already see a Terminal panel in Code, switch it on via the View menu. All being well and assuming your Pico is plugged into a USB port, you should see that Pico-Go has connected to your board!

The Pico-Go Console

If you’ve got a message about not being able to connect to the serial port, you may need to stop any other software that’s using the device (e.g. Thonny) or physically unplug and then re-plug your Pico into the USB port.

Click the + button next to the Terminal drop-down to create a second Terminal. Here, you’ll need to type

micropy init

It will suggest a project name — just choose the default:

Then you’ll need to choose at least these two options:

If you intend to put your project in a Git repository afterwards, you’ll probably want the Git Ignore Template too.

Finally, you’ll need to choose the Pico’s stubs:

All being well, you should see something like this:

Let’s code!

Add a new file to the project called Flash.py . At this point, VS Code may ask to restart itself; go ahead, if so.

Let’s try out the auto-completion and linting! Start typing

import mach

you should see that it offers “machine” as an auto-complete option:

Choose “machine” to complete the first line.

Then type the following on the next line:

import ujson

This time, you should see something like this when you hover over the orange wavey line:

While ujson is a module in ordinary Python, it’s not available in the version of MicroPython used by the Pico. Linting is telling us that we’ve got a problem we need to fix. Now delete the line you just typed.

From here, type in the following code and you should receive auto-completion help all the way through. Note that parameters on functions aren’t provided; this is a limitation of the stubbing process.

import machine
import time
pin = machine.Pin(25, machine.Pin.OUT)while True:
pin.value(1)
time.sleep(1)
pin.value(0)
time.sleep(1)

Run, baby, run!

You should now be able to click the Run button on the Pico-Go status bar at the bottom of VS Code:

The magic should now be happening!

After a few fist-pumps, type Ctrl + C to stop your code.

Moving code onto the Pico board

The code was just running on the board, which is handy for quickly trying something, but we’ve not moved a file containing code to the board yet so it can run automatically when powered up away from a computer.

Rename Flash.py to main.py and then click Upload on the Pico-Go toolbar. You should see the file is copied to the board and then the board will reboot.

When it comes back, you should have a flashing light!

As you can see from the above screenshot, it took my board an extra second to become available to the terminal again, however once you see the “connecting” message, you should be able to use Ctrl + C again to stop your code from running.

Clearing all files from the board

When you’re embarking on your next project, you’ll want to clear any existing code off the board. To do this click the All Commands button on the Pico-Go toolbar and then choose

Pico-Go > Delete all files from board

Answer Yes to the confirmation and you’ll see the names of the files that have been deleted, followed by a soft reboot:

You’re now ready to go and build your next big thing!

--

--