How to get digital inputs to Raspberry Pi board

How to get digital inputs to Raspberry Pi board

Hello and welcome back. In this tutorial, we will learn how to get digital inputs to the Raspberry Pi board. For that, I use two push buttons and two LED bulbs. I think this is the best example for this tutorial. Also, we can connect the push buttons two ways, that is Pull up and Pull down. In this tutorial, let’s learn how to set up circuits for both of these methods.

If we are doing this project with Arduino, resistors are used to make Pull-up and Pull-down circuits. Raspberry Pi does not require resistors. Because Raspberry pi has an inbuilt Pull-up and Pull-down resistor. It is a good advantage for us. You can understand it from the following circuit diagrams. Also, I didn’t use the GPIO extension board and ribbon cables for this tutorial. But, you can use it. OK, let’s move on.

OK, let’s do this project step by step. The required components are given below.

Step 1

Firstly, identify these components.

Step 2

Secondly, place the push buttons and LEDs on the breadboard. After, connect the resistors to the LED.

Step 3

Thirdly, let’s assemble the Pull up and Pull down circuits.

Pull up circuit

  • OK, now let’s assemble the pull up circuit. Use the circuit diagram below for that. For this tutorial, you can use one push button and one LED bulb. But, I used two push button switches and two LED bulbs.
How to get digital inputs to Raspberry Pi board

  • Now, connect the mouse, keyboard, monitor, and SD card (OS installed) to the Raspberry pi board.

  • And then, connect the power source to the Raspberry pi board. Then, copy the following script into Thonny IDE.

  • Python script and circuit diagram — Download
# Include the library files
import RPi.GPIO as GPIO
from time import sleep

# Enter the button pins
button_1 = 5
button_2 = 6

# Enter the LED pins
LED_1 = 20
LED_2 = 21

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

# Set the LED pins as Output pins
GPIO.setup(LED_1,GPIO.OUT)
GPIO.setup(LED_2,GPIO.OUT)

# Set the Button pins as Input pins
GPIO.setup(button_1,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(button_2,GPIO.IN,pull_up_down=GPIO.PUD_UP)


while True:
    if GPIO.input(button_1) == 0: # Get the button value
        GPIO.output(LED_1,GPIO.HIGH) # LED bulb turns ON
    elif GPIO.input(button_1) == 1: # Get the button value
        GPIO.output(LED_1,GPIO.LOW) # LED bulb turns OFF
        
    if GPIO.input(button_2) == 0: 
        GPIO.output(LED_2,GPIO.HIGH)
    elif GPIO.input(button_2) == 1:
        GPIO.output(LED_2,GPIO.LOW)       
  • Next, save this code and run it.

Pull down circuit

  • Now, let’s assemble the pull down circuit. It’s as follows.

  • Next, connect the mouse, keyboard, monitor, and SD card (OS installed) to the Raspberry Pi board.

  • And then, connect the power source and copy and paste the below script to the Thonny IDE.

  • Python script and circuit diagram — Download
# Include the library files
import RPi.GPIO as GPIO
from time import sleep

# Enter the button pins
button_1 = 5
button_2 = 6

# Enter the LED pins
LED_1 = 20
LED_2 = 21

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

# Set the LED pins as Output pins
GPIO.setup(LED_1,GPIO.OUT)
GPIO.setup(LED_2,GPIO.OUT)

# Set the Button pins as Input pins
GPIO.setup(button_1,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(button_2,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)


while True:
    if GPIO.input(button_1) == 0: # Get the button value
        GPIO.output(LED_1,GPIO.HIGH) # LED bulb turns ON
    elif GPIO.input(button_1) == 1: # Get the button value
        GPIO.output(LED_1,GPIO.LOW) # LED bulb turns OFF
        
    if GPIO.input(button_2) == 0: 
        GPIO.output(LED_2,GPIO.HIGH)
    elif GPIO.input(button_2) == 1:
        GPIO.output(LED_2,GPIO.LOW)    
      
  • Now, save this code and Run it.

OK, try this tutorial. The full video guide is below. So, see you in the next project or tutorials.

How to get digital inputs to Raspberry Pi board

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart