Wednesday, December 7, 2011

Star Wars Christmas Tree...Now with Sound

A few days ago I posted a set of Christmas tree lights that flashes to the Star Wars theme song.  I initially did not include a sound output intentionally because the tree sits in our dining room and it'd be annoying to have a song playing whenever we had the tree lit.  I decided later that as long as I could turn the sound off, it'd be a fun addition to the project, so I added a small piezo buzzer between pins 8 & 9 of the MSP430 and made the software changes necessary to output the song on the buzzer as well as on the lights.  I soldered a couple female headers onto my protoboard so that I can plug the buzzer in (to have sound) or take it out (for lights only).


In order to add the sound, I had to go back to the music and re-transcribe it with the actual note values (I ignored flats/sharps before because you couldn't really distinguish them on the lights...but it matters more with the sound).  I looked up the frequencies of those notes here.  The notes in the song range in frequency from ~294 Hz to 880 Hz.  I tried to keep the structure of my code as similar to the first version as possible, so the audio PWM is handled in software in the interrupt routine that fires when Timer A reaches the end of its period for the LED PWM.  I bumped up the DCO frequency from 1Mhz to 8Mhz to provide finer resolution for the musical notes, but the rest of the changes are pretty minor.  An updated version of the source code is available at https://sourceforge.net/projects/starwarsxmasled/.

Sunday, December 4, 2011

Star Wars Christmas Tree

In addition to all of the Christmas trees, ornaments, stockings, garlands, and other decorations that fill our house for the Christmas season, my wife lets me have one small tree of Star Wars Ornaments.  For the past few years it has been unlit, and she decided that it needed some lights this year.  She found a battery operated strand of 30 white LEDs on sale at Target last week, but I figured we could do better than just plain lights.....we needed Star Wars lights.  I built a small controller for the light strand that makes it flash in the pattern of the Star Wars theme song.  The basic concept is that the note durations are how long the lights are on, and the pitch translates to light intensity.  The lowest note in the song corresponds to the dimmest light setting, and the highest note to the brightest. 


I first cut the strand and measure that it draws ~70 mA with a new set of 4xAA batteries, which is way more than a mcu gpio can output, so I started with a 2n3904 transistor to turn the strand on and off.  I really only needed 1 pin from an mcu, so I went to my parts box and dug around for small microcontrollers.  The two options I had on hand were an AVR ATTINY85 and a handful of MSP430 microcontrollers.  I had a couple of ez430 thumbsticks that TI was giving away a while ago, along with a pack of the extra target boards, and this project was well suited to the small form factor and ease of use that those provide.  I ended up using one of the MSP430F2012 target boards.  It has a built in LED, which I did my initially firmware testing with to get  the note lengths and song pace working right, and then I used PWM output from the Timer A module to control the intensity of the LEDs on the actual strand.  

I'm not really a musical person, and all of the notes on the piano sheet music I was finding just confused me, so I found a violin arrangement (so only 1 note is being played at a time) and set about transcribing that into code for my microcontroller.  I broke the song down into repeating chunks so that I wouldn't have to code in every single note and could reuse the chunks.  Each note is represented by a single byte, where the lower three bits represent the length of the note, and the upper bits represent the pitch.  I went through and labeled the notes 1-12 from lowest to highest, and store those values, which are mapped to the range of PWM duty.  I recorded the note duration and pitch by hand for the whole song, and then used an Octave script to combine the values into my chosen representation of the information.


In the next picture you can see the circuit board a little closer.  I'm using an LM317 with a couple resistors and capacitors to provide 3.3V for the MSP430, and then the LED strand is switched on my a 2n3904 transistor with a 1k resistor on the gate.  Using the ez430 target board makes it easy to connect to the programmer/debugger, and it's not much more expensive than the bare chip (~$3.30 vs ~$2.70).  One of the value line (MSP430G series) chips would certainly have worked, but I had a few of these target boards lying around and it did the job just fine.


Below you can see a video of the tree in action.  The tree plays the full Star Wars Theme, but this is just the intro.  It looks better in person than on my camera, but you at least can get the idea.  My wife was kind enough to play violin along with the tree for the accompaniment.  You can download the source code here if you want it.