Raspberry Pi - NeoPixel LED Strip

The NeoPixel RGB LED strip is a collection of LEDs whose color and brightness can be individually adjusted. This tutorial instructs you how to use a Raspberry Pi to control the NeoPixel RGB LED strip. In detail, we will learn:

We only need to use one digital pin of the Raspberry Pi to control all the LEDs on the strip.

Hardware Preparation

1×Raspberry Pi 4 Model B
1×NeoPixel RGB LED Strip
1×1000uF Capacitor
1×470Ω resistor
1×5V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×(Optional) Screw Terminal Adapter for Raspberry Pi
1×(Optional) Power Adapter for Raspberry Pi 4B
1×(Optional) Plastic Case for Raspberry Pi 4B

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of NeoPixel RGB LED Strip

The NeoPixel LED Strip Pinout

The NeoPixel RGB LED Strip has three pins that require connection:

  • GND pin: This must be connected to GND (0V)
  • VCC pin: This must be connected to 5V of an external power supply
  • Din pin: This is the pin that receives the control signal and should be connected to a Raspberry Pi pin.
NeoPixel pinout

※ NOTE THAT:

The order of pins may differ among manufacturers. It is IMPERATIVE to always utilize the labels printed on the LED Strip.

Wiring Diagram

The wiring diagram between Raspberry Pi and NeoPixel RGB LED strip

This image is created using Fritzing. Click to enlarge image

How To Program For NeoPixel RGB LED Strip

  • Create a NeoPixel object.
import time from rpi_ws281x import Adafruit_NeoPixel, Color PIN_NEO_PIXEL = 18 # The GPIO pin that connects to NeoPixel NUM_PIXELS = 30 # The number of LEDs (pixels) on NeoPixel NeoPixel = Adafruit_NeoPixel(NUM_PIXELS, PIN_NEO_PIXEL) # Create NeoPixel object
  • Initializes the library for NeoPixel.
NeoPixel.begin() # Initialize NeoPixel strip object (REQUIRED)
  • Specify the color of each individual LED (known as a pixel).
NeoPixel.setPixelColor(pixel, Color(0, 255, 0))
  • Adjust the brightness of all the strips.
NeoPixel.setBrightness(100); # a value from 0 to 255

※ NOTE THAT:

  • NeoPixel.setBrightness() is used for adjusting the brightness of all pixels on the LED strip. To set the brightness for each individual pixel, we can scale the color values (R,G, B) with the same ratio.
  • The values set by NeoPixel.setBrightness() and NeoPixel.setPixelColor() will only be applied when NeoPixel.show() is executed.

Raspberry Pi Code

The code below:

  • Changes each pixel to green in succession, with a pause between each one
  • Switches off all pixels for two seconds
  • Lights up all pixels red simultaneously for two seconds
  • Repeats the cycle indefinitely

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Prior to utilizing the NeoPixel with a Raspberry Pi, we need to SPI interface on Raspberry Pi. Enable the SPI interface on Raspberry Pi by following the instruction on Raspberry Pi - how to enable SPI inteface
  • Install the NeoPixel library by running the following command:
sudo pip3 install rpi_ws281x
  • Create a Python script file NeoPixel.py and add the following code:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-neopixel-led-strip """ import time from rpi_ws281x import Adafruit_NeoPixel, Color # NeoPixel configuration PIN_NEO_PIXEL = 18 # The GPIO pin that connects to NeoPixel NUM_PIXELS = 30 # The number of LEDs (pixels) on NeoPixel DELAY_INTERVAL = 50 # Delay between each pixel change in milliseconds # Create NeoPixel object NeoPixel = Adafruit_NeoPixel(NUM_PIXELS, PIN_NEO_PIXEL) NeoPixel.begin() # Initialize NeoPixel strip object (REQUIRED) try: while True: # Turn on all pixels to green for pixel in range(NUM_PIXELS): NeoPixel.setPixelColor(pixel, Color(0, 255, 0)) NeoPixel.show() time.sleep(DELAY_INTERVAL / 1000.0) # Turn off all pixels NeoPixel.clear() NeoPixel.show() time.sleep(2) # Turn on all pixels to red for pixel in range(NUM_PIXELS): NeoPixel.setPixelColor(pixel, Color(255, 0, 0)) NeoPixel.show() time.sleep(DELAY_INTERVAL / 1000.0) # Turn off all pixels NeoPixel.clear() NeoPixel.show() time.sleep(2) except KeyboardInterrupt: # Clean up code before exiting the script NeoPixel.clear() NeoPixel.show()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 NeoPixel.py
  • Check out the LED effect.

The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.

※ NOTE THAT:

For any intricate LED effect, we provide the paid programming service

Video Tutorial

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!