AndrewPiB
Posts: 11
Joined: Sun Dec 25, 2016 5:01 pm

GPIO Pull UP Resistor for switch inputs

Sun Dec 25, 2016 6:05 pm

Hello,

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)  
Thank You

Andrew

User avatar
Burngate
Posts: 6302
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: GPIO Pull UP Resistor for switch inputs

Mon Dec 26, 2016 4:06 pm

AndrewPiB wrote: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.
Thank You

Andrew
Yes, that's what the internal Pull Ups are for, and, no it won't damage the Pi to use them.

However, they are quite weak (50k) so you may wish to (also) use your own pull-up resistors - 1k maybe - if you think there might be the possibility of false triggering from interference.

AndrewPiB
Posts: 11
Joined: Sun Dec 25, 2016 5:01 pm

Re: GPIO Pull UP Resistor for switch inputs

Mon Dec 26, 2016 9:12 pm

Thank You.

ElEscalador
Posts: 839
Joined: Tue Dec 15, 2015 4:55 pm
Location: Detroit, MI USA
Contact: Website

Elescalador

Mon Dec 26, 2016 10:42 pm

For what it's worth, I was concerned the internal resistors were too weak for a zero I have monitoring 9 inputs from as far as 30ft/10 meters away. I tried it anyway just to see how often I got false triggers but it simply has not been a problem even with electrically noisy power tools being run nearby. I do have the software sampling several times to verify state changes though.
Robotics tips, hacks, book extras https://youtube.com/practicalrobotics

AndrewPiB
Posts: 11
Joined: Sun Dec 25, 2016 5:01 pm

Re: GPIO Pull UP Resistor for switch inputs

Tue Dec 27, 2016 10:38 am

Thank You.

I have tried the circuit with 20m of Cat5 which is what I have installed from the garden to the house switch position. I haven't tried running a drill next to the Cat5 but I will give it a go. Great advice.

Regards

Andrew

Return to “Beginners”