Introduction: Programme a String of Xmas Lights to Blink Morse Code With Arduino & Snap Circuits

About: Technology is subversive.

This is a project my son Dylan (9) and I put together using an Arduino Circuit board, a Snap Circuit set, and a string of Christmas lights.

What you need:

An Arduino: An Arduino is a computer on a single circuit board that you can programme with a looping routine from a Windows, Mac, or Linux computer connected via USB. The unit can then run independently with a 5-12 volt power source -- USB battery packs work well. It's a great little gizmo for hobbyists and for learning the basics of programming.

Snap Circuits: Your basic electronics learning/experimentation kit, built around components that snap together. Snap Circuits are a super way to learn basic electronics.

Low-voltage Christmas Tree lights: Since you'll need to slice a wire in your string of lights, you need the kind of lights that run on low voltage from a transformer or wall wart. Do NOT mess around with mains power circuits!!!!!!!!!

Optional, but handy: Pin-to-snap wires available from the good folks at Elenco allow you to use any of the components from your Snap Circuits kit as output or input components for the Arduino, and make integrating the board with Snap Circuit projects a (heh, heh) snap.

The task of making an LED light up and go off is a really basic Arduino routine. To make it control a Christmas Tree, we decided to use an electromagnetic relay from our Snap Circuits kit. A relay can isolate two circuits, so the (in our case) 12 volt christmas tree lights wouldn't fry the delicate six volt snap circuit components. I don't know what the limit is on the Q2 transistor included in the kit, but given that it normally only handles loads of up to 6volts from 4 AA batteries, probably better to keep those circuits separate.

We originally planned to fire the Snap Circuit components with the Arduino's five volt output, but I wasn't sure enough of my own electronics knowledge not to make a mistake and send a voltage spike or a reversed polarity current into our favorite kit and toast it: we want to use this puppy for other projects! That's when we hit on the idea of using a photosensor from the Snap Circuits. By placing the LED from the Arduino into the barrel of the photosensor, we could control the relay circuit with light rather than electricity! This meant the Arduino was in a circuit all its own with just an LED, and completely safe.

The process of building this project involves three steps.

Step 1: Programme the Arduino

We simply took the "Blink" routine, one of the examples bundled with every Arduino, and modified it to send morse code by defining a subroutine called dot() and another called dash(). A Dash in morse code is 3 times longer than a dot. We turned the light on for 200 miliseconds to make a dot, 600 to make a dash, with a delay of 1 second in between characters and 3 seconds in between words. As mentioned in the Video above, however, we wanted to attache the xmas lights to the "NORMALLY ON" switch on the relay, which meant our morse was being transmitted by the dark pauses rather than the light flashes. Dylan's brilliant solution to this electronics engineering problem was to treat it as a programming problem: we just reversed the Dot() and Dash() routines to turn the LED OFF instead of on to send the signal. The full code is in the box below, it's public domain you can take it and modify as you like. There are also plenty of Morse Code libraries for the Arduino that you can Google and grab: The code below was written for learning, not for efficiency, and advanced users can certainly make improvements.

/*
  Merry Christmas in Morse Code Blink
  This example code is in the public domain.
*/

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);    
}

// the loop routine runs over and over again forever:
void loop() {
//M in morse code is two dashes. We define the dot and dash subroutines below.
dash();
dash();
space();
//E
dot();
space();
//R
dot();
dash();
dot();
space();
//R
dot();
dash();
dot();
space();
//Y
dash();
dot();
dash();
dash();
space();
//Space
wordspace();
//C
dash();
dot();
dash();
dot();
space();
//H
dot();
dot();
dot();
dot();
space();
//R
dot();
dash();
dot();
space();
//I
dot();
dot();
space();
//S
dot();
dot();
dot();
space();
//T
dash();
space();
//M
dash();
dash();
space();
//A
dot();
dash();
space();
//S
dot();
dot();
dot();
space();
wordspace();



}

void dot() {
  digitalWrite(led, LOW);   // turn the LED off (LOW is the voltage level)
  delay(200);               // keep the light off for 200 ms (dot)
  digitalWrite(led, HIGH);    // turn the LED on by making the voltage HIGH
  delay(600);               //pause between characters


}

void dash() {
  digitalWrite(led, LOW);   // turn the LED off
  delay(600);               // keep the light off for 600 ms
  digitalWrite(led, HIGH);    // turn the LED on by making the voltage high
  delay(600);

  }

void space(){             
  digitalWrite(led, HIGH); //Make sure the LED is on
  delay(1000);              //For one second to mark space between characters
}

void wordspace(){             
digitalWrite(led, HIGH); //make sure the LED is on
  delay(3000);           //for 3 seconds to mark space between words
}

Step 2: Set Up the Snap Circuit Board

This is based on a pretty straightforward Snap Circuits project. Replicate the circuit pictured above with two battery packs, the photosensor, the switch component, a 1K ohm resistor, the Q2 NPN transistor, the D3 diode,  and the S3 relay.  

You put the LED attached to the Arduino into the barrel of the photosensor (fits nicely). When the light fires, the photosensor's resistance drops, putting a current into the base of the NPN transistor. This opens the circuit across the collector and emitter, firing the relay, and turning off the xmas tree lights. When the light goes off, the resistance of the photosensor rises, cutting off the current to the transistor. The relay returns to its normal state, opening the path between the christmas tree lights. The diode you see there (yep, with the positive side hooked up to the negative side of the circuit) is to protect the transistor from stray charges that can build up from the relay firing -- it just shunts them back in a short circuit to the relay coil.

Step 3: Wire the Christmas Tree Lights In

With the lights *unplugged,* we simply clipped one wire of the Christmas Tree lights, breaking the circuit, and attached alligator clips to the ends of the wires. Those clips were then attached to the "Normally On" poles of the Snap Circuit relay, as indicated above. This completes the circuit when the relay is in the resting state, and the christmas lights are "On." When the LED fires, the Relay switches over, cutting the current flow across the "normally on" poles and shutting off the lights. So when the LED is ON, the lights are OFF. That's why if you look closely at the code that loops in the Arduino, the morse code signal is encoded in commands that turn the LED off rather than on. The normal, resting state of the xmas lights is on: the morse signal is therefore encoded in the negative, or dark pauses of the LED so the xmas lights blink inverse to the LED. 

Again: Our lights are low-voltage (12 volts DC) DON'T DO THIS unless you know you're dealing with a low voltage circuit. Alligator clips to the Snap Circuit Relay poles is an open, uninsulated, bare wire connection which a child or a cat might touch. You may end up doing what my brother calls the Dance of the Sugar Plum Fairy if you mess around with a higher voltage line and create a path to ground with your highly conductive body. And if you mess around with a mains circuit, you could seriously hurt yourself. Be smart. Be safe.

123D Circuits Contest

Participated in the
123D Circuits Contest