8 SMD LEDs +PIR

Hi guys, so in this project I have 2 PIR motion sensors (A2,A4 pins), one photo resistor (A0 pin) and 8 smd LEDs (D4-D11). Logic is next:
If no light = true,
if there is motion on PIR1 or PIR2 =true
turn LEDs ON

My main problem is with PIR sensors, they are turning on without motion. I don’t know why, and need your help. This code is working fine without functions for turning on SMD LEDs. But…when I’m implementing this functions code is going crazy.
I did testing without LEDs, and everything is working perfect…

#define PIR1 A4
#define PIR2 A2
#define photoR A0
int i, val1 = 0, val2 = 0, brojac;


void setup() {
 for (i = 4; i <= 11; i++) {
   pinMode(i, OUTPUT);
 }
 pinMode(photoR, INPUT);
 Serial.begin(9600);
}


void paljenjeSvijetlaGore() {
 for (brojac = 4; brojac <= 11; brojac++) {
   digitalWrite(brojac, HIGH);
   delay(250);
 }
 delay(5000);
 for (brojac = 4; brojac <= 11; brojac++) {
   digitalWrite(brojac, LOW);
   delay(250);
 }
}

void paljenjeSvijetlaDolje() {
 for (brojac = 11; brojac >= 4; brojac--) {
   digitalWrite(brojac, HIGH);
   delay(250);
 }
 delay(5000);
 for (brojac = 11; brojac >= 4; brojac--) {
   digitalWrite(brojac, LOW);
   delay(250);
 }

}



void loop() {

 val1 = analogRead(PIR1);
 val2 = analogRead(PIR2);

 if (analogRead(photoR) > 800) {
   Serial.println("Uso je u petlju FOTO");

   if (val1 > 0) {
       paljenjeSvijetlaGore();
       delay(1500);
   } else if (val2 > 0) {
       paljenjeSvijetlaDolje();
       delay(1500);
     }

     val1 = LOW;
     val2 = LOW;
     delay(400);
   }
 }

Is it posible that high power consumption on LEDs driving arduino nano crazy?

You're reading the PIR detectors as analogue inputs. Even if they are analogue output PIR detectors, the chance of them being at exactly 0V is very low, so they're triggering at random. If they really are analogue output PIR detectors, you need to set a threshold that's higher.

However, most all PIR detectors that I've dealt with have had a digital output, or a relay, so the pins would be configured as digital inputs and read using 'digitalRead()'.

Edit: Do you have a link to the PIR detectors that you're using?

thaks for replay
this PIR I am using : PIR (motion) sensor : ID 189 : $9.95 : Adafruit Industries, Unique & fun DIY electronics and kits

The analog and digital reads are not the problem, I did some testing with both, and working fine on analog >0 or digital == HIGH but without LEDs.

That PIR sensor has a digital output, but even so reading it with 'analogRead()' and a threshold of 0V is asking for unreliability. A digital output isn't guaranteed to go to exactly 0V. With your present threshold, even 5mV will be considered a 'trigger'. The tiniest bit of noise and you'll have a false-trigger.

Also, it's output when high is only 3.3V, so on a 5V system it might not trigger a digital pin reliably when it goes high either. Best thing would be to continue to use 'analogRead()', I guess, but with a more suitable threshold. Something in the neighbourhood of 340 would work well.

As far as the LEDs go, you haven't mentioned what power supply you're using or how much current you've limited each LED to, so it's impossible to say if they're causing the problem. (It could be a combination of your 0V threshold AND the LEDs.)

I'm not sure what kind of smd LEDs I have, I think it's 5630 SMD series 0.5W.
I will listen your advice and try analogRead with more than 300-400 in IF.
I'll let you know is it working in about a hour or so

Wow, they're not normal LEDs at all, SMD or otherwise. (Actually SMD LED modules, not just SMD LEDs.) They're very high current LEDs, with a maximum forward current of 200mA continuous. That's way too much for an Arduino to drive directly, unless you've limited the current to a very low level. An Arduino pin can only source/sink an absolute maximum of 40mA, and more than about 30mA is too much. If you're driving them at anywhere near their rated current, no wonder you're having trouble.

What value current-limiting resistors are you using?
You didn't mention a separate driver for them, and still haven't mentioned any power supply details.
Show us your schematic diagram.

uh, Im sorry. Currently It's on USB adapter with output 5V , 180mA. And having 220ohm resistor on common LED GND. Obviously I'm not expert for electornics, and newbie.
Will more powerfull adapter change things, or I'm limited with power? I have adapter with 12V and 1.5A, maybe to connect that to power pins on nano? Will then be nano stable with power?
I will draw shematic at the end of the day

