|
The source code of all examples can be downloaded from a link in the right side bar. NeoPixels are RGB color LEDs with a build-in controller. Normally they are WS2812B devices (datasheet) from Worldsemi. Each device has 4 pins: GND, VCC, DIN, DOUT. Supply voltage is specified from 3.5V to 5.5V, but they also work perfectly with 3.3V from a micro:bit. NeoPixels can be daisy chained, so many LEDs may be lined-up as follows: The color of each LED is determined by three bytes (g, r, b) in the order green/red/blue. The data stream at DIN is a byte sequence g, r, b for each LED: g1, r1, b1, g2, r2, b2, g3, r3, b3, ... where 1 is for the first LED (most left), 2 for the second, 3 for the third, etc. Every device picks out the first three bytes from the data stream coming from its input pin and uses them to turn on its green/red/blue diodes. The rest of the data stream is simply passed to the output pin and serves as input to the next device. In other words, the devices implements a recursive algorithm: From the byte stream at DIN take first 3 bytes and output the rest at DOUT. The bits are coded as duration of low and of high levels as follows:
The reset pulse (idle time) is required after the transmission of the whole data stream. Timings have to be accurate to ±0.15 μs. The following CRO image shows the pulse sequence for 1 LED with grb = (8, 16, 32) created by the program:
(As you see, the LSB is not very accurate.) NeoPixels may be arranged in different geometries: in a line (LED strip, stripe, normally a flexible band), in a circle or as rectangular matrix. But any other arrangement is also possible, even in a 3-dimensional construction. The length of a strip can be modified any time be adding more LEDs or cutting the end. To drive many LEDs at high luminosity an external power supply (3.5 to 5 V) must be used, but for as many as 150 - 200 LEDs driven with low luminosity (r,g,b < 30) the micro:bit delivers enough current at its 3.3V pin. All examples below uses either a LED strip of up to 144 LEDs or a circle with up to 24 LEDs. They are available from many distributors (among them Adafruit, Seed, and from China). The price and delivery time may vary considerably. (Be sure to get WS2812B type LEDs, there are other LED strips using the APA 102 chip that works quite differently using a data and clock line. These chips have 6 pins instead of 4 for the WS2812B.)
|
|||||||
Example 1: One pixel movement forth and back |
Aim:
Remarks: Use meaningful named constants to setup your system. |
Example 2: Growing worm in different colors |
Aim:
|
Example 3: Moving snake in different colors |
Aim:
Remarks: |
Example 4: Flickering colors |
(Inspired by programs called "fire" found on Internet.) Aim:
Remarks: |