Merry Christmas y'all!!! I know it's rare that I make an 'ible, but I'll be doing a Christmas instructable for you all today. For a while I've been wanting to make a computer controlled Christmas light setup but didn't have the parts to build one or the money to buy one, but not no more. I finally had enough loose parts to combine and make this one circuit which was a 5 channel XMas Lite Controller. Nothing special here, just a few relays that are triggered by transistors, the usual stuff. What is really probably the main feature here is the software that I wrote to control the arduino that controls the lights. Another keypoint is that this was designed to be as easy to build as possible (it had to be, otherwise I wouldn't have made it :p). But before you move onto the next step, keep in mind that this project is for people who have at least basic knowledge of electronics and the theory behind it as you will be working with mains current (power from the wall) which could potentially be dangerous if you don't at least know what you're doing in theory. If you got all that, then carry on to the next step :)
Also I am entering this into the Make It Glow contest, so be sure to vote for me :D
Step 1: Prerequisites
Ok, because it's me, I made sure this instructable would be difficult for you ordinary mortals to follow... just kidding :p. It actually might be hard though because all of this was designed to work with the Linux operating system, Ubuntu in particular, and most people use Windows or Mac. In theory the main software could be modified to support both of these platforms, but I'm not gonna do it seeing as it already works for me and since I'm giving the software for free, so take it or leave it.
But yeah, all that aside, you will probably need:
-A computer with Linux, preferably a laptop running Ubuntu
-Computer has to have Python 2.x (not 3.x) installed along with pygame and python-serial (which you could get by typing "sudo apt-get install pygame python-serial" into terminal)
-An arduino microcontroller, in my case it was an Arduino Mega 2560
-A Christmas tree with lights on it
-Knowledge of electronics and basic soldering skills
-Access to listed parts
-A lot of alligator clips (I know it's not professional, but I know the how electricity works in theory and I ran a lot of tests to make sure this was safe for the particular conditions I was running with)
-A few more things I can't think of right now :p
Step 2: Materials in Detail
Ok, so if you want to be specific about the things you need for the hardware side, here's the list:
-An arduino, can be any but I used Mega
-A cable to interface the arduino with a computer
-About (or exactly) 15 alligator clips
-5 relays capable of switching a load of at least 0.5 amps at 120 volts but can be powered by 12 volts or less
-5 2N3904 NPN switching transistors
-5 4.7K Ohm resistors
-A 9 to 12 volt wall adapter that you can modify
-A power cord that came from an old appliance or something
-Perfboard big enough to build this circuit onto, or breadboard if you really want
-Some wire to make all the required connections
Step 3: Theory
Because I'm me, I did a lot of work in theory before I ever even tried to build or program anything for this project. I did use a multimeter to find out how much current runs though a generic string of lights and found out that mine were drawing around 0.4 amperes, keep in mind, your measurements may be different. My relays were rated for 3 amps at 230 volts so I figured that they could easily take 0.4 amps at 120 volts. Rule of thumb: have your relays rated to take at least double the amount of current you intend to put through them, this should stick with most of your other components also. The resistance of the coil was on average 690 ohms.
I then did research on the 2N3904 transistors. I found out that the base could switch with a maximum signal current of 50 mA and the emitter-collector current would work at a max of 200 mA. Physics to the rescue! My signal for the transistors would be 5 volts from the arduino and current * ohms = volts so this could be turned into ohms = volts / current. This would say that I need at least 100 ohms to switch a current of 0.05 amps at 5 volts, but higher ohms means less current which is still ok until a certain point. I used a 4.7k ohm resistor because that's all I had on me in excess and it still gave enough current to switch the transistor.
To make sure the transistor could handle switching the relay, I applied similar logic. I'm going to be using 9 volts to switch the relays (even though they are rated for 12 volts, mine were still able to switch at 9 volts) and I had to make sure the current going through the transistor would not exceed 200 mA. So with current * ohms = volts, I rewrote it to current = volts / ohms. So 9 volts / 690 ohms = 0.013 amps or 13 mA, well within the safe zone.
Then to see the greatest current that would potentially be handled at any one time by the circuit, I got the value of 0.4 amps for one string of lights and multiplied it by 5 for the number of channels that could be on at any one time to get a value of 2 amps which is kinda low. But all this calculation is just to make sure that everything works in theory and that nothing would catch on fire or gets fried... imagine the theory it took to plan the building of the rocket that would take people to the moon :p
Step 4: Circuit and Prototype
So to make sure every thing would work in real life, I built only one channel of the circuit on a tiny breadboard. The circuit schematic is also given, nothing special, just one transistor, one resistor and one relay per channel... pretty cheap and simplistic.
Step 5: Testing Prototype
Since theory and circuit design was out of the way, I decided it was time to actually test it. So I just connected the 9 volt power to the circuit, connected the light to a wall outlet but connected the relay in a way so it would act like an interrupt switch. Then I connected the arduino using pin 40 for the signal pin and I linked the arduino's ground with the circuit's ground so that the signal would actually be registered in the switching circuit. I connected the arduino to my Ubuntu laptop and ran a simple little blink code to turn the light on and off. I used this steampunk cage lamp I had because it also drew 0.4 amps and therefore was an equal load to that of my Christmas lights. The code also showed the signal status on the built-in LED on pin 13 and went something like this:
As you can see, very simple proof-of-concept test code, it turns the relay on for one second and off for two seconds over and over. I attached a video that gives a demonstration of the test. The switch is there for safety but the relay bypasses it to turn the light on and off.
Step 6: Begin Construction
So I figured I should lay out the component on the perfboard in a way that would make it easiest to build with minimal bridging and stuff. So here is just a picture of how I arranged the components, pretty compact as you can see.
Step 7: Time for Soldering
Well, now that the components are laid out, I can begin soldering and making my design permanent. And yes, I did test all the components I used before I soldered them together. Not much to talk about here, but I do have a helping hands station which is pretty handy (mind the pun :p) and I recommend you to buy one if you do soldering projects and do not already have one. Also the second picture is so dark because I did the soldering at night but outside since my parents aren't too fond of me making solder smoke in my room :p
Step 8: Finished Circuit
So after a bit of soldering, here is the finished circuit that came out. Doesn't look perfect (especially not with the chipped corner :p) but it works and all the connections are there. As you can see, on the bottom I used a straightened out paper clip and little pieces of paper clips to make the connections for the 120 volts part. I figured paper clips were thick and rugged enough to withstand about 2 amps or more, but man, are they difficult to solder or what :p Also I used the clipped off leads from the components to make the other connections between parts since I prefer that to trailing out a path with solder because it wastes a lot and doesn't look as nice. After building it, I tested each channel individually to make sure all 5 were working, especially since the last time I tried to solder transistors, they didn't live to tell the tale :p But fortunately, all of the channels worked and I didn't need further modification or replacement of components.
Step 9: Making It Sturdy
I noticed a few areas were rotating, and that's not something you want as a solder joint could crack or a lead could snap off. Luckily, I consider it good practice to cover fragile areas in hot glue, especially after my beginning solder projects would break so easily. So I just borrowed my sister's pink (or purple?) hot glue gun and added hot glue to pretty much the whole bottom surface, taking great care with the areas that were not supposed to move. I just had to make sure not to cover the contact leads or else I wouldn't be able to connect parts of the circuit :p
Step 10: Finishing Off
So I wired up the whole circuit using the 15 or so alligator clips. Also, since all of the stuff was in my room, I decided to literally stuff it in a box to make it just a bit more portable between my room and the living room. I had to make sure there were no extra contact points or short circuits anywhere because that could cause a fire or (worst of all) burn out my arduino and/or laptop. Then to connect it to the Christmas lights, I arranged them in that weird pattern as seen in the second image so that there would be one continuous connection to neutral while each string of lights could have an individual "hot" connection so it could be triggered separately. The black alligator clip is the common connection, the other colors are the individual connections that get triggered separately. I only had 3 strings of light, so I wasn't able to use all 5 channels, but it was enough to test out the circuit and the software written for it.
Step 11: Software
The software is where the main focus of this project really is. It was developed by the K-Labs Software Development Unit (me in other words). I probably spent more time writing and perfecting the controller program than I did actually working on designing and building the hardware. Since this project was somewhat started late (which is why you shouldn't work on a project only a few days before the deadline), I only started programming on the 21st of December, just 3 days before Christmas eve. It took me two whole days of programming and half a day of debugging and refining the code, but there might still be a bug that has evaded my detection (happens when you are the programmer and the alpha and beta tester). Also, unfortunately for many of you, this program was designed to work for Ubuntu Linux. I didn't really spend any time working on this program for other platforms because any of the computers I use have Ubuntu on them, but in theory it would only take a few minor tweaks to get it to work on other platforms. Also, this program was kinda designed to have a similar interface to Audacity, similar color scheme and the timing sequence thing.
To get this program to work, you need to have python installed, most likely python 2.7 because that is what I used when I programmed this. Also you need two extra python packages, one for the graphical interface and one for the serial communication with the arduino. To install these packages, you just open up terminal and type in "sudo apt-get install python-serial pygame -y" but without the quotes. Then this program should work after those two packages are installed. To run it, you can't simply just double click it because it will open without root privileges and the serial communication needs root permissions. So just open up terminal, navigate to the directory using the cd command, and type "sudo python xmascontr.py" and the program will open up. But be sure the arduino is connected before you open up the program otherwise it will not work as the program only checks for the arduino at startup.
To know if it is ready to be used, just look to the upper right corner and in my picture you will see a little green circle. This is the indicator light, green means good to go. If you get a red circle instead, that means that either it cannot find the right port or the arduino is disconnected. If you get a red circle and the arduino is connected, then you need to open up the program in a text editor and change the variable after all the commented lines to the path of your arduino, in my case any Ubuntu computer I have tried always sets my arduino to /dev/ttyACM0 so I left that as the port. To find what port your arduino is on, either find it with the arduino IDE or plug it in and type "dmesg | tail" into terminal and try to find the path there. Now, if you get a yellow circle when you open up the program, it means that it detects the arduino but it does not have sufficient permission to communicate with it, that is why we had to open it with the sudo command, so you need to know the admin password.
Once you get the program started up with a green circle, it is time to generate sequences. I made it as easy as possible, all you have to do is hold down the left mouse button where you want the light to turn on and drag it to where you want the light to turn off. The blue-ish squares in the picture are where the on sections are. To delete one of the blue sections, just right click it and it will disappear. Scrolling will move the sequence horizontally to add more things, or if you hit the right edge while dragging a section, it will auto-scroll for you. Using the home and end keys will skip you to either the beginning or end of the entire sequence. Your mouse cursor will be followed by a little dialog that tells you at what time your mouse is pointing at but only when it is in one of the 5 tracks, not the top menu part.
The play button will start playing your track but if the circle is not green, nothing will be written out. The pause button doesn't do anything... yet. The stop button will stop a track from playing. The save button will save your sequence to a file in the current active directory under the name of "xmaslite.xmas" but be warned that it will overwrite an xmaslite.xmas file that is already there. The open button will open an xmaslite.xmas file that it finds but if you try to open a corrupt or false xmaslite.xmas file, it will erase the current sequence and print "Bad xmaslite.xmas file". The reset button just resets your sequence to be empty so you can start over. The debug button isn't really important but it displays the sequence in terminal if you click it, it's just there because I used it to fix this really weird glitch but could be useful if you are having problems also.
I have attached a test xmaslite.xmas file so you can test the program, just add it into the same directory, press open and you should see a few blue squares appear on the screen.
I have added a xmaslights.ino file to the mix. This is an arduino file, you must upload this to your arduino if you want the arduino to execute the commands issued by the sequence program. This file is programmed to make your arduino output the on-off signals on pins 40, 41, 42, 43, 44 as the 5 pins for each channel.
All in all, not bad for less than 200 total lines of code written in 2.5 days. As usual, I'm letting anyone use and modify this software but credit has to be given to me for the original program if it is to be redistributed.
Step 12: Finished Product
Here is a video of the finished project hooked up to my Christmas tree. It is running the sequence shown in the picture on the previous step. I hope you folks enjoyed my instructable. Don't forget to vote for me on the Make It Glow contest. Merry Christmas to all and to all a good night (or day) :p