0
0
Iot-protocolsHow-ToBeginner · 4 min read

How to Use IR Sensor with Raspberry Pi: Simple Guide

To use an IR sensor with a Raspberry Pi, connect the sensor's output pin to a GPIO input pin on the Pi and use a Python library like RPi.GPIO to read the sensor's digital signal. The sensor detects infrared light and sends a HIGH or LOW signal that your program can read to detect objects or motion.
📐

Syntax

Here is the basic syntax to read an IR sensor using the RPi.GPIO library in Python:

  • GPIO.setmode(GPIO.BCM): Sets the pin numbering system to BCM (Broadcom chip-specific).
  • GPIO.setup(pin, GPIO.IN): Configures the chosen GPIO pin as an input.
  • GPIO.input(pin): Reads the digital value (0 or 1) from the sensor connected to the pin.
python
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)  # Use BCM pin numbering
sensor_pin = 17         # GPIO pin connected to IR sensor output
GPIO.setup(sensor_pin, GPIO.IN)  # Set pin as input

try:
    while True:
        sensor_value = GPIO.input(sensor_pin)  # Read sensor
        print(f"Sensor value: {sensor_value}")
        time.sleep(1)  # Wait 1 second
except KeyboardInterrupt:
    GPIO.cleanup()  # Clean up GPIO on exit
Output
Sensor value: 0 Sensor value: 0 Sensor value: 1 Sensor value: 0 ... (updates every second)
💻

Example

This example shows how to connect an IR sensor to GPIO pin 17 and print its state every second. When the sensor detects an object, it outputs LOW (0), otherwise HIGH (1).

python
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
sensor_pin = 17
GPIO.setup(sensor_pin, GPIO.IN)

try:
    while True:
        if GPIO.input(sensor_pin) == 0:
            print("Object detected!")
        else:
            print("No object detected.")
        time.sleep(1)
except KeyboardInterrupt:
    GPIO.cleanup()
Output
No object detected. No object detected. Object detected! No object detected. ... (updates every second)
⚠️

Common Pitfalls

  • Wrong pin numbering: Use GPIO.BCM mode or GPIO.BOARD mode consistently to avoid confusion.
  • Not setting up GPIO cleanup: Always call GPIO.cleanup() to reset pins when your program ends.
  • Incorrect wiring: Connect sensor power (VCC) to 3.3V or 5V as per sensor specs, ground to GND, and output to a GPIO input pin.
  • Ignoring sensor output type: Some IR sensors output analog signals; Raspberry Pi GPIO pins read digital signals only. Use a digital IR sensor or an ADC for analog sensors.
python
## Wrong way (no cleanup, wrong pin mode):
import RPi.GPIO as GPIO
GPIO.setup(17, GPIO.IN)  # Missing GPIO.setmode
print(GPIO.input(17))

## Right way:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)
print(GPIO.input(17))
GPIO.cleanup()
📊

Quick Reference

StepDescription
Connect sensor VCCTo Raspberry Pi 3.3V or 5V (check sensor specs)
Connect sensor GNDTo Raspberry Pi GND pin
Connect sensor outputTo Raspberry Pi GPIO input pin (e.g., GPIO17)
Set GPIO modeUse GPIO.setmode(GPIO.BCM) or GPIO.BOARD
Setup pinGPIO.setup(pin, GPIO.IN)
Read sensorUse GPIO.input(pin) to get 0 or 1
CleanupCall GPIO.cleanup() on program exit

Key Takeaways

Connect the IR sensor output to a Raspberry Pi GPIO input pin and power it correctly.
Use RPi.GPIO library with GPIO.setmode(GPIO.BCM) and GPIO.setup(pin, GPIO.IN) to read sensor data.
Read sensor state with GPIO.input(pin), where 0 or 1 indicates detection status.
Always call GPIO.cleanup() to reset pins when your program ends.
Check if your IR sensor outputs digital signals; Raspberry Pi GPIO pins read digital inputs only.