Raspberry GPIO Programming

Author: Mohit Sharma

 

Getting start with Raspberry GPIO programming:

Making an LED blink is the first program as you see “Hello world” while learning any programming language. Raspberry Pi has General Purpose Input / Output (GPIO) pins which can be configured as input/output and turned on/off via a software program. In this article we will use python to control the LED. RPi.GPIO Python library is used to control the LED from Raspberry Pi.

 

What you need:

 

Hardware Setup:

Place the LED with a 270 ohm resistor in series with the GND and GPIO pin of Raspberry Pi (use a breadboard).

Resistance is used to limit the current.

Raspberry Resistor

 

Software set up:

  • Open lx terminal and install python GPIO library by typing “sudo apt-get install python-dev python-rpi.gpio” command.
  • After finishing this installation, open leafpad (text editor in raspbian).
  • Type following code in it.

 

 

 

import RPi.GPIO as GPIO              # Import GPIO library
import time                                        # Import 'time' library. Allows us to use 'sleep'

GPIO.setmode(GPIO.BOARD)    # Use board pin numbering

GPIO.setwarnings(False)              # Disable warning massage
GPIO.setup(7, GPIO.OUT)           # Setup GPIO Pin 7 to OUT

while True:                                         # forever loop

                GPIO.output(7,True)      # Turn on GPIO pin 7

                Time.sleep(1)                    #1 sec delay

                GPIO.output(7, False)    # Turn off GPIO pin 7

                Time.sleep(1)                    #1 sec delay

 

  • Now save this code with .py extension at your favorite place on pi. In my case it is “led.py”
  • Open lx terminal and type command for the folder location of the file you just saved at and hit enter. In my case it is “cd /home/pi/desktop/codes”.
  • Now execute the program by typing sudo python ‘filename’ and hit enter. In my case “sudo python led.py ”.
  • Now you can see LED blinking continuously.





 

If you get any error massage, check for errors while typing the code.


Comments

There are no comments

Post a comment