Wednesday, November 16, 2016

Dimming with regular power switch - DIY 3-step dimming / DoRS


Light bulbs that are dimmable with regular light switch have many names depending on a manufacturer: DoRS (Dim on Regular Switch), 3-step dim, Free dimming, 4-step dim... They all have the same main concept: Switch lamp on and turn it off and quickly on again to switch the dimming level.
This same concept is used in flashlights which have multiple modes, usually couple different brightness levels and some kind of blinking mode.


To use power switch as an input for a microcontroller is easier than some might think. Connecting a resistor (lets say 100k) between GPIO pin and positive side of capacitor (maybe 47uF?) and connecting negative side of the capacitor to ground is all the hardware that is needed.

So, how it works?

Above is digram of voltages over time. Red line is VCC of the microcontroller, which is first turned ON and then briefly OFF and ON again. Blue line is voltage of the capacitor connected to one of the GPIO pins through a resistor. When microcontroller is powered on (points A and C) it reads state of the pin where the capacitor is connected. At A it reads as LOW and microcontroller knows it was powered off for a while. GPIO pin will be changed to output HIGH, which makes the capacitor start charging until it reaches the VCC voltage. That is the normal state, where nothing else will happen automatically.

After switching off the VCC, capacitor will discharge through the resistor and the clamping diode inside the microcontroller internal input circuit. If VCC is switched on before capacitor's voltage reaches lower limit where microcontroller reads input as high, microcontroller will read the state of the pin at start up (at C on the diagram) and it will be HIGH. That tells the microcontroller that power was switched off only for a short duration and it will change into secondary state (dimmed LED).

But that's just two steps, not three!

One GPIO pin with resistor and capacitor gives two step dimming: One after switching the power on and second after cycling the power off and on again. Adding third (and then fourth, fifth...) step can be done in couple of ways. I chose to add another resistor and capacitor to second GPIO. I start to charge the capacitor only after the first power cycle has been detected. Another way would be to store current state in EEPROM, read it back after switching the power on and changing to next state if the GPIO reads as  HIGH.


Here is my demonstration video. I used single LED, but it could be replaced with mosfet and multiple LEDs to create lamp that is more usable in real life.


Below is the code used in the demonstration. It's something I put together on a minute or so, but it doest the job!
If the code doesn't show up, here is the link to the PasteBin: