How to connect a dimmer to microcontrollers

How to connect a dimmer to microcontrollers
By RobotDyn Support
More from this author

How to connect a dimmer to microcontrollers

How to connect a dimmer to microcontrollers.

 

The dimmer module has 4 inputs: power VCC, GND, Zero-cross, PSM (PWM).

A voltage to VCC should be the same as the microcontroller logic level. For most microcontrollers, this is 5V (AVR) or 3.3V (ESP8266 / 32, STM32, Arduino Due, Zero / M0).

Zero-cross - to microcontroller inputs with hardware interrupt function.

PSM - to microcontroller output pin. If you are using a multi-channel dimmer or many dimmers to one microcontroller, then connect each PSM output of dimmer to each input of the microcontrollers.

 

To use the dimmer in Arduino projects, you can use the rbddimmer.h library https://github.com/RobotDynOfficial/RBDDimmer.

Below the table of connecting a dimmer to Arduino microcontrollers to use with rbddimmer.h library

If your project already uses all the microcontroller inputs that are required for Zero-cross, you can use a separate microcontroller for the dimmer and transfer information via the UART serial protocol between the microcontrollers.

Also, the library uses hardware timers of the microcontroller, and is possible a conflict with other libraries, for example, with some libraries for LCD symbol screen for ATmega328/168. In this case, it is recommended to use a dedicated microcontroller for the dimmers. We will show in our projects how to use multiple microcontrollers.

Board

INPUT Pin
Zero Cross

OUTPUT Pin

Leonardo

D7 (NOT CHANGABLE)

D0-D6, D8-D13

Mega

D2 (NOT CHANGABLE)

D0-D1, D3-D70

UNO
NANO

ProMini

D2 (NOT CHANGABLE)

D0-D1, D3-D20

ESP8266

  • D1(IO5)
  • D5(IO14)
  • D7(IO13)
  • D2(IO4)
  • D6(IO12)
  • D8(IO15)
  • D0(IO16)
  • D2(IO4)
  • D6(IO12)
  • D8(IO15)
  • D1(IO5)
  • D5(IO14)
  • D7(IO13)

ESP32

  • 4(GPI36)
  • 5(GPI39)
  • 8(GPO32)
  • 10(GPI025)
  • 12(GPIO27)
  • 14(GPIO12)
  • 21(GPIO7)
  • 24(GPIO2)
  • 26(GPIO4)
  • 28(GPIO17)
  • 30(GPIO18)
  • 33(GPIO21)
  • 36(GPIO22)
  • 6(GPI34)
  • 7(GPI35)
  • 9(GP033)
  • 11(GPIO26)
  • 13(GPIO14)
  • 16(GPIO13)
  • 23(GPIO15)
  • 25(GPIO0)
  • 27(GPIO16)
  • 29(GPIO5)
  • 31(GPIO19)
  • 35(GPIO1)
  • 37(GPIO23)
  • 8(GPO32)
  • 10(GPIO25)
  • 12(GPIO27)
  • 14(GPIO12
  • 23(GPIO15)
  • 25(GPIO0)
  • 27(GPIO16)
  • 29(GPIO5)
  • 31(GPIO19)
  • 34(GPIO3)
  • 36(GPIO22)
  • 9(GP033)
  • 11(GPIO26)
  • 13(GPIO14)
  • 16(GPIO13)
  • 24(GPIO2)
  • 26(GPIO4)
  • 28(GPIO17)
  • 30(GPIO18)
  • 33(GPIO21)
  • 35(GPIO1)
  • 37(GPIO23)

Arduino M0
Arduino Zero

D7 (NOT CHANGABLE)

D0-D6, D8-D13

Arduino Due

D0-D53

D0-D53

STM32
Black Pill
Blue Pill
Etc...

PA0-PA15, PB0-PB15
PC13-PC15

PA0-PA15, PB0-PB15
PC13-PC15

 

 

 

 

 

Library connection and initialization

** If need to use the serial output (serial.print), need to use softserial #define USE_SERIAL  Serial

 

Code:

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#include <RBDdimmer.h>//
#define outputPin  5 
#define zerocross  2 // for boards with CHANGEBLE input pins

//dimmerLamp dimmer(outputPin, zerocross); //dimmer output DIM/PSM is initialized on the pin 4 and zero-cross initialized on pin 2. Only for boards whith changable zero-cross(ESP32, ESP8266, Arduino Due)
dimmerLamp dimmer(outputPin); //dimmer output DIM/PSM is initialized on the pin 4 for the bords WITHOUT changable ZERO-CROSS input pin (AVR, Arduino M0/Zero)

void setup() {
  dimmer.begin (NORMAL_MODE, ON); //dimmer initialisation: name.begin(MODE, STATE) 
}
void loop() 
{

}

 

If you use multiple dimmers, try this code:

 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
#include <RBDdimmer.h>//
#define outputPin1  5 
#define outputPin2  6
#define outputPin3  7 
// … #define outputPinN  N 
#define zerocross  2 // for boards with CHANGEBLE input pins

//dimmerLamp dimmer(outputPin, zerocross); //dimmer output DIM/PSM is initialized on the pin 4 and zero-cross initialized on pin 2. Only for boards whith changable zero-cross(ESP32, ESP8266, Arduino Due)
dimmerLamp dimmer_1(outputPin1); //dimmer output DIM/PSM is initialized on the pin 4 for the bords WITHOUT changable ZERO-CROSS input pin (AVR, Arduino M0/Zero)
dimmerLamp dimmer_2(outputPin2);
dimmerLamp dimmer_3(outputPin3);

dimmerLamp dimmer_N(outputPinN);

void setup() {
dimmer_1.begin (NORMAL_MODE, ON); //dimmer initialisation: name.begin(MODE, STATE) 
dimmer_2.begin (NORMAL_MODE, ON);
dimmer_3.begin (NORMAL_MODE, ON);
// … dimmer_N.begin (NORMAL_MODE, ON);

}
void loop() 
{

}

 

Function dimmerLamp - this function initializes the number of operating pin and is defined by the userFunction begin port initialization, timer and external interrupt from zero-cross.

dimmer.begin(NORMAL_MODE, ON/OFF); port initialization, work mode choice, ON/OFF. Parameter:

1: dimmer working modes consist of two choices - NORMAL_MODE and TOGGLE_MODE     

  • a. NORMAL_MODE to make dimmer work in defined value from 0 to 100 (%) (integer)  Example of this mode located in \RBDdimmer\examples\SimpleDimmer   
  • b. TOGGLE_MODE smooth change of dimming value up or down in a defined range. This solution simplies change of dimming values by means of hardware timer, without using the cycle code.        Example of this mode located in \RBDdimmer\examples\SimpleToggleDimmerParameter

2: ON/OFF.   

  • a. ON - turns timer ON, allows to use dimmer.   
  • b. OFF - turns timer parameters OFF, prevents the use of dimmer.

 

Conclusion.

  1. The supply voltage of the dimmer must be the same as the logic level of the microcontroller
  2. Cross zero and PSM connections - according to the table.
  3. Connect the library to your sketch. Initialize the dimmer. If you want an easy install mode dimmer.begin (NORMAL_MODE, ON)
  4. If need to connect several dimmers, there is create a separate object for each dimmer:

dimmerLamp dimmer1 (outputPin1, zero cross);

dimmerLamp dimmer2 (outputPin2, zero cross);

….

dimmerLamp dimmerN (outputPinN, zero cross);

 

 

Next is Dimming.

Examples, solutions.

January 31, 2021