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.

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

 

 

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. Make sure that the connections are not mixed up and that the terminals do not come into contact with neighboring tracks.

The following cable colors are common:
GND: black
3.3V (VCC): red
Data line (DIN): different color

 
Attention: If you use an arrangement with more than 20-30 LEDs, you must supply them with an external power supply of 5 V, for example with a PowerBank.

 

 

TASK 1: SET PIXELS TO A SPECIFIC COLOR

 

The NeoPixels are controlled by means of 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:

np = NeoPixel(pin0, 24)
(np is an arbitrary 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 microbit 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 microbit 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.

 

 

TASK 2: RUN A PIXEL

 

You only need to know very little about programming to solve the following tasks. In this first exercise, try to make a single red pixel run endlessly 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().

 

 

 

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 are to 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 wurm(color) function that creates 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.