Menu Close

Raspberry Pi – Digital Input & Output | Pushbutton & LED

This tutorial explains basic concepts of raspberry pi. Digital input and digital output. Digital input is taken through push button. This input is processes by Raspberry and it send digital command to attached LED.
When the button is pressed LED glows.

1. Introduction:

Raspberry Pi is a sort of jack of all trades when it comes to being a single board computer based on the Arm processor. The general purpose input output (GPIO) pins on the Raspberry Pi speak and listen to the outside world and can be controlled or programmed. Each pin has a specific role. Its hardware has a limited number of digital I/O pins. You can add 16 digital I/O pins by connecting a Pi wedge B + expander chip to the Raspberry Pi hardware.

A discrete signal (digital signal) supplied to the Raspberry Pi is known as digital input. This signal can be generated manually using a push button switch.

Push button switch is a switch which provides connectivity between its terminals when pressed. When the button is released terminals get disconnected.

This tutorial is for Raspberry Pi. Please note that the same tutorial can be performed on LUA as well.

S.No.ItemQuantity
1 Raspberry Pi 1
2 Pi Wedge B + 1
3 Breadboard 1
4 LED 1
5 Resistor 1k 1
6 Jumper Male to male 5
7 Push Button Switch 1
8 Resistor 10k 1

2. Building Circuit

Schematic Diagram:

Circuit Layout:

3. Programming:

Once the circuit part is done, Raspberry Pi is needed to be programmed. Here is the python code to run this circuit.

You may download this code (Python Code) from here.

Import RPi.GPIO as GPIO
From time import sleep
GPIO.setmode(GPIO.BOARD)
GPIO.setup(13, GPIO.OUT)                    #Button
GPIO.setup(22, GPIO.OUT)                    #LED
State = 0
While True:
	Input = GPIO.input(13)
	If (input == False):                    #have to press button to work
		If (state == 1):             #this is on so led will start in off
			GPIO.output(22, True)
			State = 0
		elif (state == 0):            #led will start at this position which is off
			GPIO.output(22, False)
			
			State  = 1
		Sleep(1)

4. Output

Python Coding with Raspberry Pi connects your project to the real world.The easiest introduction to Python is through IDLE, a Python development environment.

Open IDLE (Python Development Environment) from the Desktop or applications menu:

Python 3 is the newest version. Write the above code and save your code on desktop or any other location. Open Command Prompt. Write cd location_of_program > Press Enter.

Then write Python 3 file_name.py > Press Enter. (.py is file extension).

Your Python code is connected with your real world project. Try pressing and releasing the button, the LED glows if the button is pressed and gets off when the button is released.

5. Troubleshooting

LED is not glowing: Try changing polarity of LED, Pull it out, rotate it by 180 degree and insert bit again.
Still not glowing: Check the switch with multimeter, it’s terminal should be connected in pressed condition and vice-versa.

If you have any query please write us at support@roboindia.com

Thanks and Regards
Content Development Team 
Robo India
https://roboindia.com

Leave a Reply