Skip to main content

Userspace Raspberry Pi PWM/PCM/SPI library for SK6812 and WS281X LEDs.

Project description

Userspace Raspberry Pi library for controlling WS281X LEDs. This includes WS2812 and SK6812RGB RGB LEDs Preliminary support is now included for SK6812RGBW LEDs (yes, RGB + W) The LEDs can be controlled by either the PWM (2 independent channels) or PCM controller (1 channel) or the SPI interface (1 channel).

Background:

The BCM2835 in the Raspberry Pi has both a PWM and a PCM module that are well suited to driving individually controllable WS281X LEDs. Using the DMA, PWM or PCM FIFO, and serial mode in the PWM, it’s possible to control almost any number of WS281X LEDs in a chain connected to the appropriate output pin. For SPI the Raspbian spidev driver is used (/dev/spidev0.0). This library and test program set the clock rate to 3X the desired output frequency and creates a bit pattern in RAM from an array of colors where each bit is represented by 3 bits as follows.

Bit 1 - 1 1 0
Bit 0 - 1 0 0

GPIO Usage:

The GPIOs that can be used are limited by the hardware of the Pi and will vary based on the method used to drive them (PWM, PCM or SPI). Beware that the GPIO numbers are not the same as the physical pin numbers on the header.

PWM:

PWM0, which can be set to use GPIOs 12, 18, 40, and 52.
Only 12 (pin 32) and 18 (pin 12) are available on the B+/2B/3B

PWM1 which can be set to use GPIOs 13, 19, 41, 45 and 53.
Only 13 is available on the B+/2B/PiZero/3B, on pin 33

PCM:

PCM_DOUT, which can be set to use GPIOs 21 and 31.
Only 21 is available on the B+/2B/PiZero/3B, on pin 40.

SPI:

SPI0-MOSI is available on GPIOs 10 and 38.
Only GPIO 10 is available on all models.
See also note for RPi 3 below.

Power and voltage requirements

WS281X LEDs are generally driven at 5V. Depending on your actual LED model and data line length you might be able to successfully drive the data input with 3.3V. However in the general case you probably want to use a level shifter to convert from the Raspberry Pi GPIO/PWM to 5V.

It is also possible to run the LEDs from a 3.3V - 3.6V power source, and connect the GPIO directly at a cost of brightness, but this isn’t recommended.

