LED Strip


Introduction

Before thinking about the hardware or the software needed to light-up a LED strip, it’s mandatory to talk about the different driver chips used to control the LEDs.

WS2811 VS WS2812

The main differences are summarized into different categories:

  1. Power: WS2811 LED chip needs a voltage of 12V to work, while WS2812 chip only uses 5V.
  2. LEDs Controlled: WS2811 actually do not control one LED, but instead it controls a cluster of 3 LEDs together. WS2812 can control 1 LED each.

Common Software Setup

To avoid repeating myself later on, let’s describe the different environments you’ll need to set up to get everything up and running.

Software Setup for Arduino

Arduino IDE is the best choice to program the Arduino board within its C-like language. If are skilled enough, you could also use the powerful command line interface Arduino CLI.

Please refer to the official Getting Started guide to install the IDE on the OS you prefer.

Software Setup for Python

One of the best environments for Python programming (and definitely my favorite) is PyCharm by JetBrains. However, PyCharm is not really suitable for running python scripts from the Raspberry Pi board, since it’s very resource-consuming.

Some light-weight IDEs are:

  1. Visual Studio Code which is also available for Raspberry PI by following this guide
  2. Thonny which is installed by default on Raspberry Pi OS
  3. Mu

Arduino

Hardware requirements & setup

  • Arduino Uno board or any ArduinoUno-like board
  • LED strip, suggested stores are:
  • 470 Ohms resistor
  • 100 µF capacitor
  • Hook-up wires of different colors
  • Breadboard
  • 12V power supply if using a strip controlled by WS2811 chip

Arduino writing schema

FastLED

The common code for all the following effects made with FastLED library is:

#include "FastLED.h"
// digital output pin of the board
#define PIN 6
// number of LEDs of the strip
#define NUM_LEDS 100

CRGB leds[NUM_LEDS];

// increase this value to slow down the effect
unsigned short int delayTime = 5;

void setup() {
  FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
}

// void loop and effect function

void showStrip() {
  FastLED.show();
}

void setPixel(int pixelNumber, byte red, byte green, byte blue) {
  leds[pixelNumber].r = red;
  leds[pixelNumber].g = green;
  leds[pixelNumber].b = blue;
}

void setAll(byte red, byte green, byte blue) {
  for (int i = 0; i < NUM_LEDS; i++) {
    setPixel(i, red, green, blue);
  }
  showStrip();
}

NeoPixel

The common code for all the following effects made with NeoPixel library is:

#include <Adafruit_NeoPixel.h>

// digital output pin of the board
#define PIN 6
// number of LEDs of the strip
#define NUM_LEDS 100

// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  // initialize the strip
  strip.begin();
  // initialize all pixels to 'off'
  strip.show();
}

// void loop and effect function

void showStrip() {
  strip.show();
}

void setPixel(int pixelNumber, byte red, byte green, byte blue) {
  strip.setPixelColor(pixelNumber, strip.Color(red, green, blue));
}

void setAll(byte red, byte green, byte blue) {
  for (int i = 0; i < NUM_LEDS; i++) {
    setPixel(i, red, green, blue);
  }
  showStrip();
}

Raspberry PI

Hardware requirements & setup

  1. Raspberry Pi 4 board or any other Raspberry Pi board
  2. LED strip, suggested stores are:
  1. 470 Ohms resistor
  2. Hook-up wires of different colors
  3. Breadboard
  4. 12V power supply if using a strip controlled by WS2811 chip

Raspberry PI writing schema

In this schema, I do not event used the resistor since Adafruit strip already has it built-in. Moreover, the strip is powered at 5V so I do not even need an external supplier.

Please Note: NeoPixels must be connected to GPIO10 (user defined), GPIO12, GPIO18 or GPIO21 to work.

Software requirements & setup

Let’s follow the CircuitPython on Linux and Raspberry Pi guide to set everything up!

Please Note: At this time, Adafruit does not support the 64-bit version of Raspberry Pi OS so let’s find out which version we’re currently running.

Open a Terminal and type:

uname -m

If your machine is running a 32-bit OS version, the output will be i686 or i386 or armv7l. While if your machine is running a 64-bit OS version, the output will be x86_64 or armv8.

# update the Raspberry Pi and Python
sudo apt-get update

sudo apt-get upgrade

sudo apt-get install python3-pip

sudo pip3 install --upgrade setuptools

Adafruit also put together a script to easily make sure your Pi is correctly configured and install Blinka. It requires just a few commands to run. Most of it is installing the dependencies.

cd ~

sudo pip3 install --upgrade adafruit-python-shell

wget https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/master/raspi-blinka.py

sudo python3 raspi-blinka.py

