I'm working on a side project where once motion is detected by the PIR sensor, the DC motors (2x) will turn on for 5 seconds and then shut off. And if the motion has stopped, then the DC motors will turn off.
So my problem is that I am not achieving the desired results I just mentioned above. From my perspective, it seems that my motion sensor is just going off and on, on its own and the DC motors are behaving how they should be with going on for 5 seconds, but the motion sensor is saying that there is motion despite there being motion, which causes the DC motors to run. The DC motors should run when there is motion.
I have tried this exact hardware and components on another arduino UNO and breadboard, the problem seems to be code related.
I have also tried taking a step back and seeing if I can get the motion detection to turn on a LED light. In the Serial monitor it seems to be picking up that there is motion being detected, but really there isn't.
I also tried adjusting the potentiometer on the PIR sensor as well as (adjusting the sensitivity and time) of it. I have also tried switching the "repeatable" trigger and the "non-repeatable" trigger around and seeing if that was an issue.
I have tried swapping the 9V battery to see if that was affecting the performance of the DC motors.
I also doubled checked and made sure each wire was in the correct placement.
As of now, below is the serial monitor... for the given code I provided. This is what the it is providing me. **Keep in mind, that I am not putting any motion into the arduino unit, and for some odd reason it detects that there is motion.
Here is what the Serial Monitor is showing...
2:31:43.219 -> Motors are ON
02:31:43.219 -> Motion detected!
02:31:48.215 -> Motors are ON
02:31:48.249 -> Motors are OFF
02:31:48.249 -> Motion stopped!
02:31:53.232 -> Motors are ON
02:31:53.232 -> Motion detected!
02:31:58.220 -> Motors are ON
02:31:58.253 -> Motors are OFF
02:31:58.253 -> Motion stopped!
02:32:03.238 -> Motors are ON
02:32:03.238 -> Motion detected!
02:32:08.230 -> Motors are ON
02:32:08.265 -> Motors are OFF
02:32:08.265 -> Motion stopped!
const int switchMotion=2;
const int motorPin=9;
const int motorPinB=8;
int motionState=0;
int motionDetected = LOW;
void setup() {
//Selecting as an input and output the switch and the motor
pinMode(switchMotion,INPUT);
pinMode(motorPin,OUTPUT);
pinMode(motorPinB, OUTPUT);
Serial.begin(9600); //Set serial out if we want debugging
delay(5000); //Allow time for the PIR Sensor to calibrate
}
void loop() {
motionState = digitalRead(switchMotion); // Reads the motion sensor
if(motionState == HIGH) // checks if Sensor is HIGH
{
digitalWrite(motorPin,HIGH); //turn on Motor A
digitalWrite(motorPinB,HIGH); //turn on Motor B
delay(5000); //runs for 5 seconds and stops
Serial.println("Motors are ON");
if (motionDetected == LOW) {
Serial.println("Motion detected!"); // print Motion Detected
motionDetected = HIGH; // update variable state to HIGH
}
else {
digitalWrite(motorPin,LOW); //turn off Motor A
digitalWrite(motorPinB,LOW); //turn off Motor B
Serial.println("Motors are OFF");
if (motionDetected == HIGH){
Serial.println("Motion stopped!");
motionDetected = LOW; // update variable state to LOW
}
}
}
}
The goal is to have it where once an individual is near the PIR motion sensor the DC motors turn on for a set period of time and when the time period is up, the motors turn off and there is a set delay time for the motion sensor to detect movement again for the DC motors to turn on again. It should be a constant cycle, where when there is not movement - the DC motors should be off. And when there is movement - the DC motors should be on. The exception is that there is a cooling time.
Actual results are that
I expect motionDetected to function properly, but when it's time to test it, it is reading that there is motion detected/not detected despite there being no real movement. My expected results is for the motion sensor to function properly so the DC motors can turn on/off accordingly.