WorkshopNeoPixels

From SkullSpace Wiki
Jump to navigation Jump to search


Here we're going to be playing with WS2812 red+green+blue digital LEDs which AdaFruit calls NeoPixels. We're going to be doing that by by controlling them with an Arduino. After you learn the basics you can be string these together into long chains so you can use them for signs, fancy Christmas lights, or anything else fun that might need changing lights or precise colours. Most of the hard stuff is handled in software for us, so this will be more of an Arduino and programming workshop. It's just fun to interface bits to the physical world.

Required Supplies

  • Arduino-compatible micro controller board
  • Laptop with the Arduino IDE installed (see this page for details)
  • USB cable to connect the Laptop to the Arduino
  • A good attitude :D

Kit Contents ($20)

  • Three NeoPixels soldered to female headers
  • Male to male jumper wires to connect them to the Arduino
  • One knock off, but awesome, Arduino Micro (Supplies limited)

Pre-class Preparation

These lights need very precise timing to talk to them making it tricky to program. Because of that, we're going to use an Arduino library from AdaFruit to do all the hard work for us. That way we can just spend time doing awesome things with them. Instructions on installing and using that library are found here, so get that installed and if you like to be prepared give the rest of that page a read. It'll give you a huge leg up and an in-depth look at what we'll be doing in this workshop.

If you're not familiar with basic circuits, that's okay. There is magic here, but it's not needed for the class. There is a great article over at SparkFun that you can read if you're curious.

In-class work

Because we're using only three NeoPixels, the Arduino and your computer's USB port can give us all the power we need. The only physical part of this workshop is hooking the outputs of the Arduino to the inputs of the NeoPixels. You'll see that the LEDs in the kits come with wires attached, and that the ribbon is labelled GND, DI, and +5v. Those need to be wired to the 5v, Gnd and "Analog In 0" of the Arduino. That should give us the power and data we need to get onto the software part.

For the software, we've made something very simple. Start a new sketch in the Arduino IDE (see 'new' under 'file') and save it (see 'save' under 'file'). Then just paste this text block in there and press the upload button (round button with a right arrow):

// Adapted from NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library

#include <Adafruit_NeoPixel.h>

#define PIN       A0 // Which pin on the Arduino is connected to the NeoPixels?
#define LED_COUNT 3  // How many pixels

Adafruit_NeoPixel pixels(LED_COUNT, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  pixels.begin(); // This starts the NeoPixel library.
}

// This function will make the middle LED blink blue
unsigned char counter = 0;
void blink() {
  counter = counter + 1;
  
  pixels.setPixelColor(1, pixels.Color(0, 0, counter)); // Use a variable for blue
  pixels.show();
  delay(10); // Wait 10 / 1000ths of a second so we can see the change
}

// This function will turn on all the lights to low
void solid() {
  pixels.setPixelColor(0, pixels.Color(10, 0, 0));   // Low Red colour
  pixels.setPixelColor(1, pixels.Color(0, 10, 0));   // Low green
  pixels.setPixelColor(2, pixels.Color(0, 0, 10));   // Low Blue
  pixels.show(); // This sends the updated pixel color to the hardware.
}

void loop() {
  // Do something creative in here!

  //blink();
  solid();
}

This should light the three LEDs for you. If not, panic slightly and read on to the Troubleshooting section. If it did light up, congrats! You're now ready to make your own fun! Start playing with the numbers in 'pixels.Color' just under 'solid()' to find out what different values for Red, Green, and Blue make.

If you remove the comment marker ('//') from "//blink()", you should see the blue LED start flashing. This is because the 'counter' variable is counted up by one over and over again (the Arduino runs the code in 'loop' repeatedly forever). We're then putting that number into the colour so gets brighter. It turns black again when the number gets so big it loops around back around from 255 to zero.

Some interesting ideas:

  1. Traffic light (how do you make yellow?)
  2. Animation of multiple lights
  3. Reverse Flashing

The instructors should be able to help you figure out how to make any fun ideas work with these.

Troubleshooting

In all likelihood if something went wrong it was either the wires were not connected properly, or the code may have failed to upload.

The Arduino itself also has a handy light on it so if that's not lit, plug it into another USB port and see if that helps. Check the three pins to make sure they're connected to A0, +5, and Gnd in the right order.

If you see red text at the bottom of the Arduino IDE, it could be that the serial port wasn't picked properly. Try looking under the "Tools" then "Port" menu for a COM (Windows) or tty (Linux, Mac) port. You'll need the right one selected, and nowadays the Arudino is likely the only serial port on your system. Swap to different ports to see if the upload works.