Download dimmer library: RBDDimmer

  • Connection I/O
  • Library and Functions
  • Examples
    • Example: Dimming value through the serial port
    • Example: Power ON/OFF
    • Example: Dimming with a potentiometer
    • Example: Toggle Dimming values
    • Example: Smoothly Dimming with buttons action
  • Two or more dimmers connection

Dimmer is connected to Arduino controllers via two digital pins. First (Zero) to control the passing of Phase Null of AC, which is used to initiate the interrupt signal. Second (DIM/PSM) to control (dimming) current.  The Zero requires connection to designated microcontroller pins (which are different depending on the model of Uno, Nano, Leonardo, and Mega) since it is tied to microcontroller interrupts.
The voltage of dimmer VCC needs to can be to the same logic level as a microcontroller. 5V for Uno, Nano, Leonardo, and Mega, and 3.3V for STM32, ESP32, ESP8266.

The table of connections:

BoardINPUT Pin
Zero Cross
OUTPUT Pin
LeonardoD7 (NOT CHANGEABLE)D0-D6, D8-D13
MegaD2 (NOT CHANGEABLE)D0-D1, D3-D70
UNO
NANO
D2 (NOT CHANGEABLE)D0-D1, D3-D20
ESP8266D1(GPIO5), D5(GPIO14), D7(GPIO13), D2(GPIO4), D6(GPIO12), D8(GPIO15)D0(GPIO16), D2(GPIO4), D6(GPIO12), D8(GPIO15), D1(GPIO5), D5(GPIO14), D7(GPIO13)
ESP32GPIO: 36, 39, 32, 25, 27, 12, 7, 2, 4, 17, 18, 21, 22, 34, 35, 33, 26, 14, 13, 15, 0, 16, 5, 19, 1, 23GPIO: 32, 25, 27, 12, 15, 0, 16, 5, 19,
3, 22, 33, 26, 14, 13, 2, 4, 17, 18, 21, 1, 23
Arduino M0
Arduino Zero
D7 (NOT CHANGEABLE)D0-D6, D8-D13
Arduino DueD0-D53D0-D53
STM32, Blue Pill (STM32F1), Etc…PA0-PA15, PB0-PB15
PC13-PC15
PA0-PA15, PB0-PB15
PC13-PC15

Library and Functions

In Arduino, the dimmer is controlled with RBDdimmer.h library, which uses external interrupts and process time interrupts. It simplifies the code writing and gives more processing time for the main code. This is why you can control multiple Dimmers from one microcontroller.

Download dimmer library: RBDDimmer

You can find a few examples in the library. We are constantly updating our library, so we recommend checking for website updates or subscribing to our newsletter.

This library can simplify user code with the following functions:

1. Function dimmerLamp – this function initializes the number of operating pin and is defined by the user

a. dimmerLamp dimmer(4); dimmer output DIM/PSM is initialized on the pin 4 for the bords WITHOUT changable ZERO-CROSS input pin (AVR, Arduino M0/Zero)

b. dimmerLamp dimmer(4, 2); 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)

2. Function 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 solutions implies change of dimming values by means of hardware timer, without using the cycle code. Example of this mode located in \RBDdimmer\examples\SimpleToggleDimmer

Parameter 2: ON/OFF.

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

3. Function setPower sets dimming value from 0 to 100%

dimmer.setPower(90);

4. Function getPower to display current dimming value

Serial.print(dimmer.getPower()); //Result 0~100 int

5. Function setMode sets and changes the work mode (NORMAL_MODE and TOGGLE_MODE)

dimmer.setMode(NORMAL_MODE/TOGGLE_MODE);

6. Function getMode displays values of current work mode

Serial.print(dimmer.getPower()); //Result 0 (NORMAL_MODE) or 1 (TOGGLE_MODE)

7. Function setState sets dimming state ON/OFF

dimmer.setState(ON); delay(100); dimmer.setState(OFF);

8. Function getState displays current state of the dimmer

Serial.print(dimmer.getState()); //Result 0 (OFF) or 1 (ON)

9. Function changeState changes dimmer state to the opposite one

