Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Manufacturing Technology >> Manufacturing process

Reading Button Presses via GPIO on Raspberry Pi: A Step‑by‑Step Guide

If you want to build electronic devices, at some point you’re going to need to read data through GPIO. In this exercise, we’re going to read the value of a push button on a breadboard.

Set up the circuit as shown in the pictures on the right. Note that one end of the 4.7kOhm resistor connected to pin 23 is connected to the positive supply rail. The red jum[er cable links the positive rail on the breadboard to the 3.3V pin on the GPIO header. The green jumper wire connected to one of the pins on the button is connected to the negative rail on the breadboard, and there’s another jumper cable linking that negative supply rail to a GND pin on the GPIO header.

The resistor is used to pull up the voltage on pin 23 to logic 1. Without it, pin 23 would have an indeterminate value. When the button is pressed, pin 23 is connected directly to ground, so it switches to logic 0.

Save the following code in a file called button.py

#!/usr/bin/env python

import time
import RPi.GPIO as GPIO

def main():

    # tell the GPIO module that we want to use the 
    # chip's pin numbering scheme
    GPIO.setmode(GPIO.BCM)

    # setup pin 25 as an output
    GPIO.setup(23,GPIO.IN)
    GPIO.setup(24,GPIO.OUT)
    GPIO.setup(25,GPIO.OUT)
    GPIO.output(25,True)

    while True:
        if GPIO.input(23):
             # the button is being pressed, so turn on the green LED
             # and turn off the red LED
             GPIO.output(24,True)
             GPIO.output(25,False)
             print "button true"
        else:
             # the button isn't being pressed, so turn off the green LED
             # and turn on the red LED
             GPIO.output(24,False)
             GPIO.output(25,True)

For more detail: Detecting a button press through GPIO

Current Project / Post can also be found using:


Manufacturing process

  1. Accurate Raspberry Pi Temperature Profiling with Python, SQLite, and LabVIEW
  2. Build a Remote Temperature Sensor with Raspberry Pi and Python – Step‑by‑Step Guide
  3. Remote Weather Monitoring with Raspberry Pi 3 and PubNub
  4. Connecting HC‑SR04 Ultrasonic Sensor to Raspberry Pi 3 – A Complete Guide
  5. Integrate TI SensorTag with Blynk via Node‑RED on Raspberry Pi Zero
  6. Lightning Detection with Raspberry Pi and AS3935: A Practical Guide
  7. Control an LED with a PIR Motion Sensor on Raspberry Pi
  8. Capturing IR Remote Commands on Raspberry Pi without LIRC: A Python UART Approach
  9. Build a Smart Robot with the Bridge Shield – Raspberry Pi & Arduino Integration
  10. Build a Wi‑Fi‑Controlled Raspberry Pi Robot with Python – Step‑by‑Step Guide