2.1
Party Lights!
Party Lights!
What to expect
During this week’s activity, you will work with a WS2812 “Neopixel” LED light strip. These types of LEDs can be programmed using the micro:bit to be individually controlled to change color, turn on or off, and more.
Connections
You will use programmable LEDs like this in several future activities (both as a stip and included onboard), so this is a good first project to learn how they work, sharpen your programming skills, and get creative!
Materials
1 micro:bit with the battery pack connected
Three (3) alligator clips. The color isn't critical, but it would be helpful if you had:
(1) Red
(1) Green
(1) White
Neopixel LED strip - The one provided to you has 5 LEDs connected to a red, green, and white wire
This activity will use a micro:bit to program the Neopixel (programmable RGB LED strip).
Here is a little background on the technology and a few helpful terms to know:
LED - Light-emitting Diode. This amazing technology is found in many of the products and lighting devices we use today.
Neopixel - This is the popular name for programmable LEDs. Not all LEDs are programmable, but the LED strip you are using is based on a class of devices called WS2812. There are lots of cool projects out there that use WS21812 Neopixels!
Instructions
Open the MakeCode micro:bit editor in a browser at: https://makecode.microbit.org/.
Add a new project and name it “LED Strip.”
3. Add the AdaFruit Neopixel driver extension:
4. Create the following code and test it in the simulator then on your mico:bit. The color is not critical, but red is shown as an example. In the next steps, you will add more colors. Use the Neopixel blocks library to find the code blocks you need
a: If you set pin P0 here, this is the pin that your physical connection should be connected to.
b: Make sure to set this number to 5 to match the number of individually controllable LEDs on the provided LED strip.
c: Make sure this setting is in GRB format (this ensures compatibility with your particular LED strip, otherwise your colors might not make sense 🙃).
Important: The wires connected to your physical LED strip should be connected in a specific way. If you are using the same color alligator wires, just connect them based on color. Otherwise, make sure to follow the wiring based on what is connected to the LED strip:
Red: +3v (power)
Green: Data (This wire is what is used to control the color and on/off of individual LEDs. Connect this to P0, for example. This should be the same pin from step b)
White: 0V (ground)
Hint: You don’t want to connect the Red and White wires together or the Green and White wires together. This may cause a direct short (short circuit) which can harm the micro:bit and/or LED, so be careful when you are hooking these up! 🙂
5. Add the following code and test it in your simulator and on the micro:bit
6. Next, add the following code and test it in your simulator and on the micro:bit
7. Once you know how to control of the basic colors of the LEDs, things can get interesting! We can control each LED individually. We can use show, clear, shift, and rotate command blocks to turn on or off and “move" the LEDs. Let’s try out some of the things we can do with the LED strip:
Assign a position: Modify your program so that the pixel (LED) at 0 is red. Notice that in computer systems, zero is the first item in a list. So, a group of 5 items would be counted as a range 0 to 4. Also, you must include the ‘show’ command in order to see your LED state change
Shift: Modify your code so that every time button A is pressed, the LED will shift to the next position in the range. What happens when the position is shifted beyond the range? Don’t forget to include the ‘show’ command. (Hint: Reset on board/ refresh browser if needed…)
Rotate: Modify your program so that the LED pixel position rotates (shifts and wraps around the range). You can use negative numbers to indicate direction of the rotation (or shift)
8. RGB colors are based on 256 (range of 0-255, where 0 is none and 255 is the most) shades of Red, Blue, and Green (RGB) respectively. These colors mix together to create 16,777,216 possible colors (although your LED strip might not be quite that precise 🙂). For example, true “red” would be 255, 0, 0 (no blue or green). Google “RGB color picker” or use this link to create a custom RGB color code. You can type the values or drag the slider to find a color you like. Modify your code to show your custom color when you press the micro:bit logo. Hint: You'll find the custom color code block in the "more" section under Neopixel. You can drop this shape of block over a similar shape to overwrite the parameter.
9. Experiment with shifting, rotating, and pressing the logo button to introduce the “new” pixel. What do you notice?
10. Did you know…? You can right-click on almost ANY block and get contextual help. Often times, this will provide you with useful coding examples and idea for going further. Try it with a Neopixel code block!
Ideas for Going Further
Timer activated lights
Sound activated lights
Use light strip to show mood, temperature, etc.
Change color when micro:bit is moved, shook, etc.
Many others… Use your imagination and get creative!
Wrap-up
You programmed the micro:bit to control a Neopixel LED strip.
Did the program work as expected? Why or why not?
Did you come up with a creative way to use the LED strip? If so, share your project with us!
Feedback Link
How did you feel about this activity? Change the emoji to show your feelings.
Next Activity
We will learn how to test a battery using the micro:bit. Challenge: Apply what you have learned this week to create a “fuel gauge” using the LED strip to show the battery level!