Example Code for Raspberry Pi – Digital Push Button Module

Last revision 2026/01/20

This tutorial teaches you how to use a digital push button module with Raspberry Pi to control LED lights, offering example code and insights into GPIO inputs and Python programming, essential for future Raspberry Pi projects.

Introduction

In the last tutorial, we have learned how to simply control the on and off of the LED module.
In this tutorial, we will learn about Raspberry Pi buttons and GPIO inputs based on the previous tutorial, which is very important for future projects. In this process, you will be exposed to new Python programs.

Learning Contents – Control LED by the Button
Guide: In this project, we will learn the basic principles of the button module. And we will also consolidate the basic uses of Thonny Python IDE we learned before, as well as the basic Python code for operating GPIO.

Hardware Preparation

Wiring Diagram

  • Connect the necessary peripherals such as the screen, power, keyboard and mouse to your Raspberry Pi.

  • Install the Raspberry Pi IO expansion board on your ‘Pi’ and connect the LED light-emitting module to digital port 12 on the expansion board, and the digital push button module to the digital port 8. Then power on.

  • By analyzing the schematic circuit diagram of this module, we can see that when release the button, the signal output pin is at low level and the indicator light is off. When the button is pressed, the signal pin is at high level and the indicator light is on. They are corresponding to the two states of the LED light-emitting module. In this case, the button can be used to control the LED light through a simple judgment by the Raspberry Pi.

  • Open Thonny Python IDE to copy the following program into it

Sample Code


import RPi.GPIO as GPIO    # Import the python module provided by the Raspberry Pi
import time    # Import time package to control flicker

LED=12       # Define the pin number to which the LED is connected
Blue=8       # Define the pin number to which the button is connected

GPIO.setmode(GPIO.BCM)    # Set GPIO mode, BCM mode is universally available to all Raspberry Pi
GPIO.setup(LED,GPIO.OUT)    # Set GPIO12 to output mode
GPIO.setup(Blue,GPIO.IN)      #Set GPIO8 to input mode

while True:        # Execute the following commands in an infinite loop
    if GPIO.input(Blue):  # GPIO.input(Blue) will return to the state of GPIO8 and judge it. If GPIO8 is high (that is, the button is pressed), execute the following statement
        GPIO.output(LED,GPIO.HIGH)      # Set the LED signal pin high (i.e. light up the LED)
    else :          # If GPIO8 is low (that is, the button is released), execute the following statement
        GPIO.output(LED,GPIO.LOW)   # Set the LED signal pin low (i.e. turn off the LED)
time.sleep(0.1)   # Delay one second, here is to control the frequency for querying key status

  • Click ‘Save’ to set the file name and the save path you like

  • Click ‘Run’, you can see that the light is on and off with the button pressed and released.

Was this article helpful?

TOP