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
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
-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
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
Step 5: Testing Prototype
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
Step 7: Time for Soldering
Step 8: Finished Circuit
Step 9: Making It Sturdy
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