Build your own DIY dimmer circuit with zero-crossing detection to make any traditional light or AC mains device smarter.
The Wi-Fi-based smart switches and plugs you can buy contain a mechanical relay that activates when the microcontroller receives a binary input, such as 0 or 1, usually sent via an app. These devices only allow you to control the on/off status of devices, such as a fan, motor, or light. If you also want to control the speed or the brightness of the connected AC mains appliance or load, you will require a TRIAC-based solid-state relay switch.
In this DIY guide, we will build a dimmer module with a zero-crossing detector from scratch and use it to control the on/off status, speed, and brightness of the connected AC load.
A zero-crossing detector (ZCD) is a voltage comparator or an op-amp detector circuit used to detect the voltage change from the positive to the negative level of the alternating current sine-wave signal as it crosses zero volts. In a nutshell, the circuit is used to detect the zero-crossing of the input AC signal.
ZCD is used for building electronic circuits for switching, frequency counter, phase meters, etc. One can also use a zero-cross detector circuit and a TRIAC-based solid-state relay with Arduino or ESP8266-based Wi-Fi boards to control the phase of the AC voltage.
In most countries, the 50Hz (50 cycles/second) AC frequency is used with a 220-240V supply. However, a handful of countries, such as the United States, use 120V 60Hz (60 cycles/second) mains electricity. With every cycle, the wave comes to zero, at which point the microcontroller detects it and then switches or triggers the solid-state relay (TRIAC) as per the requirement.
Unlike a mechanical relay, a TRIAC-based solid-state relay is a fast-switching power electronic device and thus best suited for making a dimmer circuit.
Gather the following components to build a DIY AC dimmer module with an isolated zero-crossing detector.
These components are for a single-channel DIY dimmer module. To control more loads, you can build more DIY solid-state relays and interface with the microcontroller.
Refer to the following circuit diagram to connect and interface all the components on a general-purpose PCB board to build a zero-crossing detector. Use a soldering iron and solder to secure all connections, as shown in the diagram below. If you have never soldered before, learn how to solder with simple projects before starting this.
Refer to the following diagram to build a solid-state relay module where the load will be connected.
These circuits show you a single-channel dimmer module. By increasing the solid-state relays, you can add and control more number of appliances or AC loads. Overall, the end results should appear similar to the following. It’s a 3-channel dimmer module with a zero-crossing detector.
To compile the firmware for the module for wireless control, you will need a Home Assistant setup on a Raspberry Pi (or Docker) with the ESPHome add-on. Once you have Home Assistant and ESPHome set up, follow these steps to compile the firmware:
Then paste the following code just below the captive portal:
Change the id: and name: in the code as per the device you will be controlling. As mentioned earlier, you may also add more solid-state relays to control AC load brightness or speed. The code should look like this:
The module we made controls two motors, for the cooler pump and cooler fan, thus we named them accordingly. Once the code is ready, click Save > Install> Plug into this computer and then wait for the firmware to finish compiling. Once compiled, click Download project to download the compiled firmware.
To flash the firmware, download and launch the ESPHome Flasher tool. Then follow these steps:
In Home Assistant (HA), go to Settings > Devices & Integrations and follow these steps to add controls to HA dashboard for controlling the AC devices.
You can now control the on/off and speed/brightness of the connected AC device.
You may further use mushroom-cards integration to add beautiful-looking cards for your dimmer modules.
Unlike traditional smart switches, you can use a dimmable smart switch in many ways. For instance, you can set automation in Home Assistant to change the light brightness based on the time of the day or start dimming as you switch on your smart TV or home theater system.
In addition, you can use this DIY dimmer circuit to make any traditional light or AC device smarter. More importantly, it's cheaper and much more convenient to build one. You can build these switches to control a 15W or 4000W load based on your needs. All you need to do is change a few components, such as the TRIAC.
Ravi is an expert tech explainer, an IoT enthusiast and Linux lover with a background in big data and app development. He spends most of his weekends working with IoT devices and playing games on the Xbox. He is also a solo traveler who loves hiking and exploring new trails.
Join our newsletter for tech tips, reviews, free ebooks, and exclusive deals!