Example PlatformIO project for RP2040 using Arduino framework and FastLED library
I recently obtained these Pimoroni RP2040 "Plasma" boards that are purpose-built for driving programmable LEDs:
Prior to this I'd only used ESP32-based boards for programmable LED projects. Since I'd already written code using the Arduino framework and FastLED library for ESP32, I wanted to try using the same development environment for RP2040 (as opposed to using Raspberrry Pi Pico SDK and Pimoroni libraries). This simple project is what I wrote to confirm this was possible.
I am sharing the project and these notes in case it helps anyone else in a similar situation. Familiarity with VS Code, PlatformIO, Arduino framework and FastLED is assumed, but I have documented several things I learnt when working with RP2040-based boards in this environment that were not obvious to me when I first did this.
I used VS Code on Windows 11 with the PlatformIO extension for this project. This requires the following to be installed:
Before continuing, you must enable long filenames in both Windows and Git to avoid installation issues with Arduino-Pico:
- https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=registry#enable-long-paths-in-windows-10-version-1607-and-later
git config --system core.longpaths true
See important steps for Windows users in the Arduino-Pico documentation for more details.
The following core and platform enable code for the Arduino framework to be built for RP2040-based boards using PlatformIO:
This enables use of some Arduino libraries such as FastLED that also have RP2040 support:
PlatformIO should automatically download and install the core, platform and library when you first open the project folder in VS Code.
If this does not happen automatically when you open the project folder then try building the project, as that should also make PlatformIO download and install all required components.
Note that downloading and installing these components will take some time to complete.
Most options are common to both boards mentioned earlier, so they are included in the common [env]
section shared between all environments.
The working environments [env:plasma_stick_2040w]
and [env:env:plasma_2040]
just have the approripate board
setting for the hardware we are building for i.e. rpipicow
and pimoroni_plasma2040
.
There is more than one Arduino core available for RP2040. This project uses the Aduino-Pico (earlephilhower) core which can be specified with the board_build.core
setting e.g.
[env:pico]
platform = https://github.com/maxgerhardt/platform-raspberrypi.git
framework = arduino
board = pico
board_build.core = earlephilhower
In this example project, we are building for rpipicow
and pimoroni_plasma2040
boards which are currently only available in Arduino-Pico anyway, so the board_build.core setting is optional. See Selecting the new core for more details.
At the time of writing, building FastLED for RP2040 can result in assembler errors such as "Error: invalid offset, value too big". To avoid this, FASTLED_RP2040_CLOCKLESS_M0_FALLBACK
is set to zero in platformio.ini
to disable the code that generates this error. This fallback method is not usually required on RP2040 anyway. See FastLED PR 1576 for more details.
build_flags =
-DFASTLED_RP2040_CLOCKLESS_M0_FALLBACK=0
Please review the values of the preprocessor definitions at the top of main.cpp
and adjust accordingly for your board and LEDs before deploying the firmware to your board. The example values work with the Pimoroni 5m LED wire used to test this project.
Uploading firmware to RP2040 works differently compared to ESP32, though once everything is configured correctly the process to upload from PlatformIO in VS Code is the same for both.
Your first build will have to be uploaded manually if alternative firmware (e.g. MicroPython) was previously installed on your board.
- Enter bootloader mode by holding down BOOTSEL button while restarting board (e.g. by connecting USB cable or pressing RESET button if present on your board)
- You should see USB drive named "RPI-RP2" appear in Windows Explorer
- Copy
firmware.uf2
from.pio\build\<environment>
folder in project to RPI-RP2 drive to update firmware - Board will reboot automatically after .uf2 file has been copied to it
- If all goes well you should now see your LEDs light up red, green and blue for one second each then display a moving rainbow pattern
Once you have uploaded the firmware for the first time by copying the .u2f file, you should then be able to upload subsequent builds directly from VS Code/PlatformIO. However to do this you may need to install USB drivers for the board when it is bootloader mode:
- Enter bootloader mode as before
- Use Zadig to install required driver for "RP2 Boot (Interface 1)"
- Select "RP2 Boot (Interface 1)" from drop-down list in Zadig which by default only lists devices with no drivers
- Current driver is shown on left which should be "NONE" if you have not previously installed driver
- Driver to install is shown on right, default selection is "WinUSB" which worked for me with both boards
- Click on "Install Driver" to start installation - this can take a while
- Refer to Zadig on-line help for more information
- Reset or power cycle board and confirm that PlatformIO commands in VS Code such as "Upload" and "Upload and Monitor" now work