install python gpio on raspiberry

1 install gpio in python.

sudo apt-get install python-pip

sudo pip install rpi.gpio


GPIO Outputs

1. First set up RPi.GPIO (as described here)

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)

2. To set an output high:

GPIO.output(12, GPIO.HIGH)
 # or
GPIO.output(12, 1)
 # or
GPIO.output(12, True)

3. To set an output low:

GPIO.output(12, GPIO.LOW)
 # or
GPIO.output(12, 0)
 # or
GPIO.output(12, False)

4. Clean up at the end of your program

GPIO.cleanup()

Note that you can read the current state of a channel set up as an output using the input() function. For example to toggle an output:

GPIO.output(12, not GPIO.input(12))



Testing inputs (polling)

You can take a snapshot of an input at a moment in time:

if GPIO.input(channel):
    print('Input was HIGH')
else:
    print('Input was LOW')

To wait for a button press by polling in a loop:

while GPIO.input(channel) == GPIO.LOW:
    time.sleep(0.01)  # wait 10 ms to give CPU chance to do other things

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值