您的位置:首页 > 移动开发

Raspberry Pi GPIO with Python (A WiringPi wrapper :D)

2013-01-05 00:27 573 查看

Raspberry Pi GPIO with Python (without root)

While playing around with the raspberry pi I wanted to try out the GPIO function. I decided I wanted to use python to control the pins. I found several alternatives, one was RPi.GPIO,
but since you need to run the scripts as root I decided to try an alternative: WiringPi-python. WiringPi is a port of the popular Wiring library for the Raspberry Pi.
_______________________________________________________________________________________________
By the way, RPi.GPIO doesn't support more than wiringpi.
You could find it out on http://pypi.python.org/pypi/RPi.GPIO/0.4.1a
As what they've said:

This package provides a class to control the GPIO on a Raspberry Pi.

Note that this module is unsuitable for real-time or timing critical applications. This is becauseyou can not predict when Python will be busy garbage collecting.It also runs under the Linux kernel which is not suitable
for real time applications -it is multitasking O/S and another process may be given priority over the CPU, causing jitter in your program. If you are after true real-time performance and predictability, buy yourself
an Arduino http://www.arduino.cc !

Note that the current release does not support SPI, I2C, PWM or serial functionality on the RPi yet. This is planned for the near future - watch this space!One-wire functionality
is also planned.

I think the author of RPi.GPIO initially picked the wrong way.
A python implantation of WiringPi which
is programmed in C is the right way. The wrapper WiringPi python will inherit all the characters of

WiringPi. So, RPi.GPIO will have to run after WiringPi for the solutions to SPI,I2C,PWM or
others.

Comparing to RPi.GPIO, WiringPi is born to have all of those functions. And it runs very fast!

Thats my own opinion. :D

_______________________________________________________________________________________________

Before Installing WiringPi Python, we need to install Wiring Pi on Raspberry Pi first.

Here is the link./article/2059724.html
or here



Installing WiringPi Python

install WiringPi-python:
[code]sudo apt-get install python-dev  
git clone https://github.com/WiringPi/WiringPi-Python.git  
cd WiringPi-Python  
git submodule update --init  
sudo python setup.py install

Using the library

When everything is installed you need to setup (export) your pins. A scheme of the pins can be found here. In this tutorial I will use the BCM
GPIO numbering (the documentation is sketchy at best on explaining which pin number you should use when).

I will use a LED on the BCM 18 pin just connect the long pin of the led to pin GPIO18 and the short pin to Ground on the following picture EDIT: as a commenter suggested, you should always use a resistor when connecting
an led





To export a pin enter the following command to export your GPIO1 pin (bcm pin 18):
[code]gpio export 18 out

Then you need to set up your pin mode and test it (I don't know if these steps are necessary):
[code]gpio -g mode 18 out   #this one is much important if you want to use wiringPi of PHP, python, Ruby or Peal. If you don't do this, you PHP(or etc. )script will not call module of WiringPi.so which is binary made from WiringPi.
gpio -g write 18 1

The "-g" flag in the command means that the command will use the BCM GPIO pinout.

If your led (connected to GPIO1) is lit, everything is working. Let's create a demo file named blink.py:
[code]vim blink.py

Add the following to it and save:
[code]import wiringpi  
from time import sleep  
io = wiringpi.GPIO(wiringpi.GPIO.WPI_MODE_SYS)  
io.pinMode(18,io.OUTPUT)  # Setup pin 18 (GPIO1)
while True:  
    io.digitalWrite(18,io.HIGH)  # Turn on light
    sleep(2)  
    io.digitalWrite(18,io.LOW)  # Turn off
    sleep(2)

Execute the script:
[code]python blink.py

Your led should blink now, you are now ready to start using WiringPi-python. More documentation on WiringPi-python can be found here

__________________________________
This is my own commit.
Like what people in Raspberry Pi official forum say, million and one thanks are to Gordon who finished this job with excellent work.
Gordon said his friends wrote many wrappers of WiringPi.
On github I find there are many more wrappers , like WiringPi python, WiringPi pel, WiringPi PHP. Those are so great!
WiringPi makes the way of programming on Raspberry like that on Arduino , which is universal.

I complimented him with "Great Job"!

He replied with "Cheers"!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: