DONOFF – WiFi enabled light dimmer (Part 2)


Part-2 – The Electronics

This is part 2 of the four parts posts about the DONOFF ecosystem.

• Part 1 – Introduction
• Part 2 – The Electronics (this part)
• Part 3 – The Firmware
• Part 4 – Building a DONOFF device

In the first part I introduced the DONOFF ecosystem and the design goals.
In this part I will try to explain the electronics.

The hundreds of posts on the internet about mains dimmer designs all use a triac to cut-off part of the sine waves of the AC mains. Some do this by leading-edge cut-off, where the first part of the sine wave is cut-off:

Another method is by trailing-edge cut-off where the last part of the sine wave is cut-off:

As elegant and nice as these solutions may be, they only work with resistive loads (incandescent lights). You can, at least in the Netherlands, no longer buy incandescent lights and in the near future the only lights available will be LED lights.

You cannot dim LED lights by either trailing- or leading-edge cut-off! But (dimmable) LED lights can be dimmed with Pulse Wide Modulation (PWM). PWM switches the power to the LED light on and off at a (relatively) high frequency.

The DONOFF hardware uses this PWM to dim the (dimmable) LED lights but PWM can also be used to dim incandescent lights (So as a bonus, DONOFF can also dim incandescent lights).

A widely discussed PWM dimmer design on the internet is based on the design by Ton Giesberts. There are countless refinements on his design like the one from diy_bloke . I have used these designs and refinements as a basis for DONOFF.

DONOFF uses a MOSFET to switch the power on and off at a (modest) high frequency. You can look at a MOSFET (very simplified) as an On/Off switch. The switch is closed by adding a positive voltage (Vin > 10 Volt) to the gate of the MOSFET. Remove the gate voltage (Vin = zero) and the switch is open. In the open state (Vin = 0 Volt) the internal resistance of a MOSFET is (simplified) infinite, in the closed state (Vin > 10 Volt) the (Rds) resistance (still simplified) is zero ohm. In both states there will be no power dissipation and heat build up! It is important though that the transition between “open” and “close” is as short as possible because the MOSFET will dissipate power during that transition



Before you continue reading: please read the warning!


Do not build this design as it will probably kill you in the process and burn your house down while using it. After that it will explode!!
I’m not joking! This project uses deadly voltages and you should only build it if you’re a qualified electronics engineer. If you decide to build it, it will be your responsibility to take the necessary precautions – I’ll take no responsibility whatsoever for your actions in implementing it. Even more, I am NOT a qualified electrical engineer, thus I offer no warranties for the design or the fitness of this design to your purposes.

The complete circuit

The electronics consist loosely out of five subsystems. I will briefly address each one of them.

Mains AC to DC

The design uses a MOSFET to control the intensity of the (LED) light. As MOSFET’s only work with Direct Current (DC) this part of the circuit rectifies the 240 volt AC (RMS) into ~330 volt DC (pp). The fuse (F1) will limit the current of the circuit to about 1 ampere. RV1 is an ‘over voltage protection’ that will dissipate spikes so they will not damage the rest of the circuit.






In the schematics you will find labels [MAINS-DC-LP] (Low Power) and [MAINS-DC-HP] (High Power). You will also find those for AC. These labels are connected to each other but are needed for KiCAD to create different trace widths.

MOSFET control circuit

This is the heart of the DONOFF hardware. D2, D3, R3, R4 and C2 functions as a 15 Volt small current power supply to drive the gate of Q1. The MOSFET gate is normally pulled low by R7 (MOSFET switch “open” – LED light is Off). To close the MOSFET the optocoupler will pull the gate to 15 Volt (MOSFET switch “closed” – LED light is On). The Optocoupler (U1), R6 and R7 draw about 2.2mA (15v/(220+6800)). C2 will buffer the energy and is protected by the D2 against discharge when the rectified voltage goes below 15 Volt. Together with the D3 Zener, the group will keep a voltage between 12.5 – 15 Volt to drive the gate of Q1 (Q1 is fully closed with a gate voltage Vgs > 10 Volt). The ripple is about 150 milliVolt when the PWM is at almost 100% (worst case).

Voltage normally at 15 Volt, but drops a bit to about 13 Volt at 100% PWM and maximum gate open

Be aware that the current flowing through R4 is also 2.2mA which in turn results in 0.7 Watt (I2 * R = 4.8 * 150) that will be converted to heat (the temperature of R4 will rise some 15°C above ambient). Thats why R4 is a 400 Volt 2 Watt resistor (a little bit over dimensioned).

R6 is to protect the optocoupler and Q1 by limiting the gate current.
C1 is to filter the ‘MAINS‘ DC voltage.


The optocoupler U1 isolates MAINS from the ESP-01 electronics. The PWM signal comes from the ESP-01 microprocessor and turns the infrared diode on and off. When the diode is on the transistor in the optocoupler will conduct between the Collector and the Emitter (and will in turn pull the gate of the MOSFET to 15 Volt, which will switch the MOSFET “on”).

ESP8266 Microprocessor

The DONOFF is controlled by an ESP8266 (ESP-01 board) microprocessor. The main reason for using the ESP-01 (and not f.i. an ESP-12) is the fact that you can pull the ESP-01 out off the DONOFF dimmer to program, thereby reduce the chance to get electrocuted whilst connecting the wires from a programming device to the DONOFF dimmer. But of course, the DONOFF can also be programmed ‘Over The Air‘ (without a physical connection)!
The ESP-01 has one input (GPIO2) for an external switch and one output (GPIO3/URXD) that provides the PWM signal to the optocoupler. There is a built in LED at GPIO1. The ESP-01 is powered by a 3v3 power source.

ESP8266 3v3 DC power

For ease of use and safety the DONOFF uses a solid-state power supply from Mean Well (IRM-01-3.3) or from Hi-Link (HLK-PM03) to power the ESP-01 with 3v3.

In the next part of this sequel we will dive into the DONOFF firmware!

This entry was posted in Arduino, DONOFF, ESP8266, Hardware, SONOFF, WiFi and tagged , , , , , , , . Bookmark the permalink.

1 Response to DONOFF – WiFi enabled light dimmer (Part 2)

  1. You can place comments after Part 1

Comments are closed.