Electronic – arduino – TIP120 LED strip dimming with flickering problem + questions delay

arduinoledled strippotentiometertransistors

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 = analogRead (A7);
      pwm = map(pwm, 0, 1023, 0, 255);
     Serial.println(pwm);
  if (digitalRead(PIR) == HIGH) { // check if the sensor is HIGH
     Serial.println("Motion detected!");
  for(int fadeValue = 0 ; fadeValue <= pwm; fadeValue +=5) {

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

 delay(100);  
     ///

  for(int fadeValue = 0 ; fadeValue <= pwm; fadeValue +=5) {

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

    ///
        delay(ledDelay); //time to wait untill light switch off
    for(int fadeValue = pwm ; fadeValue >= 0; fadeValue -=5) {
    // sets the value (range from 0 to 255):
    analogWrite(TR, fadeValue);       
    // wait for 60 milliseconds to see the dimming effect   
    delay(60);
            } 
    for(int fadeValue = pwm ; fadeValue >= 0; fadeValue -=5) {
    // sets the value (range from 0 to 255):
    analogWrite(TRB, fadeValue);       
    // 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

enter image description here

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).

Best Answer

  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