• hmxmghl Created Project

    07/16/2016 | 5:57am

Loading Items

About This Project

Hi fellows. In this project, we will learn to write the first & most basic program for Raspberry Pi using Python. If you have basic knowledge of any programming language, then you should know that most basic and 'getting started' program for any language is 'Hello world!'. So we can say this guide will teach you about 'Hello world! for Pi' i.e. LED blinking code. Have fun learning.

Request Access

Project Info

Skills

  • Python
  • Raspberry Pi
  • Overall

Meet The Makers

Join Team
  • hmxmghl
    • User:hmxmghl
    • Rank:Journeyman
    • Projects:10
    • Joined:2016

No Comments yet. Why don't you add one?

.Introduction

One of the few things that separates the Pi from other SBC (Single Board Computer) is the ability to use the GPIO (General Purpose Input/Output) pins which can be set as HIGH or LOW to control any external devices. All you need is a female to male jumper wire to get started. Here I have used a HDD IDE connector to get the job done.

.Used Pins:

In this project, pin 9 is used for GND and pin 11 is for GPIO17. The LED was connected using a 470 ohm register in series with pin 9 and 11 to limit the current.

.Making Connections:

Use the following images to make connections:

or you can refer the image below for connections:

.Installing Raspbian OS:

Your Raspberry Pi board must be bootloaded with Raspbian OS in order o code it. This YouTube video is a very good guide to get start with Raspbian OS:

Raspbian OS guide: https://youtu.be/RP8Awwnlxxg?list=PLbPDsYSO7D9r381eer7O0Zf5qu4_VYCH9

.Installing Library:

The fastest way to get started is to use python which comes pre-installed with all images. Download the RPi.GPIO library and copy the gz tar ball to the RPi wheezy raspbian. Open the terminal and navigate to the extracted folder containing the RPi.GPIO library.

Download link for RPi.GPIO library: https://pypi.python.org/pypi/RPi.GPIO

Then type: $ sudo python setup.py install to install the module.

.Code:

After installing Raspbian OS, click on 'IDLE' icon and Python shell will open or you can open it by using the way shown in picture below.

After that a window of Python shell will appear that is where you need to put your code.

I have written a simple code to turn ON and turn OFF the LED after a delay of 1 sec (1000ms) each. The LED blinks 50 times.

  1. import RPi.GPIO as GPIO
  2. import time
  3. # blinking function
  4. def blink(pin):
  5. GPIO.output(pin,GPIO.HIGH)
  6. time.sleep(1)
  7. GPIO.output(pin,GPIO.LOW)
  8. time.sleep(1)
  9. return
  10. # to use Raspberry Pi board pin numbers
  11. GPIO.setmode(GPIO.BOARD)
  12. # set up GPIO output channel
  13. GPIO.setup(11, GPIO.OUT)
  14. # blink GPIO17 50 times
  15. for i in range(0,50):
  16. blink(11)
  17. GPIO.cleanup()

.Explanation of code:

Line-1: Importing GPIO library which we previously installed.

Line-2: Importing library of raspbian named ‘time’ to use time elements in the code.

Line-3: Comment.

Line-4: Defining function named ‘blink’ and passing pin as argument.

Line-5: Using function of GPIO class to set the output HIGH.

Line-6: Using function of Time class to set add the sleep time of 1 second.

Line-7: Using function of GPIO class to set the output LOW.

Line-8: Using function of Time class to set add the sleep time of 1 second.

Line-9: ‘blink’ function is terminating by returning null.

Line-10: Comment.

Line-11: Using function of GPIO class to set the mode to ‘board’.

Line-12: ‘Comment

Line-13: Using function of GPIO class to set the channel at pin 11.

Line-14: Comment.

Line-15: Initializing for loop from 0 to 50.

Line-16: Calling the blink function with pin number 11.

Line-17: Cleaning up the values set to GPIO class methods.

So, this was the first and most basic program for Pi using Python. I hope you understood the code.

  Details Downloads Size Get it

LED blink Python code for Raspberry Pi

Last updated on 07/16/2016

by hmxmghl

369 414b
Qty   Item Where to Find Est. Cost More Info
1 LED

LED

$0.12
1 Breadboard

Breadboard

$6
1 470 Ohm Resistor

470 Ohm Resistor

$5.00
1 Raspberry Pi Model B

Raspberry Pi Model B

700MHz, 512 MB SD Ram

$35.99 1 link  
1 Wire Strippers

Wire Strippers

$18.97
1 Wire Cutters

Wire Cutters

$8.49
1 Wire Kit

Wire Kit

$22.00