Bird
0
0
Arduinoprogramming~30 mins

PIR motion sensor in Arduino - Mini Project: Build & Apply

Choose your learning style9 modes available
PIR Motion Sensor
📖 Scenario: You want to create a simple security system that detects motion using a PIR motion sensor connected to an Arduino board. When motion is detected, an LED will turn on to alert you.
🎯 Goal: Build a program that reads input from a PIR motion sensor and turns an LED on when motion is detected, and off when no motion is detected.
📋 What You'll Learn
Use a variable called pirPin to store the PIR sensor input pin number (2).
Use a variable called ledPin to store the LED output pin number (13).
Create a variable called motionDetected to store the PIR sensor reading.
Set the LED pin as output and the PIR pin as input in the setup() function.
Read the PIR sensor value inside the loop() function and store it in motionDetected.
Turn the LED on if motion is detected (value HIGH), otherwise turn it off.
Print "Motion detected!" or "No motion." to the Serial Monitor accordingly.
💡 Why This Matters
🌍 Real World
PIR motion sensors are used in home security systems to detect movement and alert homeowners.
💼 Career
Understanding how to read sensors and control outputs is a key skill for embedded systems and IoT device programming.
Progress0 / 4 steps
1
Set up PIR sensor and LED pins
Create two integer variables called pirPin and ledPin. Set pirPin to 2 and ledPin to 13.
Arduino
Hint

Use int to declare the pin numbers as variables.

2
Initialize pins in setup()
Write the setup() function. Inside it, set ledPin as an output and pirPin as an input. Also start serial communication at 9600 baud with Serial.begin(9600);.
Arduino
Hint

Use pinMode() to set pin modes and Serial.begin(9600); to start serial communication.

3
Read PIR sensor and control LED
Write the loop() function. Inside it, read the PIR sensor value from pirPin and store it in an integer variable called motionDetected. If motionDetected is HIGH, turn on the LED connected to ledPin. Otherwise, turn the LED off.
Arduino
Hint

Use digitalRead() to get the sensor value and digitalWrite() to control the LED.

4
Print motion status to Serial Monitor
Inside the loop() function, after controlling the LED, add an if statement to print "Motion detected!" to the Serial Monitor if motionDetected is HIGH. Otherwise, print "No motion.".
Arduino
Hint

Use Serial.println() to print messages to the Serial Monitor.