How to use RPI sensor with arduino

In this tutorial, you will learn how to use a PIR (Passive Infrared) motion sensor with an Arduino Uno to detect motion. When motion is detected, an LED will turn on. This is a common setup for simple motion-activated alerts or indicators. 

 📝 Required components

  • Arduino Uno 

  • PIR Motion Sensor
  • LED (any color)
  • 220Ω resistor (for the LED)
  • Jumper wires

  • Breadboard 

How the PIR Sensor Works 

The PIR sensor detects infrared radiation emitted by warm bodies (like humans or animals). When it senses motion, the sensor outputs a HIGH signal through its OUT pin. This signal can then be used to control other components like LEDs, buzzers, or relays.

Step 1: Wiring the Electronics 

1. Connect the VCC pin of the PIR sensor to the 5V pin on the Arduino. 
2. Connect the GND pin of the PIR sensor to the Arduino GND. 
3. Connect the OUT pin of the PIR sensor to digital pin 7 on the Arduino. 
4. Connect the anode (long leg) of the LED to digital pin 13 through a 220Ω resistor. 
5. Connect the cathode (short leg) of the LED to GND. 


 

🖥️Step 2: Writing the Code 

Below you will find the complete code to use the rip sensor:

				
					 
const int pirPin = 7;        // PIR sensor output pin 
const int ledPin = 13;       // LED connected to pin 13 
 
void setup() { 
  pinMode(pirPin, INPUT);    // Set PIR pin as input 
  pinMode(ledPin, OUTPUT);   // Set LED pin as output 
  Serial.begin(9600);        // Start serial monitor 
} 
 
void loop() { 
  int motion = digitalRead(pirPin); 
 
  if (motion == HIGH) { 
    digitalWrite(ledPin, HIGH);    // Turn on LED 
    Serial.println("Motion detected!"); 
  } else { 
    digitalWrite(ledPin, LOW);     // Turn off LED 
    Serial.println("No motion."); 
  } 
 
  delay(200); // small delay to avoid flooding the serial output 
} 
 
				
			

🔎 Breaking down the Code

Let’s review the code section by section:

				
					const int pirPin = 7;        // PIR sensor output pin
const int ledPin = 13;       // LED connected to pin 13
				
			
  • These two lines declare constants for the pins:

    • pirPin is connected to the output of the PIR sensor.

    • ledPin is connected to the LED (usually the built-in LED on many Arduino boards).

				
					void setup() {
  pinMode(pirPin, INPUT);    // Set PIR pin as input
  pinMode(ledPin, OUTPUT);   // Set LED pin as output
  Serial.begin(9600);        // Start serial monitor
}
				
			
  • The setup() function runs once at startup:

    • pinMode(pirPin, INPUT) sets pin 7 as input to read the PIR.

    • pinMode(ledPin, OUTPUT) sets pin 13 as output to control the LED.

    • Serial.begin(9600) initializes serial communication for debugging messages.

				
					void loop() {
  int motion = digitalRead(pirPin);
				
			
  • The loop() runs continuously.
    Here we read the status of the PIR sensor:

    • If motion is detected, digitalRead(pirPin) returns HIGH.

    • If not, it returns LOW.

				
					 if (motion == HIGH) {
    digitalWrite(ledPin, HIGH);    // Turn on LED
    Serial.println("Motion detected!");
  } else {
    digitalWrite(ledPin, LOW);     // Turn off LED
    Serial.println("No motion.");
  }
				
			
  • If motion is detected:

    • The LED turns on (HIGH).

    • A message is printed in the Serial Monitor.

     If no motion:

    • The LED turns off (LOW).

    • The message “No motion.” is printed.

				
					 delay(200); // small delay to avoid flooding the serial output
}
				
			
  • A short delay (200 ms) is added to:

    • Prevent flooding the Serial Monitor with messages.

    • Stabilize the PIR reading, since it stays HIGH briefly after detecting motion.

Common Applications 

  • Motion-activated lighting
  • Security systems
  • Automatic alert or alarm systems

🛠️​Step 3: Troubleshooting 

 

The LED stays ON:

  • The sensor might be detecting motion continuously. 

  • Reduce sensor sensitivity using the onboard potentiometer. 

 

The LED never turns ON:

  • Check the wiring and power supply. 

  • Verify that the PIR sensor is working and properly aligned. 

es_ES