Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
1.9 NeoPixel LED Strip
Introduction
In this project, we will learn how to control an addressable RGB LED strip (NeoPixel) using SPI communication on a Raspberry Pi. NeoPixels are intelligent RGB LEDs that contain their own drivers, allowing individual control of each LED in a chain. This project demonstrates basic color control and filling the entire strip with different colors.
What You’ll Need
To complete this project, you will need the following components:
COMPONENT |
PURCHASE LINK |
|---|---|
- |
|
- |
|
Raspberry Pi |
- |
Wiring Diagram
Setup Steps
Before running the code, you need to install the required library:
This library provides the necessary functions to control NeoPixel LEDs using SPI communication.
sudo pip3 install adafruit-circuitpython-neopixel-spi --break
All example code used in this tutorial is available in the
ai-lab-kitdirectory. Follow these steps to run the example:cd ~/ai-lab-kit/python/ sudo python3 1.9_NeoPixel.py
When this script runs, the WS2812 LED module cycles through three solid colors:
All LEDs turn red for one second
All LEDs turn green for one second
All LEDs turn blue for one second
After displaying each color, the LEDs briefly turn off before repeating the cycle. The terminal prints the current color name as the pattern runs.
Code
The following Python code controls a NeoPixel LED strip, cycling through different colors:
import time # Used for delays
import board # Provides board-specific pin definitions
import neopixel_spi as neopixel # NeoPixel SPI driver
# Create an SPI object using the default SPI bus of the board
spi = board.SPI()
LED_COUNT = 12 # Number of LED pixels in the strip
PIXEL_ORDER = neopixel.GRB # Color order used by the LEDs (Green, Red, Blue)
# Create a NeoPixel strip object over SPI
# auto_write=False means we must call strip.show() to update the LEDs
strip = neopixel.NeoPixel_SPI(spi, LED_COUNT, pixel_order=PIXEL_ORDER, auto_write=False)
time.sleep(0.01) # Short delay to ensure the strip is ready
strip.fill(0) # Turn all pixels off (color value 0 = off)
strip.show() # Send the data to the LED strip
try:
while True:
print("RGB test")
# Display red on all LEDs
print("Red")
strip.fill((255, 0, 0)) # Full red, no green, no blue
strip.show()
time.sleep(1)
# Display green on all LEDs
print("Green")
strip.fill((0, 255, 0)) # Full green
strip.show()
time.sleep(1)
# Display blue on all LEDs
print("Blue")
strip.fill((0, 0, 255)) # Full blue
strip.show()
time.sleep(1)
# Turn all LEDs off
# print("Off for 10 seconds")
strip.fill((0, 0, 0)) # All channels 0 = off
strip.show()
time.sleep(1)
# Gracefully handle script termination (e.g., via KeyboardInterrupt)
except KeyboardInterrupt:
pass
This Python script demonstrates basic control of a 12-LED WS2812 ring using the NeoPixel SPI driver. When executed:
The script initializes the SPI interface and prepares the WS2812 LED ring.
All LEDs cycle through red, green, and blue with 1-second intervals.
Each color change is printed to the console for debugging.
The LEDs briefly turn off between cycles.
The program loops continuously until interrupted with Ctrl+C.
Understanding the Code
Library Import
The script uses the
neopixel_spilibrary to control WS2812 LEDs through Raspberry Pi’s SPI interface.import time import board import neopixel_spi as neopixel
SPI and NeoPixel Setup
The SPI bus is initialized and the NeoPixel ring is configured with 12 LEDs and the required color order (GRB).
spi = board.SPI() LED_COUNT = 12 PIXEL_ORDER = neopixel.GRB strip = neopixel.NeoPixel_SPI( spi, LED_COUNT, pixel_order=PIXEL_ORDER, auto_write=False )
Initial LED Reset
A short delay ensures the LEDs are ready, then all pixels are turned off.
time.sleep(0.01) strip.fill(0) strip.show()
Main Color-Cycling Loop
Inside the infinite loop, the script fills the LED ring with solid red, green, and blue in sequence. Each change is displayed for one second.
while True: strip.fill((255, 0, 0)) # Red strip.show() time.sleep(1) # Similar code for green and blue...
5. Color Format
Colors are defined using an RGB tuple:
(red, green, blue)with each value between 0–255.strip.fill((0, 255, 0)) # Example: green
Troubleshooting
LED Ring Not Lighting Correctly
Cause: Wrong wiring or insufficient power
Solution: Ensure VCC is 5V, GND is shared, and the data line is connected to DIN (sometimes labeled RGB)
Incorrect Colors
Cause: LED color order mismatch
Solution: Try different pixel orders such as
neopixel.RGBorneopixel.GRBW
SPI Not Working
Cause: SPI disabled or hardware conflict
Solution: Enable SPI via
sudo raspi-config
Library Import Fails
Cause: Missing dependency
Solution:
sudo pip3 install adafruit-circuitpython-neopixel-spi --break
Extendable Ideas
You can light up specific LEDs with different colors to create simple patterns or highlight certain positions on the ring.
strip[0] = (255, 0, 0) strip.show()
By cycling through calculated RGB values, you can generate a smooth rainbow transition across all LEDs.
strip.fill(wheel(50)) strip.show()
Create a chasing effect by lighting one LED at a time while keeping the others off.
strip[i] = (0, 255, 0) strip.show()
You can dim or brighten the entire LED ring by adjusting the brightness attribute.
strip.brightness = 0.3
Conclusion
This example demonstrates how to control a WS2812 12-LED ring using SPI on a Raspberry Pi. With only a few lines of code, you can cycle through colors, create animations, and explore individual LED control. This makes WS2812 rings ideal for indicator lights, robotics, decorative effects, and interactive visual projects.