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).
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:
- A) Installing VS Code on Windows
- B) Installing VS Code on Mac OS X
- C) Installing VS Code on Linux Ubuntu
4) Installing MicroPico Extension
Finally, follow the instructions to set up the MicroPico extension, configure your project folder and run your first project:
- Installing MicroPico Extension
- Configuring a MicroPython Project Folder
- Running a MicroPython File
- Uploading a MicroPython File
Installing Python
1. Go to the Python Downloads page: www.python.org/downloads and download the installation file.
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):
- Raspberry Pi Pico
- Raspberry Pi Pico W with Wi-Fi support
- Raspberry Pi Pico W with Wi-Fi and BLE (Bluetooth Low Energy) support
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.
A new Mass Storage Device called RPi-RP2 will show up.
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).
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.
Select the following options and click Next.
Press the Install button.
Finally, click Finish to finish the installation.
Open VS Code and you’ll be greeted by a Welcome tab with the released notes of the newest version.
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).
After downloading the Visual Studio Code application file, you’ll be prompted with the following message. Press the “Open” button.
Or open your Downloads folder and open Visual Studio Code.
After that, you’ll be greeted by a Welcome tab with the released notes of the newest version.
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).
Save the installation file:
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.
Open VS Code and you’ll be greeted by a Welcome tab with the released notes of the newest version.
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.
Search for “MicroPico” or “pico-w-go” and install the MicroPico Extension.
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.
At the bottom right corner, you should get a success message.
Additionally, it should have created some essential files at the left sidebar.
At the same time, you should see some commands to interact with your 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)
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’.
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.
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:
- Raspberry Pi Pico and Pico W Pinout Guide: GPIOs Explained
- Raspberry Pi Pico: Control Digital Outputs and Read Digital Inputs (Arduino IDE)
- Raspberry Pi Pico: Control Digital Outputs and Read Digital Inputs (MicroPython)
Thanks for reading.
Running the blinking.py example says machine not found so it looks like the Micropython extension is not being used.
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
Hello Sara.
Great with your tutorials using python.
There is so much that can be done with python.
Thanks 😀
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.
Upgraded to Ubuntu 22.04LTS and re-did the tutorial. All good now, thanks for posting this tutorial. You could remove my previous posts.
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?
Did you install MicoPico?
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.
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
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.
hi i want to get the existing files which is inside the pico in vscode