dimmer.setState(ON); delay(100); dimmer.changeState(); delay(100);

10. Function toggleSettings smooth change of dimming value up or down in a defined range Example located in \RBDdimmer\examples\SimpleToggleDimmer.

Examples:

Example: Dimming value through the serial port.

The following sketch is meant to define the dimming value through the serial port of the controller:

  • using USE_SERIAL.begin;
  • void printSpace() function is used for adding of space after functional data;
  • void loop() serial port evaluator, used to register and define values in dimmer.setPower(outVal);
#include <RBDdimmer.h>//

//#define USE_SERIAL  SerialUSB //Serial for boards whith USB serial port
#define USE_SERIAL  Serial
#define outputPin  12 
#define zerocross  5 // for boards with CHANGEBLE input pins

//dimmerLamp dimmer(outputPin, zerocross); //initialase port for dimmer for STM32, ESP8266, ESP32, Arduino due boards
dimmerLamp dimmer(outputPin); //initialase port for dimmer for MEGA, Leonardo, UNO, Arduino M0, Arduino Zero

int outVal = 0;

void setup() {
  USE_SERIAL.begin(9600); 
  dimmer.begin(NORMAL_MODE, ON); //dimmer initialisation: name.begin(MODE, STATE) 
  USE_SERIAL.println("Dimmer Program is starting...");
  USE_SERIAL.println("Set value");
}

void printSpace(int val)
{
  if ((val / 100) == 0) USE_SERIAL.print(" ");
  if ((val / 10) == 0) USE_SERIAL.print(" ");
}

void loop() {
  int preVal = outVal;

  if (USE_SERIAL.available())
  {
    int buf = USE_SERIAL.parseInt();
    if (buf != 0) outVal = buf;
    delay(200);
  }
  dimmer.setPower(outVal); // setPower(0-100%);

  if (preVal != outVal)
  {
    USE_SERIAL.print("lampValue -> ");
    printSpace(dimmer.getPower());
    USE_SERIAL.print(dimmer.getPower());
    USE_SERIAL.println("%");

  }
  delay(50);

}

Example: Power ON/OFF

The following sketch is meant to turn the lamp on/off with a button.

  • pinMode(14, INPUT); the button is connected to pin 14;
  • void loop() ON/OFF button evaluator of dimmer in dim.setState(ON/OFF);

Example: Dimming with a potentiometer

#include <RBDdimmer.h>//

#define outputPin  12 
#define zerocross  5 // for boards with CHANGEBLE input pins

//dimmerLamp dimmer(outputPin, zerocross); //initialase port for dimmer for ESP8266, ESP32, Arduino due boards
dimmerLamp dimmer(outputPin); //initialase port for dimmer for MEGA, Leonardo, UNO, Arduino M0, Arduino Zero

int buttonRed = 0;

void setup() {
  dimmer.begin(NORMAL_MODE, ON); //dimmer initialisation: name.begin(MODE, STATE) 
  dimmer.setPower(50);
  pinMode(14, INPUT);
}

void loop() {
  button = digitalRead(14);  
  if (button == 1)
  {
    delay(50);
    dimmer.setState(ON); //.setState(ON/OFF);
  }  
  if (button == 0)
  {
    delay(50);
    dimmer.setState(OFF); //.setState(ON/OFF);
  }
}

The following sketch is meant to define dimming value through the potentiometer:

  • The potentiometer values are changing in the range from 0 to 1023;
  • potentiometer values are converted through the map function to values from 0 to 100% and saved in dimmer.setPower(outVal);
#include <RBDdimmer.h>//

#define outputPin  12 
#define zerocross  5 // for boards with CHANGEABLE input pins

//dimmerLamp dimmer(outputPin, zerocross); //initialase port for dimmer for ESP8266, ESP32, Arduino due boards
dimmerLamp dimmer(outputPin); //initialase port for dimmer for MEGA, Leonardo, UNO, Arduino M0, Arduino Zero

int outVal = 0;

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

