# TIP120 LED strip dimming with flickering problem + questions delay

I need some help with my Arduino program.

Let me introduce the hardware and the goal.

• There is two white LED strip each one of them are controlled by TIP120 transistor.

• There is an potentiometer to set-up manually the full brightness value of the fade effect.

So, when The PIR sensor detect an human the first LED stip fade effect to the maximum brightness thanks to the potentiometer value read before. The second one has its fade-in effect just after the first LED strip.

The LED strips are on for 6 seconds after power on and each are extinguished with fadeout effect.

Problem:

When the first LED strip finish to fade-out it stays slightly on. How to fix this?

Video to show the problem:

https://imgur.com/a/Uf3pZbC

Moreover I noticed below the value 100 for fade-in/fade-out, the LED strips flickering slightly, how to fix that?

Delay question:

-As you can see there is an little delay for the second LED strip power. How to remove this delay and power it directly when the first LED strip have finish his fadein?

• I would like to know, how to start the second LED strip at the same time like the first one but with a difference of a second than the first.

If you have any suggestion to improve my code you are welcome!

The code:

int PIR = 4;              // the pin that the sensor is atteched to
int TR = 3;               // the pin that the TIP 120 Base is atteched to for the first led strip
int TRB = 5;              // the pin that the TIP 120 Base is atteched to for the second led strip
int ledDelay = 5000;          //time to wait untill light switch off
int pwm;

void setup() {
pinMode(PIR, INPUT);    // initialize sensor as an input
pinMode(TR, OUTPUT);    // initialize TIP 120 as an output
pinMode(TRB, OUTPUT);   // initialize TIP 120 as an output
Serial.begin(9600);     // initialize serial
}

void loop(){
pwm = map(pwm, 0, 1023, 0, 255);
Serial.println(pwm);
if (digitalRead(PIR) == HIGH) { // check if the sensor is HIGH
Serial.println("Motion detected!");

// sets the value (range from 0 to 255):
// wait for 60 milliseconds to see the dimming effect
delay(60);  // turn LED ON
}

delay(100);
///

// sets the value (range from 0 to 255):
// wait for 60 milliseconds to see the dimming effect
delay(60);
}
delay(100);

///
delay(ledDelay); //time to wait untill light switch off
// sets the value (range from 0 to 255):
// wait for 60 milliseconds to see the dimming effect
delay(60);
}
// sets the value (range from 0 to 255):
// wait for 60 milliseconds to see the dimming effect
delay(60);
}
}

else {
digitalWrite(TR, LOW);       // turn LED 1 OFF
digitalWrite(TRB, LOW);       // turn LED 2 OFF
Serial.println("Motion stopped!");
delay(100);                   // delay 100 milliseconds
}
}


Picture of schematic

Note :

• The led strip I used are 5V 3528 warm white not rgb
• Power supply of led strip is a step down converter on 12V battery(setting on 5V 2A).
• Please provide a schematic for you circuit Sep 17, 2020 at 12:17
• Welcome to EE.SE! "introduce the hardware" This is where a schematic comes in. EE talks in schematics. If you click on edit and the schematic symbol, a competent schematics editor will open up. If you already have one from your e-CAD of choise, please post a picture of that. Sep 17, 2020 at 14:07
– Cypf
Sep 17, 2020 at 14:56
• Tinkercad is not a schematic, and your tinkercad does not even match what you show in your video. I see issues in the tinkercad (e.g. resistors are shorted) that don't appear to be an issue in real life. Sep 17, 2020 at 15:28
• Do you understand your code? Your code sometimes doesn't fade out all the way to 0. The LEDs are just doing what you tell them. The flicker is probably unavoidable though. Sep 17, 2020 at 17:11

1. Format your code with Ctrl+T. it is than easier to read
2. Write in after "fade out" analogWrite(TR, 0);

The fadeout value has in 4 of 5 cases still a value in it.

HTH

• Now it's working perfectly thanks, do you know to solve the flickering problem ? I notice If I unplug 1 led strip , it no longer appears.
– Cypf
Sep 17, 2020 at 20:11
• @mguima correct practice is to wait to allow others to answer instead of immediately marking the first partial answer as the accepted one. Sep 18, 2020 at 3:38
• Not sure what you're alluding to there, @mguima, but Passerby is right that correct practice is to wait to allow others to answer instead of marking a first partial answer as the accepted one. Any OP may change their opinion of what fully answers their question when they read further answers. Any poster of an existing answer is then free to read them then expand and improve their own answer as they wish. Oct 2, 2020 at 8:21
• @mguima 8 says upvote before asking, while 9 explicitly says to wait to accept as the answer. So idk what to tell you, your own evidence contradicts you. Oct 2, 2020 at 8:50
• @mguima, oh good, that's that sorted - nothing personal by anyone and no harm done, it's only an engineering site. Peace reigns again :-) Oct 2, 2020 at 12:50