fbpixel
Programming a Raspberry Pi Pico with MicroPython

Programming a Raspberry Pi Pico with MicroPython

The Raspberry Pi Pico is a development board based on the RP2040 programmable with MicroPython. It has a large number of inputs

Hardware

  • Computer
  • Raspberry Pi Pico (standar H or W)
  • USB A Male to USB Mini B Male cable

Installing drivers on the computer

The Raspberry Pi Pico is recognized as a Fat32 USB storage device. No special driver needs to be installed to connect it to a computer. To convert the RPi Pico into a USB flash drive:

  • Disconnect the Pico from its power supply
  • Press the BOOTSEL button
  • Connect the Pico to the computer using the USB cable
  • Release the BOOTSEL button

Installing MicroPython on RPI Pico

Download the MicroPython UTF2 file corresponding to the version of your Pico

Then copy the file to your Raspberry Pi Pico window.

The Pico should reboot and launch MicroPython

Check operation with Putty

Once MicroPython has been installed on your Pico, you can launch Python commands via serial communication. To do this you can use Putty

Find the port name used by the Raspberry Pi in the Device Manager.

In the Putty software, select Serial communication, enter the port name and the baud rate is 9600. You can also enter Python commands in the terminal.

Launching a Python script with uPyCraft

Once MicroPython has been installed, you can launch a Python script using uPyCraft.

Download and install uPyCraft

In Tools, select the correct serial port and the “other” card type.

You can then create a main.py script

Example code

from machine import Pin
from time import sleep

led = Pin(25, Pin.OUT)
while True:
   print("LED ON")
   led.value(1)
   sleep(1)
   print("LED OFF")
   led.value(0)
   sleep(1)

If the upload is successful, this message should be displayed

Ready to download this file,please wait!
..
download ok
exec(open('main.py').read(),globals())

Results

Once the code has been loaded, you should see the text scroll in the console. You can stop the code with the “stop” button or Ctrl+c

Running a script with Thonny

Another famous IDE for MicroPython development is Thonny.

Download and install the latest version of Thonny

In “Run> Select interpreter”, select “MicroPython (Raspberry Pi Pico)”, then select the serial port.

You can then copy the previous code and run it with the “Run” command.

You should see the LED flash and the text scroll.

There are a number of tools available for developing Micropython code on Raspberry Pi Pico or other platforms. Use the one that suits you best.

Sources

Programming a Raspberry Pi Pico with Arduino IDE

Programming a Raspberry Pi Pico with Arduino IDE

The Raspberry Pi Pico is a development board based on the RP2040 programmable with the Arduino IDE. It has a large number of inputs

Installing drivers on the computer

The Raspberry Pi Pico is recognized as a Fat32 USB storage device. No special driver needs to be installed to connect it to a computer. To convert the RPi Pico into a USB flash drive:

  • Disconnect the Pico from its power supply
  • Press the BOOTSEL button
  • Connect the Pico to the computer using the USB cable
  • Release the BOOTSEL button

Add JSON handlers

To program other microcontrollers with the Arduino IDE, you need to install board managers on your Arduino, containing all the tools you need to program them. To retrieve the managers, you need to find the corresponding URL. In the case of the Raspberry Pi Pico, https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json

N.B.: If you encounter compilation problems, check whether a previous version of Pico is installed. If so, delete it manually (C:\Users\\AppData\Local\Arduino15\packages\rp2040).

Then open the File>Preferences window of the Arduino software. At the bottom of the page you will find a text box in which to enter the URL: “Additional board manager URL”. (don’t forget to separate URLs with a comma “,”)

Install managers

In the “Tools” tab, follow the path “Map type > Map manager”. The IDE will load the map catalogue from the URL.

If you search for the microcontroller in the search bar, the package to be installed should be displayed.

Select configuration

Once the management package has been installed, the microcontroller should appear in the available board types.

You’ll then need to know the characteristics of your card to select the right settings. However, except in exceptional cases, the default settings will work without a hitch.

Example code

You can copy and paste this example code to test compilation and uploading. A list of Arduino libraries compatible with the RP2040 microcontroller is available here.

//Constants
#define BRIGHTNESS 200

//Parameters
const int ledPin = 25;

//Variables
bool ledStatus = 0;

void setup() {
  //Init Serial USB
  Serial.begin(9600);
  Serial.println(F("Initialize System"));
  //Init led
  pinMode(ledPin, OUTPUT);

}

void loop() {
  testLed();
}

void testLed( ) {
  digitalWrite(ledPin, HIGH);
  Serial.println(F("LED ON"));
  delay(500);
  digitalWrite(ledPin, LOW);
  Serial.println(F("LED OFF"));
  delay(500);
}

If the upload is successful, this message should be displayed

Resetting COM15
Converting to uf2, output size: 105472, start address: 0x2000
Flashing E: (RPI-RP2)
Wrote 105472 bytes to E:/NEW.UF2

Results

Sources


Programming a Raspberry Pi Pico in C++ with Visual Studio Code

Programming a Raspberry Pi Pico in C++ with Visual Studio Code

Let’s take a look at how to program the Raspberry Pi Pico microcontroller in C++ using Visual Studio Code. The Raspberry Pi Pico board is a microcontroller based on the RP2040 chip from Raspberry Pi. It is programmable in C++ and MicroPython and can interact with any type of equipment.

Hardware

  • Computer
  • Raspberry Pi Pico
  • USB B Male to USB Micro B cable

Installing and configuring Visual Studio Code

Go to the Visual Studio Code download page and download the version corresponding to your OS.

Launch the installer and follow the procedure

In Extensions, search for and install PlatformIO IDE

N.B: If the PlatformIO installation displays an error, please follow the solutions described on this page. For solution3: update platformio version (i.e: get-platformio-1.1.2.py)

Creating a project for Raspberry Pi Pico

Cliquez sur l’icone PlatformIO dans la barre à gauche puis sélectionnez Projects & Configuration dans le menu Quick Access. Créer un nouveau projet

Search for the Raspberry Pi Pico board and select the Arduino Framework

Search for the Raspberry Pi Pico board and select the Arduino Framework

#include <Arduino.h>

//Variables 
bool ledStatus = 0; 

void testLed( ){ 
	digitalWrite(LED_BUILTIN,HIGH); 
  	Serial.println(F("LED ON")); 
	delay(500); 
	digitalWrite(LED_BUILTIN,LOW); 
  	Serial.println(F("LED OFF")); 
	delay(500); 
} 

void setup(){ 
	//Init Serial USB 
	Serial.begin(9600); 
	
	Serial.println(F("Initialize System")); 
	//Init led 
	pinMode(LED_BUILTIN,OUTPUT); 
 
} 
 
void loop(){ 
	testLed(); 
} 

Once you’ve written your code, you can compile it by clicking on the “Build” icon.

Once you’ve written your code, you can compile it by clicking on the “Build” icon.

You can then copy the firmware.uf2 file to the Raspberry Pi Pico, which is then seen as a USB storage device.

Upcoming uploads

Once the first code has been loaded onto the Raspberry Pi Pico, it will no longer be available as a USB storage device. To load a code again, you’ll need to either:

  • Unplug the RPi Pico and plug it back in, keeping the BOOTSEL button pressed. You can now copy the compiled uf2 file.
  • Use PlatformIO’s Upload function to program the Pico with Visual Studio

N.B.: If PlatformIO’s Upload button doesn’t work, you can try installing libUSB with Zadig

Results

As with Arduino, you can observe debug messages in PlatformIO’s serial monitor.

Sources