Contact us by email if you have any questions!

Blinking LED Python Scripts on Raspberry Pi

Raspberry Pi is really a nice mini PC to play, apart from that, the main programming language on RPi is also maybe one of the best on the world, it let people feel once we learn it we would like to learn it forever. There are a few great advantages as we tried so far:

  • It is a object oriented language, so let’s say we wrote a object called bird (called class in python),  then this object can have methods (or functions), variables defined in it, the methods of bird can be “sing”, “eat”, “fly”, etc, and variables could be songs the bird sing, the color of the bird;s feather.For RPI, the GPIO is the object, which has methods “setmode” “setup”, “output”, “input”, “cleanup”, etc. More information can be read on this page.
  • Python has a interactive shell, which let you try the code line by line and see the result, compare this to C language, it won’t never get start to easily before you setup everything and environment. In this way, the prototype of your project can build fast and in a tryout process. So what we should wrote to light up LED on RPI, which can be done line by line:
import RPi.GPIO as GPIO ## Import GPIO library
GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
GPIO.setup(7, GPIO.OUT) ## Setup GPIO Pin 7 to OUT
GPIO.output(7,True) ## Turn on GPIO pin 7

Now Connect a 330R resistor with a LED between RPi GPIO7 to GND, you can see it works.

If you are not yet installed the python.GPIO library, you should do:

  • $sudo apt-get update  # first update all
  • $sudo apt-get install python-dev # install the Python Development toolkit that RPi.GPIO requires
  • $sudo apt-get install python-rpi.gpio # now install the GPIO modules

After we successfully turn on the LED, let’s try to let it blink in a full script, to get this you will need to get the function called sleep to work together, which is similar to arduino delay():

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.setup(7, GPIO.OUT) ## Setup GPIO Pin 7 to OUT

##Define a function named Blink()
def Blink(numTimes,speed):
    for i in range(0,numTimes):## Run loop numTimes
    print "Iteration " + str(i+1)## Print current loop
    GPIO.output(7,True)## Switch on pin 7
    time.sleep(speed)## Wait
    GPIO.output(7,False)## Switch off pin 7
    time.sleep(speed)## Wait
    print "Done" ## When loop is complete, print "Done"

## Ask user for total number of blinks and length of each blink
iterations = input("Enter total number of times to blink: ")
speed = input("Enter length of each blink(seconds): ")

## Start Blink() function. Convert user input from strings to numeric data types and pass to Blink() as parameters

OSs in RPI

To Setup RPi on the first time, you can use Serial to USB cable to connect to it, or connect RPi to ethernet with a net cable. And for the SD card, it’s better to have the Noobs installed, then you can have openELEC (media center OS), Raspbian (most common used OS), RISC OC (fastest OS maybe) in one 8GB card.

  • Python can also works in almost all the systems, not only windows, linux, but also MAC, android,  these common systems.
  • Python is also designed for extendible, apart from its own rich modules, it also provides very rich API and tools that allow coder can write modules in other different language and then used by python. So it is also called a “glue language”?

Finally, this is a rough conclusion from us, hope you like. If you want to read more about python, you can see python wikipedia page or learn from page.

2 Replies to “Blinking LED Python Scripts on Raspberry Pi”

  1. poulbran says:

    Funny how different it is when compared to the raspberry pi

  2. poulbran says:

    ^ I mean Arduino

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.