Chapter 6 NeoPixel

This chapter will help you learn to use a more convenient RGB LED lamp, which requires only one GPIO control and can be connected in infinite series in theory. Each LED can be controlled independently.

Project 6.1 NeoPixel

Learn the basic usage of NeoPixel and use it to flash red, green, blue and white.

Component List

ESP32-WROVER x1

Chapter01_00

GPIO Extension Board x1

Chapter01_01

Breadboard x1

Chapter01_02

Freenove 8 RGB LED Module x1

Chapter06_00

Jumper M/M x2

Chapter01_05

Circuit

Schematic diagram

Chapter06_03

Hardware connection

If you need any support, please feel free to contact us via: support@freenove.com

Chapter06_04

Code

Move the program folder “Freenove_Ultimate_Starter_Kit_for_ESP32/Python/Python_Codes” to disk(D) in advance with the path of “D:/Micropython_Codes”.

Open “Thonny”, click “This computer” -> “D:” -> “Micropython_Codes” -> “06.1_Neopixel” and double click “Neopixel.py”.

06.1_Neopixel

../../../_images/Chapter06_12.png

Click “Run current script”, and Neopixel begins to light up in red, green, blue, white and black.

../../../_images/Chapter06_13.png

The following is the program code:

 1from machine import Pin
 2import neopixel
 3import time
 4pin = Pin(2, Pin.OUT)
 5np = neopixel.NeoPixel(pin, 8)
 6
 7#brightness :0-255
 8brightness=10                                
 9colors=[[brightness,0,0],                    #red
10        [0,brightness,0],                    #green
11        [0,0,brightness],                    #blue
12        [brightness,brightness,brightness],  #white
13        [0,0,0]]                             #close
14
15while True:
16    for i in range(0,5):
17        for j in range(0,8):
18            np[j]=colors[i]
19            np.write()
20            time.sleep_ms(50)
21        time.sleep_ms(500)
22    time.sleep_ms(500)
23
24

Import Pin, neopiexl and time modules.

1from machine import Pin
2import neopixel
3import time

Define the number of pin and LEDs connected to neopixel.

1pin = Pin(2, Pin.OUT)
2np = neopixel.NeoPixel(pin, 8)

Define the brightness of neopixel’s LED and an array to store color.

1#brightness :0-255
2brightness=10                                
3colors=[[brightness,0,0],                    #red
4        [0,brightness,0],                    #green
5        [0,0,brightness],                    #blue
6        [brightness,brightness,brightness],  #white
7        [0,0,0]]                             #close

Assign the color data to the array np and call function write() to send np array data to neopixel module.

1np[j]=colors[i]
2np.write()

Nest two for loops to make the module repeatedly display five states of red, green, blue, white and OFF.

1while True:
2    for i in range(0,5):
3        for j in range(0,8):
4            np[j]=colors[i]
5            np.write()
6            time.sleep_ms(50)
7        time.sleep_ms(500)
8    time.sleep_ms(500)

Reference

Class neopixel

Before each usr of neopixel module, please add the statement “ import neopixel “ to the top of Python file.

NeoPixel(pin, n): Define the number of output pins and LEDs of neopixel module

pin: Output pins

n: The number of LEDs.

NeoPixel.write(): Write data to LEDs.

Project 6.2 Rainbow Light

In the previous project, we have mastered the usage of NeoPixel. This project will realize a slightly complicated Rainbow Light. The component list and the circuit are exactly the same as the project fashionable Light.

Code

Continue to use the following color model to equalize the color distribution of the 8 leds and gradually change.

../../../_images/Chapter06_14.png

Move the program folder “Freenove_Ultimate_Starter_Kit_for_ESP32/Python/Python_Codes” to disk(D) in advance with the path of “D:/Micropython_Codes”.

Open “Thonny”, click “This computer” -> “D:” -> “Micropython_Codes” -> “06.2_Rainbow_light” and then double click “Rainbow_light.py”.

06.2_Rainbow_light

../../../_images/Chapter06_15.png

Click “Run current script”, and the Freenove 8 RGB LED Strip displays different colors and the color changes gradually.

../../../_images/Chapter06_16.png

The following is the program code:

 1from machine import Pin
 2import neopixel
 3import time
 4pin = Pin(2, Pin.OUT)
 5np = neopixel.NeoPixel(pin, 8)
 6
 7brightness=0.1         #brightness: 0-1.0
 8red=0                  #red
 9green=0                #green
10blue=0                 #blue
11
12def wheel(pos):
13    global red,green,blue
14    WheelPos=pos%255
15    if WheelPos<85:
16        red=(255-WheelPos*3)
17        green=(WheelPos*3)
18        blue=0
19    elif WheelPos>=85 and WheelPos<170:
20        WheelPos -= 85;
21        red=0
22        green=(255-WheelPos*3)
23        blue=(WheelPos*3)
24    else :
25        WheelPos -= 170;
26        red=(WheelPos*3)
27        green=0
28        blue=(255-WheelPos*3)
29
30while True:
31    for i in range(0,255):
32        for j in range(0,8):
33            wheel(i+j*255//8)
34            np[j]=(int(red*brightness),int(green*brightness),int(blue*brightness))
35            np.write()
36        time.sleep_ms(5)
37

Define a wheel() function to process the color data of neopixel module.

 1def wheel(pos):
 2    global red,green,blue
 3    WheelPos=pos%255
 4    if WheelPos<85:
 5        red=(255-WheelPos*3)
 6        green=(WheelPos*3)
 7        blue=0
 8    elif WheelPos>=85 and WheelPos<170:
 9        WheelPos -= 85;
10        red=0
11        green=(255-WheelPos*3)
12        blue=(WheelPos*3)
13    else :
14        WheelPos -= 170;
15        red=(WheelPos*3)
16        green=0
17        blue=(255-WheelPos*3)

Set the color brightness of the module.

1brightness=0.1         #brightness: 0-1.0

Use a nesting of two for loops. The first for loop makes the value of i increase from 0 to 255 automatically and the wheel() function processes the value of i into data of the module’s three colors; the second for loop writes the color data to the module.

1for i in range(0,255):
2    for j in range(0,8):
3        wheel(i+j*255//8)
4        np[j]=(int(red*brightness),int(green*brightness),int(blue*brightness))
5        np.write()
6    time.sleep_ms(5)