Skip to Content

← Tasmota Configuration | Contents

Arduino IDE Library



Version 1.0 - 2019

Do you create Arduino project? Arduino library: ESP32/8266, STM32, Arduino boards. Code examples.

Info
Download dimmer library: RBDDimmer



Connection I/O

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 pin 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 be the same logic level as a microcontroller:

  • 5V for Uno, Nano, Leonardo, and Mega
  • 3.3V for STM32, ESP32, ESP8266


Connection Table

Board INPUT Pin Zero Cross OUTPUT Pin
Leonardo D7 (NOT CHANGEABLE) D0-D6, D8-D13
Mega D2 (NOT CHANGEABLE) D0-D1, D3-D70
UNO / NANO D2 (NOT CHANGEABLE) D0-D1, D3-D20
ESP8266 D1(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)
ESP32 GPIO: 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, 23 GPIO: 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 Due D0-D53 D0-D53
STM32, Blue Pill (STM32F1) 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.

Info
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. For boards WITHOUT changeable ZERO-CROSS input pin (AVR, Arduino M0/Zero):

cpp
dimmerLamp dimmer(4); // dimmer output DIM/PSM is initialized on the pin 4

b. For boards with changeable zero-cross (ESP32, ESP8266, Arduino Due):

cpp
dimmerLamp dimmer(4, 2); // dimmer output DIM/PSM on pin 4, zero-cross on pin 2


2. Function begin

Port initialization, timer and external interrupt from zero-cross:

cpp
dimmer.begin(NORMAL_MODE, ON/OFF);

Parameter 1: Dimmer working modes:

  • NORMAL_MODE - make dimmer work in defined value from 0 to 100 (%)
  • Example: \RBDdimmer\examples\SimpleDimmer
  • TOGGLE_MODE - smooth change of dimming value up or down in a defined range
  • Example: \RBDdimmer\examples\SimpleToggleDimmer

Parameter 2: ON/OFF:

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


3. Function setPower

Sets dimming value from 0 to 100%:

cpp
dimmer.setPower(90);


4. Function getPower

Display current dimming value:

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


5. Function setMode

Sets and changes the work mode:

cpp
dimmer.setMode(NORMAL_MODE/TOGGLE_MODE);


6. Function getMode

Displays values of current work mode:

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


7. Function setState

Sets dimming state ON/OFF:

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


8. Function getState

Displays current state of the dimmer:

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


9. Function changeState

Changes dimmer state to the opposite one:

cpp
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: \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)
cpp
#include 

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

// For ESP8266, ESP32, Arduino due boards
// dimmerLamp dimmer(outputPin, zerocross);

// For MEGA, Leonardo, UNO, Arduino M0, Arduino Zero
dimmerLamp dimmer(outputPin);

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);
  }
}


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 dimmer.setState(ON/OFF)
cpp
#include 

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

// For ESP8266, ESP32, Arduino due boards
// dimmerLamp dimmer(outputPin, zerocross);

// For MEGA, Leonardo, UNO, Arduino M0, Arduino Zero
dimmerLamp dimmer(outputPin);

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);
  }
}


Example: Dimming with a potentiometer

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)
cpp
#include 

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

// For ESP8266, ESP32, Arduino due boards
// dimmerLamp dimmer(outputPin, zerocross);

// For MEGA, Leonardo, UNO, Arduino M0, Arduino Zero
dimmerLamp dimmer(outputPin);

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, max, 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%:

cpp
#include 

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

// For ESP8266, ESP32, Arduino due boards
// dimmerLamp dimmer(outputPin, zerocross);

// For MEGA, Leonardo, UNO, Arduino M0, Arduino Zero
dimmerLamp dimmer(outputPin);

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:

cpp
#include 

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

#define LAMPMAXVALUE 100

// For ESP8266, ESP32, Arduino due boards
// dimmerLamp dimmer(outputPin, zerocross);

// For MEGA, Leonardo, UNO, Arduino M0, Arduino Zero
dimmerLamp dimmer(outputPin);

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 or more dimmers connection

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

For each dimmer, need to connect the Dim pin.


The code

cpp
#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

// For ESP8266, ESP32, STM32, Arduino due boards
// dimmerLamp dimmer1(outputPin1, zerocross);
// dimmerLamp dimmer2(outputPin2, zerocross);

// For MEGA, Leonardo, UNO, Arduino M0, Arduino Zero
dimmerLamp dimmer1(outputPin1); // initialise port for dimmer 1
dimmerLamp dimmer2(outputPin2); // initialise port for dimmer 2
dimmerLamp dimmer3(outputPin3); // initialise port for dimmer 3

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

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


Multi-Channel Code Example

cpp
#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 outputPin4  9   // Dim pin for dimmer 4
#define zerocross  5    // for boards with CHANGEABLE input pin

// For ESP8266, ESP32, Arduino due boards
// dimmerLamp dimmer1(outputPin1, zerocross);
// dimmerLamp dimmer2(outputPin2, zerocross);

// For MEGA, Leonardo, UNO, Arduino M0, Arduino Zero
dimmerLamp dimmer1(outputPin1); // initialise port for dimmer 1
dimmerLamp dimmer2(outputPin2); // initialise port for dimmer 2
dimmerLamp dimmer3(outputPin3); // initialise port for dimmer 3
dimmerLamp dimmer4(outputPin4); // initialise port for dimmer 4

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);
  // ...
}

← Tasmota Configuration | Contents