vegaseat

Raspberry Pi Input Test 101

Jan 23rd, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.36 KB | None | 0 0
  1. #!/usr/bin/python
  2. ''' rp_input_test101.py
  3.  
  4. the black terminal strip is located on the upper side of the Raspberry B+ board
  5. there a two rows of 20 physical pins each
  6. the upper row starts with the even pins 2 (upper left corner, ulc) to 40
  7. the lower row starts with the odd pins 1 (lower left corner, llc) to 39
  8.  
  9. We will be using the following pins:
  10. GND (pin 9)
  11. GPIO_26 (pin 37)  RASP B+
  12.  
  13. You can tell the Raspberry Pi computer to set the input pin
  14. normally low or high.
  15.  
  16. Pulldown setting (pin is kept low):
  17. Connect a momentary pushbutton between +3.3V supply (pin 1) via a
  18. 1k ohm limiting resistor (for safety) to general purpose in/out GPIO_26
  19. pressing the pushbutton will put 3.3 volt on GPIO_26 signaling an input
  20.  
  21. Pullup setting (pin is kept high):
  22. Connect a momentary pushbutton between GND (pin 9) via a 1k ohm
  23. limiting resistor (for safety) to general purpose in/out GPIO_26
  24. pressing the pushbutton will put 0 volt on GPIO_26 signaling an input
  25.  
  26. To execute this code run IDLE as root (super user) via terminal command
  27. sudo idle
  28.  
  29. or from the terminal (needs shebang line) ...
  30. cd rpi_python
  31. sudo python rp_input_test101.py
  32.  
  33. for parts used see ...
  34. http://www.canakit.com/raspberry-pi-starter-ultimate-kit.html
  35.  
  36. code stored at
  37.  
  38. '''
  39.  
  40. import RPi.GPIO as gpio
  41. import time
  42.  
  43. def input_26_sense(pulldown=True):
  44.     # +3.3 volt applied to GPIO_26 gives True
  45.     if gpio.input(26) and pulldown:
  46.         return True
  47.     # 0 volt applied to GPIO_26 gives True
  48.     elif not gpio.input(26) and not pulldown:
  49.         return True
  50.     else:
  51.         return False
  52.  
  53.  
  54. # set board mode to Broadcom (recommended)
  55. # gpio.BOARD would use the pysical pin numbers
  56. gpio.setmode(gpio.BCM)  
  57.  
  58. # this sets up a pulldown resistor internally
  59. # input pin is kept low (around 0V)
  60. gpio.setup(26, gpio.IN, pull_up_down=gpio.PUD_DOWN)
  61. # this sets up a pullup resistor internally
  62. # input pin is kept high (around +3.3V)
  63. #gpio.setup(26, gpio.IN, pull_up_down=gpio.PUD_UP)
  64.  
  65.  
  66. try:
  67.     # wait till pushbutton has been pressed
  68.     print("Press the pushbutton ...")
  69.     while True:
  70.         pulldown = True
  71.         in26 = input_26_sense(pulldown)
  72.         if in26 == True:
  73.             print("Pushbutton pressed!")
  74.             break
  75. # respond to ctrl/c (in case electronics are not working)
  76. except KeyboardInterrupt:
  77.     # reset all GPIO to inactive
  78.     gpio.cleanup()
  79.  
  80. #help(gpio)
Advertisement
Add Comment
Please, Sign In to add comment