so I have this very simple motion sensor module from an unknown company.
The basic pinout is 5V GND and an output which sets to high when motion is detected. I have set the mode to high, so that the sensor triggers only once when continuous motion is detected and does not alternate between low and high. I have hooked this up to the arduino and read the output over a digital input pin. Basically, when motion is detected, a buzzer goes off and an LED lights up. Everything seems to work fine when the led is simply turned on and off when motion is detected but I thought this was boring and wanted to have it flash repeatedly when motion is detected. However, when I do this, the sensor gets set to high constantly and never sets the output to low again. The strange thing is that when done with the buzzer there is no problem! So I went back to the constantly lit LED approach but now wanted to have the LED flash every 5 seconds for approximately 15ms to indicate that everything is running when the sensor output is low. However, as soon as the LED lights up for the 15ms, the sensor triggers and gets set to high. At first I thought this was the light of the LED tripping the sensor, but I have run the thing completely separated, even inside a box and I still have the problem! Using the LED on pin 13 as the indicator seems to work better, but every 2nd to 3rd flash triggers the sensor and sets it to high.
I have no explanation what so ever for this and am really hoping that someone has an idea on how to solve this?
I thought it was just noise or a power issue at first but I have tried using resistors as well as capacitors and the situation does not improve.
I don't want to run the sensor and the arduino on different power supplies and use an optocoupler unless this is absolutely necessary, although I don't even know if that would solve the problem either...
I would greatly appreciate any guidance with this matter!
It makes more sense to test for == HIGH or LOW, not greater than or less then 0 or 1.
Incrementing count on every pass through loop doesn't make sense. Incrementing it only when the sensor reads HIGH or LOW, whichever indicates motion, does.
Turning the buzzer and LED on when the state of the sensor IS HIGH or LOW does not make sense. Turning them on or off when the state changes makes sense. Starting the counter when the state changes makes sense.
Then, you can toggle the LED pin, periodically, as in the blink without delay example, when the state is "motion detected".
It appears that the OP and I both have the same problem, and is being trigged by the same desire to want a blinking LED instead of a just a LED lighting up when there is motion and going out when there is none.
As it is part of a larger project, I've cut the code down, with just the motion detect and blinky led. The false positive is still present, and goes away if I comment out either of the below lines. Motion sensor works fine in a sketch that simply makes the LED go high when the sensor is high, and low when the sensor is low. Motion sensor (a generic one) is wired to port 9 with a 10k pullup resistor.
digitalWrite(PWR_STATUS_PIN,pwrPinState); //change led state