and thanks for your help

The way you are using analogRead on a digital signal is crazy - you have a noise-margin of 5mV instead of
several volts by doing that - obviously its going to misbehave when you start switching some LEDs!!

Use digitalRead on you motion sensors.

MarkT:
The way you are using analogRead on a digital signal is crazy - you have a noise-margin of 5mV instead of
several volts by doing that - obviously its going to misbehave when you start switching some LEDs!!

Use digitalRead on you motion sensors.

They only output 3.3V or a little less when high, so 'digitalRead()' won't be reliable enough.
When I last used one of the same PIR detectors on a PIC chip, I used a small-signal transistor on the PIR output to level-shift to 5V.
The alternative is 'analogRead()', with a decent threshold, but it's much slower.

The voltage thresholds for LOW and HIGH at 5V are 1.5V and 3.0V.

MarkT:
The voltage thresholds for LOW and HIGH at 5V are 1.5V and 3.0V.

I thought it was 0.7Vcc, but I just looked and you're right - 0.6Vcc @ 5V. My bad. :frowning:
Those PIR detectors go a little under 3.3V, but I can't remember exactly how far under. It was a year or two ago when I used them. Still, they wouldn't go lower than 3.2V or so. About a 200mV safety margin.

sorry for long delay,
long story short, I don't know what to do, problem is in SMD LEDs.To much power are taking from arduino nano and making him go crazy. So can I maybe use external power supply for LEDs, and make digital pins like GND, and controll them with HIGH-LOW? or some other idea to take more power?

In your last reply, you said you'd draw a full schematic diagram at the end of the day, but still haven't done so.

You said that you used a 220 ohm resistor for the LEDs "common" ground. Does this mean that you have 8 LEDs in parallel without individual resistors, and just one current-limiting resistor for them all?

