Python is the programming language which is more preferred for Raspberry Pi programming. Configuring GPIO pins is very much easy with Python. In this tutorial we are going to show you how to detect inputs in Raspberry Pi using Python.
Take a look at the following image for the GPIO configuration of Raspberry Pi.
Connect the positive input signal terminal to one of the GPIO pins. Here I choose GPIO 17. And connect the Ground to the Ground pin.
Dont allow voltages greater than 3.3 V to the Raspberry Pi GPIO pin, It will damage the board. Use a Zener breakdown circuit for safety purposes.
Python Program for Reading Input
#!/usr/bin/env python import time import RPi.GPIO as GPIO def main(): GPIO.setmode(GPIO.BCM) GPIO.setup(17,GPIO.IN) while True: if GPIO.input(17): print "Input Detected" time.sleep(1) else: print "Nothing Detected" time.sleep(1) GPIO.cleanup() if __name__=="__main__": main()
Steps to execute the code
1. Open the Raspberry Pi terminal.
2. Then type sudo nano input.py
3. Now type the python program and save it.
4. Execute using the command sudo python input.py
Any questions comment here.