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.
What Is a Zero-Crossing Detector?
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.
Things You Will Need
Gather the following components to build a DIY AC dimmer module with an isolated zero-crossing detector.
NodeMCU or D1 Mini BT136 4A or BT139 16A TRIAC (based on your load) MOC3021 optocoupler MCT2E or 4N35 optocoupler DB107 bridge rectifier 220Ω ¼ W resistor 2 x 10kΩ ¼ W resistors 2 x 100kΩ ¼ W resistors 2 x 2-pin screw terminals Male berg strip (optional) General-purpose PCB Soldering iron and solder Jumper wires (optional, you may solder wires directly to PCB)
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.
Connect the Components
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.
Compile the AC Dimmer Firmware
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:
In Home Assistant, go to ESPHome and click +New Device > Continue. Type the name of the dimmer device. We named ours symphony-cooler since we will be using it to control the cooler fan and pump speed. Click Next. Select ESP8266 or Pick specific board > D1 Mini and click Next > Skip. Find the newly created dimmer device and click Edit. In the YAML editor window, enter your Wi-Fi credentials:
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.
Flash the AC Dimmer Firmware
To flash the firmware, download and launch the ESPHome Flasher tool. Then follow these steps:
Connect the NodeMCU or D1 Mini to your PC or Mac using a micro USB cable. Click Browse to select the firmware file (. bin). Select the COM port where the microcontroller is connected and click Flash ESP. After flashing the firmware, the device will restart and connect to the WI-Fi network and will show as online in the ESPHome dashboard.
Add Controls to Home Assistant Dashboard
In Home Assistant (HA), go to Settings > Devices & Integrations and follow these steps to add controls to HA dashboard for controlling the AC devices.
Under Integrations, find the discovered devices and click CONFIGURE > SUBMIT. After adding the device, the device will be listed under ESPHome. Click on the device and then click 1 device. Click ADD TO DASHBOARD, then choose a View and click ADD TO DASHBOARD again. The device will be added to the dashboard.
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.
Make Your Home Smarter
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.