树莓派-简单传感器

1.Raspberry-Pi-GPIO-Layout-Revision-2
连线:

Name 树莓派Pin
VCC 2
DO 18
GND 6

2.运行下边的程序:

#!/usr/bin/python
#coding: utf-8

import RPi.GPIO as GPIO
import time
import sys

def output():
    x = GPIO.input(GPIO_PORT_DO)
    if x == GPIO.LOW:
        print 'low'
    else:
        print 'high'

if __name__ == '__main__':
    GPIO_PORT_DO = 24

    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(GPIO_PORT_DO,GPIO.IN)

    while True:
        output()
        time.sleep(0.1)

Comments are closed.