Example Code for Raspberry Pi-Control Relay Module
Last revision 2026/01/05
In this part, we will control the Relay module with Python via Pi's GPIO.
Hardware Preparation
- Raspberry Pi X1
- DFR0311 Arduino Expansion Shield for Raspberry Pi model X1
- DFR0017 Relay module (or and LED module) X1
Software Preparation
- Python 2 Installed.
- Setup SSH on Raspberry Pi.
If you didn't install python. Please enter the following commands below into LX Terminal via SSH.
root@raspberrypi:/home/pi# sudo apt-get update
root@raspberrypi:/home/pi# sudo apt-get install python-rpi.gpio
Wiring Diagram

Operating Steps
-
Plug the Pi Meet Arduino shield to the Raspberry Pi, Which connected with the power and Ethernet.
-
Connect the Relay module(or LED module) to your Pi as the hardware connection shown.
-
Login in your Pi by Putty.

-
Create a Python example code using nano.
root@raspberrypi:/home/pi# nano blink.pyimport RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(4,GPIO.OUT) while True: GPIO.output(4,GPIO.HIGH) time.sleep(1) GPIO.output(4,GPIO.LOW) time.sleep(1) -
Run the python sample by the command below.And Check the effect. PS: The module is connected to the GPIO 4 matching the software configuration.
root@raspberrypi:/home/pi# python blink.py
Result
Note: the switching of the relay module create the noise.
Was this article helpful?