A 220 ohm resistor should limit the total current to about 10mA, (and they'd barely be lighting up), if the LED Vf is about 3V, so if the LED current drain is affecting the Nano, something isn't right. Have you measured how much current the LEDs are drawing?

Did you change to digitalRead() for your PIR detector?

We're in the dark and can only guess what's going on from the information you've provided.

OldSteve:
We're in the dark ...

But PIRs work really well in the dark. :grinning:

Paul__B:
But PIRs work really well in the dark. :grinning:

Ha ha. True.

Yeah, PIRs are working fine in the dark, they are capturing temperature changes trough detecting area.
Here are shematics. Currently using R=100 Ohm, yes with 220 Ohm LEDs are weak
Do you need some more info?

10579355_1689660084626848_704288630_n.jpg

Kapusta:
Yeah, PIRs are working fine in the dark, they are capturing temperature changes trough detecting area.
Here are shematics. Currently using R=100 Ohm, yes with 220 Ohm LEDs are weak
Do you need some more info?

This is a bit like pulling teeth.

Did you change to using digitalRead() to read the PIR detectors?
Do the LEDs have individual on-board resistors?
Could you post the latest version of your code? (Perhaps with comments in English added. It's too hard to follow when everything is in Bosnian and uncommented.)
Does your LDR, (photoresistor), really have 3 pins as shown?

I see no reason why the LED current should be affecting the micro.
Perhaps you should also show us a clear photo of your setup. Something isn't right.

OldSteve:
This is a bit like pulling teeth.

No :slight_smile:

OldSteve:
Did you change to using digitalRead() to read the PIR detectors?

Yes

OldSteve:
Do the LEDs have individual on-board resistors?

No, they don’t

OldSteve:
Could you post the latest version of your code? (Perhaps with comments in English added. It’s too hard to follow when everything is in Bosnian and uncommented.)

Sure, and that is Croatian. But same thing :slight_smile:

#define PIR1 A4  //first PIR motin sensor
#define PIR2 A2 // second PIR motion sensor
#define photoR A0  //Photoresisting module sensor

int i;    //Counter for making OUTPUT pins for LEDs in setup
int val1 = 0;  //Value of first PIR sensor (High or Low, 0-1)
int val2 = 0;  //Value of second PIR sensor (High or Low, 0-1)
int counter;   //Counter for loops to turn LEDs on


//setup for pins and serial monitor
void setup() {   
 for (i = 4; i <= 11; i++) {
   pinMode(i, OUTPUT);
 }
 pinMode(photoR, INPUT);    
 Serial.begin(9600);
}

/* function for Turning LEDs ON and OFF in hallway from right to left
   first they are turning ON one by one,until all are on, delay 5s, and then turning off one by one
*/
void TurningLightsRightOnLeft() {
 for (counter = 4; counter <= 11; counter++) {
   digitalWrite(counter, HIGH);  //Example: 1.pass in loop digitalWrite(4,HIGH); 2.pass digitalWrite(5,HIGH..
   delay(250);
 }
 delay(5000);
 for (counter = 4; counter <= 11; counter++) {
   digitalWrite(counter, LOW);
   delay(250);
 }
}

/* function for Turning LEDs ON and OFF in hallway from left to right
  first they are turning ON one by one,until all are on, delay 5s, and then turning off one by one
*/
void TurningLightsLeftOnRight() {
 for (counter = 11; counter >= 4; counter--) {
   digitalWrite(counter, HIGH);
   delay(250);
 }
 delay(5000);
 for (counter = 11; counter >= 4; counter--) {
   digitalWrite(counter, LOW);
   delay(250);
 }
}


//main loop
void loop() {

 val1 = digitalRead(PIR1);  //reading value of motion sensor pin PIR1 A4
 val2 = digitalRead(PIR2);  //reading value of motion sensor pin PIR2 A2

 if (analogRead(photoR) > 800) {      //Is it dark? If yes, check PIR sensors for motions
   Serial.println("*TEST* it's dark and reading PIR sensors");

   if (val1 == HIGH) {    //Is there some motion? IF yes, call function for LEDs 
       TurningLightsRightOnLeft();
       delay(1500);
   } else if (val2 == HIGH) {
       TurningLightsLeftOnRight();
       delay(1500);
     }

     val1 = LOW;
     val2 = LOW;
     delay(400);
   }
 }

OldSteve:
Does your LDR, (photoresistor), really have 3 pins as shown?

Yes, it’s module, and have 4 pins. For analog, digital read and +/GND

OldSteve:
I see no reason why the LED current should be affecting the micro.
Perhaps you should also show us a clear photo of your setup. Something isn’t right.

Don’t know, I tried everything. And without LEDs in code, everything is working as it should.
Sorry, I can’t upload photo right now and it’s not on my board.

I saw this post, and maybe it’s an anwser for my trouble

Thanks for helping and happy Christmas

Kapusta:
...and that is Croatian. But same thing :slight_smile:

Ha. I was 50/50 whether to say Bosnian or Croatian. Google Translate said both, depending on which words I translated.

OldSteve:
Does your LDR, (photoresistor), really have 3 pins as shown?

Yes, it's module, and have 4 pins. For analog, digital read and +/GND

Sounds fancier than most. Not a simple LDR after all. All the same, that won't be causing the problem. I was just curious.

Don't know, I tried everything. And without LEDs in code, everything is working as it should.
Sorry, I can't upload photo right now

Pity - that's the last resort. Without one I can't do anything further.

and it's not on my board.

Do you mean you've taken the circuit apart?

I saw this post, and maybe it's an anwser for my trouble
http://forum.arduino.cc/index.php?topic=367469.0

It would be the answer if you were driving the LEDs harder, at their rated current, but even with a 100 ohm resistor, you'd only be drawing about 20mA, so a MOSFET or similar shouldn't make any difference.

OldSteve:
Do the LEDs have individual on-board resistors?

No, they don't

That's a no-no. LEDs MUST each have their own current-limiting resistors. In this case, with the low current you're drawing, they can't be damaged, but if you were trying to power them with their rated current, some could potentially burn out. LEDs don't all have precisely the same forward voltage, so connecting them in parallel means that some will pass most of the current, while others will pass very little.
This is not the cause of your problem though.

This is quite puzzling.

I've looked over your code carefully, and it's fine, so everything should work properly.
One thing though - you don't need this at the end of the 'if' statement:-

val1 = LOW;
val2 = LOW;

The reason is that a split second later, they're assigned values anyway, when you do the 'digitalRead()' for each, so there's no need to explicitly make them low.

Also, you only need to read the PIR detectors if it is dark, so those digitalRead()s could be moved inside the 'if' statement.
Again, this is not causing your problem.

At this stage, I'm lost and would love to see a photo to try to see what's wrong. I think that your problem can only be due to a connection that's accidentally different to what you described.

And once you connect the LEDs correctly, with a 100 ohm resistor each, you'll need drivers and a separate power supply, since then each LED will draw 20mA, totalling 160mA. Allowing 75mA for the Arduino, PIRs etc, that's a grand total of 235mA - too much for your USB adaptor's 180mA max.