Programming Raspberry Pi Pico with VS Code and MicroPython

In this guide, you’ll learn how to program the Raspberry Pi Pico board with MicroPython using VS Code Editor and the MicroPico extension (previously called Pico-W-Go).

Programming Raspberry Pi Pico with VS Code and MicroPython

New to the Raspberry Pi Pico? Check out our Raspberry Pi Pico Getting Started Guide.

If you like to program using VS Code, great news. There’s an extension for VS Code that allows you to program your Raspberry Pi Pico board using VS Code editor.

If you’re not used to programming using VS Code, it might be a little difficult to get started. You may stick to simpler IDEs, like Thonny IDE.

Prerequisites

To program the Raspberry Pi Pico with VS Code using MicroPython, you need to check the following steps.

1) Installing Python

You need to have Python 3.9 or newer installed on your system and in your PATH. You also need to have the pyserial package installed.

2) Flashing MicroPython Firmware

You need to install MicroPython firmware on your Raspberry Pi Pico. Follow the next section.

3) Installing VS Code

You need to install VS Code on your computer. Follow one of the next sections if you need to install VS Code:

4) Installing MicroPico Extension

Finally, follow the instructions to set up the MicroPico extension, configure your project folder and run your first project:


Installing Python

1. Go to the Python Downloads page: www.python.org/downloads and download the installation file.

Download and install Python

2. After a few seconds, you should have an executable file downloaded to your computer. Double-click the file to open it.

3. Make sure you have the option Add Python to PATH ticked to add Python to path.

4. Then, you can proceed with the installation.


Flashing MicroPython Firmware on the Raspberry Pi Pico

You can flash MicroPython firmware on your Raspberry Pi Pico by connecting it to a computer via USB, then dragging and dropping a file onto it. Click on one of the following links to download the MicroPython firmware for your board (if you have a W version, you can choose if you want to add support for Bluetooth):

Then follow the next steps:

Connect the board to your computer via the USB cable while holding the BOOTSEL button at the same time to put it in bootloader mode.

Raspberry Pi Pico BOOT Mode

A new Mass Storage Device called RPi-RP2 will show up.

Raspberry Pi Pico Storage Device

Drag and drop the MicroPython file you downloaded previously onto the RPI-RP2 device. The window will automatically close and your device will be running MicroPython.


A) Installing VS Code on Windows

Go to https://code.visualstudio.com/ and download the stable build for your operating system (Windows).

Microsoft Visual Studio Code VS Code Download Page for Windows PC

Click on the installation wizard to start the installation and follow all the steps to complete the installation. Accept the agreement and press the Next button.

Microsoft Visual Studio Code VS Code Installation wizard

Select the following options and click Next.

Microsoft Visual Studio Code VS Code Installation wizard step 2

Press the Install button.

Microsoft Visual Studio Code VS Code Installation wizard step 3

Finally, click Finish to finish the installation.

Microsoft Visual Studio Code VS Code Installation wizard final step

Open VS Code and you’ll be greeted by a Welcome tab with the released notes of the newest version.

Microsoft Visual Studio Code VS Code Installation wizard welcome screen on a Windows PC

That’s it. Visual Studio Code was successfully installed.


B) Installing VS Code on Mac OS X

Go to https://code.visualstudio.com/ and download the stable build for your operating system (Mac OS X).

Microsoft Visual Studio Code VS Code Download Page for Mac OS X

After downloading the Visual Studio Code application file, you’ll be prompted with the following message. Press the “Open” button.

Microsoft Visual Studio Code VS Code Downloading application file for Mac OS X

Or open your Downloads folder and open Visual Studio Code.

Microsoft Visual Studio Code VS Code Downloading application file for Mac OS X

After that, you’ll be greeted by a Welcome tab with the released notes of the newest version.

Microsoft Visual Studio Code VS Code Installation wizard welcome screen on Max OS X

That’s it. Visual Studio Code was successfully installed.


C) Installing VS Code on Linux Ubuntu

Go to https://code.visualstudio.com/ and download the stable build for your operating system (Linux Ubuntu).

Microsoft Visual Studio Code VS Code Download Page for Linux Ubuntu

Save the installation file:

Microsoft Visual Studio Code VS Code Save installation file for Linux Ubuntu

To install it, open a Terminal windows, navigate to your Downloads folder and run the following command to install VS Code.

$ cd Downloads
~/Downloads $ sudo apt install ./code_1.49.1-1600299189_amd64.deb

When the installation is finished, VS Code should be available in your applications menu.

Microsoft Visual Studio Code VS Code Open application Linux Ubuntu

Open VS Code and you’ll be greeted by a Welcome tab with the released notes of the newest version.

Microsoft Visual Studio Code VS Code Installation wizard welcome screen on Linux Ubuntu

That’s it. Visual Studio Code was successfully installed.


Installing MicroPico Extension

Open VS Code and click on the Extensions tab at the left sidebar.

Installing MicroPico Extension

Search for “MicroPico” or “pico-w-go” and install the MicroPico Extension.

installing micropico extension VS Code

Configuring a MicroPython Project Folder

Now, create a project folder on your computer called for example Pi Pico Test.

Then, in VS Code, go to File > Open Folder and open the folder you just created.

Press CTRL+Shift +P. A little window will show up to search commands. Write “micropico” and then, select the option MicroPico: Configure project.

