How to Control WS2818 LED Strips with Raspberry Pi

SuntechLite is a top manufacturer and supplier that focuses on pixel LED strips and LED neon lights.

Are you trying to figure out how to control WS2818 LED Strips with Raspberry Pi? Please check the following passage which talks about the whole process of it. 

1. Tool

Raspberry Pi

The small-board microcontroller, Raspberry Pi, packs essential hardware – CPU, GPU, RAM, USB ports, HDMI, SD card slot and GPIO pins – into one integrated unit. Though initially embraced for teaching programming, the utility of Raspberry Pi has grown extensively. The current application of Raspberry Pi spans from managing smart home devices to serving as a practical and low-power server solution for various needs.

Please check the following blog, where you can find more information about the Raspberry Pi and Arduino:  Raspberry Pi vs Arduino. 

power supply 5V

This power supply accepts 110V or 220V AC input voltage and delivers a stable 5V DC output. It can supply currents up to 80A and a total 400W power, whch is sufficient for both Raspberry Pi and large RGB LED strips with the usage of 5V-to 3V converter and the 5V and GND lines. The unit efficiently dissipates heat, enabling reliable and long-term operation even when driving many LEDs.

12V

This 12V LED strip is to power the WS2818 LED Strip. Please make sure that the GND of the 5V driver and 12V driver is connected. 

converter

To avoid burning the Raspberry Pi board (which tolerates only 3.3V) when using the power supply with 5V output voltage, the 4-level way converter is employed as a voltage-adapting bridge between the Raspberry Pi and the power supply.

ws2818 led strip
WS2818 led strip

Specifications:

  • IC: WS2818
  • LED Qty: 60led/m
  • Feature: The WS2818 addressable light uses external WS2818 IC, which can control groups of 3 LEDs (12V) or 6 LEDs (24V). These IC are integrated directly into the strip, which enable more detailed lighting effects through serial communication with microcontrollers. The advantages of this WS2818 LED strip are many such as 24V led strip can run 10M without voltage drop, 2000Hz PWM frequency that supports camera capture up to 67fps and dual data lines that prevents full-strip failures. If you are interested in more types of addressable led strips, please check our page: Addressable RGB LED Strip. 

2. Wire Diagram

Raspberry Pi WS2818 diagram

Note:
1. The current is too much for this light strip. Please power it using an external supply.

2. The DC3.3/5V port on the Raspberry Pi provides output voltage. Connecting it to a power source is prohibited.

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

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

5 Note that the GPlO of Raspberry Pi 4B is 3.3V logic level, so the GPlO pin needs tobe converted to a level before connecting to the light strip signal pin (74HCT125 orMOSFET level conversion).

3. Real Connection

Raspberry Pi WS2818 connection

Implement the wiring diagram specifications and align each PIN with its corresponding point.

4. Code

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

# === LED Strip Configuration ===
LED_COUNT = 100        # Number of LEDs
LED_PIN = 18           # GPIO pin (must support PWM)
LED_FREQ_HZ = 800000   # Signal frequency (Hz)
LED_DMA = 10           # DMA channel
LED_BRIGHTNESS = 150   # Brightness (0-255)
LED_INVERT = False     # Signal inversion
LED_CHANNEL = 0        # PWM channel

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

# === Color Generation Functions ===
def wheel(pos):
    """Convert 0-255 value to rainbow color (red→green→blue cycle)"""
    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):
    """Smoother rainbow transition (trigonometric 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):
    """Rainbow chasing effect
    :param speed_ms: Base animation speed (milliseconds)
    :param smooth: Use smooth color transitions
    """
    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 hue (creates chasing 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

# === Graceful Exit Handler ===
def signal_handler(sig, frame):
    print("\nTurning off LED strip...")
    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("=== Rainbow Chasing Effect for LED Strip ===")
    print("Press Ctrl+C to exit")
    
    # Configuration parameters
    USE_SMOOTH_COLOR = True  # Enable smooth color transitions
    BASE_SPEED_MS = 15       # Base animation speed (milliseconds)
    
    rainbow_flow(speed_ms=BASE_SPEED_MS, smooth=USE_SMOOTH_COLOR)
				
			

5. Video

This video shows different lighting effects which are achievable by using Raspberry Pi to control RGB LED strips. With the correct power supply, reliable wiring and an appropriate code library, customizing various effects for your WS2818 strips becomes straightforward. Need help using an Raspberry Pi with your LED strips? We’re here to assist.

If you want to know how to control WS2818 LED strip via ESP32, please check this blog: How to Control WS2818 LED Strips with ESP32.

Share To

Enquiry