Raspberry pi LED Blink

In this introduction tutorial two things are discussed one is about about Raspberry Pi and another about Raspberry Pi LED blink by using Python program, by experimenting this program you can easily understand about Raspberry pi and its GPIO pins.

Raspberry Pi

A small computer board based on ARM architecture processor and designed for Electronic Designers, Engineers and hobbyists to do experiments, designs and implementation (we can simply say as High power Electronic Development board). Raspberry Pi is available as more than four models(A/B/A+B+/Zero) with different specifications.

GPIO pins

General Purpose Input/Output is refered as GPIO and it is the way to connect external electronic elements. We can control all pins except power pins by mentioning the physical number of these pins in program.

Wiring

Connect wires as mentioned in the illustration, here pin 9 is GND and connected to the Cathode pin of LED then
Pin 11 is GPIO and connected to LED Anode pin through 220R resistor. Refer pinouts if you use other version or model of Raspberry Pi.

Program

Here Python based program is used to perform raspberry pi LED blink and make sure about the Python version also update before making the code. Use Ethernet LAN port or WiFi adapter to Update.

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

Create python script as given below and name it as “blink.py”

 

import RPi.GPIO as GPIO
import time

LedPin = 11    # pin 11 used to control LED

def setup():
  GPIO.setmode(GPIO.BOARD)       # Numbers GPIOs by physical location
  GPIO.setup(LedPin, GPIO.OUT)   # Set LED Pin's mode is output
  GPIO.output(LedPin, GPIO.HIGH) # Set LED Pin high to turn ON LED

def blink():
  while True:
    GPIO.output(LedPin, GPIO.HIGH)  # LED turned ON
    time.sleep(1)
    GPIO.output(LedPin, GPIO.LOW) # LED turned OFF
    time.sleep(1)

def destroy():
  GPIO.output(LedPin, GPIO.LOW)   # LED OFF
  GPIO.cleanup()                  # Release resource

if __name__ == '__main__':     # Program start from here
  setup()
  try:
    blink()
  except KeyboardInterrupt:  # When 'Ctrl+C' is pressed, the child program destroy() will be  executed.
    destroy()

After creating the python script open the command prompt on Raspberry Pi and enter the following code.

sudo python blink.py

Finally you are ready to see the LED blink.
 



Leave a Reply

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