HomeTurtlegraficsGPanelRobotics WebTigerPython
 Python - Online
neopixel
Deutsch   English   

8. NEOPIXEL

 

 

WHAT ARE NEOPIXELS

 

In the foreseeable future, there will only be light sources that are made of LEDs. An LED is a semiconductor diode that converts the current flowing through it into light with a high degree of efficiency. Depending on the material, different colors can be produced. If you combine a red, green and blue LED in one housing, the result is a color LED that can be used to produce any other color through additive color mixing. The principle is also used in color television.

Color LEDs are sometimes combined with an electronic circuit (controller) in the same housing and cascaded in series. With only 3 supply lines, it is still possible to give each individual LED a different color. The LEDs are usually mounted on an LED strip. However, there are also rings or rectangular arrangements (matrix).

In the following, you use either an LED strip, an LED ring or an LED matrix with at least 12 LEDs. These must be of type WS2812B. Sources of supply: Electronics stores, online shipping (Adafruit, Seed, Pi-Shop, Ebay, Aliexpress, etc.)

 

 

PROGRAMMING WITH FUN

 

A lighting system controlled by a microcontroller such as the micro:bit is extremely flexible and versatile, and it is entirely up to your imagination and ingenuity when writing the control program what you end up seeing. In this worksheet you will learn some basic procedures that you can then apply to your own project. As you will see, programming lighting systems is fun because the effects of each program line are immediately visible.

 

 

CONNECTION

 

For simple test systems, you can connect the LED strip, ring or matrix to the micro:bit with three lines.

Use the GND (-), 3.3V (+) and P0 pins.
The following cable colors are common:
GND (-): black
3.3V (+): red

Data line (P0): different color, here white.

 

 

 

 

TASK 1: SETTING PIXELS TO A SPECIFIC COLOR

 

The NeoPixels are controlled using a serial protocol. The 3 color values are sent in sequence for the first pixel, then for the second and so on. The first pixel “grabs” its values and forwards the rest of the data stream. Under MicroPython, programming is very simple: you create an “object” NeoPixel by specifying which is the data port (usually pin0) and how many NeoPixels your arrangement has, i.e. for 24 LEDs:

Translated with www.DeepL.com/Translator (free version)

np = NeoPixel(pin0, 24)
(np is a variable name)

A list with 24 tuples is now created in the memory system, which can hold the RGB colors of the 24 LEDs (as (R, G, B) color values each in the range 0..255).

You can access the individual values with a list index, e.g. set the color of the first LED to a red intensity of 30 with:
np[0] = (30, 0, 0)
or set the color of the second LED to green with
np[1] = (0, 30, 0)
However, the color value is only set in the list with this assignment. To make it actually visible, you must send the list information to the LEDs by calling
calling np.show().

The program looks like this:

Program:

from calliope_mini import *
from neopixel import *

nbLeds = 24
np = NeoPixel(pin0, nbLeds)
np[0] = (30, 0, 0)
np[1] = (0, 30, 0)
np.show()
► Copy to clipboard
 

As you can see, in addition to the calliope_mini module, you also need to import the neopixel module. If you forget the np.show(), you will not see any change in the colors, as your LEDs only receive the data stream when this call is made.

Let more LEDs light up in different colors: blue, yellow, cyan, etc. In principle, you could use the tuple (255, 0, 0) to get a red color. As the LEDs are very bright, the value 30 is sufficient.

 

 

TASK 2: RUN A PIXEL

 

 

From a programming point of view, you only need to know how a for loop works. In this first exercise, you run a single red pixel from the beginning to the end. Use a suitable waiting time between the pixel change (e.g. 100 ms). You can play around with the colors and waiting times.

Note:
To delete a single pixel, use np[i] = (0, 0, 0). You can also clear all pixels together with np.clear().

 

Programm:

from calliope_mini import *
from neopixel import *

nbLeds = 24
np = NeoPixel(pin0, nbLeds)
for i in range(24):
    np[i] = (30, 0, 0)
    np.show()
    sleep(100)
    np[i] = (0, 0, 0)
► Copy to clipboard

Change the program so that the red pixel moves “endlessly” from the beginning to the end.

 

 

TASK 3: LENGTHENING COLORED WORM

 

It is fun to create a “worm” of pixels of the same color. Starting from the deleted state, the pixels should be switched on one after the other with the desired color until all pixels light up. The process should be repeated endlessly with different colors.

The best way to do this is to write a worm(color) function that generates the worm and call it endlessly in sequence with the colors red, green, blue, yellow, cyan and white.

 

 

 

You can also dispense with deleting the old worm so that all LEDs always remain switched on.

 

 

 

TASK 4: CONTROL ALL LEDs BRIGHT/DARK

 

For many color games, you want all LEDs to light up together. Write a program that makes all LEDs light up and darken again in small steps with a fixed color. The program should run endlessly and the largest color component should not exceed 30.

Play with different colors that can also change continuously.

 

 

 

 

TASK 5: FLICKERING CANDLES

 

Start from the previous program, but set the color components randomly as they get brighter and darker. So if the current maximum color value is k, use the color

color = int(random() * k, int(random() * k,         int(random() * k)

You have to import the module random:
from random import *

 

 

 

TASK 6: REALIZE YOUR OWN IDEAS

 

Create a light show according to your own ideas.