This tutorial explains how to use digital input on the Raspberry Pi. To do this, we will interface the Raspberry Pi with a push button and write a Python program to control the Raspberry Pi's GPIO pin to read the digital input signal from the push. We will implement all of this by building a simple circuit and programming it to turn on a series of LEDs based on the input signal read by the Raspberry Pi.
Digital Input and Digital Output
The Raspberry Pi is a single-board programmable device capable of carrying out computer tasks. The General Purpose Input Output (GPIO) pins on the Raspberry Pi provide a physical interface to connect and communicate with external electronic devices. The pins can be programmed as inputs or outputs and can be used to build Internet of Things solutions.
The digital inputs are signals supplied to the Raspberry Pi's GPIO by external devices like sensors, switches, push buttons, and other equipment. In this tutorial, the push button acts as a digital input and can be in one of two states: High or Low / Pushed or Released. The GPIO pin of the Raspberry Pi is configured as an input pin to read input signals from the push button. Based on the state of the button (High or Low), the LED glows. Thus, this tutorial is for both digital input (Push button) and digital output (LED).
Push Button
Pull Up / Pull Down resistors
A pull-up or pull-down resistor is a resistor that is commonly used in combination with components such as switches and transistors to provide a default state for a signal. In general, a digital circuits can have three logic states : High (True), Low (False) and Floating (High Impedence). In digital circuits, the high impedance state is a state that occurs when the state of the pin is neither logic high nor logic low. The above phenomenon is also termed the "floating state." The floating state needs to be avoided as it could lead to some undesirable results. The Raspberry Pi supports both internal and external pull-up and pull-down resistors.
External Pull-Up and Pull-Down resistor
Pull-up resistors are resistors that are used to set a high logical level as the default state of the digital pin. In the case of a push button, if we want our input digital pin to detect a low signal, then it must be initially pulled to high using a pull-up resistor. To achieve this, connect one end of the push button to the ground and the other end to the Raspberry Pi's GPIO pin. Finally, a pull-up resistor is connected between the input pin (GPIO pin) and the voltage supply (Vcc) to make the default state of the digital pin (GPIO pin) high. Thus, with a pull-up resistor, when the button is pressed, it will read or receive a logical LOW.
Pull-down resistors are resistors that are used to set a low logical level as the default state of the digital pin. In the case of a push button, if we want our input digital pin to detect a high signal, then it must be initially pulled to low using a pull-down resistor. To achieve this, connect one end of the push button to the voltage supply (Vcc) and the other end to the Raspberry Pi's GPIO pin. Finally, a pull-down resistor is connected between the input pin (GPIO pin) and the ground (GND) to make the default state of the digital pin (GPIO pin) low. Thus, with a pull-up resistor, when the button is pressed, it will read or receive a logical HIGH.
Internal Pull-Up and Pull-Down resistor
Raspberry Pi has built-in internal pull-up and pull-down resistors which can be activated through python code. To enable internal pull-up or internal pull-down, add an additional argument to the GPIO.setup().
GPIO.setup(button, GPIO.IN, pull_up_down=GPIO_PUD_UP)
Circuit Diagram
In this tutorial, we will see the circuit connection for interfacing a push button and LED using an Internal Pull Up resistor. The following steps define the hardware setup,
- Connect one terminal of the push button to the ground (GND) pin of Raspberry Pi.
- Connect another terminal of the push button to the Raspberry Pi's GPIO21 pin. (Since we use internal pull-up resistor, there is no need for additional pull-up resistor)
- Connect the anode of the LED to Raspberry Pi's Pin 12 (GPIO18).
- Connect the cathode of the LED to one terminal of a 10KΩ (Brown Black Orange Gold) resistor.
- The other end of the resistor terminal is connected to the ground pin (GND) of Raspberry Pi.
Python Code
With your Push button and LED connected, let’s now look at the python code to turn on/off a series of LEDs based on the input signals read from the push button. The python script with internal pull up resistor is given below.
0 Comments