Read a Push Button

On off? The software can handle it.

At the left is the schematic for a circuit to read a switch. 

Why all the resistors?


             

About Prototype (breadboard) Connections




Connections to the Pi

And the code to read the switch.

import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

btn = 20

# Set up Switch as input with pull up resistor.
GPIO.setup(btn, GPIO.IN, GPIO.PUD_UP)

# Carry on forever
while True:
    input = GPIO.input(btn)
    time.sleep(.5)
    print input
while True:
    input = GPIO.input(btn)
    time.sleep(.5)
    print 1 ^ input

Reference

BCM GPIO# 2nd func pin#
pin# 2nd func BCM GPIO#
- +3V3 1
2 +5V -
GPIO2 SDA1 (I2C) 3
4 +5V -
GPIO3 SCL1 (I2C) 5
6 GND -
GPIO4 GCLK 7
8 TXD0 (UART) GPIO14
- GND 9
10 RXD0 (UART) GPIO15
GPIO17 GEN0 11
12 GEN1 GPIO18
GPIO27 GEN2 13
14 GND -
GPIO22 GEN3 15
16 GEN4 GPIO23
- +3V3 17
18 GEN5 GPIO24
GPIO10 MOSI (SPI) 19
20 GND -
GPIO9 MISO (SPI) 21
22 GEN6 GPIO25
GPIO11 SCLK (SPI) 23
24 CE0_N (SPI) GPIO8
- GND 25
26 CE1_N (SPI) GPIO7
(Models A and B stop here)
EEPROM ID_SD 27
28 ID_SC EEPROM
GPIO5 - 29
30 GND -
GPIO6 - 31
32 - GPIO12
GPIO13 - 33
34 GND -
GPIO19 - 35
36 - GPIO16
GPIO26 - 37
38 - GPIO20
- GND 39
40 - GPIO21

A game

Craig has created a "Reaction Time Game on the Pi" that you can play with:

You can download it into your current directory with the following command:

Homework

1) Turn the LED on and off with the switch using Python

Yes you could just wire up the switch so that the LED would go on when the switch is pressed. But where's the fun in that?

You have already done most of the programming to accomplish this. You just need to add two programs together and stir a bit.

Steps

Use the code from the LED session and the switch session

2) Use the switch to change the flash time of the LED

Debounce.

Some method of delaying or eliminating the rapid on/off that happens when a mechanical switch closes or opens until it makes solid contact or solid release.

Interrupts

Rather than polling to see if the button is pressed we can set the program so that when the button is pressed an interrupt is generated causing a declared function (named the callback function) to be executed. The advantage of doing this is that we will still execute the interrupt function even if the rest of the code is busy sleeping. As it well might be doing if the LED cycle is a couple of seconds or more.

More Scary Code


import time
import signal
import os

import RPi.GPIO as GPIO
	
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

# Values
btn = 20       # Pin number for switch input
count = 0.0    # Count of number of button presses


# signal handlder for SIGINT
def sigint_handler(signal, frame):
    shutdown_requested = True
    print("\nCaught SIGINT, Exiting.")
    os._exit(0)

# Define the interrupt function.  All this does in the example
# is print that the button has been pressed.  channel is gpio pin
def interrupt(channel):
    # Use the already defined variable count, not a unique variable for this function.
    # Doing this allows count to increment and the value to be remembered.
    global count

    # Wait for a bit then read the input to check that we did not get here 
    # due to a spurious falling interrupt on a noisy button release. 
    time.sleep(.1)

    # Return and ignore the interrupt if the gpio value is 1 e.g. button released.
    if GPIO.input(btn) == 1:
      return
    print ("button on input ", channel, " pushed. count=", count)
    count = count + 1.0


# Set up Switch as input with pull up resistor.
GPIO.setup(btn, GPIO.IN, GPIO.PUD_UP)

# Set up interrupt detection.  
#    Falling means this will happen when the input pin goes from 3.3V to ground
#    Bouncetime=300 means that when the interrupt happens, further button
#      interrupts will be ignored for 300ms.  This allows the button to settle.
GPIO.add_event_detect(btn, GPIO.FALLING, callback=interrupt, bouncetime=200)  
 
# register SIGINT signal handler
signal.signal(signal.SIGINT, sigint_handler)

# Carry on forever doing nothing in this example. 
# We could put the LED flashing code here.
while True:
    time.sleep(.01)