void loop() 
{
  outVal = map(analogRead(0), 1, 1024, 100, 0); // analogRead(analog_pin), min_analog, max_analog, 100%, 0%);
  dimmer.setPower(outVal); // name.setPower(0%-100%)
}

Example: Toggle Dimming values

The following sketch is meant to define by function the smooth changes of dimming values in a range of defined values.

  • values are defined in a range from 0 to 100%
#include <RBDdimmer.h>//

#define outputPin  12 
#define zerocross  5 // for boards with CHANGEBLE input pins

//dimmerLamp dimmer(outputPin, zerocross); //initialase port for dimmer for ESP8266, ESP32, Arduino due boards
dimmerLamp dimmer(outputPin); //initialase port for dimmer for MEGA, Leonardo, UNO, Arduino M0, Arduino Zero

void setup() {
  dimmer.begin(TOGGLE_MODE, OFF); //dimmer initialisation: name.begin(MODE, STATE) 
  dimmer.toggleSettings(0, 70); //Name.toggleSettings(MIN, MAX);
  dimmer.setState(ON); // state: dimmer1.setState(ON/OFF);
}
void loop() {
  ...
  dimmer.setState(ON); // set power to MAX value
  ...
  dimmer.setState(OFF); // set power to MIN value
}

Example: Smoothly Dimming with buttons action

The following sketch is meant to smoothly turn the dimmer ON after pressing the first button and turn it OFF after pressing a second button

#include <RBDdimmer.h>

#define outputPin  12 
#define zerocross  5 // for boards with CHANGEABLE input pins

#define LAMPMAXVALUE 100

//dimmerLamp dimmer(outputPin, zerocross); //initialase port for dimmer for ESP8266, ESP32, Arduino due boards
dimmerLamp dimmer(outputPin); //initialase port for dimmer for MEGA, Leonardo, UNO, Arduino M0, Arduino Zero

int stateL = 0, valLamp;
int mainLamp = 0;
int buttonRed = 0; 
int buttonBlue = 0; 
bool setLamp = true;

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

void RiseFallLamp(bool RiseFallInt)
{
  if ((RiseFallInt == true) && (mainLamp < LAMPMAXVALUE)) mainLamp++;
  else if ((RiseFallInt != true) && (mainLamp > 0)) mainLamp--;
}

bool setLampState(int val)
{
  bool ret;
  if (val >= 1) ret = true;
  else ret = false;
  return ret;
}

void readButtonState()
{
  buttonRed = digitalRead(13);
  buttonBlue = digitalRead(15);
  
  if (buttonRed < 1) stateL++;
  if (buttonBlue < 1) stateL--;
  if (stateL < 0) stateL = 0;
  if (stateL > 1) stateL = 1;
}

void loop() {
  readButtonState();
  dimmer.setPower(mainLamp); // setPower(0-100%);
  RiseFallLamp(setLampState(stateL));
  delay(25);
}

Two and more dimmers to one phase AC. Connecting and code.

Z-C (ZeroCross). ZC pin needs only for 1-st dimmer. For the next dimmers don’t need to connect Z-C.

For each dimmer, need to connect the Dim pin.
The code:

#define outputPin1  12  // Dim pin for dimmer 1
#define outputPin2  11  // Dim pin for dimmer 2
#define outputPin3  10  // Dim pin for dimmer 3
#define zerocross  5 // for boards with CHANGEABLE input pin, ESP8266, ESP32, STM32, Arduino due boards

//dimmerLamp dimmer1(outputPin1, zerocross); dimmerLamp dimmer2(outputPin2, zerocross); //initialase port for dimmer for ESP8266, ESP32, STM32, Arduino due boards
dimmerLamp dimmer1(outputPin1); //initialase port for dimmer 1 for MEGA, Leonardo, UNO, Arduino M0, Arduino Zero
dimmerLamp dimmer2(outputPin2); //initialase port for dimmer 2 for MEGA, Leonardo, UNO, Arduino M0, Arduino Zero
dimmerLamp dimmer3(outputPin3); //initialase port for dimmer 3 for MEGA, Leonardo, UNO, Arduino M0, Arduino Zero

