2
\$\begingroup\$

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://i.sstatic.net/W3Nwi.jpg

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).
\$\endgroup\$
18
  • 3
    \$\begingroup\$ Please provide a schematic for you circuit \$\endgroup\$
    – mike65535
    Commented Sep 17, 2020 at 12:17
  • \$\begingroup\$ 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. \$\endgroup\$
    – winny
    Commented Sep 17, 2020 at 14:07
  • \$\begingroup\$ Schematic added, please check :) \$\endgroup\$
    – Cypf
    Commented Sep 17, 2020 at 14:56
  • \$\begingroup\$ 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. \$\endgroup\$
    – mbedded
    Commented Sep 17, 2020 at 15:28
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Commented Sep 17, 2020 at 17:11

1 Answer 1

3
\$\begingroup\$
  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

\$\endgroup\$
12
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Cypf
    Commented Sep 17, 2020 at 20:11
  • 1
    \$\begingroup\$ @mguima correct practice is to wait to allow others to answer instead of immediately marking the first partial answer as the accepted one. \$\endgroup\$
    – Passerby
    Commented Sep 18, 2020 at 3:38
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – TonyM
    Commented Oct 2, 2020 at 8:21
  • 1
    \$\begingroup\$ @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. \$\endgroup\$
    – Passerby
    Commented Oct 2, 2020 at 8:50
  • 1
    \$\begingroup\$ @mguima, oh good, that's that sorted - nothing personal by anyone and no harm done, it's only an engineering site. Peace reigns again :-) \$\endgroup\$
    – TonyM
    Commented Oct 2, 2020 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.