Controlling Multiple NeoPixels with a micro:bit
2018-12-31 | By Maker.io Staff
NeoPixels are incredibly smart LEDs that can be programmed and serially chained. In this how-to article, we will learn how to connect multiple NeoPixel modules together and control all of them using the micro:bit.
The Magic Of Pixels
NeoPixels are multicolor LEDs that have a special chip integrated into them that allows them to be chained together, and each LED can be addressed individually. Unlike most other devices, NeoPixels only use a single wire for communication, and none of the LEDs need address pin settings. NeoPixels can be individually addressed when the first LED in the sequence looks at the streamed data from the microcontroller and only responds to the first data packet. All data packets after that packet are for LEDs down the chain, so the first LED repeats all the data coming to it to the next LED, save for the first packet.
Connecting multiple NeoPixels is very easy and only requires three wires. The first wire, the power wire, is needed to power the NeoPixel module (in our example, we connect this to the MicroBit’s 3V pad). The second wire is the ground wire that connects to 0V, and the third wire connects the second NeoPixel’s in pad to the first NeoPixel’s out pad. The out pad on the NeoPixel Spark ring can be found on the top left (it’s a small via labeled on the top side of the PCB).
Coding Multiple NeoPixels
NeoPixel addresses are incremental, so if the first NeoPixel module has 10 LEDs (addresses 0 to 9), the first LED in the next NeoPixel module will have the address 10 (counting from 0). This makes addressing them easy, so long as you know how many LEDs are on each module and which order that module is in. This how-to uses a micro:bit, so you first need to go use MakeCode.
The first block that we will be using is the “on start” block, which will execute code when the micro:bit starts. The code inside this block creates a NeoPixel object called “neo”, and it is configured to use pin 1, use 48 LEDs, and use the GRB Format. We also set the brightness of the NeoPixels to 15 for two reasons. The first reason is that NeoPixels are incredibly bright, and a brightness level of 15 is comfortable for viewing when coding. The second reason is that the micro:bits cannot source a lot of current. Keeping the brightness low reduces the strain on the micro:bit’s voltage regulator.
The second main block is the forever loop, which runs continuously. Since there are 48 LEDs in total, we start by executing a for loop 48 times (0 to 47). Then, on each loop, the NeoPixel LEDs are all cleared and then turn on one LED. The specific LED that gets switched on in each for loop is the one whose address is equal to n. For example, the first iteration of the for loop would see LED 0 turn on, and the second iteration would see LED 1 turn on. The result of this for loop is a red chasing LED that goes around each NeoPixel display in the chain.
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum