If you want to have a more comfortable evening, it's a nice extra to be able to dim the lights. Unfortunately, this isn't possible with relays, they can only go on and off. So we need something else here. Hello MOSFET.
The basics
(We only refer to DC via PWM in this tutorial)
To dim the brightness of LEDs or other direct current lamps, this is done via the voltage. The more voltage we apply, the brighter the lamp becomes. So we have to regulate the voltage. This can be done using a pulse width modulated signal.
As the name suggests, we pulse the voltage to the light source using a square wave. We therefore switch the lamp on and off very quickly. The longer the lamp stays on between pulses, the brighter it shines. Through these shifts, we create an effective voltage, which then brings the lamp to the desired brightness.
How fast does this all have to happen? In order to have lighting that doesn't flicker, the frequency should be over 100Hz. On our Dimmy, the frequency is set to 200Hz. But it shouldn't be higher than 500Hz. So we're talking about extremely fast switching of the LEDs on and off. That's why we can't do this using relays alone. A relay wouldn't be able to handle this speed and would break after a short time. That's why transistors are used here.
Transistors? The components that are used in the billions on modern CPUs are, at the very least, digital relays.
If you apply a certain voltage to the "base" of the transistor, a connection is created between the emitter and collector and the current can flow. The big advantage here is that we don't need any mechanics for it and the switching process can happen very quickly. Perfect for controlling our lights via PWM. I've actually already described a MOSFET here. In other words, a " field effect transistor". With a normal transistor, a current must be able to flow on the base. MOSFETs are suitable for high currents and not so fast circuits. The frequency of CPUs is much higher than our PWM, which is why normal transistors are used there.
So much for the basics.
How does our DIMMY work
Our Dimmy uses an extra chip to generate the PWM, the pca9685. This chip is often used to control servos, as these are also controlled via PWM signals. This chip communicates with an ESP8266 (Wemos D1) via the I2C bus. A customized version of Tasmota runs on this. Our PekawayMOTA. In principle, this is a normal Tasmota in which we have activated the driver for the PCA9685. This means that we can now address the PWM chip using the Driver15 commands.
https://tasmota.github.io/docs/PCA9685/
Using these commands you can now directly access the PCA9685 from the Tasmota console.
However, we want to do this from our system. That's why we use MQTT again and have set pekaway.local as a broker directly in PekawayMOTA.
In short: VanPI -> MQTT to Tasmota -> i2c to PCA9685 -> PWM Mosfet -> LED.
You can flash our PekawayMota directly via our web installer (Chrome).
How could you build a dimmer yourself?
This would be very easy with an Esp8266/Esp32 board. It doesn't matter whether it's Wemos, NodeMCU or similar. You simply flash Tasmota directly onto it using the online flasher (Chrome) https://tasmota.github.io/install/ then as usual the normal connection to your WLAN.
In the module config of Tasmota you can set one or more PWM pins.
After you have done all this, a slider for dimming will appear in the frontend.
So now we already have a device in the network that generates the PWM. Now we only need a MOSFET to dim the actual voltage on the LEDs. However, there are a large number of ready-made small modules on the common marketplaces that you can use.
For example here:
https://eckstein-shop.de/Pololu-Big-MOSFET-Slide-Switch-with-Reverse-Voltage-Protection-MP
Of course, you can then connect Tasmota to your desired control system via MQTT and dim the LEDs.