void setup() {
  dimmer1.begin(NORMAL_MODE, ON);
  dimmer2.begin(NORMAL_MODE, ON);
  dimmer3.begin(NORMAL_MODE, ON);
    
}
void loop() {
....
  dimmer1.setPower(outVal1);
  dimmer2.setPower(outVal2);
  dimmer3.setPower(outVal3);
....  
}

2 and 4-lines dimmers to one-phase AC. Connecting and code.

The 2 and 4 lines dimmers have 1 ZC and 2-4 Dim pins.

The code:

#define outputPin1  12  // Dim pin for dimmer 1
#define outputPin2  11  // Dim pin for dimmer 2
#define outputPin3  10  // Dim pin for dimmer 3
#define outputPin3  9   // Dim pin for dimmer 4
#define zerocross  5 // for boards with CHANGEABLE input pin

//dimmerLamp dimmer1(outputPin1, zerocross); dimmerLamp dimmer2(outputPin2, zerocross); //initialase port for dimmer for ESP8266, ESP32, Arduino due boards
dimmerLamp dimmer1(outputPin1); //initialase port for dimmer 1 for MEGA, Leonardo, UNO, Arduino M0, Arduino Zero
dimmerLamp dimmer2(outputPin2); //initialase port for dimmer 2 for MEGA, Leonardo, UNO, Arduino M0, Arduino Zero
dimmerLamp dimmer3(outputPin3); //initialase port for dimmer 3 for MEGA, Leonardo, UNO, Arduino M0, Arduino Zero
dimmerLamp dimmer4(outputPin4); //initialase port for dimmer 3 for MEGA, Leonardo, UNO, Arduino M0, Arduino Zero

void setup() {
  dimmer1.begin(NORMAL_MODE, ON);
  dimmer2.begin(NORMAL_MODE, ON);
  dimmer3.begin(NORMAL_MODE, ON);
  dimmer4.begin(NORMAL_MODE, ON);    
}
void loop() {
....
  dimmer1.setPower(outVal1);
  dimmer2.setPower(outVal2);
  dimmer3.setPower(outVal3);
  dimmer4.setPower(outVal4);  
....  
}

How many dimmers can connect to the microcontroller:

The number of dimmers is limited by the available output pins of your microcontroller. For example, ESP32 can connect 22 dimmers.

3 Comments

  1. Dmitry

    Dimmer library and example not compiling on arduino ide with nodemcu v3 (ESP8266)

    18n9kkm.llyfk\SimplePotentiometer\SimplePotentiometer.ino:73:29: error: cannot convert ‘bool’ to ‘ON_OFF_typedef’
    73 | dimmer.begin(NORMAL_MODE, ON); //dimmer initialisation: name.begin(MODE, STATE)
    | ^~
    | |
    | bool
    In file included from C:\Temp\.arduinoIDE-unsaved202401-4896-18n9kkm.llyfk\SimplePotentiometer\SimplePotentiometer.ino:59:
    c:\Users\������\Documents\Arduino\libraries\RBDdimmer\src/RBDdimmer.h:63:68: note: initializing argument 2 of ‘void dimmerLamp::begin(DIMMER_MODE_typedef, ON_OFF_typedef)’
    63 | void begin(DIMMER_MODE_typedef DIMMER_MODE, ON_OFF_typedef ON_OFF);
    | ~~~~~~~~~~~~~~~^~~~~~

    exit status 1

    Compilation error: cannot convert ‘bool’ to ‘ON_OFF_typedef’

  2. Dmitry

    in library(.h-file) downloaded this page not definde variables ‘ON’ and ‘OFF’.
    instead of them ‘OND’ and ‘OFFD’.
    typedef enum
    {
    OFFD = false,
    OND = true
    } ON_OFF_typedef;

    I replase in lib code ON -> OND and OFF -> OFFD. Lib code finaly compiled but lib not work in hardware.
    Too bad!, It’s a pity!

  3. Dmitry

    after changes in the library code. Everything worked.
    It’s a pity that there is no wiring diagram.
    for reference:
    the lamp is connected to the N and OUT pins

Leave a Reply

Your email address will not be published. Required fields are marked *