Detect Objects with The Infrared Motion Sensor in Arduino
Using an Infrared Motion Sensor with Arduino
An Arduino can be used to detect motion using an infrared motion sensor. This sensor works by detecting the infrared radiation emitted by objects in its field of view. When it detects movement, it will then send a signal to the Arduino, which can be used to trigger a response or take action.
Components Needed
- Arduino Board
- Infrared Motion Sensor
- Jumper Wires
- (Optional) Breadboard
Connecting the Components
The infrared motion sensor should be connected to the Arduino board using jumper wires. First, connect the ground pin on the sensor to the GND pin on the Arduino. Then, connect the power pin on the sensor to the +5V pin on the Arduino. Finally, connect the output pin on the sensor to the digital pin 2 on the Arduino.
Programming the Arduino
To program the Arduino to detect motion with the infrared motion sensor, open a new sketch and copy the following code into it. This code will keep checking the state of the sensor's output pin and print "Motion Detected!" to the serial monitor when movement is detected.
int sensorPin = 2; // Sensor is connected to digital pin 2 void setup() { Serial.begin(9600); // Start serial communication at 9600 baud pinMode(sensorPin, INPUT); // Declare the sensor pin as an input } void loop() { int sensorVal = digitalRead(sensorPin); // Read the state of the sensor pin if (sensorVal == HIGH) { // Check if the pin is HIGH Serial.println("Motion Detected!"); // Print "Motion Detected!" to the serial monitor } delay(100); // Delay 100ms before checking again }
Now that the code is written, upload it to your Arduino board. If everything was connected correctly, you should see "Motion Detected!" printed in the serial monitor each time movement is detected by the infrared motion sensor.
Conclusion
Using an infrared motion sensor with Arduino is a great way to add the ability to sense movement. With just a few components and a bit of code, you can quickly create projects that can detect motion and react accordingly. Have fun building!