Troubles with dimmer

My goal is to eventually control a halogen light with a proximity sensor via a dimmer and Arduino. I am posting here because the product’s support has not been helpful, and there might be more information here.

My current challenge and question is more specific now that I have gotten further into the project. I have this AC Dimmer Module connected to my halogen, Arduino Uno, and power. I know that the halogen functions (I’ve plugged it directly into the wall). And it seems to work with this whole system to an extent because when I plug in the Arduino, the Arduino, the blue LED on the dimmer, and the connected halogen all flash upon setup. However, the simple code that I wrote is just trying to keep the halogen on (before venturing to coordinate it with a sensor). I am using the RBDDimmer.h library. I have print statements in the following code to track the state of the dimmer, which seems to be changing as intended, but I still don’t see a change happening in the halogen or the LED indicator (which I thought should mirror the halogen’s behavior). My code is below. Please help, and thanks!

#include <RBDdimmer.h>

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

dimmerLamp dimmer(outputPin);

void setup() {
  dimmer.begin(NORMAL_MODE, ON); //dimmer initialisation: name.begin(MODE, STATE) 
  dimmer.setPower(50);
  dimmer.setState(ON);
  Serial.begin(9600);
}

void loop() {
  dimmer.setState(ON); //name.setState(ON/OFF);
  delay(1000);
  Serial.println(dimmer.getState());
  dimmer.setState(OFF); //name.setState(ON/OFF);
  delay(1000);
  Serial.println(dimmer.getState());
}

How did you connect that dimmer module to arduino?

...Just a random thought but does the library have a 50/60Hz option? Or do you know if it's correct for the country where you live?

And sorry, what's the symptom? Is it stuck-on or stuck-off? Or, it gos on & off but won't dim?

What happens with the Arduino un-plugged? It should stay off.

You can try the Blink Example. The LED and the Halogen should both blink. If that works the output-side of the hardware is working.

