Table of contents:

Intro to the hardware

LED strips are a quick, easy, simple way to get lots of light and color into a project.

The type we have in the Phys Comp Lab (part # 2001 in our inventory) is composed of a series of small LED modules arranged next to each other on a band of plastic. We buy them on Amazon. Each individual LED module has four electrical connections:

  • 5V: power (5 volts in this case)
  • GND: ground
  • DI: data in
  • DO: data out

The plastic tape has some ebedded wires in it which connect all the LED modules on the strip to power and ground. The data in and data out work a little differently—note the little arrows on the strip:

Closeup of LED strip with four LEDs shown. Each LED module has three electrical connectors on its left (ground, data in, and 5V top to bottom) as well as another three on its right (ground, data out, and 5V top to bottom). There are white arrows printed on the strip pointing left to right.

The white arrows show the direction the data flows. The first module gets data into its left side (DI), and then processes it, and passes the data through, out its DO pin. The next module takes that data into its DI pin, processes it, and emits it through its own DO pin. In this way, the LED strip modules act as a little bucket brigade for the data telling each LED which color to light up.

Wiring and soldering

The LED strip needs power, ground, and a signal from a microcontroller (like an Arduino). You can cut the strip at any of the vertical white lines using regular scissors and then solder your wires right on to the gold-colored pads. Be sure to solder your data connection to the input side (labeled “DI” for “data input”). The power and ground wires can connect at the right, or left, or both.

Closeup of left edge of LED strip. Three wires are soldered onto the strip's surface: a black wire is soldered to the top pin labeled "GND," a purple wire is soldered to the middle pin labeled "DI," and a red wire is soldered to the bottom pin labeled "5V." The image includes two sentences: "You can solder straight onto the strip to make your connections. Just be sure your data connection (purple wire in this photo) is on the left 'DI' end of the strip."

For a relatively short strip (less than ~30 LEDs) you can safely use the Arduino Uno’s 5V power and ground to power the strip. But for a longer strip, you should use another 5V supply to supplement the Arduino’s power because when the LEDs all turn on they’ll need a fair bit of current (more than the Uno can supply).

Software

All LEDs the same color sample sketch

Here is some sample code that lights up the whole strip black, white, red, orange, yellow, green, blue, and indigo. It uses the “Pololu LED strip” library, which you will have to download through the Arduino IDE before compiling or uploading the code. (You can read the authors’ library documentation here.)

[download this code]


/*
  LED strip solid colors sample code

  Drives an entire LED strip to black, white, red, orange, 
  yellow, green, blue, and indigo. (Each LED on the strip 
  shows the same color at the same time.)

  Pin mapping:

  Arduino pin | role  | description
  ----------- | ----- | ------------
  2             output  transmits color data into LED strip


  This sample code based on based on LedStripGradient sketch
  from Pololu's LED Strip library, and released to the public
  domain by the author

  Robert Zacharias, rzachari@andrew.cmu.edu
  Feb. 2024
*/

#include <PololuLedStrip.h>

const int LEDSTRIPPIN = 2; // pin to connect strip to
const int NUMLEDS = 30; // number of LEDs in the strip

// Create an ledStrip object and specify the pin it will use.
PololuLedStrip<LEDSTRIPPIN> ledStrip;

// Create an array for holding the colors (3 bytes per color).
rgb_color colors[NUMLEDS];

// Make some named colors of variable type "rgb_color".
// Black is all LEDs off, and white is all LEDs on.
rgb_color black = rgb_color(0, 0, 0);
rgb_color white = rgb_color(255, 255, 255);

// Each rainbow color is either one LED fully on and others  
// off (that's red, green, and blue) or some mixture of them
// at varying strengths.
rgb_color red = rgb_color(255, 0, 0);
rgb_color orange = rgb_color(255, 75, 0);
rgb_color yellow = rgb_color(255, 255, 0);
rgb_color green = rgb_color(0, 255, 0);
rgb_color blue = rgb_color(0, 0, 255);
rgb_color indigo = rgb_color(75, 0, 130);


void setup(){
  // Nothing needed here for this sketch.
}

void loop(){
  
  // Run through the "colors[]" array and set every element to black.
  for (int i = 0; i < NUMLEDS; i++){
    colors[i] = black;
  }

  // Instruct the LED strip to light up according to "colors[]".
  ledStrip.write(colors, NUMLEDS);

  // Wait a second to see the color before changing.
  delay(1000);



  // Run through the "colors[]" array and set every element to white.
  for (int i = 0; i < NUMLEDS; i++){
    colors[i] = white;
  }

  // Instruct the LED strip to light up according to "colors[]".
  ledStrip.write(colors, NUMLEDS);

  // Wait a second to see the color before changing.
  delay(1000);


  
  // Run through the "colors[]" array and set every element to red.
  for (int i = 0; i < NUMLEDS; i++){
    colors[i] = red;
  }

  // Instruct the LED strip to light up according to "colors[]".
  ledStrip.write(colors, NUMLEDS);

  // Wait a second to see the color before changing.
  delay(1000);
  


  // Run through the "colors[]" array and set every element to orange.
  for (int i = 0; i < NUMLEDS; i++){
    colors[i] = orange;
  }

  // Instruct the LED strip to light up according to "colors[]".
  ledStrip.write(colors, NUMLEDS);

  // Wait a second to see the color before changing.
  delay(1000);
  


  // Run through the "colors[]" array and set every element to yellow.
  for (int i = 0; i < NUMLEDS; i++){
    colors[i] = yellow;
  }

  // Instruct the LED strip to light up according to "colors[]".
  ledStrip.write(colors, NUMLEDS);

  // Wait a second to see the color before changing.
  delay(1000);



  // Run through the "colors[]" array and set every element to green.
  for (int i = 0; i < NUMLEDS; i++){
    colors[i] = green;
  }

  // Instruct the LED strip to light up according to "colors[]".
  ledStrip.write(colors, NUMLEDS);

  // Wait a second to see the color before changing.
  delay(1000);



  // Run through the "colors[]" array and set every element to blue.
  for (int i = 0; i < NUMLEDS; i++){
    colors[i] = blue;
  }

  // Instruct the LED strip to light up according to "colors[]".
  ledStrip.write(colors, NUMLEDS);

  // Wait a second to see the color before changing.
  delay(1000);



  // Run through the "colors[]" array and set every element to indigo.
  for (int i = 0; i < NUMLEDS; i++){
    colors[i] = indigo;
  }

  // Instruct the LED strip to light up according to "colors[]".
  ledStrip.write(colors, NUMLEDS);

  // Wait a second to see the color before changing.
  delay(1000);
}

[download this code]