WorkshopNeoPixels

From SkullSpace Wiki
Revision as of 04:38, 6 January 2016 by Edwin (talk | contribs)
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.
}

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

  pixels.setPixelColor(0, pixels.Color(10,0,0));   // Low Red colour
  pixels.setPixelColor(1, pixels.Color(0,20,0));   // Low green
  pixels.setPixelColor(2, pixels.Color(0,0,10));   // Low Blue
  pixels.show(); // This sends the updated pixel color to the hardware.
}

This should light the three LEDs for you. If not, panic slightly and read on.

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.