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!
Lesson 08: IR Obstacle Avoidance Sensor Moduleď
In this lesson, you will learn how to detect obstacles using a sensor with the Raspberry Pi. We will guide you through connecting a digital input sensor to GPIO pin 17. Youâll learn how to write a Python script that continuously monitors the sensor to determine the presence of an obstacle. The program will output a message indicating whether an obstacle is detected or not. This straightforward yet practical project is an excellent way to get started with GPIO interfacing and Python programming, making it ideal for beginners interested in exploring sensor integration with the Raspberry Pi.
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 |
|---|---|---|
Universal Maker Sensor Kit |
94 |
You can also buy them separately from the links below.
Component Introduction |
Purchase Link |
|---|---|
Raspberry Pi 5 |
|
Wiringď
Codeď
from gpiozero import InputDevice
from time import sleep
# Initialize the sensor as a digital input device on GPIO 17
sensor = InputDevice(17)
while True:
if sensor.is_active:
print("No obstacle detected") # Prints when no obstacle is detected
else:
print("Obstacle detected") # Prints when an obstacle is detected
sleep(0.5)
Code Analysisď
Importing Libraries
The script begins by importing the
InputDeviceclass from the gpiozero library for interacting with the sensor, and thesleepfunction from Pythonâs time module for pausing execution.from gpiozero import InputDevice from time import sleep
Initializing the Sensor
An
InputDeviceobject namedsensoris created, connected to GPIO pin 17. This line assumes that the obstacle sensor is connected to this specific GPIO pin.sensor = InputDevice(17)
Implementing the Continuous Monitoring Loop
The script uses a
while True:loop to continuously check the sensorâs state. This loop will run indefinitely until the program is stopped.Inside the loop, an
ifstatement checks theis_activeproperty of thesensor.If
is_activeisTrue, it indicates no obstacle is detected, and âNo obstacle detectedâ is printed.If
is_activeisFalse, indicating an obstacle is detected, âObstacle detectedâ is printed.sleep(0.5)pauses the loop for 0.5 seconds between each check, which helps in reducing the scriptâs processing demand and provides a delay between consecutive sensor readings.
while True: if sensor.is_active: print("No obstacle detected") else: print("Obstacle detected") sleep(0.5)
Note
If the sensor is not working properly, adjust the IR transmitter and receiver to make them parallel. Additionally, you can adjust the detection range using the built-in potentiometer.