ESP32 Micropython Servo 舵机控制

网上找的都是PYB库,直接导入servo,例如:

http://docs.micropython.org/en/latest/library/pyb.Servo.html#pyb-servo

import pyb
s1 = pyb.Servo(1)   # create a servo object on position X1
s2 = pyb.Servo(2)   # create a servo object on position X2
s1.angle(45)        # move servo 1 to 45 degrees
s2.angle(0)         # move servo 2 to 0 degrees
# move servo1 and servo2 synchronously, taking 1500ms
s1.angle(-60, 1500)
s2.angle(30, 1500)

而我在用ESP32的时候,没有pyb库,只有machine,然后自己找了一圈资料,发现可以直接用PWM控制,如:

https://docs.micropython.org/en/latest/esp8266/tutorial/pwm.html

Hobby servo motors can be controlled using PWM. They require a frequency of 50Hz and then a duty between about 40 and 115, with 77 being the centre value. If you connect a servo to the power and ground pins, and then the signal line to pin 12 (other pins will work just as well), you can control the motor using:

servo = machine.PWM(machine.Pin(12), freq=50)
servo.duty(40)
servo.duty(115)
servo.duty(77)

实际测试,duty use 0-100(%),在频率 f = 50Hz  即T=20mS时,占空比在2-12(%),可以控制0°-180° ,如:

import time
from machine import PWM, Pin
servo = PWM (Pin(23), freq=50, duty=0)
servo.duty(2)
servo.duty(12)

 

  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值