Unintended, instant LED Power-UP right after turning on 12V SMPS

Hi all! I made a 10W LED Color and Brightness controller for my own use. despite it works well, there's a problem.

I use 12V SMPS to power LED module, and when I turn on SMPS, at that instant, LED turns on and off very quickly.

despite I added a relay module to solve this problem, but it seems like that both NO - COM and NC - COM are short when module is not powered with 5V VCC.

(1) Why this happens?
(2) How Can I prevent LED turning on right after supplying 12V input, and before arduino is on?

[video]

[schematics of project]

[arduino code]

#include <FastLED.h>

#define LED_R 9
#define LED_G 10
#define LED_B 11

#define STRIP_DIN 6
#define STRIP_LED_NUM 21

#define POT_R A1
#define POT_G A0
#define POT_B A2

#define PUSH_1 12
#define PUSH_2 13
#define RELAY 2

CRGB led[STRIP_LED_NUM];
bool push1_pressed_now = false;
bool push1_pressed_before = false;
bool push2_pressed_now = false;
bool push2_pressed_before = false;

bool led_on = false;
bool strip_on = true;

// Timing variables
unsigned long previousMillis = 0;  // Stores the last time LEDs were updated
const long interval = 100;         // Interval at which to update LEDs (in milliseconds)

// Stores previous potentiometer values to detect changes
int prev_pot_R_val = 0;
int prev_pot_G_val = 0;
int prev_pot_B_val = 0;

bool is_button_pressed(int button_no, bool &check_now, bool &check_before);

void setup() {
  Serial.begin(9600);
  // Initialize pins
  pinMode(LED_R, OUTPUT);
  pinMode(LED_G, OUTPUT);
  pinMode(LED_B, OUTPUT);

  pinMode(POT_R, INPUT);
  pinMode(POT_G, INPUT);
  pinMode(POT_B, INPUT);

  pinMode(PUSH_1, INPUT_PULLUP);
  pinMode(PUSH_2, INPUT_PULLUP);

  FastLED.addLeds<WS2812B, STRIP_DIN>(led, STRIP_LED_NUM);
  for (int i = 0; i < STRIP_LED_NUM; i++) {
    led[i] = CRGB::White;
    FastLED.show();
    delay(50);
  }

  pinMode(RELAY, OUTPUT);

  // Adjust PWM frequency to 100 Hz
  // For Timer1 (pins 9, 10), set prescaler to 1024
  // This changes the PWM frequency from 490 Hz to approximately 61 Hz (16MHz / 1024 / 256)
  // To get as close to 100 Hz as possible, use prescaler 256 (approximately 244 Hz)
  TCCR1B = (TCCR1B & 0b11111000) | 0x04; // CS12 = 1, CS11 = 0, CS10 = 0 for prescaler of 256

  // For Timer2 (pins 3, 11), set prescaler to 1024
  // This changes the PWM frequency from 980 Hz to approximately 61 Hz (16MHz / 1024 / 256)
  // To get as close to 100 Hz as possible, use prescaler 256 (approximately 244 Hz)
  TCCR2B = (TCCR2B & 0b11111000) | 0x06; // CS22 = 1, CS21 = 1, CS20 = 0 for prescaler of 256

  
}

