Skip to content

Embedded Programming 1: Blinking an LED

5 Shades of blinking with an RP2040

Blinking With Block-Based Visual Programming

One of the most straight forward ways of programming your microcontroller is using blocks. MicroBlocks is an accessible tool that allows you to do just that. It can even run in a browser (chrome or edge), requiring no installation.

Once you are in MicroBlocks, the first step is to connect your microcontroller. If it succeeds, the button should turn green:

If this step doesn’t succeed, you probably need to update the firmware on the board:

Select the RP2040:

Once you are connected to the RP2040, you can start programming with blocks. When you want to test your code, press the play button (here is the code for blinking the green LED):

It is also possible to play with the RGB LED. For this, you’ll need to add the NeoPixel library:

Here is a short example showing how to change the LED color. Pay attention to the pin 11 that needs to be set HIGH. The RGB LED is then attached to pin number 12.

Blinking with MicroPython

Set up for coding in MicroPython

We’ll follow this tutorial that you can check if you want to go further. First, download the Thonny IDE and install this software. Then launch Thonny and go to Tools > Options:

Chose the Interpreter tab and select the MicroPython(Raspberry Pi Pico) interpreter. You can leave the Port to Try to detect port automatically.

Afterwards, you need to set the board to bootloader mode. On the Seed Studio RP2040, you can achieve this by pushing the RESET (R) and BOOT (B) buttons simultaneously. Go now to Install or update MicroPython.

The Target volume should be automatically set to your RP2040. Select the MicroPython variant: Raspberry Pi . Pico / Pico H:

If no volume is selected once you have set the board to bootloader mode, try to push the buttons again, several times if it is still not working. If nothing happens, try the following:

  • Change the interpreter to MicroPython (RP2040)

  • Select manually the port corresponding to your RP2040 under Port or WebREPL.

  • Go back to Install or update MicroPython and set the board to bootloader mode until you see something in Target volume.

  • Once the Installation is done, switch back the Port to Try to detect port automatically

The code for blinking the LED is the following:

import time

led = machine.Pin(25, machine.Pin.OUT)  #sets the pin 25 to OUTPUT mode

while True:
    led.high()                          #sets the pin 25 to HIGH
    time.sleep(0.5)                     #waits 500 ms
    led.low()                           #sets the pin 25 to LOW
    time.sleep(1)

Once you have coded your program, press Run current script. An LED on your board should start blinking.


NOTE

On the RP2040, setting the pin HIGH turns OFF the LED and setting the pin LOW turns ON the LED. This is the opposite behavior of many other microcontrollers, like the Arduino.


Blinking with c++

Set up for coding in c++

The first step is to download the Arduino IDE and to install it.

Since the RP2040 is not supported by the Arduino IDE by default, we need to add it to the boards. In order to do this, go to Preferences:

Then, press this button:

Paste the link that you’ll find here to the Additional boards manager URLs. Currently, this link is the following, but it might change over time:

https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json

Afterwards, you need to install the board. Go to the Boards Manager accessible from the left panel. Search for pico and install Raspberry Pi Pico/RP2040 by Earle Philhower:

The boards should be installed and you can now navigate to Tools > Board > Raspberry Pi Pico/RP2040 > Seed XIAO RP2040:

After connecting your RP2040 to your computer, it should show in Tools > Port. In Windows, it often shows as COMX, where X is a number.

Here is an example of how to blink the LED on pin 25:

void setup() {
  pinMode(25, OUTPUT);    //sets the pin 25 to OUTPUT mode
}

void loop() {
  digitalWrite(25,HIGH);  //sets the pin 25 to HIGH
  delay(500);             //wait for 500ms
  digitalWrite(25,LOW);   //sets the pin 25 to LOW
  delay(500);
}

Once your code is written, push the Upload button:

What to do if your RP2040 doesn’t work

If the RP2040 is not recognized by Arduino IDE or if you cannot upload any code on it after multiple attempts, you can try the following:

  1. Download the blink uf2 file.

  2. Press the Reset and Boot buttons of the RP2040 board (R and B buttons) simultaneously.

  3. A new hard drive should appear on your computer.

  4. Copy the blink uf2 file that you downloaded to this new drive.

  5. Once the file has been copied, the RP2040 should be visible to your Arduino IDE and an LED from the RP2040 should blink.


Last update: February 17, 2024