The AC voltage variator is a module that allows to vary the power of an alternating current. It has the same use as a transistor in direct current. It can be used to vary the brightness of a lamp supplied with 220V or to vary the speed of a fan, for example. We will see how to manage the AC voltage dimmer with a NodeMCU ESP8266.
Material
- Computer
- NodeMCU ESP8266
- USB A Male/Micro B Male Cable
- AC Light Dimmer
- A dimmable bulb
Principle of operation
The AC dimmer consists of a triac (equivalent to a DC transistor) and a phase zero crossing detector to synchronize the voltage variation and the phase of the AC current.
Scheme
As far as the wiring diagram is concerned, the module is connected to the mains via the AC-IN terminal block and the bulb is connected to the LOAD terminal block. On the electronic side, the pins are connected as follows:
- Vcc at pin 5 or 3.3V of the microcontroller
- GND to ground GND of the microcontroller
- Z-C to pin D1 (possible pins D2,D5,D6,D7,D8)
- PWM at pin D2 (possible pins D0,D1,D5,D6,D7,D8 )
If you use several drives, the Z-C pins are all connected to the same microcontroller pin (in our case D1). The RBDdimmer library uses some pins in particular, depending on the microcontroller used. Please check the documentation of the library to see which pins you can use.
Caution: When using a NodeMCU, there are different versions of these cards with different pinouts and labels that can sometimes have errors. Ask your supplier and check the documentation of your card.
Code
Before programming your NodeMCU ESP8266, make sure that the board manager is installed on the Arduino IDE. To configure the IDE, you can follow this tutorial.
To use the AC Light Dimmer module, we use the RBDdimmer.h. library. The library will manage the synchronization between the PWM signal, which sets the power, and the phase of the AC current. Once the library has been imported and the module initialized, we only have to choose the power level between 0 and 100%.
Before uploading the code, you need to install the RBDdimmer library.
//Libraries #include <RBDdimmer.h>//https://github.com/RobotDynOfficial/RBDDimmer //Parameters const int zeroCrossPin = D1; const int acdPin = D2; int MIN_POWER = 0; int MAX_POWER = 80; int POWER_STEP = 2; //Variables int power = 0; //Objects dimmerLamp acd(acdPin,zeroCrossPin); void setup(){ //Init Serial USB Serial.begin(115200); Serial.println(F("ESP8266 System")); acd.begin(NORMAL_MODE, ON); } void loop(){ testDimmer(); } void testDimmer(){/* function testDimmer */ ////Sweep light power to test dimmer for(power=MIN_POWER;power<=MAX_POWER;power+=POWER_STEP){ acd.setPower(power); // setPower(0-100%); Serial.print("lampValue -> "); Serial.print(acd.getPower()); Serial.println("%"); delay(100); } for(power=MAX_POWER;power>=MIN_POWER;power-=POWER_STEP){ acd.setPower(power); // setPower(0-100%); Serial.print("lampValue -> "); Serial.print(acd.getPower()); Serial.println("%"); delay(100); } }
Result
The brightness of the bulb varies according to the ‘power’ value sent to the module. It is good to note that this module works with dimmable loads and works best with incandescent bulbs. I use an LED bulb and the dimming works well between 6 and 40. Below 6, the lamp goes out, above that, I don’t see any change in brightness and the bulb goes out above 85.
Applications
- Create a web interface to control lights remotely.
my ESP8266 is crashing with your code please solve his problem and upload new code
what is the error?
Here’s the error I am getting with your code:
01:52:37.841 -> ————— CUT HERE FOR EXCEPTION DECODER —————
01:52:37.841 ->
01:52:37.841 -> ets Jan 8 2013,rst cause:2, boot mode:(3,6)
01:52:37.841 ->
01:52:37.841 -> load 0x4010f000, len 3460, room 16
01:52:37.841 -> tail 4
01:52:37.841 -> chksum 0xcc
01:52:37.841 -> load 0x3fff20b8, len 40, room 4
01:52:37.841 -> tail 4
01:52:37.841 -> chksum 0xc9
01:52:37.841 -> csum 0xc9
01:52:37.841 -> v000428d0
01:52:37.888 -> ~ld
01:52:37.935 -> ESP8266 System
01:52:37.935 -> ISR not in IRAM!
Seems to be a core esp8266 library incompatibility. Have a look at ICACHE_RAM_ATTR
Hi, i have the same problem. Can you be more specific please, Xukyo or somebody who knows ? Thanks in advance.
Hi,
According to this
With board NodeMCU 1.0, you can add “ICACHE_RAM_ATTR” in between the “void isr_ext()” RBDmcuESP8266.cpp file.
Like this:
void ICACHE_RAM_ATTR isr_ext()
I cannot get it to work on a nodemcu v3 module (with esp-12e). I have added the ICACHE_RAM_ATTR as mentioned in the post above. The bulb does not dim at all; in fact, it does not turn on at all.
Are you sure the bulb is dimmable?
Yes, it is an incandescant lamp. The dimming works fine when connected to an arduino.
dear all use esp32 ,esp8266 not working .i try it but not success ,esp32 is better or ac dimmer and smoothly worlk .i suggest u to use esp32