zookeeper python接口实例详解
380
2022-09-03
树莓派python 控制GPIO(树莓派python安装第三方库)
sudo pip install rpi.gpio
#!/usr/bin/env python# encoding: utf-8import RPi.GPIO as GPIOimport time# 指定GPIO口的选定模式为GPIO引脚编号模式GPIO.setmode(GPIO.BCM)# 指定GPIO14的模式为输出模式,且初始状态为低电平。# 如果上面GPIO口的选定模式指定为主板模式的话,这里就应该指定8号而不是14号。GPIO.setup(14, GPIO.OUT,initial = GPIO.LOW)# 循环10次for i in range(0, 10): # 让GPIO14输出高电平(LED灯亮) GPIO.output(14, True) # 持续一段时间 time.sleep(0.5) # 让GPIO14输出低电平(LED灯灭) GPIO.output(14, False) # 持续一段时间 time.sleep(0.5)GPIO.cleanup()# 清理GPIO口
#!/usr/bin/env python# encoding: utf-8import timeimport RPi.GPIO as GPIOGPIO.cleanup()GPIO.setmode(GPIO.BCM)btn_input = 27;LED_output = 17;# GPIO btn_input set up as input.GPIO.setup(btn_input, GPIO.IN)GPIO.setup(LED_output, GPIO.OUT)# handle the button eventdef buttonEventHandler_rising (pin): # turn LED on print('buttonEventHandler_rising',pin) GPIO.output(LED_output,True) GPIO.add_event_detect(btn_input, GPIO.RISING , callback=buttonEventHandler_rising, bouncetime=200) try: while True : time.sleep(0.1) finally: GPIO.remove_event_detect(btn_input) GPIO.cleanup()
#!/usr/bin/python#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+#|R|a|s|p|b|e|r|r|y|P|i|-|S|p|y|.|c|o|.|u|k|#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+## ultrasonic_1.py# Measure distance using an ultrasonic module## Author : Matt Hawkins# Date : 09/01/2013# Import required Python librariesimport timeimport RPi.GPIO as GPIO# Use BCM GPIO references# instead of physical pin numbersGPIO.setmode(GPIO.BCM)# Define GPIO to use on PiGPIO_TRIGGER = 23GPIO_ECHO = 24print "Ultrasonic Measurement"# Set pins as output and inputGPIO.setup(GPIO_TRIGGER,GPIO.OUT) # TriggerGPIO.setup(GPIO_ECHO,GPIO.IN) # Echowhile 1==1: # Set trigger to False (Low) GPIO.output(GPIO_TRIGGER, False) # Allow module to settle time.sleep(0.5) # Send 10us pulse to trigger GPIO.output(GPIO_TRIGGER, True) time.sleep(0.00001) GPIO.output(GPIO_TRIGGER, False) start = time.time() while GPIO.input(GPIO_ECHO)==0: start = time.time() while GPIO.input(GPIO_ECHO)==1: stop = time.time() # Calculate pulse length elapsed = stop-start # Distance pulse travelled in that time is time # multiplied by the speed of sound (cm/s) distance = elapsed * 34300 # That was the distance there and back so halve the value distance = distance / 2 print "Distance : %.1f" % distance # Reset GPIO settingsGPIO.cleanup()
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~