Turning on a light
Examine the test1.py file:
import board
import neopixel
pixels = neopixel.NeoPixel(board.GP0, 20)
pixels[2] = (10,0,0)
Looking through the code line by line:Lines 1 and 2:
import board
import neopixel
Import two modules that we need to light the LED's.
- import board: The board module helps control the general purpose input/output (GPIO) pins on the Pi.
- import neopixel: The neopixel module specifically focuses on operating this type (WS2811) of individually addressable LED lights.
Line 4:
pixels = neopixel.NeoPixel(board.GP0, 20)
This activates the LED strip. Specifically it indicates that:
- pixels =: It gives the strip a variable name, pixels so we can refer to it later.
- neopixel.NeoPixel(): it uses an object called NeoPixel from the neopixel library we imported on line 2.
- board.GP0: indicates that the strip is being controlled by the GPIO 18 pin, (you can look at the Pi and see that the data wire of the LED is connected to Pin 18)
- 20: there are 20 LED's in the strip.
Line 6:
pixels[2] = (10,0,0)
This makes the third light red.
- pixels: this is the variable that refers to the LED strip that we created on Line 4 that comes from the neopixel library we imported on Line 2.
- [2]: the pixels variable has an array that refers to each LED light, all 20 of them. However, the array indices starts at 0 so the 2 here refers to the third LED.
- (10,0,0): sets the color. Colors are RGB--(red, blue, green). The color values range from 0 to 255, so
(10,0,0)
indicates a low level of red, but no green or blue. Thus the third light shows a faint red.
Assignment 1
Light up the 5th light.
import board
import neopixel
pixels = neopixel.NeoPixel(board.GP0, 20)
pixels[4] = (10,0,0)
Assignment 2
Make the second light green.
import board
import neopixel
pixels = neopixel.NeoPixel(board.GP0, 20)
pixels[1] = (0,20,0)
Assignment 3
Make the fourth light yellow.
import board
import neopixel
pixels = neopixel.NeoPixel(board.GP0, 20)
pixels[3] = (255,255,0)
Reverse Indexing (in python)
Make the last light blue (using reverse indexing in python).
With 20 pixels you can light up the last one using reverse indexing:
pixels[-1] = (0,0,100)
which gives the same result as:
pixels[19] = (0,0,100)
This is quite useful, because to do the second to last pixel, you'd use:
pixels[-2] = (0,0,100)
So the full code to turn the last light blue would be:
import board
import neopixel
pixels = neopixel.NeoPixel(board.GP0, 20)
pixels[-1] = (0,0,100)
Assignment 5
Make the last three lights red, blue, and green respectively.
import board
import neopixel
pixels = neopixel.NeoPixel(board.GP0, 20)
pixels[-3] = (100,0,0)
pixels[-2] = (0,100,0)
...
Mixing Colors
Each LED Pixel really has three small LED's inside: one each of red, blue, and green.
To create different colors you'll just need to mix different levels of red, blue, and green. These are refered to as rgb values because the intensity of each color is given in that order. So, to make yellow you'll need to mix red and green:
pixels[0] = (20,20,0)
And if you look carefully, you might be able to see the red and green led's are lit in the pixel.
Mix the three colors equally to get white, and no color at all (0,0,0) gives black.
If you do an internet search for "color picker" you should get something that gives you rgb values for different colors.
Assignment 6
Make the second light pink.
import board
import neopixel
pixels = neopixel.NeoPixel(board.GP0, 20)
pixels[1] = (25,0,25)
...