The test program is designed to drive a 8x8 grid of LEDs e.g.from Adafruit (http://www.adafruit.com/products/1487) or Pimoroni (https://shop.pimoroni.com/products/unicorn-hat). Please see the Adafruit and Pimoroni websites for more information.

Know what you’re doing with the hardware and electricity. I take no reponsibility for damage, harm, or mistakes.

Important warning about DMA channels

You must make sure that the DMA channel you choose to use for the LEDs is not already in use by the operating system.

For example, using DMA channel 5 will cause filesystem corruption on the Raspberry Pi 3 Model B. See: https://github.com/jgarff/rpi_ws281x/issues/224

The default DMA channel (10) should be safe for the Raspberry Pi 3 Model B, but this may change in future software releases.

Limitations:

PWM

Since this library and the onboard Raspberry Pi audio both use the PWM, they cannot be used together. You will need to blacklist the Broadcom audio kernel module by creating a file /etc/modprobe.d/snd-blacklist.conf with

blacklist snd_bcm2835

If the audio device is still loading after blacklisting, you may also need to comment it out in the /etc/modules file.

On headless systems you may also need to force audio through hdmi Edit config.txt and add:

hdmi_force_hotplug=1
hdmi_force_edid_audio=1

A reboot is required for this change to take effect

Some distributions use audio by default, even if nothing is being played. If audio is needed, you can use a USB audio device instead.

PCM

When using PCM you cannot use digital audio devices which use I2S since I2S uses the PCM hardware, but you can use analog audio.

SPI ~~

When using SPI the ledstring is the only device which can be connected to the SPI bus. Both digital (I2S/PCM) and analog (PWM) audio can be used.

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 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

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

Comparison PWM/PCM/SPI

Both PWM and PCM use DMA transfer to output the control signal for the LEDs. The max size of a DMA transfer is 65536 bytes. Since each LED needs 12 bytes (4 colors, 8 symbols per color, 3 bits per symbol) this means you can control approximately 5400 LEDs for a single strand in PCM and 2700 LEDs per string for PWM (Only PWM can control 2 independent strings simultaneously) SPI uses the SPI device driver in the kernel. For transfers larger than 96 bytes the kernel driver also uses DMA. Of course there are practical limits on power and signal quality. These will be more constraining in practice than the theoretical limits above.

When controlling a LED string of 240 LEDs the CPU load on the original Pi 2 (BCM2836) are: PWM 5% PCM 5% SPI 1%

4.2.2

  • Fix: Fixed Slice support when setting pixels

  • New: Added support for Revision 0xa02083

  • New: Added support for Revision 0xa22083

4.2.1

  • Fix: Added white colour channel to Color()

4.2.0

  • New: Added support for Raspberry Pi 4

4.1.0

  • New: Added support for the Pi 3A+

  • Fix: Bugfix for ARM64 support

4.0.0

  • Warning: Removed “neopixel” module from this library, use “rpi_ws281x” instead, see #8

3.1.0

  • New: Exposed all contents of ws for easy access to strip_type constants

  • Fix: Matched functionality of upstream legacy library by supporting strip_type

  • Note: Default strip type is WS2811_STRIP_GRB

3.0.7

  • New: Added support for Pi 3B+

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rpi_ws281x-4.2.2.tar.gz (65.4 kB view details)

Uploaded Source

Built Distributions

rpi_ws281x-4.2.2-cp37-cp37m-linux_armv7l.whl (104.7 kB view details)

Uploaded CPython 3.7m

rpi_ws281x-4.2.2-cp35-cp35m-linux_armv7l.whl (110.6 kB view details)

Uploaded CPython 3.5m

rpi_ws281x-4.2.2-cp34-cp34m-linux_armv7l.whl (90.3 kB view details)

Uploaded CPython 3.4m

rpi_ws281x-4.2.2-cp27-cp27mu-linux_armv7l.whl (102.9 kB view details)

Uploaded CPython 2.7mu

File details

Details for the file rpi_ws281x-4.2.2.tar.gz.

File metadata

  • Download URL: rpi_ws281x-4.2.2.tar.gz
  • Upload date:
  • Size: 65.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/2.7.16

File hashes

Hashes for rpi_ws281x-4.2.2.tar.gz
Algorithm Hash digest
SHA256 d06cd31217dec40d715a3a07ccb2b4b0b0719527c5aa020cbdd77d0c0b31d113
MD5 7914013f6e0ee17ce23d51622d60761d
BLAKE2b-256 3bb3cdc84887ead3f15b9f3f2b5fe2b48168757a606c6d08feb53536a8afb396

See more details on using hashes here.

File details

Details for the file rpi_ws281x-4.2.2-cp37-cp37m-linux_armv7l.whl.

File metadata

  • Download URL: rpi_ws281x-4.2.2-cp37-cp37m-linux_armv7l.whl
  • Upload date:
  • Size: 104.7 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/2.7.16

File hashes

Hashes for rpi_ws281x-4.2.2-cp37-cp37m-linux_armv7l.whl
Algorithm Hash digest
SHA256 dbbb04947b3166d96ba48a82702be9e9e762eb78922434e491d0de958f8fe8c7
MD5 dd9890b7520418de484088f3a5f94f47
BLAKE2b-256 cb6415806dab1fa49f91a3983b6bc8c128ff247450232855b8ba175772865c98

See more details on using hashes here.

File details

Details for the file rpi_ws281x-4.2.2-cp35-cp35m-linux_armv7l.whl.

File metadata

  • Download URL: rpi_ws281x-4.2.2-cp35-cp35m-linux_armv7l.whl
  • Upload date:
  • Size: 110.6 kB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.5.3

File hashes

Hashes for rpi_ws281x-4.2.2-cp35-cp35m-linux_armv7l.whl
Algorithm Hash digest
SHA256 262ad65562333c20ccb1f10850f2413521dd300a2af16de32cbfa27b5b8353ac
MD5 b89010f103fd28bf95009a2799dfe3bf
BLAKE2b-256 32991140d72d58e31d1251c2009238473e818f2d991e130c59f09dc7502d99bc

See more details on using hashes here.

File details

Details for the file rpi_ws281x-4.2.2-cp34-cp34m-linux_armv7l.whl.

File metadata

  • Download URL: rpi_ws281x-4.2.2-cp34-cp34m-linux_armv7l.whl
  • Upload date:
  • Size: 90.3 kB
  • Tags: CPython 3.4m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.5.3

File hashes

Hashes for rpi_ws281x-4.2.2-cp34-cp34m-linux_armv7l.whl
Algorithm Hash digest
SHA256 bad191da9945d7ba9fe0f88ab70d04e6af1b554f520eaa50415f7bff1fa31150
MD5 6419d451c227785096d999873d57c1db
BLAKE2b-256 5b44341b291801817bd258adf709d1d62cbc5ea647d2d9ea5c35aaa369c6b43e

See more details on using hashes here.

File details

Details for the file rpi_ws281x-4.2.2-cp27-cp27mu-linux_armv7l.whl.

File metadata

  • Download URL: rpi_ws281x-4.2.2-cp27-cp27mu-linux_armv7l.whl
  • Upload date:
  • Size: 102.9 kB
  • Tags: CPython 2.7mu
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/2.7.16

File hashes

Hashes for rpi_ws281x-4.2.2-cp27-cp27mu-linux_armv7l.whl
Algorithm Hash digest
SHA256 eb2365f513b333fb1574131a1a719b66ffd252624c192ac0e954a5970cfea84f
MD5 93b967007ddbf51449caaee04b448fe3
BLAKE2b-256 94ac84e88cc07510858c37ae4907e3e61f025729935b44027ea4010d5b669c45

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page