Apologies if this has been answered or if it is obvious. I am only a few days in with RPi and Python.
I have used various bits of code from the web (thank you to those people) and using the code I have pulled together a program where I have 4 momentary lighting scene selects and a off button which are connected to 5 seperate GPIO pins as PUD_UP with the return wiring going to GND. The scenes then select either one or more outputs which operate relays.
Essentially is it right to use Pull Up resistors to do this? I have not used any physical resistors in the switch circuits. So I take a wire from say pin 11 and go to one side of the switch, then the other side of the switch goes to GND. Everything works great on my breadboard with the switches and lights (to replace relays) but am I damaging the RPi.
Code: Select all
#glc.py
#RPI 3, Python 3
#5 switchs and 5 relays / channels
import RPi.GPIO as GPIO
import time
from time import sleep
prev_input = 0
GPIO.setmode(GPIO.BOARD) #BOARD selected so I can simply count the pins
GPIO.setwarnings(False) #Disable GPIO warnings
#Set up 5 relays / channels
GPIO.setup(29,GPIO.OUT) #CH1 - Water Feature Relay
GPIO.setup(31,GPIO.OUT) #CH2 - House Wall Lights Relay
GPIO.setup(33,GPIO.OUT) #CH3 - Decking Lights Relay
GPIO.setup(35,GPIO.OUT) #CH4 - Mid Level Feature Lights Relay
GPIO.setup(37,GPIO.OUT) #CH5 - High Level Feature Lights Relay
#Set 5 relays / channels to OFF
GPIO.output(29,GPIO.LOW) #CH1 - Set to Off
GPIO.output(31,GPIO.LOW) #CH2 - Set to Off
GPIO.output(33,GPIO.LOW) #CH3 - Set to Off
GPIO.output(35,GPIO.LOW) #CH4 - Set to Off
GPIO.output(37,GPIO.LOW) #CH5 - Set to Off
#Set up four scene switches and off
GPIO.setup(11,GPIO.IN,pull_up_down=GPIO.PUD_UP) #Switch 1 - Water Feature Button (isolated to operate independantly)
GPIO.setup(12,GPIO.IN,pull_up_down=GPIO.PUD_UP) #Switch 2 - House Wall Lights Button - Isolates CH's 3,4,5
GPIO.setup(13,GPIO.IN,pull_up_down=GPIO.PUD_UP) #Switch 3 - House Wall Lights and Decking Lights - Isolates CH's 4,5
GPIO.setup(15,GPIO.IN,pull_up_down=GPIO.PUD_UP) #Switch 4 - House Wall Lights and Decking Lights and Feature Lights - Isolates Nothing
GPIO.setup(16,GPIO.IN,pull_up_down=GPIO.PUD_UP) #Switch 5 - All Off - Isolates CH's 1,2,3,4,5
#Set the state condition of the 5 switches
statesw11 = 1 #Switch 1
statesw12 = 1 #Switch 2
statesw13 = 1 #Switch 3
statesw15 = 1 #Switch 4
statesw16 = 1 #Switch 5
#While TRUE (infinite loop) to check for switch status
while True:
inputsw11 = GPIO.input(11)
inputsw12 = GPIO.input(12)
inputsw13 = GPIO.input(13)
inputsw15 = GPIO.input(15)
inputsw16 = GPIO.input(16)
if (inputsw11 == False):
if (statesw11 == 0):
GPIO.output(29,GPIO.LOW)
print("Water Feature Off")
statesw11 = 1
elif (statesw11 == 1):
GPIO.output(29,GPIO.HIGH)
print("Water Feature On")
statesw11 = 0
sleep(0.5)
if (inputsw12 == False):
if (statesw12 == 0):
GPIO.output(31,GPIO.LOW)
print("Wall Lights Off")
statesw12 = 1
elif (statesw12 == 1):
GPIO.output(31,GPIO.HIGH)
print("Wall Lights On")
statesw12 = 0
GPIO.output(33,GPIO.LOW) #Switch off ch3
GPIO.output(35,GPIO.LOW) #Switch off ch4
GPIO.output(37,GPIO.LOW) #Switch off ch5
statesw13 = 1 #Reset state of switch 3 in case it was pressed before switch 2
statesw15 = 1 #Reset state of switch 4 in case it was pressed before switch 2
statesw16 = 1 #Reset state of switch 5 in case it was pressed before switch 2
sleep(0.5)
if (inputsw13 == False):
if (statesw13 == 0):
GPIO.output(31,GPIO.LOW)
GPIO.output(33,GPIO.LOW)
print("Wall and Decking Lights Off")
statesw13 = 1
elif (statesw13 == 1):
GPIO.output(31,GPIO.HIGH)
GPIO.output(33,GPIO.HIGH)
print("Wall and Decking Lights On")
statesw13 = 0
GPIO.output(35,GPIO.LOW) #Switch off ch4
GPIO.output(37,GPIO.LOW) #Switch off ch5
statesw12 = 1 #Reset state of switch 2 in case it was pressed before switch 3
statesw15 = 1 #Reset state of switch 4 in case it was pressed before switch 3
statesw16 = 1 #Reset state of switch 5 in case it was pressed before switch 3
sleep(0.5)
if (inputsw15 == False):
if (statesw15 == 0):
GPIO.output(31,GPIO.LOW)
GPIO.output(33,GPIO.LOW)
GPIO.output(35,GPIO.LOW)
GPIO.output(37,GPIO.LOW)
print("All Lights Off")
statesw15 = 1
elif (statesw15 == 1):
GPIO.output(31,GPIO.HIGH)
GPIO.output(33,GPIO.HIGH)
GPIO.output(35,GPIO.HIGH)
GPIO.output(37,GPIO.HIGH)
print("All Lights On")
statesw15 = 0
statesw12 = 1 #Reset state of switch 2 in case it was pressed before switch 4
statesw13 = 1 #Reset state of switch 3 in case it was pressed before switch 4
statesw16 = 1 #Reset state of switch 5 in case it was pressed before switch 4
sleep(0.5)
if (inputsw16 == False):
if (statesw16 == 0):
GPIO.output(29,GPIO.LOW)
GPIO.output(31,GPIO.LOW)
GPIO.output(33,GPIO.LOW)
GPIO.output(35,GPIO.LOW)
GPIO.output(37,GPIO.LOW)
print("Everything Off")
statesw16 = 1 #The state is not important for the off as any button press is for off.
elif (statesw16 == 1):
GPIO.output(29,GPIO.LOW) #LOW is used here in both conditions to ensure everything is off. All switches are reset.
GPIO.output(31,GPIO.LOW) #LOW is used here in both conditions to ensure everything is off. All switches are reset.
GPIO.output(33,GPIO.LOW) #LOW is used here in both conditions to ensure everything is off. All switches are reset.
GPIO.output(35,GPIO.LOW) #LOW is used here in both conditions to ensure everything is off. All switches are reset.
GPIO.output(37,GPIO.LOW) #LOW is used here in both conditions to ensure everything is off. All switches are reset.
print("Everything Off")
statesw16 = 0 #The state is not important for the off as any button press is for off.
statesw11 = 1 #Reset state of switch 1 in case it was pressed before switch 5
statesw12 = 1 #Reset state of switch 2 in case it was pressed before switch 5
statesw13 = 1 #Reset state of switch 3 in case it was pressed before switch 5
statesw15 = 1 #Reset state of switch 4 in case it was pressed before switch 5
sleep(0.5)
Andrew