Strange, unexpected behavior from RobotDyn Dimmer / RBDimmer

Hi all,

I am a complete newbie to Arduino (and in fact to coding of any sort), so please be gentle. :grimacing:

I have an 8A RobotDyn RBDimmer (link) that I'm using with an Arduino Nano clone board (ATMega328p with a CH340g USB-serial chip; link). I have a sketch running to gradually brighten a tungsten/incandescent bulb (not an LED, which I know can cause problems), and it is exhibiting weird behavior:

From 1% to 13% power, the bulb is lit at a constant brightness of about 60% (the brightness does not change between 1% and 14% power).

Then from 14% to 16% power, the bulb flickers at a high frequency (maybe 50 Hz or so).

Then from 16% to 17% power, the bulb turns on and off at a much lower frequency (maybe 5 Hz).

Again, this is a tungsten/incandescent bulb I'm using, not an LED.

It is also behaving weirdly above that (at 100% power it is not lighting LED bulbs at all), but I have a harder time deciphering that behavior.

The code I am running is pasted below (it was generated by ChatGPT... sorry :see_no_evil_monkey:. I'm just starting to learn to code).

I know the sketch involves the RBDimmer.h library, which I installed into the IDE (I'm using the v2 IDE).

Can anyone possibly explain (and/or provide a fix for) the weird behavior I'm seeing?

Thank you.

Here's the code:

[#include <RBDdimmer.h> // RobotDyn AC Dimmer library

#define AC_LOAD 3 // DIM/PWM pin connected to the RobotDyn module

#define ZC_PIN 2 // Zero-cross pin from the RobotDyn module (wired to D2, but handled internally)

// Create dimmer object (older library: only takes 1 argument)

dimmerLamp dimmer(AC_LOAD);

void setup() {

Serial.begin(9600);

// Initialize dimmer (older library handles ZC internally)

dimmer.begin(NORMAL_MODE, OFF);

dimmer.setPower(0);

Serial.println("Starting 30-minute gradual brightening...");

}

void loop() {

const int startBrightness = 0; // Start at 0%

const int endBrightness = 100; // End at 100%

const unsigned long totalTime = 30UL * 60UL * 1000UL; // calculating total time in milliseconds of fadeup; minutes, seconds per minute, milliseconds per second

const int steps = 100; // 1% increments

unsigned long delayPerStep = totalTime / steps; // calculating milliseconds per step

// Gradually brighten

for (int brightness = startBrightness; brightness <= endBrightness; brightness++) {

dimmer.setState(ON);

dimmer.setPower(brightness); // Set brightness (0–100%)

Serial.print("Brightness: ");

Serial.print(brightness);

Serial.println("%");

delay(delayPerStep);

}

// Hold full brightness indefinitely

Serial.println("Reached full brightness. Holding...");

dimmer.setPower(100);

dimmer.setState(ON);

while (true) {

delay(1000); // keep AC dimmer timing active

}

}](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966)

Leaving a blank lne between every actual line is not helpful. Did you use the code tag icon?
Screenshot 2025-10-15 at 14.44.00

ChatGPT makes lots of errors. It is better if you do not get a proper education, to start with a sample sketch contained in an appropriate library. However, when you are very new, you likely have no idea what the library will do for you.
If at all possible, do NOT use anything where the mains voltage is not isolated from your project. That is why we use LEDS. Old incandescent bulbs can be made to work, but for safety reasons, you need opto couplers for isolation.

I don't know what is causing your specific problem, as I am not going to spend time looking at libraries that promote potentially unsafe practices. Put the devices in the Amazon link away for your experienced future self.

Get the Arduino Cookbook and work your way through that. If your electronics knowledge is also at the beginner level, then the book Electronics Cookbook is a good start, but I always also recommend 2 or 3 additional.
Good luck.

I used the "format code" command in the IDE, and also used the code tag button here on the forum. It seems the IDE left the line breaks between the lines of code. Sorry if they shouldn't be there.

While I am brand new to Arduino, I am quite experienced working with electricity and have a couple of decades under my belt working with mains voltages, and am very familiar with how to do so safely. For my project, utilizing zero-cross-timed PWM, an incandescent bulb is used to test the project specifically because it is superior to LEDs as a test bed, with the tungsten filament responding directly to voltage applied, thereby avoiding other potential issues that could arise from incompatibility with LEDs.

Refusing to look at libraries that involve mains voltages would seem to limit you from a great variety of projects using Arduino to control household devices. And in browsing these forums, the RobotDyn dimmer specifically seems to be commonly used as an Arduino companion module.

Would anyone be willing to look at this code?

Many thanks.

I will take a SWAG (Scientific Wild Assuming Guess)It sounds like EMI (Electromagnetic Interference) feeding back through the power system IE the zero cross is failing. Post an annotated schematic showing exactly how this is wired.

Hi @gilshultz,

Thanks for the SWAG! :joy:

I don't have circuit schematic software, but I took a picture and labeled it. How's this?

If you look at the images on the Amazon page you linked it shows the dimmer Vcc at 5v and not 3.3. I would try powering it with the 5v.

Unfortunately that particular photograph on the Amazon listing is of an older model of the dimmer. The current revision of the dimmer is dual power 3.3v/5v.

(I tried the 5v power option just now just in case, but sadly the behavior is still the same.)

I had that same dimmer controlled by Esp (3.3V) without any hiccups. I tested it with halogen bulb and later used it with 2kW heating element.

I recommend to verify your wiring and your code. Give just different static timings to see how it behaves.

I'm confident in the wiring, but since I don't yet have the knowledge to check the code, I was hoping someone here would read the code to check it.

It's not easy task.
Just came to my mind, calibrating the zero crossing was the biggest obstacle. If you don't do it correctly, it can easily shift the phase cut delay from one half cycle to another.
I wrote about it in another topic some while ago.

Interesting. I was under the impression the RobotDyn dimmer handled all of that part without any need for calibration of the zero cross on my part. Perhaps this then is a hardware limitation of the RobotDyn dimmer?

It's "limitation" of zero crossing detection generally. In theory zero crossing doesn't have "duration". In practice it's significant, ~400us in this case. Some other circuit can be better or worst.

That doesn't cause any problem or flickering in the middle range, so if your problems are present there, you have other issues.

Is this a tungsten halogen bulb?
I don't see anything wrong with the code.

After a bit of testing I've found that all the bulbs I've tried (a tungsten incandescent and two different LEDs) work fine from 23% to 85%... they stay on, don't flicker, and don't even seem to get any brighter above 85%.

That said, for a purpose-built circuit to not function for its purpose below 23% or above 85% seems like a really big chunk of range. :man_shrugging:

Thank you for taking a look at the code!

(Re: the bulb... it's not a halogen bulb, just a regular old-fashioned tungsten incandescent; a generic old Edison-style bulb.)

That's how it is. Phase delay range 15-85% gives you power range 2-98% so those first and last 1500us don't have really any effect on power. And that's where the zero crossing "shift" causes problems, so if you don't bother to tinker with that, just limit the delays to 1500-8500us on your code.

Edit: Above delays were for my 50Hz setup... for 60Hz it would be ~1250-7100us

Are you 60Hz or 50Hz?

In the U.S. 60Hz.

I don't think that library measuers the frequency and makes the appropriate adjustments, seem like it's set-up for 50Hz only.

Might work OK with a different library.

Interesting.

I don't know of another library, but @kmin's comments above (about how 15-85% timing correlate to 2-98% power) appear accurate (or at least close enough), and even the 23-85% range I'm using appears to yield nearly the entire power range (based purely on unscientific visual observation).
So even if another library might optimize the functionality of the dimmer, it appears I'm in okay shape as is!