IR Presence Detector with Smart Pi One¶
In this guide, we will demonstrate how to read the values from an IR presence detector 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 presence detector (e.g., HC-SR501 or similar)
- Connecting wires
- Breadboard (optional for easier connections)
Wiring Diagram¶
The IR presence detector typically has three pins: VCC, GND, and DOUT (digital output).
- VCC connects to 3.3V (Pin 1).
- GND connects to Ground (Pin 6).
- DOUT connects to GPIOG11 (Pin 7) to read the presence detection signal.
Pin Number | Pin Name | Function |
---|---|---|
1 | 3.3V | Power Supply |
7 | GPIOG11 | IR Presence Output |
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:
Reading Values via CLI¶
You can read the values from the IR presence detector using the CLI.
Steps:¶
- Configure the pin for digital input:
- Example to read and display values continuously: Use a loop to read the state of the IR presence detector and print a message only when presence is detected:
This will display "IR presence detector value: Pin 7: 1" when the detector senses something.
Using Python¶
Reading Values with Python¶
With SmartPi-GPIO and Python, you can write a simple script to read the value from the IR presence detector.
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 presence detector (GPIO7)
ir_detector_pin = 7
# Configure the pin as input
gpio.set_direction(ir_detector_pin, "in")
print("Reading values from the IR presence detector...")
try:
while True:
# Read the value from the IR presence detector
value = gpio.read(ir_detector_pin)
if value == '1': # Presence detected
print("Presence Detected!")
time.sleep(1) # Read every second
except KeyboardInterrupt:
print("Exiting...")
-
Save and exit (
CTRL+X
,Y
, andEnter
). -
Run the Python script:
This will continuously display "Presence Detected!" when detection occurs.