PIR Sensor that turns on DC Motors - arduino

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.

I'm not sure I understand but if i'm correct isn't this just what you are looking for:
const int switchMotion = 2;
const int motorPin = 9;
const int motorPinB = 8;
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() {
if(digitalRead(switchMotion) == HIGH) // checks if Sensor is HIGH
{
Serial.println("Motion detected!");
digitalWrite(motorPin, HIGH); //turn on Motor A
digitalWrite(motorPinB, HIGH); //turn on Motor B
Serial.println("Motors are ON");
delay(5000); //runs for 5 seconds and stops
digitalWrite(motorPin, LOW); //turn off Motor A
digitalWrite(motorPinB, LOW); //turn off Motor B
Serial.println("Motors are OFF");
}
// You can add a delay here if you want
}

Your logic is messed up.
If the sensor is low, nothing happens.
first run of loop after sensor turned high:
turn on the motors,
wait 5 seconds,
print "Motors are ON",
print "Motion detected",
set motionDetected HIGH.
second run of loop (if sensor is still high):
turn on the motors,
wait 5 seconds,
print "Motors are ON",
now motionDetected is HIGH so:
turn off motors
print "Motors are OFF"
print "Motion stopped"
set motionDetected LOW
second run of loop if (sensor is low again):
nothing happens -> motors stay on
To fix this make sure the else belongs to the correct if!
You want to turn the motors off when the sensor is low, not when the sensor is high and was high before.
Also the prints should be placed befor the delay. What's the point of printing "motion detected" 5 seconds after you actually detected it.

Related

Arduino - Light is staying on for more than expected duration

Code is suppose to Light the inbuit-LED at Pin 13 whenever Pin 5 is High, however i have encountered couple of problem.
While measuring Voltage though Digital Meter - one pin at arduino GND and other at 1,2,3,4. They are showing some non-zero values. Earlier triggering Pin was 4 and light was staying on all the time.
When Pin 5 is high (by connecting 5V Pin from Arduino to Pin 5) it lights the LED as it should, but if Pin 5 stays high for more than 1/2 second, light stays high for more than 0.5 second even after the Pin 5 is disconnected from 5V Pin.
int buttonState = LOW;
int light = 13;
void setup() {
// put your setup code here, to run once:
pinMode(gateopen,INPUT);
pinMode(light, OUTPUT);
}
void loop()
{
// put your main code here, to run repeatedly:
buttonState = digitalRead(gateopen);
if (buttonState == HIGH)
{
digitalWrite(light, HIGH);
}
else
{
digitalWrite(light, LOW);
}
//delayMicroseconds(500);
}
As suggested earlier, use a pull down resistor. Or change your circuit to active low and use INPUT_PULLUP.
I believe you're using delayMicroSeconds, which means the led is just blinking waaay too fast for your eyes. If you want 'half a second' as you hinted on your query, you would be using delay(500) instead.
You are experiencing electrical noise, just like Juraj said, just put a 220 Ohm pull down resistor

cannot turn on relay indefinitely on an arduino

