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.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
---|---|---|
Raphael Kit |
337 |
You can also buy them separately from the links below.
COMPONENT INTRODUCTION |
PURCHASE LINK |
---|---|
Schematic Diagram¶
Experimental Procedures¶
Step 1: Build the circuit
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
This line sets the script to run using Python 3. It imports the
Button
class from gpiozero (used for the obstacle sensor) and thetime
module for delays.#!/usr/bin/env python3 from gpiozero import Button import time
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)
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). Thetry-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