4 How to install py neopixel spidev
mishi edited this page 2021-05-22 11:37:40 +02:00

How to install py-neopixel-spidev for WS2812 RGB LED stripes

  • Data: MOSI,pin 19

  • GND: pin 20

  • Using https://pypi.org/project/py-neopixel-spidev/

  • sudo apt-get install python3-numpy python3-spidev --> already part of reqs.txt

  • sudo pip3 install py-neopixel-spidev --> needs to be done manually up til now

Note: SPI must be enabled on thre Raspberry Pi (sudo raspi-config >> interface >> SPI)

Many distributions have a maximum SPI transfer of 4096 bytes. This can be changed in /boot/cmdline.txt by appending

spidev.bufsiz=32768

On a RPi 3 and a RPi Zero you have to change the GPU core frequency to 250 MHz, otherwise the SPI clock has the wrong frequency. Do this by adding the following line to /boot/config.txt and reboot.

core_freq=250

On a RPi 4 its dynamic frequency clocking has to be disabled, since it will desync the SPI clock. Do this by adding this line to /boot/config.txt. (core_freq does not have to be changed, since the default value of 500MHz is SPI compatible)

core_freq_min=500

SPI requires you to be in the gpio group if you wish to control your LEDs without root.

Example

import time
from lib import neopixel_spidev as np
from lib.pixelbuf import wheel

# Init 56 LEDs on SPI bus 2, cs 0 with colors ordered green, red, blue
with np.NeoPixelSpiDev(0, 0, n=16, pixel_order=np.GRB) as pixels:
    try:
        while True:
            for i in range(255):
                # Use wheel function from pixelbuf module to calculate RGB color
                pixels.fill(wheel(i))
                time.sleep(0.03)
    except KeyboardInterrupt: