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

你可能感兴趣的:(python)