void loop() {
  unsigned long currentMillis = millis();  // Get the current time

  // Check for button presses
  if (is_button_pressed(PUSH_1, push1_pressed_now, push1_pressed_before)) {
    led_on = !led_on;    
  }
  if (is_button_pressed(PUSH_2, push2_pressed_now, push2_pressed_before)) {
    strip_on = !strip_on;
  }

  // Read potentiometer values
  int pot_R_val = analogRead(POT_R);
  int pot_G_val = analogRead(POT_G);
  int pot_B_val = analogRead(POT_B);

  int R_brightness = pot_R_val / 4;
  int G_brightness = pot_G_val / 4;
  int B_brightness = pot_B_val / 4;

  Serial.print(" R : ");Serial.print(pot_R_val); 
  Serial.print(" G : ");Serial.print(pot_G_val); 
  Serial.print(" B : ");Serial.println(pot_B_val); 

  // Apply threshold
  if (R_brightness < 20) { R_brightness = 0; }
  if (G_brightness < 20) { G_brightness = 0; }
  if (B_brightness < 20) { B_brightness = 0; }

  // Update LED only if brightness or button state changes
  if (currentMillis - previousMillis >= interval || 
      R_brightness != prev_pot_R_val || 
      G_brightness != prev_pot_G_val || 
      B_brightness != prev_pot_B_val) {
      
    previousMillis = currentMillis; // Update the time

    digitalWrite(RELAY, led_on);

    // Set the individual LED brightness
    analogWrite(LED_R, led_on * 0.8 * R_brightness);
    analogWrite(LED_G, led_on * 0.8 * G_brightness);
    analogWrite(LED_B, led_on * 0.8 * B_brightness);

    // Set the LED strip colors
    for (int i = 0; i < STRIP_LED_NUM; i++) {
      led[i].setRGB(strip_on * G_brightness, strip_on * B_brightness, strip_on * R_brightness);
      // Swapping RGB channels to match the interpretation of the LED strip
    }

    FastLED.show();

    // Store the current potentiometer values
    prev_pot_R_val = R_brightness;
    prev_pot_G_val = G_brightness;
    prev_pot_B_val = B_brightness;
  }
}

// Function to detect if a button is pressed
bool is_button_pressed(int button_no, bool &check_now, bool &check_before) {
  check_now = !digitalRead(button_no);
  if (check_now && !check_before) {
    check_before = check_now;
    return true;
  } else {
    check_before = check_now;
    return false;
  }
}

1 Like
digitalWrite(RELAY, HIGH);
delay(100);
pinMode(RELAY, OUTPUT);

OR !

//pinMode(RELAY, OUTPUT); comment out in setup()

replace

led_on?pinMode(RELAY, OUTPUT):pinMode(RELAY, INPUT);

thank you for answer! but after adding
digitalWrite(RELAY, HIGH);
on the first line of setup() function, still there's very short LED Turning On moment.

so I think it's a problem with the order of power supply. which is...
(1) SMPS turns on, and LED driver and buck converer is powered with 12V. arduino is power off.
-> LED Turns on
(2) buck converter starts to power +5V to the arduino and LED driver module.
-> LED Turns off.

Is there any way to power LED driver's +12V input AFTER 5V input?

  • Very neat wiring :+1:

  • Try adding delay in setup( ) to give time for things to settle down at power up time.

Yes but it would easier to delay the control signal to the relay.
I can't tell from the picture is a Relay Module with HIGH level trigger?

Then the very first two lines in setup() should be
pinMode(RELAY, OUTPUT);
digitalWrite(RELAY, LOW);

post #2 updated

@kolaha
That's why I asked if it's HIGH or LOW trigger.

Well I have a simple solution if interested.

thank you for all the answers. I've tried those on post #2 and #5, but not worked.

Here, NO - COM is connected when relay module is triggered with HIGH logic sivnal.

I used ohmmeter to the relay module, and figured out that my relay module connects all 3 pins (NO - COM - NC) , when it is not powered with 5V VCC.

(1) Is this how a relay supposed to work? I thought that a relay is sort of mechanical switch, operated by electric signal. and when not powered, one of 3 pins is not connected to the rest 2 pin. should I look for another relay module?

(2)

Any other suggestions? fixing arduino code won't make it, as this "flashing" moment is even before arduino makes logic level signal.

I have not told you how to do it, so exactly what did you do?

No, if what you say is true then that module is defective.
Buy a new one

but check before, if some part you can replace, or may be it is soldering issue

↓ I mean, this.

pinMode(RELAY, OUTPUT);
digitalWrite(RELAY, LOW);

I think I should buy another relay module, then.

but is there any other way to solve this issue, without using relay?

May well be.
What are those constant current drivers you are using?
Are they Sparkfun femtobuck?

I would invert these two commands.

digitalWrite(RELAY, LOW);
pinMode(RELAY, OUTPUT);
1 Like

@xfpd
The relay is broken and is of no use or even necessary. Anything in the code relating to the relay can be eliminated.

1 Like

Well if interested, I'll be off and on for a few hours.

Just meter probes - nothing else (wiring) ?

This is the LED driver I used for this project.

I didn't understand what your suggestion means. Can you give me some more about your inquires?

You posted that you made some resistance measurements.
In response I asked whether there were any wires connected to the relay terminals (C, NC, NO) when you did that.