I want to make a device like Knocki(https://knocki.com), which essentially is a relay control using a vibration sensor. i can detect vibrations rn but the problem is, once i knock the relay blinks on and then turns off. i understand this is a lack of programming that is causing this. could someone help me write code which makes it so that when i knock the relay is turned on indefinitely; until I knock again to turn relay off.
And yes u can probably tell that this code is copied from somewhere(https://wiki.keyestudio.com/Ks0068_keyestudio_37_in_1_Sensor_Kit_for_Arduino_Starters#Project_21:_Vibration_Sensor).I took it from the home page of the vibration sensor. the code was initially so that every time i knocked, the onboard Arduino led lit up. Also, right now the relay is blinking faintly every time i knock(Although correctly,in sync with my knocks)
#define SensorLED 13
#define SensorINPUT 3 //Connect the sensor to digital Pin 3 which is Interrupts 1.
unsigned char state = 0;
int Relay = 5;
void setup()
{
pinMode(SensorLED, OUTPUT);
pinMode(SensorINPUT, INPUT);
attachInterrupt(1, blink, FALLING);// Trigger the blink function when the falling edge is detected
}
void loop()
{ if(state!=0)
{
state = 0;
digitalWrite(SensorLED,HIGH);
delay(500);
digitalWrite(Relay,HIGH);
}
else
digitalWrite(SensorLED,LOW);
digitalWrite(Relay,lOW);
}
void blink()//Interrupts function
{ state++;
Yes its in your code: The (bad) example works only because there is a
digitalWrite(SensorLED,HIGH);
->>> delay(500);
a delay for 1/2 sec to keep the led on.So as a check put an other delay after the relay line and it should go on for 1/2 sec too (so the led is lit 1 sec in total)
digitalWrite(SensorLED,HIGH);
delay(500);
digitalWrite(Relay,HIGH);
delay(500);
Thats just for checking -> NEXT STEP:
Get rid of the delays (see blinkwithoutdelay example in
Arduino->File->Examples->2.Digital -> blinkwithoutdelay
and introduce a second state variable e.g.
bool relayStateOn = false;
to get an independent on/off of the relay and the led.(If thats - what I understand -what you want to do)
If you feed your relay from the board, that is not the problem. Please, check the voltage in your relay when you try to set it on, if your voltage falls down, it means that this output to your relay does not supply the necessary current.

How to turn motor counter-clockwise

Currently, my code below turns on the motor, delays for a bit then starts again. This is all being done in the clockwise direction, however how can I write my code so it can turn counter-clockwise?
int motorPin = 3;
void setup()
{
pinMode(motorPin, OUTPUT);
}
void loop()
{
startStopMotor(135);
delay(1000);
startStopMotor(0);
delay(1000);
}
void startStopMotor(int speed){
analogWrite(motorPin, speed);
}
By the code you share i'am guessing you are running a low power 5v dc motor... but you should edit your answer to give us what type of hardware you are using. This is not an answer but an idea of what you should be looking for... Basically on the motor i suppose you have, you have pin 1 and pin 2. Pin 1 is connected to a PWM signal and pin 2 is connected to ground. This configuration allows you to run your motor clock'wise. To run your motor counter clock'wise you need to invert the direction of the current basically have pin 1 connected to ground and pin 2 connected to a PWM signal.
Now there are multiple ways of doing this, i am unsure of the exact code to do this on an arduino but your pin 1 and 2 will be connected each to a PWM pin. In the code you will need to tell the arduino to put Pin 1 or 2 as a pullDown pin which basically mimics a ground thus telling the direction the other pin will output a PWM
this is not example code but it will give you an idea of what it should look like
void loop(){
//move clock'wise
pin1.pullup();
pin2.pulldown();
analogWrite(pin1, 180);
//move counterclock'wise
pin2.pullup();
pin1.pulldown();
analogWrite(pin2, 180);
}

Arduino Motor fan will not TOGGLE from HIGH to LOW after delay

Functionality:
Have made use of of IR sensor to toggle the state of LED light and motorFAN. Therefore, when trigger distance condition is met, both LED light and motorFAN state will toggle from LOW to HIGH. After a delay of 5 seconds, the motorFAN state will toggle from HIGH to LOW, while the state of the LED light will remain HIGH, as long as the trigger distance condition is met. The LED light will toggle to LOW when the trigger distance is no longer satisfied.
Issue:
When the trigger distance condition is met, both the LED light and motorFAN state will toggle from LOW to HIGH.
However, the motorFan state will not toggle from HIGH to LOW after a 5 seconds delay, it will still remain HIGH.
Second issue, after both LED light and motorFan state has toggled to LOW from HIGH when the trigger distance condition is no longer satisfied. And when the condition is satisfied again, only the LED light is toggled to HIGH from LOW but the motorfan state is not toggled and remain in state of LOW.
Hence, need to ask for help in rectifying the issue and what has been done wrong.
Thanks.
Code:
const int signalPin = 1; //wire pin to analog for IR Sensor
//Motor-Fan connected to arduino pin number
//const int FanPin = 5;
//Motor-Fan Relay
byte FanRelay = 4;
//const int FanRelay = 4;
//Light Relay
byte LightRelay = 6;
//const int LightRelay = 5;
int IRSignal; //variable signal, will hold the analog value read by Arduino
long duration;
int distance;
unsigned long Timer;
unsigned long Interval = 10000; //teh repeat Interval
unsigned long SmellInterval = 10000;
void setup()
{
//Execute only once at startup
//pinMode (FanPin , OUTPUT) ; // Set pinMode for FanPin as OUTPUT, display
pinMode (signalPin, INPUT); //infared sensor line will be an input to the Arduino
pinMode(FanRelay, OUTPUT);
pinMode(LightRelay, OUTPUT);
Serial.begin(9600); // Open serial port to communicate with the Ultrasaonic Sensor
}
void loop()
{
//execute multiple times in a loop
IRSignal = analogRead(signalPin); //arduino reads the value from the infared sensor
distance = 9462 / (IRSignal -16.92);
Serial.println(distance);
if(distance < 30 && distance > 0)
{
Timer = millis();
// Write a pin of HIGH
Serial.println("1");
digitalWrite (FanRelay, HIGH);
digitalWrite (LightRelay, HIGH);
if (Timer > SmellInterval){
//digitalWrite (FanPin, LOW);
digitalWrite (FanRelay, LOW);
}
}
else
{
Serial.println("0");
//Check if Timer is longer than 10s
if ((millis()-Timer)>Interval){
//digitalWrite (FanPin, LOW);
//digitalWrite (FanRelay, LOW);
digitalWrite (LightRelay, LOW);
}
}
delay(1000);
}
The issue lies here:
Timer = millis();
....
if (Timer > SmellInterval){
//digitalWrite (FanPin, LOW);
digitalWrite (FanRelay, LOW);
}
The Timer variable will hold current value of millis() which is system time from the last reset. That means that, the if condition will be satisfied always after 10s of running the program.
This is why FanRelay remains low the second time your condition is satisfied - enough time expired, so immediately after it is turned on, it is turned off.
Turning off after a delay will also not work (BTW both your delays are 10s, not 5s like you mention in the post). You need to save the time only the first time the distance condition was met and check the time difference from that moment.
See https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay for a similar example. Also your design will be much more readable and easy to debug if you use a state machine concept. Some reading on wiki, and StackOverflow. In a state machine you define a set of states (like no_object_detected, object_just_detected, object_detected_a_while_ago), and transitions between them (when they happen, and what should be done when they occur).

Toggled LED and MotorFan State unable to TOGGLE back after delay

functionality:
User approaches ultrasonic/infra-red sensor connected to Arduino Uno, if the proximity distance is less than 20cm, triggerPin of ultrasonic/infra-red sensor will toggle from LOW to HIGH -> list of '0's will be toggled to list of '1's.
When the ultrasonic/infra-red sensor is toggled from '0' to '1', the state LED and state MotorFan will toggle to HIGH from LOW. Both states will remain as long as the return data from the ultrasonic sensor is '1'.
When user leaves the proximity of the ultrasonic/infra-red sensor, triggerPin of ultrasonic/infra-red sensor will toggle from HIGH to LOW -> list of '1's will be toggled to list of '0's.
However, at this point, the state LED and state MotorFan will remain in state HIGH for 10s, before toggling to state LOW. In the event, that another user approaches the proximity sensor and the ultrasonic/infra-red sensor toggles back to '1' within the delay of the 10s, state LED and state MotorFan should remain in state HIGH without toggling.
Therefore, the correct flow as follows:
ultrasonic/infra-red Sensor returns '0's -> but when sense user within (distance < 20) ultrasonic/infra-red sensor toggles to '1's when ultrasonic/infra-red sensor toggles to '1's, MotorFan state and LED state will toggle to HIGH from LOW ultrasonic/infra-red Sensor is returns '1's when sensing proximity-> but when sense user more (distance > 20) ultrasonic/infra-red sensor toggles to '0's, at this moment, MotorFan state and LED state will remain in state HIGH for 10s before toggling to LOW. Condition: when another user approaches before the 10s delay is up, MotorFan state and LED state will remain in state HIGH.
What I have done:
I have assigned the following digital pins for the following vairable:
trigPin of ultrasonic sensor is digital Pin 8 echoPin of ultrasonic sensor is digital Pin 9 FanPin of motor Fan is digital Pin 5 LED is assigned to relay 4.
Secondly, I have tried to make use of millis() to get the time and if it should exceed limit of 10000, it will digitalWrite both state to LOW from HIGH.
Code:
const int trigPin = 8;
const int echoPin = 9;
//Motor-Fan connected to arduino pin number
const int FanPin = 5;
byte relay = 4;
long duration;
int distance;
unsigned long Timer;
unsigned long Interval = 10000; //teh repeat Interval
void setup() {
Timer = millis();
pinMode(FanPin, OUTPUT); // Set pinMode for FanPin as OUTPUT, display
pinMode(trigPin, OUTPUT);
pinMode(relay, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600); // Open serial port to communicate with the Ultrasaonic Sensor
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
if (distance < 20) {
// Write a pin of HIGH
Serial.println("1");
//Set motor-fan to operate
digitalWrite(FanPin, HIGH);
digitalWrite(relay, HIGH);
} else {
Serial.println("0");
//Check if Timer is longer than 10s
if ((millis() - Timer) > Interval) {
digitalWrite(FanPin, LOW);
digitalWrite(relay, LOW);
}
}
delay(100);
}
Issue:
The Sensor is able to toggle from '0' to '1' when sense proximity, secondly, it is also able to toggle both the LED state and motorFan state to HIGH from LOW.
However, Both the LED state and motorFan toggle back to LOW when the Arduino sensor toggle from HIGH to LOW without waiting for the 10s delay
I would like to request for some assistance on this as I am absolutely stuck on this problem.
Thanks
You assign a value to Timer in setup() function, which means you do it only once, when the system starts. I would move Timer = millis(); to if (distance < 20) { condition. Then, if user moves out if sensing distance, you will already have a time of "last contact".
Remember: if you want to have something executed only at startup - put it in setup(). If you want something to be executed repeatedly - put it in the loop().

Resources