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.
Meet The MakersJoin Team
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.
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.
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
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.
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.
- import RPi.GPIO as GPIO
- import time
- # blinking function
- def blink(pin):
- # to use Raspberry Pi board pin numbers
- # set up GPIO output channel
- GPIO.setup(11, GPIO.OUT)
- # blink GPIO17 50 times
- for i in range(0,50):
.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-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-11: Using function of GPIO class to set the mode to ‘board’.
Line-13: Using function of GPIO class to set the channel at pin 11.
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.
LED blink Python code for Raspberry Pi
Last updated on 07/16/2016
|Qty||Item||Where to Find||Est. Cost||More Info|
470 Ohm Resistor
Raspberry Pi Model B