This guide contains instructions which involve handling 110/220V AC main supply which may create electric shock and death. Children and novices are suggested not to try this guide without monitoring by an expert. The AC light dimmer for Arduino is a type of modules which allow 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 110V or 220V or to vary the speed of a fan, for example. Most of these modules are relatively costlier ($10 -$15) and not all the online shops store them.
You should try to use the AC dimmer module which works with this library:
1 | https://github.com/RobotDynOfficial/RBDDimmer |
Apart from LED, this module can also control the incandescent bulbs and fans. This module consists of BTA16-600B TRIAC, 4N26 optocoupler and MOC3021 optoisolator. The module looks like this one:
---

This is 5A variant. These dimmers are controlled with RBDdimmer.h
library, which uses external interrupts and process time interrupts. You can control multiple dimmers from one microcontroller. The module itself is quite unsafe as like any cheap priced China products.
As for wiring, the module is connected to the AX mains via the AC-IN terminal and the LED 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 (Arduino UNO or ESP2)
GND to ground GND of the microcontroller
Z-C at pin 2
PWM on pin 3
This is a chart of pins which you can use:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | * ---------------------- OUTPUT & INPUT Pin table --------------------- * +---------------+-------------------------+-------------------------+ * | Board | INPUT Pin | OUTPUT Pin | * | | Zero-Cross | | * +---------------+-------------------------+-------------------------+ * | Lenardo | D7 (NOT CHANGABLE) | D0-D6, D8-D13 | * +---------------+-------------------------+-------------------------+ * | Mega | D2 (NOT CHANGABLE) | D0-D1, D3-D70 | * +---------------+-------------------------+-------------------------+ * | Uno | D2 (NOT CHANGABLE) | D0-D1, D3-D20 | * +---------------+-------------------------+-------------------------+ * | ESP8266 | D1(IO5), D2(IO4), | D0(IO16), D1(IO5), | * | | D5(IO14), D6(IO12), | D2(IO4), D5(IO14), | * | | D7(IO13), D8(IO15), | D6(IO12), D7(IO13), | * | | | D8(IO15) | * +---------------+-------------------------+-------------------------+ * | ESP32 | 4(GPI36), 6(GPI34), | 8(GPO32), 9(GP033), | * | | 5(GPI39), 7(GPI35), | 10(GPIO25), 11(GPIO26), | * | | 8(GPO32), 9(GP033), | 12(GPIO27), 13(GPIO14), | * | | 10(GPI025), 11(GPIO26), | 14(GPIO12), 16(GPIO13), | * | | 12(GPIO27), 13(GPIO14), | 23(GPIO15), 24(GPIO2), | * | | 14(GPIO12), 16(GPIO13), | 25(GPIO0), 26(GPIO4), | * | | 21(GPIO7), 23(GPIO15), | 27(GPIO16), 28(GPIO17), | * | | 24(GPIO2), 25(GPIO0), | 29(GPIO5), 30(GPIO18), | * | | 26(GPIO4), 27(GPIO16), | 31(GPIO19), 33(GPIO21), | * | | 28(GPIO17), 29(GPIO5), | 34(GPIO3), 35(GPIO1), | * | | 30(GPIO18), 31(GPIO19), | 36(GPIO22), 37(GPIO23), | * | | 33(GPIO21), 35(GPIO1), | | * | | 36(GPIO22), 37(GPIO23), | | * +---------------+-------------------------+-------------------------+ * | Arduino M0 | D7 (NOT CHANGABLE) | D0-D6, D8-D13 | * | Arduino Zero | | | * +---------------+-------------------------+-------------------------+ * | Arduino Due | D0-D53 | D0-D53 | * +---------------+-------------------------+-------------------------+ * | STM32 | PA0-PA15,PB0-PB15 | PA0-PA15,PB0-PB15 | * | Black Pill | PC13-PC15 | PC13-PC15 | * | BluePill | | | * | Etc... | | | * +---------------+-------------------------+-------------------------+ * |
Here is a sample sketch:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #include <RBDdimmer.h> #define USE_SERIAL Serial #define outputPin 12 #define zerocross 5 #define potPin A0 const int potMin = 70; const int potMax = 804; int potValue; dimmerLamp dimmer(outputPin); int outVal = 0; void setup() { USE_SERIAL.begin(9600); dimmer.begin(NORMAL_MODE, ON); //dimmer initialisation: name.begin(MODE, STATE) } void loop() { potValue =analogRead(potPin); correctValue(); outVal = map(potValue, 22, 1023, 100, 0); USE_SERIAL.print("potValue:"); USE_SERIAL.print(potValue); USE_SERIAL.print(" "); USE_SERIAL.print(outVal); USE_SERIAL.println("%"); dimmer.setPower(outVal); } void correctValue() { if(potValue <potMin) { potValue =potMin; } if(potValue >potMax) { potValue =potMax; } } |