In the following code, the main goal is for the Vibration Motor to vibrate when the PIR Sensor detects movement. However, it seems like the PIR Sensor is not capturing any kind of movement. In other moments, the vibration motor vibrates in all moments. Even when the PIR Sensor is not capturing movement. I don't know if the issue is the coding or, the physical model.
PHYSICAL CONSTRUCTION
ARDUINO + SENSOR
VCC → 5V
OUT → D2
GND → GND
ARDUINO + MOTOR
RED → GND
BLUE → D3
CODE BEING USED
void setup() {
pinMode(2, INPUT); // Define Arduino PIN for PIR sensor
pinMode(3, OUTPUT); // Define Arduino PIN for vibration motor
Serial.begin(9600); // Enable serial monitor
}
void loop() {
bool motionDetected = digitalRead(2); // Read PIR sensor value
if (motionDetected) {
Serial.println("Motion Detected");
analogWrite(3, 255); // Vibrate at maximum intensity (255)
delay(1000); // Vibration duration (adjust as needed)
} else {
Serial.println("No Motion Detected");
analogWrite(3, 0); // Turn off the vibration motor
}
}
IN THIS FIRST VIDEO, IT LOOKS LIKE THE PIR SENSOR IS NOT WORKING (VIDEO LINK: OOO)
IN THIS SECOND VIDEO, THE MOTOR VIBRATES EVEN WHEN THE SENSOR DOESN'T CATCH MOVEMENT (VIDEO LINK: IIII)
THE CODE BEING USED FOR THE SECOND VIDEO IS SHOWN BELOW
const int pirSensorPin = 2; // PIR sensor OUT pin connected to digital pin 2
const int motorPin = 3; // Vibration motor control pin connected to digital pin 3
const unsigned long timeLimit = 5000; // Time limit for motor operation in milliseconds (5 seconds)
void setup() {
pinMode(pirSensorPin, INPUT);
pinMode(motorPin, OUTPUT);
Serial.begin(9600);
// 5-second delay at the beginning to allow the system to stabilize
delay(5000);
}
void loop() {
int pirValue = digitalRead(pirSensorPin);
if (pirValue == HIGH) {
// PIR sensor detects movement
Serial.println("MOVIEMENTO");
analogWrite(motorPin, 100); // Vibrate at intensity 100
// Delay == 5s (5000 milliseconds)
delay(timeLimit);
// Turn off the motor
analogWrite(motorPin, 0);
Serial.println("OFF");
} else {
// No movement
Serial.println("SEM MOVIMENTO");
// Turn off the motor if it was previously turned on
analogWrite(motorPin, 0);
}
// 5-second delay before the next iteration
delay(1000);
}
Information about the sensor and vibration motor:
PIR SENSOR: HC-SR501 PIR Sensor --> (https://produto.mercadolivre.com.br/MLB-1343109528-modulo-sensor-de-presenca-hc-sr501-pir-arduino-e-raspberry-_JM#position=7&search_layout=grid&type=item&tracking_id=83e07d3d-8873-442b-b49e-114d1fe77832)
VIBRATION MOTOR: Motor 1027 for Arduíno ( Nfe ) --> (https://produto.mercadolivre.com.br/MLB-2924479634-motor-de-vibraco-1027-para-arduino-nfe--_JM#position=27&search_layout=grid&type=item&tracking_id=385241eb-d22f-48c1-92fb-e0b73f8f50b8)