Making Magic In A Cooler – Part 2

Welcome to Part 2 of the Magic Cooler project.  If you missed Part 1, you can read it here. 

Thus far we have focused only on building the AC circuit that will serve to directly control the 10 switchable lights via 10 relays.

Now, we want to hook up the Teensy 3.1 board to control those relays in time to music, and have the Teensy drive the music itself out to a speaker of some sort.  The Teensy 3.1 board uses the Freescale Kinetis K20 microcontoller.

For controlling the relays, I used 10 of the Teensy board GPIOs.   These were configured in software to serve as digital outputs.   Since the relays used negative logic, i.e. a logic 0 activates the relay, the LEDs on the circuit were set up to also use negative logic so that the same GPIO on the Teensy board activates both an LED and a relay.   The LEDs on the circuit protoboard will help in debugging any issues with the relays and give a reminder that the code is running properly.  For this project, pins 13-22 were used for LED/relay control.  (See the board pinout chart here).  Pin 13 also controls an LED directly on the Teensy board– so we have yet another ‘heartbeat’ to remind that the code is running.

The speaker or headphones are connected to one of the PWM-capable outputs of the Teensy.  PWM stands for Pulse-Width Modulation and basically is an output signal that looks like a square wave.  The Arduino IDE tone() function used in the software permits generating a square wave of variable frequency.  When this square wave is applied to a speaker, you get a sound of that frequency!  For this project, PWM-capable pin 23 was chosen for sound output.

The diagram below shows how the Teensy protoboard circuit was wired up.


Teensy Circuit Protoboard

Appropriate valued resistors must be chosen for the above circuit depending on the LEDs and speaker used to limit the current through each.   For my circuit, I used 1.3k ohm for the LEDs and 100 ohm for each of the 2 ‘channels’ of the headphone jack.  2 different resistors were connected to each of 2 sides of the jack in order to get stereo sound from the computer speakers.

The top of the circuit diagram above shows the 12 wires that will serve to control the relays:

  • 10x digital logic control of each relay (white)
  • vdd / power (red) (in actuality 2 of these were needed to power the 2 relay boards)
  • ground (black) (in actuality 2 of these were needed to ground the 2 relay boards)

Here is the actual board used:


Shown with computer speaker wire connected

The board is powered by a standard micro USB cable connected to a USB AC adapter plugged into the ‘always on’ AC outlet.

Next, we hook up the board to a simple set of computer speakers to make sure it works:

(In this video you can still see the black speakers we were initially going to use attached to the side of the cooler, but we decided against them at the last minute.  Computer speakers worked out just fine.)

The next step was to hook it up to the relay boards!  The 14 wires had to be ported through a hole in the clear protective guard then through one of the clamp connector holes in the metal junction box.


Control wires

Once the control wires are in the metal box, they had to be carefully attached to the control, supply and ground pins of the 2 relay boards.


We have control!

Now that everything is wired up, it’s time to run some music and lights!

Here in the Freescale MakeIt Lab we ran a full light test:

(Not shown:  One of the ‘songs’ added was a music scale which was a perfect was to test out the lights!)

Stay Tuned for Part3, where we review the software and how easy it is for kids to program it!


Teensy Music!

Finally got Music and Lights program previously working on Arduino Uno ported over to the Teensy 3.1!

(Did this the night before the SuperBowl, so had the NFL on the brain).

The Teensy supports an add-on to the Arduino IDE (software editor) called Teensyduino.  This permits taking the same Arduino sketches and using them on the Teensy, in theory with no modifications!

Well in reality I did have to make a few updates to the code to get the same Arduino code to work on the Teensy, but they were generally very minor.  Here are the 3 changes I had to make:

1. Rename pins.

Pins used for LEDs and the Audio speaker output were renamed.  This just because the LEDs and speaker were simply hooked up to different pins than they were in the Arduino Uno incarnation.

int leds[] = {22,21,20,19,18,17,16,15,14,13,12,11}; // WRITE HERE WHICH PIN THE LEDS ARE CONNECTED TO: 1, 2, 3, 4, 5 ....

Here is the pinout diagram showing the location of the pins for reference.  The LEDs can be connected to any of the digital pins (in gray, 0 to 23).  The speaker is driven by a PWM output (the ones in the darker pink).

2. Rename note #defines.

#define statements used to represent the musical note values had to be updated as there were conflicts with additional reserved terms in the Teensy core library.  The code already had to account for an Arduino conflict for the A notes, e.g. “A5” had to be renamed “AA5” as “A5” is reserved for port A, pin 5.   But the Teensyduino library also found conflict with C5, B5, etc. so all notes were given the new nomenclature, AAx (Note A), Abx (Note A flat), BBx (Note B), Bbx (Note B flat), etc.  This was an easy rename and maintains backward compatibility if I want to run it again on Arduino.

3. Turn off tone between notes.

The play_note function in my code had to be modified to turn off the tone() applied to a particular pin between notes.  The Arduino Uno library appeared to handle playing tones back to back of different frequencies no problem.  The Teensy required the addition of a noTone() call between notes for the songs to play back reliably.   To debug this I had the program do a musical scale to see what notes were having problems and it seemed to be randomly dependent on how many notes were in a song as to whether a note was played or not.  Definitely signing up on the Teensy forum ( was very helpful to resolving this problem.  I also got a hint from the Arduino tone reference documentation on using noTone().  Again, this is backward compatible to running on the Arduino.

Overall I was pretty happy with how I was able to start using the Teensy almost right away with an existing Arduino sketch!

Here is the Sketch if you’re interested to try it:  

Must do more!



The Teeny, Tiny, Teensy

It’s Teeny!

It’s Tiny!

It’s a Teensy!


Speaking of little things making a big difference, I just got this little board called “Teensy” to try things on.

Here are some basic specs as compared to some popular Arduino models:

BoardTeensy 3.1Arduino Uno R3Arduino MicroArduino Nano 3.xArduino Due
ProcessorFreescale K20 (Kinetis)
Cortex-M4 (ARM) 72MHz
Atmel ATmega328
Atmel ATmega32u4
Atmel ATmega328
Atmel SAM3X8E (ARM)
Digital I/O34
(12 PWM)
3.3V (5V tolerant!)
(6 PWM)
(7 PWM)
(6 PWM)
(12 PWM)
Analog In
21 (13-bit)6 (10-bit)12 (10-bit)8 (10-bit)12 (12-bit)
Analog Out
1 (12-bit)0002

With its 256kB of Flash, 64kB of RAM, and 72MHz processor, it fits between an Arduino Uno and Arduino Due in capabilities, although closer to the Arduino Due, which has 512kB of RAM.  It is roughly the cost of the Arduino Uno, with almost all the capabilities of the Arduino Due!

And all that in a roughly 13mm x 36mm size board!

I see great things coming…

Yummy, yummy!

“Even better, longer lasting”

(Above: 5 Teensies in a box of gum.  Wonder what I’ll do with them??)