Linux Format

Control NeoPixel LEDs

Create animated lighting effects with the brightest LEDs connected to your Raspberry Pi Pico.

-

We’re using MicroPytho­n here to control a small set of LEDs. Known as NeoPixels, they are a string of LEDs that are generally multicolou­red and can be addressed individual­ly. NeoPixel is the name given to this style of LED by Adafruit, but the same driver is used for a large number of them (WS2811/WS2812), no matter who the manufactur­er or supplier. These models are available with separate RGB channels, so that any colour can be made by mixing the values (think of HTML colours).

First, download and install MicroPytho­n from https://bit.ly/lxfupdate. Then hold down the Bootsel button on the Pico board and connect to your PC or laptop. Copy the downloaded file to the newly mounted disk, then the Pico W reboots and loads MicroPytho­n.

Download and install the Thonny IDE from https:// thonny.org. Open the IDE and, from the bottom-right, select MicroPytho­n (Raspberry Pi Pico). This connects the terminal in the bottom of the window to the Pi Pico’s REPL (Read-Eval-Print Loop) – this allows code to be entered line by line. This is useful for basic tests, but not very useful when trying to write a program. Programs can be entered into the main section of the window, then saved to the board. When saving, you can choose between the local computer and the Pi Pico. When saving to the Pico, use the filename Boot.py and the code executes as soon as the board boots.

Step three is to connect the Raspberry Pi Pico or Pico W to the LEDs and the PC for programmin­g (see the circuit diagram below).

The following code smoothly pulses the LEDs. They start with a dim red colour and gradually build to full brightness before fading back down again to nothing, before this cycle repeats. import machine import neopixel import time

#Pulse all LEDs numPixels = 8 np = neopixel.NeoPixel(machine.Pin(1), numPixels)

pulseTime = 2 numLoops = 100 while True: for a in range(1,numLoops): for i in range(0,numPixels):

np[i] = (int(a*254/numLoops),0,0) np.write() time.sleep(pulseTime/(2*numLoops)) for a in range(numLoops,1,-1): for i in range(0,numPixels):

np[i] = (int(a*254/numLoops),0,0) np.write() time.sleep(pulseTime/(2*numLoops))

In this code, we first import the necessary libraries, then store the number of pixels in a variable, create an object to reference and set values on the LEDs, then two more variables. The larger the value of numLoops, the smoother the effect. Next we define a while loop that will run forever, before setting a for loop that runs for the number of times defined in the numLoops

variable. For each iteration of this loop, we set each LED of the string (using the inner loop) to a brightness, as defined by the value in the outer loop. After we have set the value of each LED, we write the values that cause the LEDs to change state, wait for a short period before iterating again and getting brighter each time. Once this outer loop has completed, the second stage, decreases the brightness until the LEDs are practicall­y switched off. The colour can be altered by changing the numbers in the brackets when setting np[i] . The values in the tuple represent red, green and blue.

You can find two other examples of effects at https://github.com/mattmole/LXF298.

 ?? ?? Follow the circuit diagram to connect NeoPixels to the Raspberry Pi Pico or Pico W.
Follow the circuit diagram to connect NeoPixels to the Raspberry Pi Pico or Pico W.
 ?? ?? Here you can see the LEDs running the program we have written.
Here you can see the LEDs running the program we have written.

Newspapers in English

Newspapers from Australia