WorkshopRotaryEncoder

From SkullSpace Wiki
Revision as of 20:56, 9 April 2016 by Benbergman (talk | contribs) (Created page with "In this workshop we will be using a rotary encoder to play with WS2812 red+green+blue digital LEDs which [https://www.adafruit.com/ AdaFruit] calls [https://www.adafruit.com/p...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

In this workshop we will be using a rotary encoder to play with WS2812 red+green+blue digital LEDs which AdaFruit calls NeoPixels. We will be using an Arduino to read from the encoder and write to the LEDs. After you learn the basics you can make fancy rotational user input devices (volume knobs, light dimmers, etc). 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

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

Pre-class Preparation

Rotary encoders need to be monitored very carefully by your software in order to perform precise reads. To help us with this, we will be using an Arduino library that will use a timer to monitor the encoder for us. The library we are using can be found here which depends on a timer library found here.

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 inputs of the rotary encoder to the inputs of the Arduino, and the outputs of the Arduino to the inputs of the NeoPixels. You will 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 "Digital 6" of the Arduino. The rotary encoder has 5 pins: 3 on one side, two on the other. Connect the center of the 3 pins to Gnd and the other two pins to "Analog In 0/1". On the other side of the encoder, connect one pin to Gnd and the other to "Analog In 2". That should give us the power and data we need to get onto the software part.

For the software, we've made a demo application which uses the encoder to switch between different led animations. 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):

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! Turn the rotary encoder knob or click the button to play a new animation.

Some interesting ideas:

  1. Light mixer - use the knob to change the colour of leds
  2. "Move" the light between the LEDs using the encoder

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.