Index>Robot Kit>RaspClaws Spider Robot Kit for RPi>Lesson 7 How to Control WS2812 LED

Lesson 7 How to Control WS2812 LED

3331

Lesson 7 How to Control WS2812 LED

 

In this lesson, we will learn how to control WS2812 LED.

7.1 Components used in this course

 

Components

Quantity

Picture

Raspberry Pi

1

wps1.png 

Robot HAT

1

wps2.jpg 

3 pin wire

1

wps3.jpg 

WS2812 RGB LED

1

wps4.jpg 

 

7.2 Introduction of WS2812 RGB LED

WS2812 RGB module is a low-power RGB tri-color lamp with integrated current control chip. Its appearance is the same as a 5050LED lamp bead, and each element is a pixel. The pixel contains an intelligent digital interface data latch signal shaping amplifier driving circuit, and also contains a high-precision internal oscillator and a 12V high-voltage programmable constant current control part, which effectively guarantees that the color of the pixel light is highly consistent.

wps5.jpg 

WS2812 LED is a very commonly used module on our robot products. There are three WS2812 LEDS on each module. Pay attention to the direction of the signal line when connecting. The signal line needs to be connected to the “INport of WS2812 LED after being led from the Raspberry Pi.  When the next WS2812 LED needs to be connected, we connect a signal wire drawn from the "OUT" port of the previous WS2812 LED with the "IN" port of the next WS2812 LED.

When using the Raspberry Pi to install the driver board RobotHAT, the WS2812 LED can be connected to the WS2812 interface on the RobotHAT using a 3pin cable.

We use a third-party library [rpi_ws281x] to control the WS2812 LED. You can learn about it via https://github.com/richardghirst/rpi_ws281x.

If you connect the WS2812 LED to the WS2812 interface of RobotHAT, the signal line is equivalent to connecting to the GPIO 12 of the Raspberry Pi.

 

7.3 Wiring diagram (Circuit diagram)

When the WS2812 LED is in use, the IN port needs to be connected to the WS2812 port on the RobotHAT driver board, as shown in the figure below:

wps6.jpg 

 

7.4 How to control WS2812 LED

Run the code

1. Remotely log in to the Raspberry Pi terminal.

image.png 

2. Enter the command and press Enter to enter the folder where the program is located:

cd adeept_raspclaws/server/

image.png 

3. View the contents of the current directory file:

ls

image.png 

4. Enter the command and press Enter to run the program:

sudo python3 LED.py

image.png 

5. After running the program successfully, you will observe that the WS2812 alternately flashing red, green and blue lights.

6. When you want to terminate the running program, you can press the shortcut key "Ctrl + C" on the keyboard.

7.5 Main code program

For the complete code, please refer to the file LED.py.

1. import time  

2. from rpi_ws281x import * 

Import dependency.

 

1. class LED:    

2.     def __init__(self):    

3.         self.LED_COUNT      = 16 #  Set to the total number of LED lights on the robot product, which can be more than the total number of LED lights connected to the Raspberry Pi

4.         self.LED_PIN        = 12 # Set as the input pin number of the LED lamp group    

5.         self.LED_FREQ_HZ    = 800000     

6.         self.LED_DMA        = 10     

7.         self.LED_BRIGHTNESS = 255    

8.         self.LED_INVERT     = False    

9.         self.LED_CHANNEL    = 0    

10.   

11.     # Use the configuration items above to create a strip    

12.         self.strip = Adafruit_NeoPixel(    

13.         self.LED_COUNT,    

14.         self.LED_PIN,    

15.         self.LED_FREQ_HZ,    

16.         self.LED_DMA,    

17.         self.LED_INVERT,     

18.         self.LED_BRIGHTNESS,     

19.         self.LED_CHANNEL    

20.         )    

21.     self.strip.begin()    

22.   

23.     def colorWipe(self, R, G, B): # This function is used to change the color of the LED    

24.         color = Color(R, G, B)  

25.         for i in range(self.strip.numPixels()): # Only one LED light color can be set at a time, so a cycle is required    

26.             self.strip.setPixelColor(i, color)     

27.             self.strip.show() # After calling the show method, the color will really change  

Build LED control class.

 

1. if __name__ == '__main__':    

2.     led= LED()    

3.     try:    

4.         while 1:    

5.             led.colorWipe(255, 0, 0)  #All lights turn red 

6.             time.sleep(1)    

7.             led.colorWipe(0, 255, 0)  # All lights turn green  

8.             time.sleep(1)    

9.             led.colorWipe(0, 0, 255)  # All lights turn blue  

10.             time.sleep(1)    

11.     except:    

12.         LED.colorWipe(0,0,0)  #Turn off all lights 

The above code will control all the WS2812 lights to switch among the three colors, press CTRL+C to exit the program.

 If you want to control the color of a single light, you can use the following code to achieve, where i is the serial number of the light, the serial number of the first light connected to the signal line from the driver board is 0, and the serial number of the second light is 1. , And so on, R, G, B are the brightness corresponding to the three color channels:

LED.strip.setPixelColor(i, Color(R, G, B))

LED.strip.show()

Note: You must use the Color() method to pack the RGB value, and then pass it to setPixelColor().