Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- ''' rp_input_test101.py
- the black terminal strip is located on the upper side of the Raspberry B+ board
- there a two rows of 20 physical pins each
- the upper row starts with the even pins 2 (upper left corner, ulc) to 40
- the lower row starts with the odd pins 1 (lower left corner, llc) to 39
- We will be using the following pins:
- GND (pin 9)
- GPIO_26 (pin 37) RASP B+
- You can tell the Raspberry Pi computer to set the input pin
- normally low or high.
- Pulldown setting (pin is kept low):
- Connect a momentary pushbutton between +3.3V supply (pin 1) via a
- 1k ohm limiting resistor (for safety) to general purpose in/out GPIO_26
- pressing the pushbutton will put 3.3 volt on GPIO_26 signaling an input
- Pullup setting (pin is kept high):
- Connect a momentary pushbutton between GND (pin 9) via a 1k ohm
- limiting resistor (for safety) to general purpose in/out GPIO_26
- pressing the pushbutton will put 0 volt on GPIO_26 signaling an input
- To execute this code run IDLE as root (super user) via terminal command
- sudo idle
- or from the terminal (needs shebang line) ...
- cd rpi_python
- sudo python rp_input_test101.py
- for parts used see ...
- http://www.canakit.com/raspberry-pi-starter-ultimate-kit.html
- code stored at
- '''
- import RPi.GPIO as gpio
- import time
- def input_26_sense(pulldown=True):
- # +3.3 volt applied to GPIO_26 gives True
- if gpio.input(26) and pulldown:
- return True
- # 0 volt applied to GPIO_26 gives True
- elif not gpio.input(26) and not pulldown:
- return True
- else:
- return False
- # set board mode to Broadcom (recommended)
- # gpio.BOARD would use the pysical pin numbers
- gpio.setmode(gpio.BCM)
- # this sets up a pulldown resistor internally
- # input pin is kept low (around 0V)
- gpio.setup(26, gpio.IN, pull_up_down=gpio.PUD_DOWN)
- # this sets up a pullup resistor internally
- # input pin is kept high (around +3.3V)
- #gpio.setup(26, gpio.IN, pull_up_down=gpio.PUD_UP)
- try:
- # wait till pushbutton has been pressed
- print("Press the pushbutton ...")
- while True:
- pulldown = True
- in26 = input_26_sense(pulldown)
- if in26 == True:
- print("Pushbutton pressed!")
- break
- # respond to ctrl/c (in case electronics are not working)
- except KeyboardInterrupt:
- # reset all GPIO to inactive
- gpio.cleanup()
- #help(gpio)
Advertisement
Add Comment
Please, Sign In to add comment