Controlling Addressable LEDs with the XRP

You can control LED light strips and rings with the XRP. Here is a photo showing two Adafruit 16 pixel rings and a 60 LED strip from Amazon controlled by a single XRP. Different colors are a great way to show what is happening with the robot.

An experienced micropython user may have no trouble setting this up but it took me quite a bit of searching and experimentation to get it working. Below are notes that would have been helpful at the start. This worked for me but there are likely issues with the electrical setup and the code below so look at the comments for best practices.

The type of LEDs used may be called WS2812B LEDs - Addressable LEDs - NeoPixel. (Not a recommendation but I used NeoPixel Ring - 16 x 5050 RGBW LEDs w/ Integrated Drivers [Warm White - ~3000K] : ID 2854 : Adafruit Industries, Unique & fun DIY electronics and kits and Amazon.com: SEZO WS2812B LED Strip Light 3.3FT/1M 60LEDs/m Individually Addressable Programmable 5050SMD Digital RGB Alloy Wires Flexible Dream Color IP30 Non-Waterproof PCB Black DC5V : Tools & Home Improvement)

When using XRP code web editor there are no Blockly blocks for addressable LED strips (as of 07/25/24) so have to use MicroPython to program them. RGB LEDs are not part of the XRP kit so you need to consider where you will plug them in.

Servo 1 port (5v) use machine.Pin(16) - Must have battery power ON. Used by kit servo.

Servo 2 port (5v) use machine.Pin(17) - Must have battery power ON. Open by default.

QWIIC port (3v3) use Machine.Pin(18) or Machine.Pin(19) - USB power - Can use with battery power OFF. Need a QWIIC cable.

Jumper wire aka DuPont wires are useful for connecting NeoPixels to the XRP.

Servo 2 port is a great place to start as it is not used for the default XRP setup.

The LED strip may have printed arrows. Connect to the wires in the direction of the bottom of the arrows or on the side labeled Din.

Connect the GND pin on the XRP to the GND/power signal ground pad or wire on the NeoPixel.

Connect the 5V/3V3 pin on the XRP to the +5V/Power 5V DC pad or wire on the NeoPixel.

Connect the IO17 pin on the XRP Servo 2 port to the Din/Data Input pad or wire on the NeoPixel.

The NeoPixel driver installed with Micropython defines fill and write functions.

The LED brightness values range from 0-255 and are in order red, green, blue for the 3 LEDs making up an RGB pixels and red, green, blue, white for the 4 LEDs making up an RGBW pixel.

Can use RGB Neopixels (3 numbers for color). In the code below, the RGB strip has 60 pixels and is connected to Servo 2 which uses Pin 17. The np and COLOR in the code are variable names and can be changed. Copy and paste this code into a program window in XRP web code editor, save the program to the XRP and run.

import neopixel
np = neopixel.NeoPixel(machine.Pin(17),60)
COLOR = (0, 50, 0)
np.fill(COLOR)
np.write()

Can use RGBW NeoPixels (4 numbers for color) In the code below, the RGBW strip has 16 pixels and is connected to Servo 2 which uses Pin 17. The 4 in the code designates an RGBW neopixel.

import neopixel
np = neopixel.NeoPixel(machine.Pin(17),16,4)
COLOR = (0, 50, 0, 0)
np.fill(COLOR)
np.write()

In the example code above COLOR = (0,50,0) and COLOR = (0,50,0,0) will show a dim green light. Change the numbers between 0-255 to create different colors and brightnesses.

Following internet tutorials for programming neopixels in Micropython for the microcontroller of the XRP, the Pico W/ RP2040, can be confusing if they use a different neopixel driver. Doesn’t use the write function uses show instead. It is frustrating when the example code doesn’t work. To follow these tutorials that use different functions and have more capabilities than the default neopixel driver, you will need to copy the driver to your XRP and import it in your program.

Might be possible to program NeoPixels using block based programming with MicroBlocks.

Might be possible to program NeoPixels using block based programming with Microsoft MakeCode treating the XRP as a generic Raspberry Pi Pico RP2040.

Notes on possible concerns: More LEDs use more power. Higher brightness uses more power. Powering 5V LED strips with 3.3V may cause signal issues. Creating variables for the pin number and the number of pixels may make it clearer and easier to modify the code for different ports and neopixels of different lengths.

Thank you for any additions or correction in the comments. Please let me know if this helped you control LEDs with your XRP.

2 Likes