Guillaume RICHARD

Dec 05, 2024

Using a Raspberry Pi 4 to drive WS2811 with an external power supply

drawing

+

It's so beautifuul

+

It's so beautifuul

=

It's so beautifuul

Purchase List

Item Purchase Link Price (EUR)
5V Power Supply Amazon (EU) €11.98
DC Power Adapter Amazon (EU) €6.99
WS2811 LEDs Amazon (EU) €28.99
Raspberry Pi 4B raspberrypi €80~
Female to Male Jumper Wires Amazon (EU) €19.55
Length of Cable for Data Connection Amazon (EU) €19.99
Total €167.50~

Preparing the Raspberry PI

You’ll need to install some software on your Raspberry Pi to control the LED strip. The ws2811-mqtt Python package is what we will be using for this purpose.

  1. ⚠️ Become root ! ⚠️
    You have to be root for the python library to control the GPIO pins and manage the LED strip.
    sudo -s
    Remain root for the rest of the steps

  2. Update the Raspberry Pi (Cause why not):

    sudo apt-get update
    sudo apt-get upgrade
  3. Enable necessary raspi interfaces

    raspi-config nonint do_i2c 0
    raspi-config nonint do_spi 0
    raspi-config nonint do_serial 0
  4. REBOOT the raspberry pi ⚠️reboot⚠️

  5. Install PyEnv as root, and add it to bashrc for easy management of Python versions

    curl https://pyenv.run | bash
    echo 'export PATH="/root/.pyenv/bin:$PATH"' >> ~/.bashrc
    echo 'eval "$(pyenv init -)"' >> ~/.bashrc
    echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
    source ~/.bashrc
  6. Install, create, and activate a python env
    PYTHON_VERSION=3.11.2

    pyenv install $PYTHON_VERSION
    pyenv virtualenv $PYTHON_VERSION ws2811-env
    pyenv activate ws2811-env
    pip install ws2811-mqtt

Wiring Setup

  • WS2811 LED strip: WS2811 refers to the microcontroller integrated into the LED strip, which is responsible for driving the LEDs, not the LEDs themselves. If your LED strip includes the WS2811 microcontroller, it means this setup and tutorial will work with your LEDs.
    Fig 1:
    Plug it this way
  • Jumper Wires: For connecting the Raspberry Pi GPIO pins to the LED strip.
    Fig 2:
    "Plug it this way"
  • Power Supply: Use the 5V power supply suitable for your LED’s wattage. I utilized an old USB to DC cable, stripping it to expose the red (positive) and black (ground) wires. Connect the red wire to the LED strip’s 5V line and the black wire to its GND. Always verify voltage and polarity to prevent damage.
  1. Connect WS2811:
    • Data Pin: Connect the data input of the WS2811 strip (Fig 1) to a GPIO pin on the Raspberry Pi (Fig 2) (e.g., GPIO 18 is commonly used for PWM). I used GPIO 18
    • Ground Pin: Connect the data ground of the WS2811 strip to the ground of the Raspberry Pi
    • Power:
      Connect the VCC (5V) of the WS2811 strip to the 5V rail of the power supply.
      Connect the GND of the WS2811 strip to the ground on the power supply

"It should look like this"

ws2811-mqtt

I have created a simple python package that publishes the lights to homeassistant and allows you to control them with some “light” objects and some switches.
Follow the instructions on the GitHub repository to install and configure the package:
https://github.com/lord0gnome/ws2811_mqtt

Once everything is properly set up, you should have a new MQTT device discovered in Home Assistant.
Home Assistant MQTT Device
This image shows the MQTT device as it appears in the Home Assistant interface.
The All Leds light and Alternate Colors are mutually exclusive options. Only one can be active at a time.

Troubleshooting

  • No Lights Turning On: Double-check your connections and ensure both the Raspberry Pi and LED strip are powered correctly. The leds I have flash RED if they receive power without any data signals.
  • Incorrect Polarity: Confirm that the positive and negative terminals of the power supply are correctly connected to the LED strip to avoid damage.

This guide should help you successfully set up a Raspberry Pi 4 with WS2811 lights. Enjoy your lighting project!

< NEWER

monero

bitcoin

Ethereum