The zero-crossing is a bit trickier but you can run the Digital Read Serial Example, but take-out the delay to read as fast as possible. If the zero-crossing detection (hardware) is working you should sometimes read high and sometimes read low. (Reading in a loop like this you will only-occasionally "catch" a zero crossing.

If those two things work, you've probably got a software problem (either your code or the library).

or the LED indicator (which I thought should mirror the halogen's behavior).

Possibly... It only takes a short-pulse to trigger the TRIAC. It's when in (the AC half-cycle) the pulse occurs that determines the dim level, so the pulse width may not change (depending on how the library is written). And if the pulse is short-enough you might not see the LED come-on at all.

Attached is a picture of the connections. The code I have provided should turn the halogen (and LED) on and off every other second. I put print statements in there to check and show that the state IS turning on and off when you look in the serial monitor, so I am confused why the halogen (or LED) is not turning on and off. They only blink upon initialization. I cannot get them to stay on beyond that initial flash.

I cannot find the Blink example to test.

I don’t understand why the state is apparently changing but there is no effect on the dimmer board or the halogen attached to it.

And what if you just upload blink but use pin 13?

I cannot find the Blink example. Can anyone link me to that?

In all of the code I find online, their blue LEDs are always lit when the light is on, but my blue LED is never on except for that initial flash. I have confirmed that the power can be set and changed, as can the state. I cannot find a way to keep that LED on.

Did you see this in the specs.you linked to…???

Theory:
Dimming can be achieved by Pulse Skip Modulation:

Method 1 — One or more cycles (sine wave signal) are transferred to the load, while following one or several cycles are blocked.
Method 2 — Partial transferrence of each sine wave to the load.
Method 3 — Generation of modulated full sine signal of different frequency up to few hundred hertz. This method requires specialized powerfull AC generators with different modulation.
Methods 1 and 2 are the easiest to execute with the help of a Dimmer and programm code: in both cases, there is a need of circuit that detects the zero crossing and can control a triac.

srgochman:
I cannot find the Blink example. Can anyone link me to that?

REALLY? Is Google so broken today? Lot of pages on it. Also, it's the 3th example in the Basics example folder and probably the first sketch you came in contact with...

I am running some new code (below), which has got the light working, randomly at least. I am getting this weird situation in which the whole system flickers when I put an object near the boards (without even touching anything). Any ideas how to resolve this? The dimmer company said they’ll just send me a new one but I have a feeling that won’t help this problem or the problem I’ve been having (power is >0 and state is ON, but no light goes on). Thoughts?

Pin    |  Interrrupt # | Arduino Platform
---------------------------------------
2      |  0            |  All -But it is INT1 on the Leonardo
3      |  1            |  All -But it is INT0 on the Leonardo
18     |  5            |  Arduino Mega Only
19     |  4            |  Arduino Mega Only
20     |  3            |  Arduino Mega Only
21     |  2            |  Arduino Mega Only
0      |  0            |  Leonardo
1      |  3            |  Leonardo
7      |  4            |  Leonardo
The Arduino Due has no standard interrupt pins as an iterrupt can be attached to almosty any pin. 
 
In the program pin 2 is chosen
*/
int AC_LOAD = 13;    // Output to Opto Triac pin
int dimming = 1024;  // Dimming level (0-128)  0 = ON, 128 = OFF
 
void setup()
{
  Serial.begin(9600);
  pinMode(AC_LOAD, OUTPUT);// Set AC Load pin as output
  attachInterrupt(0, zero_crosss_int, RISING);  // Choose the zero cross interrupt # from the table above
}
 
//the interrupt function must take no parameters and return nothing
void zero_crosss_int()  //function to be fired at the zero crossing to dim the light
{
  // Firing angle calculation : 1 full 50Hz wave =1/50=20ms 
  // Every zerocrossing thus: (50Hz)-> 10ms (1/2 Cycle) 
  // For 60Hz => 8.33ms (10.000/120)
  // 10ms=10000us
  // (10000us - 10us) / 128 = 75 (Approx) For 60Hz =>65
 
  int dimtime = (75*dimming);    // For 60Hz =>65    
  delayMicroseconds(dimtime);    // Wait till firing the TRIAC    
  digitalWrite(AC_LOAD, HIGH);   // Fire the TRIAC
  delayMicroseconds(8.33);         // triac On propogation delay 
         // (for 60Hz use 8.33) Some Triacs need a longer period
  digitalWrite(AC_LOAD, LOW);    // No longer trigger the TRIAC (the next zero crossing will swith it off) TRIAC
}
 
void loop()  {
for (int i=880; i <= 902; i++){
  if (i==902){
    dimming=0;
    delay(100);
    }
  else{
    dimming=i;
    Serial.println(i);
    delay(5);
  }
   }
 
for (int i=902; i >= 880; i--){
    if (i==902){
    dimming=0;
    delay(100);
    }
    else{
    dimming=i;
    Serial.println(i);
    delay(5);
    }
   }
   
}

That really sounds like a GND problem. To check that (/ other basics), I asked to simply run blink but apparently you don't want our help... ::slight_smile:

I’m so sorry! I got distracted by a marginal success (but maybe random) of flicker through the halogen. But I found this code (it was in an example folder from a different library) and it works!! The halogen and dimmer LED both blink, and when I replace the light.blink with light.fade, the LED fades but the halogen doesn’t (it only blinks).

// Arduino RBD Light Library v2.1.7 Example - Blink the Arduino LED without delay.
// https://github.com/alextaujenis/RBD_Light
// Copyright 2016 Alex Taujenis
// MIT License

#include <RBD_Timer.h> // https://github.com/alextaujenis/RBD_Timer
#include <RBD_Light.h> // https://github.com/alextaujenis/RBD_Light

RBD::Light light(13);

void setup() {
  light.blink(250,250);
}

void loop() {
  light.update();
}

I am not really advanced enough to know why this works whereas the past code doesn’t. My ultimate goal is to get the halogen to shine at intermediate brightness levels (not just on and off) and eventually control that with analog input from a sensor. Any thoughts?

Also, @DVDdoug, I’m not sure to find the Digital Serial Read Example that you mentioned, assuming it is script more than just reading serial. Can you direct me to that example if it is still the next step in troubleshooting?

I appreciate all of y’all’s help very much!

Hi, here is a bit more progress; hopefully this information will be helpful for making recommendations for solutions. I appreciate all of your help!

After successfully running the Blink example (code in last post) based on your recommendation, I tried the Fade example. As described in my last response, the LED on the dimmer board faded nicely, but the attached light only turned off when the brightness value hit 0 and turned on the rest of the time (no intermediate brightness values).

Then I added in a proximity sensor (see full code below) so that I can get intermediate inputs besides 0 and 100%. The LaserPING proximity sensor is working correctly. This would allow me to control the light brightness with distance to the sensor. I still get the same issue: light is off when proximity is below my set threshold and the light is on at any distance value above the threshold, even if it is not the maximum distance. Here is a video showing this. You can see the blue LED and another external white LED dimming and the attached halogen not dimming; the halogen only turns off completely at the minimum distance.

Printing the BrightnessPercentage, it seems to me that the dimmer is getting the right brightness value (not just on or off), but the attached light isn’t showing those intermediate values. To be sure that this issue wasn’t just due to the light being a halogen (image attached), I replaced the bulb with an incandescent and later a dimmable LED bulb and got the same results both times.

I’m not sure why this is happening, so I am looking to your more advanced experience. Thanks so much for the help!

// Arduino RBD Light Library v2.1.7 Example - Fade the Arduino LED without delay.
// https://github.com/alextaujenis/RBD_Light
// Copyright 2016 Alex Taujenis
// MIT License

#include <RBD_Timer.h> // https://github.com/alextaujenis/RBD_Timer
#include <RBD_Light.h> // https://github.com/alextaujenis/RBD_Light

#define rLED 6
#define gLED 5
#define bLED 3

int signal=11;
int dist;
int dist_adj;
unsigned long pulseduration=0;

RBD::Light light(10);

void setup() {
//  light.fade(500,250,500,250);
  Serial.begin(9600);

  pinMode(rLED, OUTPUT);
  pinMode(gLED, OUTPUT);
  pinMode(bLED, OUTPUT);
  pinMode(signal, INPUT);
}

void measureDistance() {
 // set pin as output so we can send a pulse
 pinMode(signal, OUTPUT);
// set output to LOW
 digitalWrite(signal, LOW);
 delayMicroseconds(5);
 
 // now send the 5uS pulse out to activate Ping)))
 digitalWrite(signal, HIGH);
 delayMicroseconds(5);
 digitalWrite(signal, LOW);
 
 // now we need to change the digital pin
 // to input to read the incoming pulse
 pinMode(signal, INPUT);
 
 // finally, measure the length of the incoming pulse
 pulseduration=pulseIn(signal, HIGH);

  // divide the pulse length by half
 pulseduration=pulseduration/2; 
 
 dist = int(pulseduration/29.1); // for centimeters
// dist = int(pulseduration/74); // for inches
}

void lightLED() {
  if(dist_adj>200){
    analogWrite(rLED, 255);
    analogWrite(gLED, 255);
    analogWrite(bLED, 255);
  }
  else if(dist_adj>0){
    analogWrite(rLED, dist_adj);
    analogWrite(gLED, dist_adj);
    analogWrite(bLED, dist_adj);
  }
  else{
    analogWrite(rLED, 0);
    analogWrite(gLED, 0);
    analogWrite(bLED, 0);
  }
  dist_adj=dist*4-200;
  delay(10);
}

void loop() {
  measureDistance();
  lightLED();

  light.update();
  light.setBrightnessPercent(dist_adj);
  Serial.println(light.getBrightnessPercent());
}

You use a mains bulb? (No ballast/power supply/transformer or led)

Do you life in a 50 or 60 Hz country?

Does the library need modifications for that?

Do you actually get sync pulses?

Hi septillion,

I will try to answer your questions the best I can. I’m not exactly sure what you mean by “mains” bulb; I am fairly new to this. But I do know my setup. I have a halogen light (full picture attached) or an LED or candescent (they all worked the same) screwed into a socket that goes into the load on the Robotdyn AC dimmer module. Then the AC-IN on the dimmer module goes through an extension cord to a surge protector and then to the wall. I assume there is a ballast built into the bulb because the light doesn’t burn out, but I am not sure. I haven’t bought anything in addition to these materials.

I live in the US, which I understand operates on 60 Hz. I don’t see an opportunity to specify 60 Hz in the RBD_light.h or RBD_Timer.h libraries. The only mention of frequency I see is in the RBD_Timer.h library, which has a method “void setHertz(int value)” but I am not sure how to implement that into my code or if it is appropriate to do so.

I am also not sure what you mean by sync pulses. If you are talking about the blinking of pin 13’s LED from the bootloader, that built-in LED on the Arduino does flash at startup, but the halogen does not turn on at all until after that. (FYI, the halogen did blink with pin 13 on startup with other past code, but not here.) If you are asking if the halogen pulses when it is on, it does not; it is a steady stream of light when it is on.

Hopefully this is insightful feedback. Thanks again for the help.

#include <RBD_Timer.h> // https://github.com/alextaujenis/RBD_Timer
#include <RBD_Light.h> // https://github.com/alextaujenis/RBD_Light

The libraries you are using do not support the zero cross timing required for the module.

With a bit more experience with the system and confirmation of the wiring, can you go back and use the RBDdimmer.h from the first example. There are several library examples as well.

I would move the output from pin 13. The connected led and resistor may be influencing the signal to triac.

NOTE - I've made a dimmer a long time ago with a different microcontroller, but I've never done it with the Arduino and I don't know anything about those libraries. (I didn't use any libraries. I wrote the whole program myself.)

Also, @DVDdoug, I'm not sure to find the Digital Serial Read Example that you mentioned, assuming it is script more than just reading serial. Can you direct me to that example if it is still the next step in troubleshooting?

...I am also not sure what you mean by sync pulses.

[u]Built-In Examples[/u]

The "sync pulses" are the zero-crossing signals. The idea is to read the zero crossing input and write the results to the Serial Monitor so you can see the results on your computer screen. It's a crude test but if you read 1 (high) sometimes and 0 (low) sometimes then it's probably working.

The halogen and dimmer LED both blink, and when I replace the light.blink with light.fade, the LED fades but the halogen doesn't (it only blinks).

The regular fade example from "Built In Examples" uses [u]PWM[/u] which runs at about 400Hz. It's turning the LED on & off faster than the eye can see so it appears to dim.

If you run PWM through an incandescent lamp, it can't respond as fast as an LED so it truly does dim.

But here's the problem... AC dimmers (including yours) use a TRIAC. Once a TRIAC is turned-on it stays on until the current drops to zero. That normally happens at the next AC zero crossing. If you re-trigger the TRIAC 500 times per second it will be on most of the time so it doesn't dim properly.

AC dimmers work by triggering the TRAIC with a short-pulse at some known-time after the zero crossing. The pulse has to be "short" because it has to be off before the next zero-crossing comes-around.

The TRIAC holds-on until the next zero crossing so the brightness is related to the percentage of cycle that the TRIAC is on. If you trigger it at the beginning of the half-cycle the light will be bright. If it's on for only for a short time at the end of the half-cycle the light is dim.

[u]How TRIAC dimmers work[/u]

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Please show how you have connected the power supply and label pins used.

Can you tell us your electronics, programming, arduino, hardware experience?

Thanks.. Tom.. :slight_smile:

Hi all,

Attached is a schematic of my setup and wiring. It might be worth knowing that I have peripherals setup in there too like a proximity sensor and tiny LED, but taking them out doesn’t seem to make a difference. My current code below uses a rough sine wave to test setting various powers:

#include <RBDdimmer.h>//

#define USE_SERIAL  Serial
#define outputPin  10 
#define zerocross  2 // 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() {
  USE_SERIAL.begin(9600);
  pinMode(outputPin, OUTPUT);
  pinMode(zerocross, INPUT);
  dimmer.begin(NORMAL_MODE, ON); //dimmer initialisation: name.begin(MODE, STATE) 
  dimmer.toggleSettings(0, 100); //Name.toggleSettings(MIN, MAX);
  dimmer.setPower(50);
  dimmer.setState(ON); // state: dimmer1.setState(ON/OFF);
}

