LED Blink With Raspberry Pi | How to Use GPIO Pins on Raspberry Pi

by Utsource in Circuits > Raspberry Pi

2836 Views, 0 Favorites, 0 Comments

LED Blink With Raspberry Pi | How to Use GPIO Pins on Raspberry Pi

01_Blinking-LED_bb-1024x777.jpg
Hi guys in this instructables we will learn how to use GPIO's of Raspberry pi. If you have ever used Arduino then probably you know that we can connect LED switch etc. to its pins and make it work like. make the LED blink or get input from switch something like that.
Since Raspberry pi is also having GPIOs so we will learn how to use those GPIOs and we will connect a LED to it and make it blink. Just a simple LED blink project we will do to make you understand how to use GPIOs of Raspberry pi.

Things You Need

raspberry-pi-png-raspberry-pi-b-4x-usb-2-0-40-pin-gpio-raspberry-pi-raspberry-pi-1560.png
images(36).jpg
images(37).jpg

For this instructables you will need following things :

Raspberry Pi 3 setup with monitor and USB Mouse & Keyboard
(Make sure Raspbian OS is setup properly in your Raspberry pi )

breadboard

Jumper wires

Resistors

LED

Circuit

01_Blinking-LED_bb-1024x777.jpg
a-and-b-physical-pin-numbers-768x359.png
The circuit part is very easy .
I connected LED to pin 8.
Which means negative leg of LED is connected to Gnd pin (6 no.) And Positive leg is connected to 100ohm (100-1000ohm use aby value) and the other leg of resistor is connected to pin 8 of Raspberry pi.

Coding Part

images(39).jpg
Then open the pi terminal to make the LED blink :

To install the Python library open a terminal and execute the following command :

$ sudo apt-get install python-rpi.gpio python3-rpi.gpio

to initialize the GPIO ports of Raspberry Pi we need to import the Python library, then we need to initialize the library and setup the pin 8 as output pin of Raspberry pi.

import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library
from time import sleep # Import the sleep function from the time module

GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(8, GPIO.OUT, initial=GPIO.LOW) # Set pin 8 to be an output pin and set initial value to low (off)

Next thing we need to do is to make the pin 8 high (on) for one second and low (off) for one second and we will put it in a while loop so that it will blink forever.

while True: # Run forever
GPIO.output(8, GPIO.HIGH) # Turn on
sleep(1) # Sleep for 1 second
GPIO.output(8, GPIO.LOW) # Turn off
sleep(1) # Sleep for 1 second

Combining the above two parts of code together and creating a complete code :

import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library
from time import sleep # Import the sleep function from the time module

GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(8, GPIO.OUT, initial=GPIO.LOW) # Set pin 8 to be an output pin and set initial value to low (off)

while True: # Run forever
GPIO.output(8, GPIO.HIGH) # Turn on
sleep(1) # Sleep for 1 second
GPIO.output(8, GPIO.LOW) # Turn off
sleep(1) # Sleep for 1 second


So our program is finished, then we need to save it as blinking_led.py and then run it either inside your IDE or in your console with the following :

$ python blinking_led.py

LED Blink

Screenshot_20190920-112255__01.jpg
Screenshot_20190920-112248__01.jpg
After running the code you will see you LED Blinking as mine.so i hope this instructables could have helped you so let me know about that in comments.