Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

👉 Ready to explore and create with us? Click [here] and join today!

2.2.4 IR Obstacle Avoidance Sensor¶

Introduction¶

In this project, we will learn IR obstacle avoidance module, which is a sensor module that can be used to detect obstacles at short distances, with small interference, easy to assemble, easy to use, etc. It can be widely used in robot obstacle avoidance, obstacle avoidance trolley, assembly line counting, etc.

Required Components¶

In this project, we need the following components.

../_images/2.2.5_ir_obstacle_list.png

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

Raphael Kit

337

Raphael Kit

You can also buy them separately from the links below.

COMPONENT INTRODUCTION

PURCHASE LINK

GPIO Extension Board

BUY

Breadboard

BUY

Jumper Wires

BUY

Obstacle Avoidance Module

BUY

Schematic Diagram¶

../_images/2.2.5_ir_obstacle_list_schematic.png

Experimental Procedures¶

Step 1: Build the circuit

../_images/2.2.5_ir_obstacle_circuit.png

Step 2: Change directory.

cd ~/raphael-kit/python-pi5

Step 3: Run.

sudo python3 2.2.5_IrObstacle_zero.py

After the code runs, when you put your hand in front of the module’s probe, the output indicator on the module lights up and the “Detected Barrier!” will be repeatedly printed on the screen until the your hand is removed.

Code

Note

You can Modify/Reset/Copy/Run/Stop the code below. But before that, you need to go to source code path like raphael-kit/python-pi5. After modifying the code, you can run it directly to see the effect.

#!/usr/bin/env python3
from gpiozero import Button
import time

# Initialize the obstacle sensor connected to GPIO pin 17
# The sensor is configured with a pull-up resistor
obstacle_sensor = Button(17, pull_up=True)

try:
   # Continuously monitor for obstacles
   while True:
      if obstacle_sensor.is_pressed:  # Check if the sensor is triggered
            print("Detected Barrier!")  # Print a message when an obstacle is detected
            time.sleep(1)  # Delay for 1 second to avoid repetitive messages

except KeyboardInterrupt:
   # Handle KeyboardInterrupt (Ctrl+C) for a clean and safe exit
   pass

Code Explanation

  1. This line sets the script to run using Python 3. It imports the Button class from gpiozero (used for the obstacle sensor) and the time module for delays.

    #!/usr/bin/env python3
    from gpiozero import Button
    import time
    
  2. Initializes an obstacle sensor connected to GPIO pin 17 with an internal pull-up resistor.

    # Initialize the obstacle sensor connected to GPIO pin 17
    # The sensor is configured with a pull-up resistor
    obstacle_sensor = Button(17, pull_up=True)
    
  3. In a continuous loop, the program checks if the obstacle sensor is activated (is_pressed). If an obstacle is detected, it prints “Detected Barrier!” and then waits for one second (to prevent repetitive messages). The try-except structure is used to handle a KeyboardInterrupt for a clean exit.

    try:
       # Continuously monitor for obstacles
       while True:
          if obstacle_sensor.is_pressed:  # Check if the sensor is triggered
                print("Detected Barrier!")  # Print a message when an obstacle is detected
                time.sleep(1)  # Delay for 1 second to avoid repetitive messages
    
    except KeyboardInterrupt:
       # Handle KeyboardInterrupt (Ctrl+C) for a clean and safe exit
       pass