Configure MicroPython Project Folder Raspberry Pi Pico VS Code

At the bottom right corner, you should get a success message.

VS Code MicroPython Raspberry Pi Project Configuration Complete

Additionally, it should have created some essential files at the left sidebar.

Raspberry Pi Pico VS Code Project Configuration

At the same time, you should see some commands to interact with your Raspberry Pi Pico.

VS Code micropico extension Raspberry Pi Pico

Running a MicroPython File

Inside your project folder, create a new Python file, for example blinking.py, and copy the following code.

from machine import Pin
from time import sleep

led = Pin('LED', Pin.OUT)
print('Blinking LED Example')

while True:
  led.value(not led.value())
  sleep(0.5)
Creating MicroPython Project Raspberry Pi Pico VS Code

Then, click on the little Run icon at the bottom of the window.

If everything went as expected, the Raspberry Pi Pico onboard LED should be blinking every half second.

Additionally, it will open a new Terminal window (REPL) where it will print the message ‘Blinking LED Example’.

Blinking LED REPL VS Code Raspberry Pi Pico

To stop the execution of the program, click on the Stop button at the bottom of the window.

Important note: just running the file doesn’t copy it permanently to the board’s filesystem. This means that if you unplug it from your computer and apply power to the board, nothing will happen because it doesn’t have any Python files saved on its filesystem. The Run function is useful to test the code, but if you want to upload it permanently to your board, you need to create and save a file to the board filesystem. MicroPython will automatically run a file called main.py on boot. See the next section.

Uploading a MicroPython File

Inside your project folder, create another file called main.py and copy the exact same code.

from machine import Pin
from time import sleep

led = Pin('LED', Pin.OUT)
print('Blinking LED Example')

while True:
  led.value(not led.value())
  sleep(0.5)

Then, press CTRL+Shift+P, or click on the All commands button at the bottom of the window. Write MicroPico and select the option MicroPico: Upload current file to Pico.

Raspberry Pi Pico MicroPico Upload current File

You should get a success message after a few seconds. Now, you can unplug the board from your computer and power it using a power bank for example. You’ll see that the board will start blinking the LED as soon as power is applied (for this to happen, the file should be called main.py and it should be uploaded to the board).

Wrapping Up

This article was a quick guide to show you how to set up VS Code with the MicroPico extension (previously called Pico-W-Go) to program the Raspberry Pi Pico with MicroPython.

If you’ve never used VS Code, it might be a little difficult to get started. If you find that this option is very advanced, you might consider using simpler IDEs like Thonny IDE.

Alternatively, if you prefer to program the Raspberry Pi Pico using C/C++ with Arduino IDE, you can follow the next tutorial:

Other MicroPython tutorials to get started:

Thanks for reading.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Enjoyed this project? Stay updated by subscribing our newsletter!

12 thoughts on “Programming Raspberry Pi Pico with VS Code and MicroPython”

  1. Then, in VS Code, go to File > Open Folder and open the folder you just created.
    Save the file as a workspace?
    I don’t get the project configuration complete and .micropico is missing under .vscode

    Running the blinking.py example says machine not found so it looks like the Micropython extension is not being used.

    Reply
    • Hi.
      Don’t save the file as a workspace.
      Double-check that the extension is installed.
      Try restarting VS Code after installing the extension.
      REgards,
      Sara

      Reply
    • Sara, thank you for this excellent lesson. Can you explain how to add libraries such as BME280? Do we 1. download a copy from GitHub, 2. make a folder named “lib” , 3. move the BME280 library into it 4. after moving the folder onto the pico, 5. then lastly adding the firmware? Again, many thanks.

      Reply
  2. Upgraded to Ubuntu 22.04LTS and re-did the tutorial. All good now, thanks for posting this tutorial. You could remove my previous posts.

    Reply
  3. Hi – I’ve been using this for a while now. It works – but any library that is micropython specific gets the yellow underline treatment from pylance.

    Also because it can’t find the micropython library intellicode can’t give suggestions as I code.

    I’m a beginner in both vscode and micropython so how to fix this is throwing me.
    Any clues?

    Reply
  4. Hi Sara,
    I’m hoping you can answer a couple of questions for me.
    I’m using Micropython to program a Pi Pico which has a small PIO program in it. It works fine, but when I edit it in VS Code every line of the PIO program is flagged as an error. Also, if I try to use debug a large red box is displayed saying I can’t import machine as it doesn’t exist. So it seems the debugger is not using Micropython, but something else.
    I have MicroPico installed, and also PIO ASM syntax highlighting, just in case that did anything, but I still can’t get the debugger to work.
    Is there a way to debug micropython in VS Code?
    Thanks, Ian
    PS if you are wondering why I’m not using Thonny, it’s because debugging is not supported for micropython in Thonny.

    Reply
    • Hi.
      Unfortunately, I haven’t tried the VS Code debugging feature.
      Sometimes, the code will be underlined, but if you upload it and ignore the red squiggles, it will upload without any problem.
      Regards,
      Sara

      Reply
  5. Sara, thank you for this excellent lesson. Can you explain how to add libraries such as BME280? Do we 1. download a copy from GitHub, 2. make a folder named “lib” , 3. move the BME280 library into it 4. after moving the folder onto the pico, 5. then lastly adding the firmware? Again, many thanks.

    Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.