Short flash LED strip at 'startup'

Hi everyone,

This is my first post here and I hope someone can help me with a little ‘problem’ I have. Unfortunately I’m one of those guys that mostly get information since my knowledge and understanding isn’t quite the level needed to help others. It always makes me feel a bit awkward to ask for help and not being able to do something in return…but anyway:

The problem I’m having is with a LED-strip (dimmable, 12V, 36W, white light only) controlled by an Arduino using PWM. The LED-strip represents the sun at my model railroad so I use an Arduino to process commands from a PC and let the sun come up and go under.
The setup is quite basic:

  • PC providing power and commands for the Arduino through USB.
  • The same PC provides 12V to the LED-strip.
  • The LED-strip is controlled by a MOSFET (P16NF06L) with a 10k pulldown resistor, so the most basic way it can be done as far as I know.

This is working fine except for one little thing. When the sun starts to come up the Arduino writes a value of 1 (using AnalogWrite) to the corresponding output pin which at that moment has a value of 0 (LED-strip is completely off). At that moment the LED-strip flashes very short but noticeable at full power before being dimmed to the correct value. After that it works perfectly. Since this is noticeable it’s a bit annoying.

The first tests I did I used an Arduino Uno and a few cheap Chinese LED-spots (also about 36W in total) with the same result. When using a normal low power LED or ordinary light bulb (around 0,5 W) instead of the LED-strip or spots it doesn’t happen. I changed the spots to a strip because they started to flicker and the Uno to a Mega to add more ‘channels’ for controlling the other lights.

So now I’m wondering what causes this short ‘flash’. Is it a kind of starting pulse from the Arduino PWM control or a side-effect off the MOSFET I’m using? Unfortunately I don’t have a scope so I cannot test any of these things myself. The next question, of course, is can it be prevented? I’ve googled this issue but can’t find a problem similar to this.

Just in case you might need it here’s the arduino code:

// Map names to the outputs
const int OutPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 44, 45, 46, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 47, 48, 49}; // the first 15 outputs are PWM, the rest is digital
const int In1 = A0;
const int In2 = A1;

long GS = 0; //long representing commandstring
long LI = 0; //value to write to output
int OP = 9999; //output number
int InVal1;
int InVal2;
int OutVal = 0;
int inChar = 0;
String IN = ""; // character read from serial
boolean SendMsg;

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  // set input pins as pullup
  pinMode(A0, INPUT_PULLUP);
  pinMode(A1, INPUT_PULLUP);
  // set pins as output
  for (int i=15; i<40; i++) {
    pinMode(OutPins[i], OUTPUT);
  }
}

void loop() {
  //read input from railroad crossings
  //OutVal is calculated based on InVal1 and InVal2:
  //0 = both off, 1 = 1 on/2 off, 2 = 1 off/2 on, 3 = both on
  InVal1 = analogRead(In1);
  delay(10);
  InVal2 = analogRead(In2);
  delay(10);
  if (InVal1 < 512 and OutVal != 1 and OutVal != 3) {
    OutVal = OutVal + 1;
    SendMsg = true;
  }
  else if (InVal1 > 512 and (OutVal == 1 or OutVal == 3)) {
    OutVal = OutVal - 1;
    SendMsg = true;
  }
  if (InVal2 < 512 and OutVal != 2 and OutVal != 3) {
    OutVal = OutVal + 2;
    SendMsg = true;
  }
  else if (InVal2 > 512 and (OutVal == 2 or OutVal == 3)) {
    OutVal = OutVal - 2;
    SendMsg = true;
  }
  //send command to PC
  if (SendMsg == true) {
    Serial.println(String(OutVal));
    delay(10);
    SendMsg = false;
  }

  //process incoming commands
  while (Serial.available() > 0) {
    inChar = Serial.read();
    delay(10);
    // analyse incoming message
    if (isDigit(inChar)) {
      // convert the incoming byte to a char
      // and add it to the number:
      IN = (char)inChar;
      GS = (GS * 10) + IN.toInt();
    }
    else if (inChar == '#') { // if you get a newline:
      OP = GS / 1000; // OP = outputnumber
      LI = GS - (OP * 1000); // value to write to output (analog 0-255 or digital 0-1)
      OP = OP - 1; // correct for 0-based array index
      if (OP < 15) { // use analog output
        analogWrite(OutPins[OP], LI);
      }
      else { // use digital output
        digitalWrite(OutPins[OP], LI);
      }
      delay(10);
      // clear the string for new input:
      GS = 0;
    }
    else if (inChar == 'A') { // command send by PC to determine which COM-port to use
      Serial.write("Arduino\n");
      delay(10);
    }
  }
}

Thanks in advance for any replies,

Richard de Koning

@kuepje

Your topic was Moved to it's current location / section as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

Thanks for moving it, I’ll pay attention to the correct section in the future :slight_smile:

I would print-out the LI and OP variables to see what is going on.

Thanks for the tip, it made me realize I didn’t mention that I debugged this code a few years ago when I started trying to build this project. Just to see what I was doing (it was my first arduino project…) I did exactly what you suggest (for all variables) and the values of these variables are correct.

The values written to the output also don’t change as quickly as the flash appears and disappears (it’s realy short like a blowing fuse). The value written to the output changes about once a second.

Only the output controlling the high power ‘sun’ is having this issue. All of the other lights (< 0.5 W) do not. Also when I connect a low power light to the ‘sun’ output there’s no problem. So it seems to be related to the amount of power that the MOSFET is controlling. Which makes me realize it’s probably more an electronical problem instead of an Arduino problem.

What is the value of the pulldown on the gate? What is the distance between the output and the gate (how long it the wire)?

The pull down is 10k Ohm. The wire between the Arduino and the gate is about 10-15 cm long.

kuepje:
So now I’m wondering what causes this short ‘flash’.

A schematic would help. And, please, not a “pretty” fritzing picture.
Just guessing, but your output pins are uninitialized, so until the first loop, you don’t know their state.
Try this:

  // set pins as output
  for (int i=15; i<40; i++) {
    pinMode(OutPins[i], OUTPUT);
    digitalWrite(OutPins[i],0);
  }
}

Here's a schematic. I'm not familair with the term fritzing but this was the fastest way of making my own :slight_smile:

I'm certainly going to try and initialize the outputs. I didn't think of that so thanks for the tip. It could take me a while to find the time to test it and share the result but i will...

You’re missing a series resistor to the gate of the FET and the Arduino pin. See e.g. https://www.gammon.com.au/motors

Ok, gonna try that, thanks! As mentioned before it could take some time before I can test anything. It's a half hour drive from my home to my model railroad so I'm not going there just to test this.

After initializing the output pins as suggested by SteveMann the flash was gone. Problem solved :-)! Thanks again everyone for your suggestions