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

本文介绍了如何使用Python控制Raspberry Pi的GPIO接口,并对比了RPi.GPIO与WiringPi-python两种方法。作者推荐使用WiringPi-python,因为它无需root权限且支持更多功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

or   here


Installing WiringPi Python

 install WiringPi-python:

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 
Raspberry pi pinout


To export a pin enter the following command to export your GPIO1 pin (bcm pin 18):

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):

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:

vim blink.py

Add the following to it and save:

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:

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"!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值