zoukankan      html  css  js  c++  java
  • 树莓派python 控制GPIO

    sudo pip install rpi.gpio

    #!/usr/bin/env python
    # encoding: utf-8
    
    import RPi.GPIO as GPIO
    import 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-8
    import time
    import RPi.GPIO as GPIO
    
    GPIO.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 event
    def 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 libraries
    import time
    import RPi.GPIO as GPIO
    
    # Use BCM GPIO references
    # instead of physical pin numbers
    GPIO.setmode(GPIO.BCM)
    
    # Define GPIO to use on Pi
    GPIO_TRIGGER = 23
    GPIO_ECHO    = 24
    
    print "Ultrasonic Measurement"
    
    # Set pins as output and input
    GPIO.setup(GPIO_TRIGGER,GPIO.OUT)  # Trigger
    GPIO.setup(GPIO_ECHO,GPIO.IN)      # Echo
    
    while 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 settings
    GPIO.cleanup()
    

      

  • 相关阅读:
    思考:学习redis的数据结构应该从三个维度来学习?
    思考:一个程序员老说不会碰到或者用到复杂的数据结构或者算法,是这样吗?
    思考:软件系统设计的(前期)权衡?
    思考:一个推荐引擎工程师的能力覆盖
    思考:关于服务架构的取舍:
    模拟斗地主真人在线发牌
    java反射机制
    C-练习题
    java-线程的生命周期
    生产者和消费者模型
  • 原文地址:https://www.cnblogs.com/ahuo/p/10868816.html
Copyright © 2011-2022 走看看