利用树莓派漏极输出控制电机 Using Raspberry Pi open drain output GPIO to control a moter

  • Keyword关键字

Raspberry Pi树莓派, GPIO 通用IO, open drain output漏极输出

  • Motivation动机

项目中有一个pwm控制的电机,需要漏极开路[8]( An open collector is a common type of output found on many integrated circuits (IC), which behaves like a switch that is either connected to ground or disconnected.)作为输入。控制端采用raspberry pi3.

也就是说需要raspberry pi3输出一个漏极开路的PWM信号

然而在raspberry pi3中没有现成的解决方案提供漏极开路输出pwm信号[5]

找到RPi.GPIO.PUD_OFF功能[7](输入高阻,相当于断开),于是我们通过输入高阻,输出低电平和延时自行搭建漏极开路输出pwm信号。

  • Precondition前提

raspberry pi3+(2018-10-09-raspbian-stretch, raspberry pi基本操作超出本文档范围,请参考[1])

RPi.GPIO[2](安装 pip install RPi.GPIO, 关于pip参见[3] )

python3

  • Circuit Diagram电路

利用树莓派漏极输出控制电机 Using Raspberry Pi open drain output GPIO to control a moter_第1张图片

  • Code代码

代码主要参考[4],如果python2.x,请将input()改为raw_input()

import RPi.GPIO as GPIO
import time
pwm_pin =4
GPIO.setmode(GPIO.BCM)

def buzz(pitch, duration, dutyratio):
    period = 1.0 / pitch
    delay = period * dutyratio
    cycles = int(duration * pitch)
    for i in range(cycles):
        GPIO.setup(pwm_pin, GPIO.IN, pull_up_down=GPIO.PUD_OFF)
        time.sleep(delay)        
        GPIO.setup(pwm_pin, GPIO.OUT)
        GPIO.output(pwm_pin, False)
        time.sleep(period-delay)
        
while True:
    pitch_s = input("Enter Pitch (1 to 1000): ")
    pitch = float(pitch_s)
    duration_s = input("Enter Duration (seconds): ")
    duration = float(duration_s)
    dutyratio_s = input("Enter dutyratio (>0,<1): ")
    dutyratio = float(dutyratio_s)    
    buzz(pitch, duration,dutyratio)
  • Reference参考文献
  1. https://projects.raspberrypi.org/en/projects/raspberry-pi-setting-up
  2. https://pypi.org/project/RPi.GPIO/
  3. https://pypi.org/project/pip/
  4. Raspberry.Pi.Cookbook.pdf,Simon Monk,9.3. Make a Buzzing Sound
  5. https://electronicshobbyists.com/raspberry-pi-pwm-tutorial-control-brightness-of-led-and-servo-motor/ 
  6. https://learn.sparkfun.com/tutorials/raspberry-gpio/all
  7. https://www.programcreek.com/python/example/98873/RPi.GPIO.PUD_OFF
  8. https://en.wikipedia.org/wiki/Open_collector

 

 

你可能感兴趣的:(raspberry,pi)