fbpixel
Tags: , ,

In home automation, it is interesting to be able to manipulate alternating current in order to control different electrical appliances such as lamps or fans. The AC voltage dimmer is a module allowing 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 ESP32.

ac-light-dimmer-module Using an AC dimmer with ESP32

Material

  • Computer
  • NodeMCU ESP32
  • USB A Male/Micro B Male Cable
  • AC Light Dimmer

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.

ac-light-dimmer-module-pinout Using an AC dimmer with ESP32

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 1 (GPIO possibles 0,1,2,4,5,7,12,13,14,15,16,17,18,19,21,22,23,25,26,27,32,33,34,35,36,39)
  • PWM to pin2 (GPIO possibles 0,1,2,3,4,5,12,13,14,15,16,17,18,19,21,22,23,25,26,27,32,33 )

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.

nodemcu-esp32-ac-dimmer-module_bb Using an AC dimmer with ESP32

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 ESP32, 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%.

//Libraries
#include <RBDdimmer.h>//https://github.com/RobotDynOfficial/RBDDimmer

//Parameters
const int zeroCrossPin  = 1;
const int acdPin  = 2;
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("ESP32 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.

arduino-ac-dimmer-module-result Using an AC dimmer with ESP32

Applications

  • Create a web interface or a Bluetooth application to control electrical devices remotely.

Sources