Last active
January 15, 2024 22:00
-
-
Save johnwargo/ea5edc8516b24e0658784ae116628277 to your computer and use it in GitHub Desktop.
Simple Python app for controlling a relay through an GPIO Zero Output device
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# A simple Python application for controlling a relay board from a Raspberry Pi | |
# The application uses the GPIO Zero library (https://gpiozero.readthedocs.io/en/stable/) | |
# The relay is connected to one of the Pi's GPIO ports, then is defined as an Output device | |
# in GPIO Zero: https://gpiozero.readthedocs.io/en/stable/api_output.html#outputdevice | |
import sys | |
import time | |
# Make sure you install required libraries: | |
# https://gpiozero.readthedocs.io/en/stable/installing.html | |
import gpiozero | |
# change this value based on which GPIO port the relay is connected to | |
RELAY_PIN = 18 | |
# create a relay object. | |
# Triggered by the output pin going low: active_high=False. | |
# Initially off: initial_value=False | |
relay = gpiozero.OutputDevice(RELAY_PIN, active_high=False, initial_value=False) | |
def set_relay(status): | |
if status: | |
print("Setting relay: ON") | |
relay.on() | |
else: | |
print("Setting relay: OFF") | |
relay.off() | |
def toggle_relay(): | |
print("toggling relay") | |
relay.toggle() | |
def main_loop(): | |
# start by turning the relay off | |
set_relay(False) | |
while 1: | |
# then toggle the relay every second until the app closes | |
toggle_relay() | |
# wait a second | |
time.sleep(1) | |
if __name__ == "__main__": | |
try: | |
main_loop() | |
except KeyboardInterrupt: | |
# turn the relay off | |
set_relay(False) | |
print("\nExiting application\n") | |
# exit the application | |
sys.exit(0) |
I m agtting error.
Traceback (most recent call last):
File "relay-test.py", line 11, in
import gpiozero
ModuleNotFoundError: No module named 'gpiozero'
@krahul9473 did you install the gpiozero
library? It's not just there, you have to install it.
No I m not installed gpizero library
…On Mon, 2 Aug, 2021, 9:18 pm John M. Wargo, ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
@krahul9473 <https://github.com/krahul9473> did you install the gpiozero
library? It's not just there, you have to install it.
https://gpiozero.readthedocs.io/en/stable/installing.html
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/ea5edc8516b24e0658784ae116628277#gistcomment-3840480>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AOFWT7BW22CDWIUE4UMYS23T2245HANCNFSM4HJ63NQA>
.
Well, its not going to work unless you install all required libraries. There's nothing I can to do to help here.
Thank you so much ,now it's working
…On Mon, 2 Aug, 2021, 11:00 pm John M. Wargo, ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Well, its not going to work unless you install all required libraries.
There's nothing I can to do to help here.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/ea5edc8516b24e0658784ae116628277#gistcomment-3840680>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AOFWT7HPFMELHWIHXRZMBD3T23I2FANCNFSM4HJ63NQA>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&utm_campaign=notification-email>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great example code. Will be adding to https://github.com/ROBO-BEV/CafeBeep/blob/master/CafeBeepKiosk-0.1/Actuator.py