Obstacle IR Sensor with Smart Pi One¶
In this guide, we will demonstrate how to display a message when an obstacle is detected by an IR obstacle sensor connected to the Smart Pi One, using the SmartPi-GPIO library.
We will cover the following methods: - CLI commands - Python script
Required Materials¶
- Smart Pi One
- IR obstacle sensor
- Connecting wires
- Breadboard (optional for easier connections)
Wiring Diagram¶
The IR obstacle sensor is connected to GPIOG11 (Pin 7) as the input pin for detecting obstacles. When an obstacle is detected, the sensor outputs a LOW signal (0).
Pin Number | Pin Name | Function |
---|---|---|
1 | 3.3V | Power Supply |
7 | GPIOG11 | Obstacle Sensor Input |
6 | GND | Ground |
Prerequisites: Configuration of smartpi-gpio¶
To install SmartPi-GPIO on your Smart Pi One, follow these steps:
- Update system:
sudo apt update
sudo apt-get install -y python3-dev python3-pip libjpeg-dev zlib1g-dev libtiff-dev
sudo mv /usr/lib/python3.11/EXTERNALLY-MANAGED /usr/lib/python3.11/EXTERNALLY-MANAGED.old
- Clone the repository:
- Install the library:
- Activate GPIO interfaces:
Displaying a Message via CLI¶
You can detect obstacles using the CLI and print a message accordingly.
Steps:¶
- Configure the sensor pin as input:
- Read the sensor state:
- Example to display a message: Use a loop to continuously check the sensor status and display a message when an obstacle is detected:
This will display the current value read by the Obstacle IR every second..
Using Python¶
Displaying a Message with Python¶
With SmartPi-GPIO and Python, you can write a simple script to detect obstacles and display a message.
Steps:¶
- Create a Python file:
- Write the following code:
from smartpi_gpio.gpio import GPIO
import time
# Initialize GPIO instance
gpio = GPIO()
# GPIO pin number for the IR sensor (GPIOG11)
sensor_pin = 7
# Configure the sensor pin as input with pull-up resistor
gpio.set_direction(sensor_pin, "in", "pull-up")
print("Monitoring for obstacles...")
while True:
# Read the sensor state
sensor_state = gpio.read(sensor_pin)
if sensor_state == '0': # If an obstacle is detected
print("Obstacle Detected!")
break
time.sleep(0.1)
-
Save and exit (
CTRL + X
,Y
, andEnter
). -
Run the Python script:
When an obstacle is detected, the message "Obstacle Detected!" will be displayed.