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