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)

 

你可能感兴趣的:(操作总结文档)