Raspberry Pi - Obstacle Sensor

This tutorial instructs you how to use Raspberry Pi with the infrared obstacle avoidance sensor. In detail, we will learn:

Hardware Preparation

1×Raspberry Pi 4 Model B
1×IR Obstacle Avoidance Sensor
1×Jumper Wires
1×(Optional) Screw Terminal Adapter for Raspberry Pi

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of IR Obstacle Avoidance Sensor

The infrared obstacle sensor is capable of detecting the presence of an object in front of it by using infrared signals. The range of detection is from 2cm to 30cm, and can be adjusted with a built-in potentiometer.

The Infrared Obstacle Avoidance Sensor Pinout

The IR obstacle avoidance sensor has three pins:

  • GND pin: must be connected to GND (0V)
  • VCC pin: must be connected to VCC (5V or 3.3v)
  • OUT pin: is an output pin, LOW when an obstacle is present, HIGH when there is no obstacle. This pin should be connected to a Raspberry Pi input pin.
IR Obstacle Avoidance Sensor pinout

How It Works

The infrared obstacle sensor module has a built-in IR transmitter and IR receiver. The IR transmitter sends out the IR signal. The IR receiver looks for the reflected IR signal to detect if an object is present or not. The OUT pin of the sensor reflects the presence of an obstacle:

  • If there is an obstacle in front of the sensor, the OUT pin of the sensor will be LOW
  • If there is no obstacle in front of the sensor, the OUT pin of the sensor will be HIGH

※ NOTE THAT:

During shipment, the sensor may get deformed, which can cause malfunctions. If the sensor is not working properly, adjust the IR transmitter and receiver to ensure they are parallel to each other.

Wiring Diagram

The wiring diagram between Raspberry Pi and IR Obstacle Avoidance Sensor

This image is created using Fritzing. Click to enlarge image

How To Program For IR Obstacle Avoidance Sensor

  • Initializes the Raspberry Pi pin to digital input mode using the GPIO.setup() function.
GPIO.setup(SENSOR_PIN, GPIO.IN)
  • Utilize the GPIO.input() function to examine the state of the Raspberry Pi pin.
obstacle_state = GPIO.input(SENSOR_PIN)

Raspberry Pi Code

Two approaches can be taken when programming for an obstacle avoidance application:

  • Taking action depending on whether the obstacle is present or not
  • Taking action based on whether the obstacle has been detected or removed

Raspberry Pi code for checking if the obstacle is present

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Create a Python script file obstacle_sensor.py and add the following code:
# This Raspberry Pi code was developed by newbiely.com # This Raspberry Pi code is made available for public use without any restriction # For comprehensive instructions and wiring diagrams, please visit: # https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-obstacle-sensor import RPi.GPIO as GPIO # Set the GPIO mode to BCM (Broadcom SOC channel numbering) GPIO.setmode(GPIO.BCM) # Set the pin number connected to the ir obstacle avoidance sensor SENSOR_PIN = 12 # Set the GPIO pin as an input GPIO.setup(SENSOR_PIN, GPIO.IN) try: while True: # Read the state from the ir obstacle avoidance sensor obstacle_state = GPIO.input(SENSOR_PIN) # The ir obstacle avoidance sensor outputs LOW (0) when obstacle is present, otherwise HIGH (1) if obstacle_state == GPIO.LOW: print("The obstacle is present") else: print("The obstacle is NOT present") except KeyboardInterrupt: # Clean up the GPIO settings on program exit GPIO.cleanup()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 obstacle_sensor.py
  • Place an obstacle in front of the sensor for a period of time, then take it away.
  • Check the outcome in the Terminal.
PuTTY - Raspberry Pi
The obstacle is NOT present The obstacle is NOT present The obstacle is NOT present The obstacle is NOT present The obstacle is NOT present The obstacle is present The obstacle is present The obstacle is present The obstacle is present The obstacle is NOT present The obstacle is NOT present

The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.

Raspberry Pi code for detecting obstacle

Detailed Instructions

  • Create a Python script file obstacle_sensor_events.py and add the following code:
# This Raspberry Pi code was developed by newbiely.com # This Raspberry Pi code is made available for public use without any restriction # For comprehensive instructions and wiring diagrams, please visit: # https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-obstacle-sensor import RPi.GPIO as GPIO import time # Set the GPIO mode to BCM (Broadcom SOC channel numbering) GPIO.setmode(GPIO.BCM) # Set the pin number connected to the ir obstacle avoidance sensor SENSOR_PIN = 12 # Set the GPIO pin as an input GPIO.setup(SENSOR_PIN, GPIO.IN) # Variable to track the ir obstacle avoidance sensor state prev_obstacle_state = GPIO.HIGH # Assuming no obstacle initially try: while True: obstacle_state = GPIO.input(SENSOR_PIN) if obstacle_state != prev_obstacle_state: if obstacle_state == GPIO.LOW: # obstacle is detected print("An obstacle is detected") else: # An obstacle is removed print("An obstacle is removed") prev_obstacle_state = obstacle_state time.sleep(0.1) # A small delay to debounce the input except KeyboardInterrupt: # Clean up the GPIO settings on program exit GPIO.cleanup()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 obstacle_sensor_events.py
  • Place an obstacle in front of the sensor for a while, then take it away.
  • Check the results on the Terminal.
PuTTY - Raspberry Pi
An obstacle is detected An obstacle is removed

Video Tutorial

Learn More