利用树莓派漏极输出控制电机 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电路

  • 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

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值