If your system default Python is Python 2 (which is likely on a first install), it will ask to confirm that you want to proceed. Choose yes. It may take a few minutes to run. When it finishes, it will ask you if you would like to reboot. Choose yes. Once it reboots, the connection will close. After a couple of minutes, you can reconnect.

Please Note: Python2 support has been dropped, so you will need to either use pip3 and python3 as commands or set Python 3 as the default python install.

NeoPixel with CircuitPython

To install the python library to drive the NeoPixel LEDs just type:

sudo pip3 install adafruit-circuitpython-neopixel

The common code for all the following effects made with NeoPixel library with CircuitPython is:

import time
import board
import neopixel

# board pin
pixel_pin = board.D10

# the number of NeoPixels
num_pixels = 100

# The order of the pixel colors - RGB or GRB. Some NeoPixels have red and green reversed!
# For RGBW NeoPixels, simply change the ORDER to RGBW or GRBW.
ORDER = neopixel.RGB

pixels = neopixel.NeoPixel(
    pixel_pin, num_pixels, brightness=0.2, auto_write=False, pixel_order=ORDER
)

# utility functions
def show_strip():
    pixels.show()

def set_pixel(pixel_number, red, green, blue):
    pixels[pixel_number] = (red, green, blue)

def set_all(red, green, blue):
    pixels.fill((red, green, blue))

# define the effect
def my_effect():
    pass

while True:
    # run the effect
    my_effect()

Effects

You can take all the sketches from:

  1. led-strip directory from arduino-projects GitLab repository
  2. Raspberry Pi LED Strip Project

Jump to effects folder to see the list of effects in both the repositories. Fill free to send me a pull request if you have any suggestion for the effects I could add or improve.

Here is the list:

  • BouncingBalls

This effect looks best with the LED strip vertical, and shows one or more bouncing balls in a given color.

  • BouncingRGBFade

This effect uses a sequence of fixed colors’ list for all LEDs: red, green and blue. It slowly increases brightness and when the maximum brightness has been reached, it starts decreasing the brightness again until the LEDs are all OFF.

  • ChasingLight

A backhoe of LEDs moves forward and then downward the strip. Each LED is colored with a different composition of the red, green and blue components.

  • ColorWipe

You can define different colors that will fill the whole strip, from the first to the last. When a color lights-up all the LEDs, the next color starts over the strip to replace the previous color. You can also allow the color to fill the strip form the last to the fist element using reverseColorWipe method.

  • FadeInFadeOut-ColorAndBrightness

It fades in and out both color and brightness of the colors you want.

  • FadeInFadeOut-ColorWithoutBrightness

It fades in and out color and fakes the fade of the brightness. It is very similar to the effect above.

  • Fire

This effect is adapted from an example from FastLED, which is adapted from work done by Mark Kriegsman (called “Fire2012”). It looks awesome when using diffuse light!

  • MeteorRain

This effect looks better with the whole strip placed in vertical position. It creates a meteor that runs through the sky with some randomness LEDs switched on for the tail.

  • MultiColor-BouncingBalls

This is a more complex variant of the Bouncing Balls effect. Instead of just one color, it allows the use of multiple colors. This makes the function call a little more complex, since it works for any number of balls you’d like set. To pass the color for each ball you will have to define a multidimensional array filled with the RGB backhoe of each balls.

  • NewKITT

This effect follows this pattern:

  1. <---------
  2. --------->
  3. ----><----
  4. <---------
  5. --------->
  6. <---------
  7. ----><----
  8. <-------->

Inspiration for this effect was taken from instructables’ article The KITT-duino DIY Larson Scanner.

  • RainbowCycle

Fills all the LDEs with different shades of rainbow colors and slowly moves the colors from the start to the end of the strip. It also looks good on a LED matrix like this Flexible 8x8 NeoPixel RGB LED Matrix from adafruit.com.

  • RandomColorTwinkle

This is a variation on the Twinkle effect. The only difference is that the colors are now randomly generated.

  • RunningLights

This effect makes multiple groups of LEDs chase each other.

  • Scanner

This effect mimics the LED of a scanner that runs forward (and downward) the length of the document to scan.

  • Strobe

This effect turn the strip in a stroboscopic light. Please Note: Strobe effect could lead to epilepsy phenomena in photosensitive subjects.

  • TheatreChase

With this effect LEDs are chasing each other like what you’d see in an old Theatre.

  • TheatreChaseRainbow

This effect combines RainbowCycle and TheatreChase.

  • Twinkle

This effect will blink one or more LEDs in a given color.

Conclusion

Here we are, at the end of this long blog post. Here are some ideas where you could use those effects:

  • smart home with adaptive lights
  • alarm system
  • notification system
  • party lights

Hope you enjoyed this post as much I loved write it. It does not matter weather you prefer to use Arduino or Raspberry Pi or any other board to light up a LED strip.

Keep in mind that there is always a way to do what you want to do.

Documentation

Some useful links: