Arduino and related stuff (including Attiny and ESP8266) and the Raspberry Pi

Dimming an AC lamp via MQTT

Years ago I published a TRIAC dimmer that could be controlled by a simple microcontroller such as an arduino. Times have changed and right now it is of more importance to be able to control that lamp from a home automation system such as OpenHab, Homematic or NodeRed.

So, let’s have a look at the circuit first:
This is a rather classic circuit. It detects the zerocrossing on the grid and then a microcontroller ignites the TRIAC with a time delay that determines the brightness of the attached Lamp. The full cycle is 10mS (at 50Hz)
A circuit like this is easy to make for very low cost (1-2 Euro).

However, if you shy away from soldering such a circuit, there are ready made modules available as well:You should not need to pay more than 3-4 USD for such a module. Anything above that is robbery

The value freqStep is usually set at 75 or 78 with this type of circuits, which allows for 128 steps of dimming at 50Hz gridfrequency. I have set the value here purposely to 100 allowing for only 100 steps, which is convenient for use in OpenHAB as the slider goes from 0-100%.
The calculation is as follows:

50Hz
As we have two zerocrossings per sine wave, the frequency of zerocrossings is 100Hz. Therefore the period between two zerocrossings is 10mSec is 10000uS.
So if you want to have 100 levels of dimming the steps you need to take are 10000/100=100uS

60Hz
The frequency of zerocrossings is 120Hz, therefore the period between two zerocrossings is 8.3mS is 8300uS. So if you want to have 100 levels of dimming, the steps you need to take are 8300/100=83uS -> freqStep=83

Thanks for your answer.
Looking at the code I see that you use an interruption PIN but (at least in Arduinos UNO and NANO) interrupts are disabled by SoftwareSerial, that is needed to connect using a ESP8266.
Which arduino did you use for this? or how can you managed to stablish network connection for mqtt without SoftwareSerial?
A solution could be to run this directly on an ESP8266. Do you know if it is possible or only runs on an Arduino board?

This was on a UNO.
I am not using software serial at all, neither do I use an ESP8266 here, that would be foolish to use just for internet connection. I am using an ArduinoUNO with Ethernetshield here.
As you can see in the code, there is no setup of a software serial, nor are there any AT commands being send to it.

As the ESP8266 is a powerful microcontroller in its own right it would be a waste to use it simply as a connection to Ethernet, but even if you wanted that, there is no need for software serial, hardware serial will do just fine.

I presume it is possible to run the code on an ESP8266, but that is not a ‘solution’ but just another project.

Sergio, I don’t think that will be a big problem. But can I maybe emphasize that you can buy a ready made board for 3-4 dollars. I am all for DIY, but sometimes it is better to just buy it ready made 🙂

Thanks for reply. You are right, Sometimes I buy ready made modules, they are often cheap and useful and do not justify making them yourself.
This time, I have all the parts in stock so I decided to build it to save time.
But I have somes problems. The first one is when I connect the 220V supply the 47K resistors turning very hot (with or whithout load). Second, at the output of bridge I measure no more than 1.3V, never detect the zero cross signal.
I have one more question. For the ESP8266, the interrupts are not equal to Arduino. Arduino uses the Ticker library and is not supported by the ESP8266. I’m right?. Greetings and thanks again.

Sergio, I fully understand, I sometimes do the same.
The heat dissipated by the 47k resistors is not depending on the load, they merely drive the zerocross detector.
Roughly 5mA will flow through each resistor, which amounts to abt 1 Watt.
The value of these resistors, and thus the powerdissipation is a bit between a roc and a hard place as you could increase them, but at a certain point that will lead to the zerocross pulse becoming wider. Feel free though to experiment with a higher value.
I am not sure how you measured the output. If just with a voltmeter it is higly unlikely that you could measure any difference as the pulses come by with 100-120 pulses/second, so what you will see on a standard multimeter will be anaverage voltage.
You are right about the ESP8266, interrupts but I dont think that has anything to do with the ticker library. The Arduino has hardware interrupts.
The Tickerlibrary works both on Arduino as on ESP afaik https://github.com/esp8266/Arduino/blob/master/libraries/Ticker/examples/TickerBasic/TickerBasic.ino

Which PubSub library did you use?
Let me get this straight, so I understand. Before you changed the MQTT server and port, did the code compile then?
I copied the code from this post and tried myself. I was surprised to see I got similar compilation errors and i didnt immediately se where the problem was, although I have a hunch:
Wordpress is pretty bad at posting code, it easily changes UTF characters into other characters and I suspect that is where the problem might be.
So…went looking for my original code… but that sadly was on a crashed computer. Fortunately I remembered having sent it to someone and I found that back.
It does contain some code you may not immediately need, but it does dim with the MQTT subject “home/br/sb” Payload being a number
If you leave me your email (I will not make that post public) I can send it to you, as for now it takes me a bit too much time to see where exactly the problem is with the corrupted code

reported errors are
estMQTT_WORKING_AC_DIMROBOTDYN3.ino.ino:88:3: error: ‘mqttClient’ does not name a type
mqttClient.loop();
^
testMQTT_WORKING_AC_DIMROBOTDYN3.ino.ino:89:3: error: ‘Serial’ does not name a type
Serial.print(“dim1 in loop = “);
^
testMQTT_WORKING_AC_DIMROBOTDYN3.ino.ino:90:3: error: ‘Serial’ does not name a type
Serial.println(dim1);
^
testMQTT_WORKING_AC_DIMROBOTDYN3.ino.ino:91:1: error: expected declaration before ‘}’ token
}

The components are in the circuit. The BR is a rectifier bridge. T1,T4andT5 are 2 optocouplers and a TRIAC.
As you see a bit unfamiliar with circuits, may I suggest you buy the ready made madule at AliExpress or banggood. It is really cheap

I have got the dimmer module but it doesn’t quite dimm my lamp with this sketch.
The lamp is always like 100% on, no matter what dim value is used, but on full dim (100), it is completely off.
The onboard LED however seems to dimm just fine, corresponding to the value.

Any idea what this could be? I tried multiple timerone libraries and have no clue that it could be.

Other approaches with delays dimm the lamp just fine, but i like a solution without delays.

On first instance that sounds like a faulty module. It could be that your interrupt isn’t working, but the behaviour of the led is weird. Let’s try to isolate the fault. Could you try and load the testing software from the earlier article so we can see if the fault is with the module or with the code

I have a second module to test if the fault lies there. Just weird that the other approaches with the delay work just fine.
I’ll try out the code from the earlier article this evening and report back.
Did you mean “Dimmer-Arduino” or “AC dimming with PWM and Arduino”?

I did not mention I was running an arduino uno and stripped out without the ethernet and client functionality. But I am going to try the original code on nodmcu too.