Programming a Raspberry Pi to Control WS2811 LED Strips

Are you looking for a guidance about the specific steps of controlling WS2811 LED Strips through Raspberry Pi? This blog might give you the answer.

1. Tools Needed

Raspberry Pi

Raspberry Pi board

As a micro-controller, all components of the Raspberry Pi are put on a small board. The main components consist of the CPU, GPU, RAM, USB ports, HDMI output, an SD card slot, the operating system as well as the General Purpose Input & Output pin.

Originally used for education purpose, Raspberry Pi was once popular in programming. As time goes by, it is gradually used for more purposes such as smart home lighting. Currently, Raspberry Pi has been used as a lightweight server for various applications.

Power Driver

5V LED Driver

  • Output voltage: DC 5V
  • Power: 400W
Arduino 5V LED Power Supply

12V LED Driver

  • Output voltage: DC12V
  • Power: 400W
Arduino 12V

Function:

  • 12V LED driver is used to power the WS2811 LED strip.
  • The GND port of the 5V LED driver and 12V LED driver should be connected.

Converter

converter

The 4-level converter is a power electronic circuit used in high-voltage applications like HVDC transmission or large motor drives. Instead of generating just high/low voltage outputs like simpler converters, it creates four distinct voltage levels at its output terminal.

WS2811 LED Strip

DA1 12V WS2811

Parameters:

  • Color: RGB
  • Control way: SPI/TTL control
  • Voltage: DC12V
  • LED Qty: 60LEDs/m
  • Feature: This WS2811 has simple IC chips but provide aboundant color modes. Through programming, you can achieve the lighting effect you want.

 

WS2811 LED strip has  pure, soft and gorgeous color. 5V/12V/24V input voltage are all availbele for WS2811 LED strip. If you are interested in more specific parameters of WS2811, please check this blog: WS2811 Everything You Need To Know.

2. Raspberry Pi 4B Control Board and WS2811 LED Strip Wiring Diagram

Raspberry Pi WS2811 wire diagram

Note:

1. The current of the light strip is too large, which will lead to voltage drop if we only use one power supply. Please use an external power supply to power the light strip;

2. The DC3.3/5V interface is the voltage output port of the control board and cannot be connected tothe power supply;

3. The control board is powered by a dedicated power supply, and the light strip is powered byDC12V Please note that the GND is shared;

4. The wiring of RGB is the same as that of RGBW, but the programs cannot be shared;

5. The more points, the lower the refresh rate;

6. Note that the GPl0 of Raspberry Pi 4B is 3.3V logic level, so the GPl0 pin needs to be converted toa level before connecting to the light strip signal pin (74HCT125 or MOSFET level conversion).

3. Raspberry Pi WS2811 LED Connection Diagram

Raspberry Pi WS2811 connection

Please check this wire connection diagram carefully. Each PIN should be connected with the corresponding wire. For security, please close off the power before you make any changes of the connection.

4. Programming

Raspberry Pi programming
				
					#!/usr/bin/env python3
from rpi_ws281x import PixelStrip, Color
import time
import math
import signal
import sys

# === LED strip setup ===
LED_COUNT = 100        # LED quantity
LED_PIN = 18          # GPIO18
LED_FREQ_HZ = 800000  # signal frequency(Hz)
LED_DMA = 10          # DMA channel
LED_BRIGHTNESS = 150  # brightness(0-255)
LED_INVERT = False    # signal reverse
LED_CHANNEL = 0       # PWM channel

# format led strip
strip = PixelStrip(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)
strip.begin()

# === color generation function ===
def wheel(pos):
    """change the value of0-255 into rainbow color(red→green→b→→lue recycle)"""
    pos = 255 - pos  # reverse color direction(optional)
    if pos < 85:
        return Color(pos * 3, 255 - pos * 3, 0)
    elif pos < 170:
        pos -= 85
        return Color(255 - pos * 3, 0, pos * 3)
    else:
        pos -= 170
        return Color(0, pos * 3, 255 - pos * 3)

def smooth_wheel(pos):
    """more smooth rainbow color transition(trigonometric function version)"""
    r = int(255 * (0.5 + 0.5 * math.sin(pos * 0.0245)))
    g = int(255 * (0.5 + 0.5 * math.sin(pos * 0.0245 + 2.094)))  # +120°
    b = int(255 * (0.5 + 0.5 * math.sin(pos * 0.0245 + 4.188)))  # +240°
    return Color(r, g, b)

# === main effect function ===
def rainbow_flow(speed_ms=20, smooth=True):
    """recycle full color running effect
    :param speed_ms: basic speed(ms)
    :param smooth: whether USE_SMOOTH_COLOR
    """
    step = 0
    try:
        while True:
            # dynamic_speed control(optional)
            dynamic_speed = speed_ms * (0.8 + 0.2 * math.sin(step * 0.01))
            
            for i in range(strip.numPixels()):
                # calculate color_phase(form flow effect)
                hue = int((i * 256 / strip.numPixels()) + step) % 256
                color = smooth_wheel(hue) if smooth else wheel(hue)
                strip.setPixelColor(i, color)
            
            strip.show()
            time.sleep(dynamic_speed / 1000.0)
            step += 1
    except KeyboardInterrupt:
        pass

# === escape safely ===
def signal_handler(sig, frame):
    print("\n close LED...")
    for i in range(strip.numPixels()):
        strip.setPixelColor(i, Color(0, 0, 0))
    strip.show()
    sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)

# === main program ===
if __name__ == '__main__':
    print("=== addressable LED strip full color flow effect ===")
    print("Ctrl+C close program")
    
    # parameter setup
    USE_SMOOTH_COLOR = True  #  USE_SMOOTH_COLOR
    BASE_SPEED_MS = 15       # basic flow speed(ms)
    
    rainbow_flow(speed_ms=BASE_SPEED_MS, smooth=USE_SMOOTH_COLOR)
				
			

5. Video

Please check the following video and see the wire connection as we introduced in the above passage.

As you can see, by using Raspberry Pi and the programming, WS2811 RGB LED strip can achieve complex lighting effects. 

Please contact us if you need more help about controlling WS2811 LED strips with Raspberry Pi.

If you are interested in how to control WS2811 LED strip via similiar microcontroller ESP32, please check these blogs:

ESP32 Pixel Programming WS2811 LED Strip Guide

If you are intereste in how to control other LED Strips via Raspberry Pi, please check this blog:

How to Control RGBWW LED Strips with Raspberry Pi

Feel Free To Contact Us

We will reply your email within 12 hours.

Ask For A Quote