Introduction: Powerful Digital AC Dimmer Using STM32
By Hesam Moshiri, firstname.lastname@example.org
AC loads live with us! Because they are everywhere around us and at least home appliances are supplied with the mains power. Many types of industrial equipment are also powered with the single-phase 220V-AC. Therefore, we frequently face situations that we need to have full control (dimming) over an AC load, such as a lamp, an AC motor, a vacuum cleaner, a drill, … etc.
We should know that controlling an AC load is not as simple as a DC load. We have to use a different electronic circuit and strategy. Moreover, if an AC dimmer designed digitally, it is considered a time-critical application, and the microcontroller’s code must be written carefully and efficiently. In this article, I introduced an isolated 4000W digital AC dimmer that consists of two parts: the mainboard and the panel. The panel board provides two push buttons and a seven-segment display that allows the user to adjust the output voltage smoothly.
Step 1: Figure 1, Schematic Diagram of the AC Dimmer’s Mainboard
IC1, D1, and R2 are used to detect zero-crossing points. The zero-crossing points are quite essential for an AC dimmer. IC1  is an optocoupler that provides galvanic isolation. R1 is a pullup resistor that reduces the noise and allows us to capture all changes (both rising and falling edges).
IC3 is a 25A rated Triac from ST . This high current rating allows us to easily reach 4000W dimming power, however, the temperature of the Triac must be kept low and as close to the room’s temperature. If you intend to control high power loads, don’t forget to mount a big heatsink or use a Fan to cool down the component. According to the datasheet, this Triac can be used in a variety of applications: “Applications include the ON/OFF function in applications such as static relays, heating regulation, induction motor starting circuits, etc., or for phase control operation in light dimmers, motor speed controllers, and similar”.
C3 and R6, R4 and C4 are snubbers. In a simple term, Snubber circuits are used to reduce the noise, however for more reading, please consider the AN437 application note from ST . IC3 is a snubber-less Triac, however, I decided to use external snubber circuits as well.
IC2 is an optoisolator Triac  that is used to control the IC3. It also makes proper galvanic isolation. R5 limits the diode current of the IC2.
IC4 is the famous AMS1117 3.3V voltage regulator  that provides the power for the digital part circuits. C1 reduces the input noise and C2 reduces the output noise. P1 is a 2 pins male XH connector that is used to connect the external power to the device. Any input voltage from 5V to 9V is enough.
IC5 is the STM32F030F4 microcontroller and the heart of the circuit . It provides all instructions to control the load. P2 is a 2*2 male header that provides an interface to program the microcontroller through the SWD.
R7 and R8 are pullup resistors for the pushbuttons. Therefore the pushbutton input pins of the MCU are programmed as active-low. C8, C9, and C10 are used to reduce the noise according to the MCU’s datasheet. L1, C5, C6, and C7 reduce the supply noise, also build a first order LC filter (Pi) to provide stronger filtering for the input noise.
IDC1 is a 2*7 (14 pins) male IDC connector that is used to make a proper connection between the mainboard and the panel board through a 14-way flat cable.
PCB Layout [mainboard]
Figure-2 shows the PCB layout of the mainboard. It is a two-layer PCB design. The power components are through-hole and digital components are SMD.
Step 2: Figure 2, PCB Layout of the AC Dimmer’s Mainboard
As it is clear in the image, the board is divided up into two parts and optically isolated using IC1 and IC2. I also made an isolation gap on the PCB, under IC2 and IC3. The high current carrying tracks have been strengthened using both top and bottom layers and tied up using Vias. IC3 has been placed at the edge of the board, so it is easier to mount a heatsink. You should not have difficulties with soldering the components except for IC5. Pins are thin and close to each other. You should be careful not to make solder bridges between pins.
Using the industrial rated SamacSys component libraries for TLP512 , MOC3021 , BTA26 , AMS1117 , and STM32F030F4 , significantly reduced my design time and prevented possible mistakes. I can not imagine how much time I was wasting if I was intending to design these schematic symbols and PCB footprints from scratch. To use the Samacsys component libraries, you can either use a plugin for your favorite CAD software  or download the libraries from the component-search-engine. All SamacSys services/component libraries are free. I used Altium Designer, So I preferred to use the SamacSys Altium plugin (Figure 3).
Step 3: Figure 3, Selected Component Libraries From SamacSys Altium Plugin
Figure 4 shows 3D views from the top and bottom of the board. Figure 5 shows the assembled mainboard PCB from a top view and figure 6 shows the assembled mainboard PCB from a bottom view. The majority of the components are soldered on the top layer. Four SMD components are soldered on the bottom layer. In figure-6, the PCB’s isolation gap is clear.
Step 4: Figure 4, 3D Views From the PCB Board
Step 5: Figure 5/6, Assembled Mainboard PCB (top View/bottom View)
Circuit Analysis [panel]
Figure 7 shows the schematic diagram of the panel. SEG1 is a two digits multiplexed common-cathode seven-segment.
Step 6: Figure 7, Schematic Diagram of the AC Dimmer’s Panel
R1 to R7 resistors limit the current to the seven-segment LEDs. IDC1 is a 7*2 (14 pins) male IDC connector, so a 14-way flat wire provides the connection to the mainboard. SW1 and SW2 are tactile pushbuttons. P1 and P2 are 2-pins XH male connectors. I have provided them for the users who intend to use external panel pushbuttons instead of onboard tactile pushbuttons.
Q1 and Q2 are N-Channel MOSFETs  that are used to turn ON/OFF each part of the seven-segment. R8 and R9 are pull-down resistors to hold the gate pins of the MOSFETs low, to prevent unwanted triggering of the MOSFETs.
PCB Layout [panel]
Figure 8 shows the PCB layout of the panelboard. It is a two layers PCB board and all components except IDC connector and tactile pushbuttons are SMD.
Step 7: Figure 8, PCB Layout of the AC Dimmer’s Panelboard
Except for the seven-segment and pushbuttons (if you don’t use external buttons), other components are soldered on the bottom layer. The IDC connector is also soldered on the bottom layer.
The same as the mainboard, I used the SamacSys industrial component libraries (schematic symbol, PCB footprint, 3D model) for 2N7002 . Figure 9 shows the Altium plugin and the selected component to be installed in the Schematic document.
Step 8: Figure 9, Selected Component (2N7002) From the SamacSys Altium Plugin
Figure 10 shows 3D views from the top and bottom of the panelboard. Figure 11 shows a top view from the assembled panelboard and figure 12 shows a bottom view from the assembled panelboard.
Step 9: Figure 10, 3D Views From the Top and Bottom of the Panelboard
Step 10: Figure 11/12, a Top/bottom View From the Assembled Panelboard
Figure 13 shows the wiring diagram of the AC Dimmer. If you intended to check the output waveform using an oscilloscope, you must not connect your oscilloscope probe’s ground lead to the dimmer output or nowhere on the mains.
Attention: Never connect your oscilloscope probe directly to the mains. The probe’s ground lead can build a closed loop with the mains terminal. It would blow up everything in the path, including your circuit, probe, oscilloscope, or even yourself!
Step 11: Figure 13, Wiring Diagram of the AC Dimmer
To overcome this problem, you have 3 options. Using a differential probe, using a floating oscilloscope (the majority of the oscilloscopes are ground referenced), using a 220V-220V isolation transformer, or just simply use a cheap step-down transformer, such as 220V-6V or 220V-12V … etc. In the video and figure-11, I used the last method (step-down transformer) to check the output.
Figure 14 shows the complete AC dimmer unit. I’ve connected two boards using a 14-way flat wire.
Step 12: Figure 14, a Complete Digital AC Dimmer Unit
Figure 15 shows the zero-crossing points and the Triac’s ON/OFF time. As it is clear, both the rising/falling edge of a pulse was considered to not to face any flickering and instability.
Step 13: Figure 15, Zero Crossing Points (the Purple Waveform)
Step 14: Bill of Materials
It is better to use 630V rated capacitors for C3 and C4.
Step 15: References
: TLP521 Datasheet: https://www.futurlec.com/Datasheet/LED/TLP521.pdf
: BTA26 Datasheet: https://www.mouser.com/datasheet/2/389/cd00002264...
: AN437, ST Application Note: https://www.st.com/resource/en/application_note/c...
: MOC3021 Datasheet: http://www.farnell.com/datasheets/97984.pdf
: AMS1117-3.3 Datasheet: http://www.advanced-monolithic.com/pdf/ds1117.pdf
: STM32F030F4 Datasheet: https://www.mouser.com/datasheet/2/389/dm00088500...
: Schematic Symbol and PCB Footprint of TLP521: https://componentsearchengine.com/part/1774908/mo...
: Schematic Symbol and PCB Footprint of MOC3021: https://componentsearchengine.com/part/229006098/...
: Schematic Symbol and PCB Footprint of BTA26-600: https://componentsearchengine.com/part/1587919/mo...
: Schematic Symbol and PCB Footprint of AMS1117-3.3: https://componentsearchengine.com/part-view/AMS11...
: Schematic Symbol and PCB Footprint of STM32F030F4: https://componentsearchengine.com/part/182014793/...
: Electronic CAD Plugins: https://www.samacsys.com/library-loader-help
: 2N7002 Datasheet: https://www.diodes.com/assets/Datasheets/ds11303....
: Schematic Symbol and PCB Footprint of 2N7002: https://componentsearchengine.com/part/228320879/...