Hi. Right now I have a relay with my arduino so I can turn on and off my lamp but I'm wondering if there is any option to control brightness of that lamp? How can I do it?
Turn it off & on really fast, the ratio of on to off determines brightness. Can't use mechanica relay, the clattering will drive you nuts & wear it out. Look at a solid state relay instead.
An incandescent light dimmer works by "chopping" the AC waveform. There is an explanation [u]here[/u].
The concept is similar to how PWM dimming works on an LED, except the chopped waveform has to be synchronized with the AC line frequency, and an incandescent bulb actually glows dimly, whereas an LED turns on & off rapidly, fooling the eye.
A TRIAC can conduct in either direction, so they work with AC. Once a TRIAC is triggered, it says on until current drops to zero. With AC power, that's at a zero-crossing. So if you send a trigger pulse half-way through the AC cycle half-cycle (90 degrees or 270 degrees into a cosine wave), current will flow for the remainder of the half-cycle, and you are at "half brightness".
That means you need to detect the AC zero-crossing.* The brightness depends on how long you wait before firing the TRIAC. If you trigger the TRIAC at the zero-crossing you get full brightness. If you trigger the TRIAC just before the end of the half-cycle (just before the next zero-crossing) it will be very dim.
You need to isolate the low-voltage Arduino (and yourself) from the AC on the output side, and on the input where you detect the zero-crossing and on the TRIAC output.
You can use a solid state relay, or you can build your own solid state relay from a special [u]opto-isolator[/u] that's made for a TRAIC. (In either case, don't use a zero-crossing solid state relay or a zero-crossing opto-isolator, because these will not turn-on in mid-cycle.)
The zero-crossing detector can use an opto-isolator, or a transformer for isolation. (An opto-isolator will generally require a large (maybe 2-Watt) resistor when driven from 120 or 220VAC.) When I built a microcontroller-dimmer (with a different microcontroller) I tapped-into the secondary of the power supply transformer to get the isolated zero-crossing signal.
- It's actually easier to find a constant point near the zero crossing (or somewhere else along the waveform), and since you know the line-frequency, you know when the next zero-crossing is coming.
It's easier if you just find one point (such as just after the positive-going zero-crossing) to calculate the next two zero crossings. That way, you don't have to deal with reading negative voltages.
- It’s actually easier to find a constant point near the zero crossing (or somewhere else along the waveform), and since you know the line-frequency, you know when the next zero-crossing is coming.
Which means you could use a capacitor instead of a resistor in series with the opto-coupler, and not need that 2 Watt resistor.