Raspberry PI Button Control Code
Buttons are simple switch mechanisms for controlling some aspect of a machine or a process. Buttons can be pressed or released. You can always ask the state of a button. You can connect multiple buttons to your microcontroller, but do not forget to add them to your programcode. Buttons use digital pins which are special microcontroller pins with HIGH or LOW states. This depends if the button is pressed or not.
Required hardware
- Raspberry PI
- Button
- Resistor 1kΩ
Source code to install on controller
#!/usr/bin/python import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(17, GPIO.IN) prev_input = GPIO.input(17) mode = input('Please choose the mode (1=Raising, 2=Falling, 3=Change): ') while True: input_state = GPIO.input(17) if mode == 1: if input_state != prev_input: if input_state == 1: print('Button pressed') if mode == 2: if input_state != prev_input: if input_state == 0: print('Button pressed') if mode == 3: if input_state != prev_input: print('Button pressed') prev_input = input_state time.sleep(0.2)
More information