Welcome to Code Sharer, a code gallery for creating, saving and sharing all kinds of code snippets.

Python — Control a relay on the Raspberry Pi using Python

Posted On: Sunday, August 5, 2018 at 1:17 PM Last updated: Sunday, August 5, 2018 at 1:19 PM

This code is written in python 3. It is intended to be part of a larger program to control lights on a timer using a relay attached to GPIO pin 18 on the raspberry pi.





THIS IS A WORK IN PROGRESS
#!/usr/bin/python  

import time
import datetime



#set the start and end times you want the pin to be high
TimeStart = datetime.time(17,0,0)
TimeEnd = datetime.time(18,30,0)

#Define main fuction
def onoff():
if TimeEnd > datetime.datetime.now().time() and TimeStart < datetime.datetime.now().time() :

print ("Pin 18 High" )


else:
print ("Pin 18 Low" )
i = 2
while i >> 1:
try:
onoff()
except (KeyboardInterrupt, SystemExit):
print ("quiting")
raise