Use the NeoPixel library to call the Rpi-ws2811/2 light strip (without using sudo to start the light strip)

Table of Contents

1. Selection of light strips

1. Rpi-ws2811 light strip LED

2. Rpi-ws2812 matrix LED

Second, try to use the Rpi-ws281x library to test the LED (you can ignore it if you try to solve the sudo permission problem)

1. Understand the light strip wires

2. Start the Raspberry Pi and connect to the network

3. Install Rpi-ws281x library

4. Write LED program

5. Run the py file directly

3. Use the Neopixel library to call LED

1. Install the Neopixel library

2. Set SPI and Serial Port

3. Modify the configuration file

4. Rewiring

5. Write the LED program again

6. Run the py file directly

4. Reference webpage


One, the choice of light strips

1, Rpi-ws2811 light with LED

2, Rpi-ws2812 matrix LED

Two, try to use the Rpi-ws281x library to test the LED (you can ignore it if you try to solve the sudo permission problem) )

1, know the light strip lines

Among them, Rpi-ws2812 matrix LED also has the standard interface line shown in the picture above.

GIN stands for GPIO interface line

GND means GND interface line (0V)

5V represents 5V positive power access

The interface used by the author is as follows:

GIN>>>GPIO.1 (BOARD encoding 12)

GND>>>>any

5V>>>>BOARD code 2

2, start the Raspberry Pi, connect to the network

3, install Rpi-ws281x library

Use the Linux terminal command line to execute the following commands in sequence:

sudo apt-get update

sudo apt-get upgrade

sudo pip3 install rpi_ws281x

4, write LED program

Create a new Test_LED.py file. The author uses Thonny IDE to edit it:

from rpi_ws281x import PixelStrip,Color
import time

Led_Num=60 #Define the number of Led lamp beads
Led_Pin=18 #Define the BCM code of the GPIO interface connected to the Led light strip (GPIO.1 is 18)
Led_Brightness=128 #Define the brightness of Led lamp beads (0~255 0 is not bright, 255 is the brightest)

strip=PixelStrip(Led_Num,Led_Pin,Led_Brightness) #Instance lamp bead configuration

strip.begin() #Initialize lamp beads

def ColorTest(strip,color,wait_ms=20):
    strip.setPixelColor(1,color) #Control the first lamp bead configuration
    strip.show() #Light up the lamp beads

ColorTest(strip,Color(255,0,0)) #Start the lamp beads

time.sleep(3) #Wait for three seconds

ColorTest(strip,Color(0,0,0)) #Turn off the lamp beads



5, run the py file directly

error? error code:

RuntimeError: ws2811_init failed with code -5 (mmap() failed)

Are you running it directly in the IDE?

I regret to tell you that currently the Rpi-ws281x library can only be run on the Linux terminal using the following code:

sudo python xxx.py

Note: When using the above code, please cd to the corresponding directory of the py file first

Three, use the Neopixel library to call LED

Since I recently need to develop OpenCV and LED projects

But when I put the cv2 and rpi_ws281x libraries together, I found a very annoying thing:

When the file is not executed using sudo, the rpi_ws281x library cannot be called.

When using sudo to execute a file, cv2 cannot be called.

In desperation, I searched a lot on the Internet to find out how to activate the LED without using sudo to obtain root privileges.

……

Then I found the answer on a foreign forum: use the Neopixel library

1, install the Neopixel library

Enter the following code in the Linux terminal:

pip3 install adafruit-circuitpython-neopixel

#If you don’t have permission, you can try the following code

sudo pip3 install adafruit-circuitpython-neopixel

#Then enter the following three lines of code in sequence

python3 -m venv .venv

source .venv/bin/activate

pip3 install adafruit-circuitpython-neopixel

Then restart the Raspberry Pi

2, set SPI and Serial Port

Enter the following code in the Linux terminal:

sudo raspi-config

The following interface will then appear:

Select the 3rd option Interface Option

Select P4 SPI and P6 Serial Port respectively

Then confirm “enable” respectively

3, modify configuration file

Enter the following code in the Linux terminal:

sudo nano /boot/config.txt

#Then enter the following code at the end of the file

dtparam=spi=on
enable_uart=1

#Press ctrl + o and enter to save, then ctrl + x to exit editing


Restart Raspberry Pi

4, rewiring

If you want to call the Neopixel library without using sudo root permissions

You need to connect the green GPIO line to the MOSI interface (BCM code is 10)

5, write LED program again

Create a new Test_LED_Neopixel.py file. The author still uses Thonny IDE for editing:

import board
import neopixel
pixels=neopixel.NeoPixel(board.D10,1) #Call MOSI interface (BCM encoding is 10)
pixels[0]=(100,100,100) #The first LED outputs white light

6, run the py file directly

Lighted up successfully! ! !

Four, reference data

Adafruit CircuitPython NeoPixel – Adafruit CircuitPython NeoPixel Library 1.0 documentationicon-default.png?t=N7T8https://docs.circuitpython.org/projects/neopixel/en /latest/index.htmlPython Usage | NeoPixels on Raspberry Pi | Adafruit Learning Systemicon-default.png?t=N7T8https://learn.adafruit.com/neopixels- on-raspberry-pi/python-usage

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. CS entry skill treeLinux introductionFirst introduction to Linux36333 people are learning the system