Activating a Relay on Smart Pi One¶
This page describes how to activate a relay using the Smart Pi One, with detailed steps, wiring instructions, and code examples in both Python and C.
Required Materials¶
- Smart Pi One
- Relay module (with optoisolator recommended)
- Connecting wires
- Breadboard (optional for easier connections)
Wiring Diagram¶
Below is a sample wiring diagram for connecting a relay module to the Smart Pi One:
Pin Number | Pin Name | Function |
---|---|---|
2 | 5V | Power Supply |
7 | GPIOG11 | Signal |
6 | GND | GROUND |
Connecting the Relay¶
- Connect the Relay:
- Connect the input pin of the relay module (IN) to a GPIO pin on the Smart Pi One (GPIO7/PIN: 7).
- Connect the VCC pin of the relay module to the (5V/PIN:2) on the Smart Pi One.
-
Connect the GND pin of the relay module to the ground (GND/PIN:3) pin on the Smart Pi One.
-
Connect the Load:
- Connect the device you want to control (e.g., a lamp) to the relay's output terminals. Ensure proper electrical connections are made according to the relay's specifications.
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:
Turning on a RELAY via Command Line (CLI)¶
Step 1: Turn on the RELAY¶
To turn on the RELAY on GPIO 7:
Step 2: Turn off the RELAY¶
To turn off the RELAY:
Using Python¶
Creating the Python Script¶
- Open a terminal on your Smart Pi One.
- Create a new Python file using
nano
:
- Copy and paste the following Python code into the file:
import time
from smartpi_gpio.gpio import GPIO
# Initialize GPIO
gpio = GPIO()
# Set GPIO7 as output for the relay
gpio.setup(7, gpio.OUT)
try:
while True:
# Activate the relay (turn on)
gpio.output(7, gpio.HIGH)
print("Relay is ON")
time.sleep(2) # Keep it on for 2 seconds
# Deactivate the relay (turn off)
gpio.output(7, gpio.LOW)
print("Relay is OFF")
time.sleep(2) # Keep it off for 2 seconds
except KeyboardInterrupt:
pass
finally:
gpio.cleanup() # Clean up GPIO
- Save the file by pressing
CTRL + X
, thenY
, and finallyEnter
.
Running the Python Script¶
To run the Python script, use the following command: