Hi everybody, this is Ladyada speaking! So far, I've been writing pretty much all the tutorials on the site but I'd like to introduce someone who is going to be doing some really really awesome LED projects and tutorials. First off is a simple but comprehensive "Ambient Light" project for your computer using digital RGB pixels to simplify wiring.
Take it away, Phil Burgess!
There are any number of projects for which it would be handy to animate LEDs from a PC. Not a microcontroller, but a full-on PC. Media — music and video — are a natural for PCs, and tools like Max/MSP and Processing are a natural for creating media-based software sketches. (We use “PC” here in the generic “personal computer” sense, not in opposition to Mac; Using a combination of Processing and Arduino, everything shown here runs as well on Mac or Linux as it does on a Windows system!)
As a first demonstration, we’ll build a simple "Ambilight" clone. Ambilight is a feature of some Philips televisions that projects colored light onto the wall behind the display, synchronized with the content on the screen to create an immersive effect. The authentic Philips system is well-integrated into the TV and works from any video source. Our facsimile, being computer-driven, works specifically with media content from your PC. This means its perfect for watching Youtube, TV or Movies on your PC or playing games!
This video isn't a Philips commercial!
That's for the 'main body' of the display - we have this available as a discounted project pack!
Triple check that your power supply is a 5V supply! Any higher than that could damage the LEDs and Arduino! Even if you got a pack (one person accidentally received a 9V supply) CHECK IT AGAIN BEFORE USING
You'll also need:
Some alterations were made to the input wiring of the LEDs to more easily connect to the Arduino and the power supply. Breadboarding jumper wire ends were soldered to the clock and data wires. A jumper wire end was also added to the ground wire, but this one as a pigtail rather than inline splice. That pigtail and the +5V line are then screwed to the power socket (the correct +/- polarity is embossed just above the screw terminals).
The wire colors for these LEDs have changed over time, so use this photo for mechanical reference only, not necessarily for wire colors. The Adafruit RGB LED tutorial explains how to identify the function of each wire leading to the LED strand. And make sure you’re connecting everything to the LED strand’s INPUT side! From October 2011+, Red should be +5V, Black is Ground, Clock is Green, and Data is Yellow.
The three jumper wires then connect to ground, clock and data pins on the Arduino. If using a “traditional” Arduino board (Uno, etc.), use Digital pin 13 for clock and 11 for data. For the ATmega 32u4 or Teensy boards, use B1 for clock and B2 for data. For Arduino Mega, use pin 52 for clock and 51 for data. The ground wire can connect to any GND pin on the Arduino.
Next we’ll need some way of holding the LEDs behind the monitor. Let’s get all crafty and make a template! The choice of materials will depend on availability and the intended permanence (or not) of the installation. It could range from sophisticated laser-cut acrylic to nothing more than a pizza box and some duct tape. Here we’ve cut a template from illustration board, sized to match the back of the monitor, with a notch cut out at the bottom to accommodate the monitor stand:
Next, calculate the spacing for a ring of 25 LEDs. For this monitor, it worked out nicely as a 9x6 rectangle, roughly 2" spacing, with a one pixel gap at the bottom where the stand fits. Perfect! This monitor has holes for a VESA wall bracket that’s not being used here, so holes were punched to match, and the template can be held in place with screws. But this doesn’t need to be rocket science — for a quick demo, don’t feel bad just using tape or something. In fact, that’s exactly what we used for positioning the LEDs:
When designing your LED holder, make sure it doesn’t block any air vents on the monitor or computer.
Once the LEDs are situated, the Arduino can be placed (double-stick foam tape works nicely) and wired in. Connect a USB cable between the Arduino and computer, and a compatible 5 Volt supply to the LED power connector. The mass of wires and tape may look chaotic from this side, but once the monitor is set up close to a wall, nobody will see it.
Download the Adalight codebase by visiting the github repository and clicking DOWNLOADS in the top right corner. Now uncompress the folder. Inside you’ll find an “Arduino” folder, and inside that is an “LEDstream” folder. Move or copy the LEDstream folder to your Arduino sketches folder and open the sketch in the Arduino IDE. Select your board type and serial port, compile the sketch and upload to the board. The same sketch may come in handy for later LED projects; the board may not even need reprogramming.
An interesting feature of this code is that it streams (rather than buffers) the LED data. This will later allow it to scale up to many more LEDs, despite the constrained memory of the Arduino. It’s serial bandwidth that becomes the limiting factor. Typical boards like the Uno or the Duemilanove used here should be able to stream up to 50 or mmmaybe 100 pixels at decent, video-like frame rates.Bigger projects will require the ATmega32u4 Breakout Board or Teensy, with their fast native USB interfaces. On those boards, the code should be able to stream upwards of 1,200 LEDs at 30 frames per second. If using either of those devices, download and install the “Teensyduino” add-on for the Arduino IDE, and possibly follow the extra 32u4 directions on the Adafruit product page.
The downloaded code also contains a “Processing” folder, and inside that is an “Adalight” folder. Move or copy the Adalight folder to your Processing sketches folder and open the sketch in the Processing IDE. If your LED array is laid out similarly to the above — 25 LEDs in a ring, 1 pixel gap at the bottom, with the first pixel starting just left of the gap — then there’s nothing more to do, just compile and run! If you’ve made changes, the code will need some alteration to match your layout. Also, as written, the code currently assumes that the Arduino is the first or only serial device on the system. If that’s not the case, in Processing, the code println(Serial.list()); displays a list of all connected serial devices, and you can change the name (or just the index) in the code to match your configuration.
LINUX USERS: the Processing serial library fails to acknowledge the 32u4 and Teensy boards. This is a known bug that will be fixed in the Processing 2.0 release. In the interim, the work-around is to create a link from the actual 32u4 serial device (typically /dev/ttyACM*) to an unused /dev/ttyS* number, e.g.: sudo ln -s /dev/ttyACM0 /dev/ttyS42
As it runs, the sketch performs a continual series of screen captures, averaging the pixels in blocks around the perimeter of the screen and issuing the resulting color data to the LEDs. You can hide the preview window and let the sketch continue working in the background. Being capture-based, it’s not tied to any one specific media player, and most anything you can put on your display — MPEG movies, YouTube, games and so forth — can benefit from the effect. It seems to work especially well with the outer space sequences in Cosmos…a bit ironic in that Sagan’s “Spaceship of the Imagination” from this series featured a giant flat screen and mood lighting decades before Philips turned it into a commercial product!
We’ll not delve too deeply into the operation of the software here. Both the Arduino and Processing source code are fairly well-commented, if you want to dig deeper and make your own improvements. A second sketch folder, “Colorswirl,” is a simpler case that does not rely on screen captures or other media — it simply outputs rainbow patterns to the LEDs. If you’re looking for a simpler example in order to get a handle on things, this might be easier to grasp. The Arduino will not need any code changes.
You might find the Adalight effect a bit choppy if running on anything less than the latest hardware. The continual screen captures seem to be the main bottleneck, so it helps to set the display to a lower resolution before starting the sketch and playing back video. At 800x600 resolution, an older Atom-based nettop could barely muster 13 frames per second. A Core 2 Duo-based MacBook Air did reasonably well at this resolution, just shy of 30 FPS. The beefier CPUs available now should have no trouble running smoothly even at higher resolutions.
Extra bonus cake: if programming closer to the bare metal is more your style, host-side code can be written in C as well, with no changes to the Arduino code. The challenge there is in handling serial output consistently across different operating systems, and the RXTX library (which Processing relies upon) proved educational in this regard. The Adalight.zip file contains a third folder, simply named “C,” which is the same colorswirl demo written in C, and has been tested in Cygwin (for Windows), Mac OS X and Linux.
So what will come next? Filling in all those pixels in the middle, creating an entirely LED-based display…stay tuned!