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

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.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Things You Need

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

Step 2: Circuit

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.

Step 3: Coding Part

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

Step 4: LED Blink

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.

Be the First to Share

    Recommendations

    • Instrument Contest

      Instrument Contest
    • Make it Glow Contest

      Make it Glow Contest
    • STEM Contest

      STEM Contest

    Discussions