Featured PostsHackerspaceMain Stories

Dimmable AC Light Box


This blog post is about a box that could dynamically dim AC bulbs via computer control. The main purpose being to react to music. I will be providing code, and some schematics, and I hope other hobbyists will find this post useful.

(If you’d like to go straight to the technical stuff, simply start at page two)

First, a brief history.

My desire to make lighting that would react to music largely began in late 2003. A Perfect Circle released their album, Thirteenth Step. Upon first listen of the song, Blue, I pictured a series of lights reacting to the music, and blue shifting upon the chorus line of “She’s turning blue.” Years passed, and in 2008, I learned about the Winamp plugin, Discolitez , a plugin that allowed Winamp to control lights connected to a computer. I decided to build the most basic 8 channel circuit that ran off an LPT Port. As the circuit only required resistors, and LEDs, I knew I was capable of building it.

I built the circuit, but to my dismay, the result was lackluster at best. While it was simple to build something like a VU meter, the function of mapping LEDs to frequency bands looked like crap. With DiscoLitez, you were given the option of setting a thresholds on the volume that would trigger an LED, but the LED was either on, or off. I also accepted that using LPT port pins to drive any load directly was not best practice.

I read about using super brights with transistors, but the non-dimmable nature of the plugin really disappointed me. I made the following video using the song “Too Young” from the Venitian Snares. I chose the song as the music rapidly cuts in and out at the beginning and works well with the non-pwm lights. You could see towards the end of the video how badly the lights looked when the thresholds were met, and held..



In the hope I could make the LEDs do more than just sit there, I began playing with the more advanced “Discolitez Pro” plugin which allowed such things as strobing the LEDS, and comparing data streams with logic gates. I wrote a “desktop,” as the configurations are known as to compensate for this. I set multiple thresholds for each band, and had those feed into a logic gate that would “AND” it with different timers. The idea was the louder a band got, the faster I’d strobe the bulb. Of course this methodology is opposite of how a PWM works. I knew that the pulsing was much too slow to have any sort of noticeable PWM, and that the software was not NEARLY powerful enough. Let alone, I wasn’t even fully introduced to the concept, yet.



The results didn’t look that bad in person, but I still wanted better results. I liked the idea of controlling hardware from an I/O port, so I went back to the LPT LEDs I had breadboarded. I connected it to a linux server, and found a program called “lptest” I could use to drive individual pins, and hence drive individual lights.


I had bought some opto-isolated solid state relays and connected one to an IO pin. I measured 13mA draw from a multimeter, which was on the edge of the Printer Port spec, but I felt was safe enough. I connected a string of Christmas lights to the SSR, and was excited by the fact I had command line control over an AC socket.

I knew I wanted to do more automation, but wanted a better way to drive hardware. Enter Microcontrollers…

7 thoughts on “Dimmable AC Light Box

  • Pingback: Dimming AC lights the hard way - Hack a Day

  • Juan Sebastian Cifuentes

    Does the transformer induce some phase (delay) to the zero cross detection?

  • waterbury

    Juan, I’d imagine the Sine Wave looks the same after being stepped down, only with less power, so I don’t think a delay is being caused here, or the wave is being pushed out of phase.

    The wave becomes rectified and drives a Transistor that in turn inverts the signal into a a square wave. i.e. when voltage is 0, the transistor passes no power, and a pull up drives the interrupt high. What I believe is happening is once the wave hits a low threshold, say 4v, for example, the transistor no longer functions, and the interrupt is triggered. I’d like to find a solution to this.

    Hope that helps.

  • I know this has been idle for a few months but just to give a possible solution.

    You have a processor, which allows you more flexibility that a transistor circuit monitoring a full wave rectifier.

    If you use a single separate diode as a half wave rectifier,with some resistors you’ll get a square wave at the line frequency, that you could read with logic input.

    Now if life were perfect,you’d be set, because all you would have to do is monitor that input for an edge. Rising or falling. That would be a zero crossing. It may even work just that simple. But possibly there is line noise on the AC that would give you several as the signal is near Vinputhigh. There is also a symmetry issue, where voltage crossing would be different on one phase from the other. Its the kind of thing you’d use a Schmidt trigger to read if you were using pure logic.

    This is where the processor comes in. You can calculate how deep into the wave form the input crosses Vih, and when it will crass back again on the way down. From there you can calculate where the real zero crossing is.

    With further elaboration you should be able to use that data make a phase locked loop in software that locks into the 60 Hz signal of the AC line. Using that as a time base, then you can determine your triac firing times.

  • On additional thought, you can really factor out the asymmetrical aspect of the half wave detector. Whatever Vih is, the peak of the AC powerline waveform is going to be half way between the rising edge and the falling edge. Likewise the nadir of the low part of the cycle will be half way between the falling edge and the next rising edge.

    This PIC has a timer. So if (after deglitching) you take the times of the rising and falling edges, you should be able to calculate where the peaks are, and thereby the zero crossings.

    I’d probably set it up so that a timer caused an interrupt when it rolled over, intending that to be a zero crossing. Reloading the timer count should take into account the error based on the the expected peak times. If the peak is not at 1/4 of a cycle, and the low at 3/4 of a cycle, calculate the error, and add that in when setting the timer count back 1/2 a cycle in the interrupt service routine.

    You can even use an initialization process at the start of day to figure out how many timer ticks a cycle is, and not worry about an absolute scaled time base. just get an average for a few cycles.

    Therefore with a bitof deglitching, you should be able to know

  • deathventure

    You can use another optocoupler on the transformer line in place of the transistor circuit instead. Look up the 4N26. The internal diode will half rectify the wave and the transistor will cause a more precise square wave you can base your timing from.

    • You must be Psychic. The last comment here was over a month ago, and I JUST had gotten done purchasing it’s sister chip, the H11AA1 when you commented. I was researching this earlier. The Renard PIC controller uses it, and I think I’ll adapt it also.

Comments are closed.