树莓派声控LED灯

#!/usr/bin/env python
# encoding: utf-8

import RPi.GPIO
import time


#LED灯输出口

LED = 24


#声控器输出口

SENSOR = 16

flag = False

RPi.GPIO.setmode(RPi.GPIO.BCM)

RPi.GPIO.setup(SENSOR, RPi.GPIO.IN, pull_up_down=RPi.GPIO.PUD_UP)

RPi.GPIO.setup(LED, RPi.GPIO.OUT)

try:
        while True:
                if (RPi.GPIO.input(SENSOR) == 0):
                        flag = not flag
                        RPi.GPIO.output(LED, flag)
                        time.sleep(0.5)
except KeyboardInterrupt:
        pass

RPi.GPIO.cleanup();


把声控和LED按上述接口接好,然后就可以声控LED灯了

你可能感兴趣的:(树莓派)