Sunday, 20 October 2013

Python programming for controlling GPIO pins

Raspberry Pi has 26 GPIO pins, and GPIO is general purpose input/output. So if I can control the GPIO in raspberry pi, I will use raspberry pi control the devices. In this project, I chose Python to control Raspberry Pi.

There is a library can be imported to control the GPIO pins called RPI.GPIO  or another one is wiringPi. In my project, I will use RPI.GPIO. 

For testing the output GPIO pins, first of all I will use LED to test it. If LED is turned on, it means high voltage, otherwise, low voltage. So I wrote a small program for testing the output of pins as follows (save as Led_py.py):

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT)
while True:
           GPIO.output(11,True)
           time.sleep(1)
           GPIO.output(11,False)
           time.sleep(1)

After that, when I run this program, the command is like this:
$ sudo python Led_py.py
Notice: We cannot run this program directly because we are not root, so we must use "sudo" like root.

The results are in the following video.
Video1: How to control Raspberry Pi

The LED will be turned on for 1 second, and then turned off for 1 second, so the program will run all the time expect I stop it.


No comments:

Post a Comment