void loop() {
  for (int i = 0; i <= 100; i++) {
    dimmer.setPower(i);
    USE_SERIAL.println(dimmer.getPower());
    delay(10);
  }
  for (int i = 100; i > 0; i--) {
    dimmer.setPower(i);
    USE_SERIAL.println(dimmer.getPower());
    delay(10);
  }
}

When I run this code and plug in the light bulb, the bulb is off when the power is on the lower end of the “sine” wave and then on when the power is at the higher end of the “sine” wave. There are no intermediate brightnesses except for the moment of the bulb itself turning on.

@TomGeorge: I am fairly new to a lot of this, as my background is in architecture, and this project’s goal is to make an interactive light system that brightens as someone approaches it.

@DVDdoug: Thanks for all of that information. That helps me understand a bit more. As a test, I ran USE_SERIAL.println(analogRead(zerocross)); to see when 0 is displayed, but I have a feeling this is not what you were asking me to try, because I was getting numbers decreasing and then leveling out at around 230.

The discussion around TRIACs makes me think that I am not running the right code anymore. I returned to the above code using the RBDDdmmer.h library as recommended by @cattledog, but the code I am using doesn’t seem to incorporate the pulse, interrupts, or a function for zerocrossing (which might allow for specifying 60 Hz if it existed). It all seems incomplete to me, but I don’t have the experience to resolve these issues. Some other code I found that has some of the potentially-missing elements described earlier in this paragraph are here:

Hopefully you all will have a better idea of how to combine them or which is the best way to achieve my dimming goal. Thanks!

If the bulb is lighting up but not dimming, and the serial monitor is showing changing values, you might have a problem with the actual AC dimmer itself or the bulb. Try a different bulb, maybe less power and maybe with the ability to be dimmed. Some bulbs cannot be dimmed (though they can be manipulated into it, with poor results most of the time), I have messed around with home automation before, and specific bulbs are sometimes required. That might be your only problem, but if it is the actual AC dimmer, make sure you get the company to replace it. Hope this helps!