Tasnim Zotder

How To Write Code on Pi Pico using CircuitPython and VS Code?

Author: Tasnim Zotder
How ToProgrammingIoT

Raspberry Pi Pico

The Raspberry Pi Pico is a small yet powerful microcontroller that is based on the RP2040 chip. It is an excellent choice for hobbyists and professionals who need a reliable and low-cost microcontroller board.

One of the standout features of the Raspberry Pi Pico is its dual-core ARM Cortex M0+ processor. This processor provides excellent performance and makes the board suitable for a wide range of applications. Additionally, the board has an on-board temperature sensor, which makes it suitable for projects that require temperature sensing.

Another impressive feature of the Raspberry Pi Pico is its 26 multi-function GPIO pins. These pins can be used to connect the board to a wide range of devices, including sensors, actuators, and other microcontrollers. This makes the board highly versatile and suitable for many different types of projects.

Learn More 👉 Raspberry Pi Pico

Steps to use CircuitPython with VS Code

The following steps will help you to write code on Pi Pico using CircuitPython & VS Code.

1. Download and upload CircuitPython .uf2 file

  1. Download the .uf2 file for Raspberry Pi Pico from Pico Download (circuitpython.org).
  2. Push and hold the BOOTSEL button on the Pi Pico and plug in the USB cable.
  3. The Pi Pico will be mounted as a mass storage device (RPI-RP2) on the computer.
  4. Copy the .uf2 file to the RPI-RP2 volume.

Learn More 👉 CircuitPython

Connect Pi Pico

2. Download VS Code and install CircuitPython extension

  1. Download VS code from Visual Studio Code.
  2. Install the CircuitPython extension by joedevivo from VS Code Marketplace.

CircuitPython demo

3. Write Code

  1. Open a new directory in VS Code.
  2. Open the command palette and select CircuitPython: Select Serial Port.
  3. Select the COM port of your Pi Pico (for example, COM3).
  4. Create a Python file in the root directory (for example, main.py).
  5. Write your code in the file.

The following code sample will blink the LED on the Pi Pico board.

# build-in LED blink in Pi Pico using CircuitPython
import time
import board
import digitalio
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT # set the direction of the pin
while True:
led.value = True # turn the LED on
time.sleep(0.5) # wait for 0.5 seconds
led.value = False # turn the LED off
time.sleep(0.5) # wait for 0.5 seconds

4. Upload code to Pi Pico

Here "E:" is the drive letter of the RPI-RP2 volume. Change it according to your system.

  1. If you are using Windows, open a new terminal and run the following command.
$currDir = Get-Location
$targetDir = "E:"
Remove-Item $targetDir\* -Recurse -Force
Copy-Item $currDir\* $targetDir\ -Recurse -Force
  1. If you are using Linux, open a new terminal and run the following command.
cp -r * /media/$USER/RPI-RP2