This winter my girlfriend had a lot of trouble waking up in the morning and seemed to be suffering from SAD (Seasonal Affective Disorder). I even notice how much harder it is to wake up in the winter since the sun hasn't come up yet. Symptoms of severe SAD can include irritability, oversleeping but still being tired, not being able to get out of bed, depression, and even some physical problems such as joint pain and lowered resistance to infection. I had heard about sunrise alarms that simulated the sunrise and figured it could be a possible solution to her problem.
I remembered seeing an instructable (https://www.instructables.com/id/Blue-LED-dawn-simulator-for-Soleil-Sun-Alarm/) about modifying a sunrise alarm to provide blue light using LEDs since blue is supposed to be the good light to help out. I liked the idea but the way the microcontroller is used in this instructable intimidated me since I've had limited experience with programming one after the code is developed. It also did not solve the other concern of mine: spending $80 on an alarm clock and modifying it, not that my girlfriend isn't worth it :D
I first thought about developing a clock from scratch using a microcontroller. We built a binary counting clock in one of my college classes, so I was familiar with the logic. I later gave up on that idea since I wouldn't be using the same programming language and I would take a lot of time to develope the code. I then had the idea to use a cheap digital alarm clock that could, hopefully, provide a voltage when the alarm went off. I could take this voltage and use it as a switch with a microcontroller. When the alarm went off and the voltage went high, the dimming process would begin. If the snooze button was hit, or the alarm was turned off, the voltage would go low and the dimming process would stop, turning the lights off. I researched this idea and found out that it was possible to use a voltage from a clock and use it with a microcontroller! A guy had completed a similar project that opened his blinds automatically in the morning (http://hackaday.com/2008/11/18/alarm-clock-automated-blinds/).
The ideas started flowing and all I had to do was pick a microcontroller to use. I saw an article on sparkfun.com that went through the process of building a circuit to run an ATMega168. I read carefully and decided that it seemed simple enough and that it was the microcontroller I wanted to use. Upon further research, I found this Arduino thing everyone has been using for their DIY projects. It used the ATMega168, was open source, and had numerous help forums and starting examples; perfect for the beginner. I decided to use it to program my ATMega168 and transplant it into a breakout board that contained the bare essentials for the ATMega168 to run. With the last piece of the puzzle in hand, I could start.
Quick Side Note: Before I start, I just want to say thanks to all of the sources I used. I tried to make sure I linked any reference I used within the instructable. The code is just manipulations of the examples included within the Arduino environment and a bit of my own so thanks to the people who coded those! Also, this is my first microcontroller project. I'm sure I didn't do everything completely correct, such as adding filter caps to places and other various parts to my circuits. If you see something that can be improved, let me know! I'll be sure to update or make notes of it. Enjoy!
Step 1: Probing the Clock and Alarm Output Circuit
Probing the Clock
This is the clock I chose. I got it at Walmart and it was cheap so if I wasn't able to use it, I wouldn't be too upset. It also has a 9v battery backup in case the power goes out. I later found out that the alarm sequence from the ATMega168 still goes off! So it will still wake you up if there is no power! When it's running off of battery power the front display turns off and it switches to a different internal clock that is less accurate but still works fine. When power is reconnected the clock may have to be adjusted but the alarm settings will stay.
The clock comes apart fairly easily. There are four screws on the bottom and three screws that keep the button PCB board fastened to the top of the clock case. To take the top off and get better access to the LCD, you need to thread the 9v clip through the hole in the bottom half. The front LCD pops out and upon inspection, there were vary few parts. I found a transformer, a piezoelectric speaker for the alarm, some diodes for the rectifier circuit, some buttons for the inputs, and a clock display that seemed to have all of the clock circuitry underneath it. I found the ground and started probing away. BE CAREFUL WHILE DOING THIS ON YOUR CLOCK, THERE'S AN EXPOSED TRANSFORMER THAT WILL DELIVER A HEFTY SHOCK. I took note of the voltages on each pin when the alarm was off and when the alarm was on. I was hoping for a pin that provided a nice 5v logic voltage when the alarm was on and 0v when the alarm was off. I wasn't that lucky but the voltage that went to the speaker provided a voltage that varied from 9.5v-12.5v. I figured I could use this. I also found a pin labeled VCC that provided a voltage that varied from 10v-12v. This comes into play later when building the power supply for the microcontroller.
Alarm Output Circuit
I soldered a wire to ground and one to the alarm pin and started working on a circuit to steady out the voltage. I figured I could use a 5v regulator but I only had an adjustable regulator laying around. I did some math and my values provided a voltage slightly below 5v. I tinkered a bit and swapped resisters until it provided the 5v I needed. I used a 470uF capacitor on the input to smooth out the voltage. With the capacitor, the voltage only varied from 10.5v-10v. Below is the schematic of the circuit I used to condition my alarm output and a picture of the parts together on a breadboard.
Step 2: Power Supply Circuit, LED Driver Circuit, and Wiring
Power Supply Circuit
If I were to hook the microcontroller straight up to the Vcc of the clock, I would blow it up (well not really, but render it useless). I needed to condition the voltage and bring it down to 5v. I used a simple regulator circuit that uses just two capacitors and a 5v regulator. I went to the schools lab and located a 5v regulator in the trash pile. I hooked up the circuit and tested it out. It provided a nice and steady 4.99v.
LED Driver Circuit
Since the ATMega168 can only deliver about 16mA of current to each of its digital outputs, a current regulator is needed to power the LEDs. I found this circuit on the Arduino help forums and it seems like a fairly common and simple circuit. To direct the light of the LEDs, I decided to use a reflector from a flashlight. The flashlight I bought had three holes for three LEDs. I decided to grind them bigger and put four in each hole, thus explaining the way the circuit is drawn.
Once I figured out I could successfully use the clock's Vcc and the the alarm output, I decided to solder up some thin wires and thread them out through a hole in the side. I also had the idea to add a loop in my microcontroller program to play a song instead of the original alarm. I soldered two longer wires to the piezoelectric speaker and threaded those out the side as well. I used some wire clippers to cut out a small notch in the top half of the clock and screwed everything back together.
Step 3: Hooking Up the ATMega168 and Building the Prototype
Hooking up the ATMega168
There are only a few pins that need to be connected for the ATMega168 to run. I found this pinout of the ATMega168 at http://www.moderndevice.com/Docs/RBBB_Instructions_05.pdf connections are as follows:
-Pin 1 to Vcc with a 10k resistor.
-Pin 7 and Pin 20 to Vcc
-Pin 8 and Pin 22 to Ground
-Pin 21 to Ground with a .1uF electrolytic capacitor
-Pin 4 (Digital pin 2) is connected to my alarm wire
-Pin 15 to NEGATIVE Lead of piezoelectric speaker
-Pin 16 to the input of the LED driver circuit
-16Mhz Crystal - One leg to Pin 9 the other leg to Pin 10
--11 Connections in all--
Note: I believe I could have hooked up some caps to the legs of the crystal but since my program does not need a highly accurate clock, I left it as is. I used the alarm's input digital pin at random, any other digital pin should work. The piezoelectric speaker and LEDs must be connected to a Digital PWM Pin or they will not work. Also, I couldn't find a good model in Eagle for the 28 pin model so I just MS painted it all together :D Sorry if it looks confusing. Ask questions if you need to! I made a block diagram as well to help with understanding where everything goes or comes from.
Building the Prototype
Alarm Output Circuit
-LM317T Adjustable Positive Voltage Regulator (You could use a 5v regulator, i just had this one)
-1k Ohm Resistor
-3.8k Ohm Resistor
-470uF Electrolytic Capacitor
-UA7805C 5v Regulator
-100uF Electrolytic Capacitor
-10uF Electrolytic Capacitor
LED Driver Circuit
-150 Ohm (You can experiment with lower or higher resistor values depending on your LEDs)
-1k Ohm Resistor
-28 Pin Socket (Optional but I reprogrammed my ATMega168 numerous times with my Arduino)
-.1uF Electrolytic Capacitor
-16 MHz Crystal
-10k Ohm Resistor
-Prototyping Perf Board
-Prototyping Board Legs and Screws
When I prototyped my circuit I built each section on a breadboard, tested it, and transferred it to the perf board. I started with the alarm output circuit and made sure it worked correctly. I then moved on to the power supply portion, then the LED driver, and finished with the microcontroller circuit. But, seeing as you don't have to test the circuit and make sure the concepts work, since I've already done that, you can just build the whole circuit. Make sure you get the right voltages at the right places. 0v at the output of the alarm output circuit when the alarm is off and 5v when it's on. 5v at the output of the power supply circuit. Don't stick the ATMega168 in the socket just yet, it needs to be programmed.
I could have used a smaller perf board or cut mine down but I decided to leave it alone. It's not extremely large. After the circuit has been prototyped, the construction of the LED bulb can start.
Step 4: Building the LED "Bulb"
The Triple Quad LED Bulb!!!!"'
If you want, you can skip this step and use a single LED to test your circuit. You can come back to this when you have the circuit confirmed and working. Also, I used white LEDs because I did not have any more high brightness blues left. I've heard blue helps better with the SAD.
I went to the dollar store to pick up a cheap flashlight because I needed a reflector to direct the LEDs light. The flashlight I purchased contained three LEDs. I decided to Stuff four LEDs into each hole and I needed a way to wire them all up. I came up with this process that solders four LEDs together and then connect three of these "quad LEDs" together. All LEDs are in parallel, keeping the voltage the same as one LED and raises the current. This is what the LED driver circuit provides.
Protip: Small needle nose pliers help
Step1: Hold two LEDs together with the ground leads touching. The flat edges of the LEDs should sit next to each other. Load up the tip of your soldering iron with some solder so there is a liquid solder drop on the tip. Quickly touch the two ground leads with your soldering iron as close to the LED as you can. If you leave the tip on there long, the leads will heat up and it won't feel to great.
Step2: Using a dremel tool, file, or course sand paper, sand the edges of one side of a pair flat so they will sit next to another pair flush. I sanded the LEDs to help diffuse the light a little bit. Now, bend the leads as shown. Kinda of hard to take pictures of the process but basically bend the positive leads outward. Bend the negative leads towards the flattened sides and the straight up so that when you put two pairs together, the four negative leads come all together as one big lead. Taking two pairs, hold them together. The negative pins will all be in the center. Touch them with your soldering iron to fuse them all together.
Step3: Now that the four negative leads are soldered together, clip three of them, leaving just one. Now, bend one of the positive leads around the outside of the quad LED, soldering at each connection. Clip all but one of the positive leads leaving one positive and one negative lead.
You're done! Now make two more :]
Once you have three quad LEDs, it's time to fit them into the flashlight reflector. I bought this flashlight for $3 at the dollar store. It's a dorcey and all of the parts twist apart, so it's easy to access all the parts. I use the silver reflector and the black cone back. The black cone can be stripped of it's metal parts leaving just the plastic piece. It's used later to fasten the bulb to the adjustable neck. Depending on the flashlight you find, you may have to fit your LEDs on the adjustable neck differently. I tried to find a generic flashlight that would be available in a lot of place.
Step4: I used a dremel took to widen the three holes in the reflector. I then pushed each of the four quad LEDs into their holes with the negative leads towards the inside. Bend and solder the negative and positive leads together completing the TRIPLE QUAD LED BULB!
I then soldered on two long, thin wires that will later be fed down the adjustable neck and soldered to the main circuit board. I also put some glue on each quad LED package to make sure they would stay in place.
Step 5: Adjustable Neck and the Base
The Adjustable Neck
In order to direct the "sunlight" that the alarm clock generates, I chose to add an adjustable neck. At first I thought I could use conduit for the neck but since I have limited tooling and hardware at college, I couldn't secure it to the base very well. Plus, it was pretty tough to bend and did not adjust too well. I ended up using just one of the wires inside of the conduit. It turned out pretty good. I was able to fasten it with no hardware, just a hole in the base.
I started off by taking one wire out of the conduit and wrapping it around the outside, creating a nice spiral. Then I twisted just the wire off the conduit. I then stretched it out and connected it to the black cone I mentioned earlier. The black cone comes with some circuitry from the flashlight attached to it but it's easily removed. Now that you have just the plastic cone piece, make two holes on the edges, each big enough for the wire to fit through. I fed it up and then down and out the other side, curling it under. I then used the thin, flexible wire from the conduit to further secure it. The two long wires that were soldered earlier can be fed through the black cone back and the bulb can be twisted in place. I added a bit of glue to keep it attached.
To attach the adjustable neck, I drilled a 7/64 inch hole in the wooden base and stuck the wire in. It fits pretty snugly so no glue is necessary but it's loose enough so the the neck be turned and twisted. The two LED wires can be wrapped around the neck and soldered to the prototyping board.
To attach the board, I used four PCB mounts. I had a threading drill bit available, but it wasn't necessary. If you don't have a threading bit, just drill a hole that is smaller than the screw and twist it in with some pliers. I attached the clock to the base using some velcro. I didn't screw it down since my clock has a battery backup and when the battery dies, it will need to replaced. Lastly, I added some rubber feet to the corners.
Step 6: The Program
In order to program your ATMega168 with the USB connection and Arduino board, you will need an ATMega168 chip that has the Arduino bootloader already on it. This was the simplest way, that I could find, to program the microcontroller. When I purchased my board, I picked up an extra ATMega168 with the bootloader from the same supplier. You may have to pay a bit more for the pre-programmed chip, but it was worth it to me since I did not want to mess with serial cable adapters etc.
I've attached the code as a .txt file and a .pde file. I didn't want to make this instructable long by posting all the code. I used the latest Arduino programing environment: arduino-0015. What I love about the Arduino boards is that there are tons of examples included with the environment, the program environment is free, and there are some many project and help pages out there. It's also super easy to build a breakout board to run your program on it's own. I tried to comment the code to the best of my understanding so I'll keep the descriptions to a minimum.
I used the "Fading LED" example by BARRAGAN to become familiar with the pulse width modulation (PWM) the ATMega168 is capable of. I have three "if" statements. The first faded in the lower dimness levels (0-75 out of 255) slower since the higher levels look the same. The second fades in the upper dimness levels faster. The whole fade in process takes 15 minutes. Once the LEDs have reached full brightness, the song loop will play until the alarm is shut off.
The original alarm was fairly annoying. It was just the typical alarm clock sound that everyone hates. I thought, why not use the speaker to make a pleasant song to wake up to? Since my girlfriend loves The Beatles and I knew Hey Jude has a fairly simple melody, I decide to use it. A square wave is generated and then PWM is used to play the notes of Hey Jude on the piezoelectric speaker. To program the song, I manipulated the "Melody" example from the Arduino environment examples. I found some simple sheet music and translated that into notes in the code. I had to increase the number of notes that are played to 41 and do the math to figure out a lower note than the 'c' that is provided. I then implanted that code into my main code.
To program the chip, you will first need to install the USB drivers provided with the Arduino environment. Then, select your board from the drop down menu and select the appropriate COM port. This whole process is described in detail here: http://arduino.cc/en/Guide/Windows
And that's about it! After programming the ATMega168, it can be taken out of the Arduino and popped into the prototyped circuit!
Step 7: Conclusion
After I had finished the sunrise alarm, I though of some improvements or extra features I could have added. One of the ideas I came up with was a switch to turn the bulb on full brightness so it can be used as a reading lamp. Another switch could be used to turn the alarm sound on or off. The circuit board could also have been a lot smaller. I just had this one laying around and decided to leave it in one piece.
The Final Product
Here it is! I've added a few pictures of what it looks like when the lights are fading in. I also took some video of the alarm playing Hey Jude. Again, if you have any questions about this project, just ask, I love to help!