Lesson 1: Lighting Lights

Basics on how to control the LED strip.
Hide Notes ▼
Examine the test1.py file:

          import board
          import neopixel

          pixels = neopixel.NeoPixel(board.D18, 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.D18, 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.D18: 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.

Exercises

1) Light up the fifth light.
Show Notes ▼
Show Solution ▼
2) Make the second light green.
3) Make the 4th light yellow.
Show Notes ▼
Show Solution ▼
4) Light up the first 5 lights.
Show Notes ▼
Show Solution (loop) ▼
5) Light up the first 16 lights.
6) Make the first 5 lights red and the rest blue
Show Notes ▼
Show Solution (two loops) ▼
7) Make all of the lights light up one at a time (with a 0.25 seconds between each lighting up.)
Show Notes ▼
Show Solution (time) ▼
8) Over 2.5 seconds have a single light of the 20 light up and march across the strip, with all the other lights being black.
Show Hint ▼