## PWM驱动舵机 舵机使用频率为50Hz,脉冲宽度cs 1.0 ~ 2.0ms的脉冲作为控制信号。下面是产生基本舵机位置中间时的输出控制脉冲。 from machine import Pin,PWM import time pwm = PWM(Pin(15)) pwm.freq(50) pwm.duty_u16(4915) 舵机具有三个接线: 棕色:GND 红色:+4.5 ~ +6V 黄色:指令脉冲信号 计算Duty_16公式为: duty_u16 = 1.5 * 0x1000/20 对应脉冲宽度与duty_u16之间的关系: 1 3277 1.5 4915 2 6554 from machine import Pin,PWM import time pwm = PWM(Pin(16)) pwm.freq(50) for _ in range(100): pwm.duty_u16(3276) print("Out pulse width : 1ms") time.sleep(1) print("Out pulse with : 2ms.") pwm.duty_u16(6553) time.sleep(1) ### 来自Maker Pi # This code controls one servos to move from 0 degree to 180 degree, # then back to 0 degree, and repeats forever. # --- # Connection: 1x Servo ports at GP12. Take note on the polarity. # --- # Hardware: # 1. Cytron Maker Pi RP2040 (www.cytron.io/p-MAKER-PI-RP2040) # - Any RP2040 boards should work too. # 2. TS90A Micro Servo 3-6V (www.cytron.io/p-analog-micro-servo-9g-3v-6v) # - Any servo motors within the rated voltage of 3.6-6V. # --- from machine import Pin, PWM import time # fine tune the duty cycle values to suit your servo motor MIN_DUTY = 1600 MAX_DUTY = 8400 pwm = PWM(Pin(12)) pwm.freq(50) while True: pwm.duty_u16(MIN_DUTY) time.sleep_ms(1000) pwm.duty_u16(MAX_DUTY) time.sleep_ms(1000) Example2 # This code controls all 4 servos to move from 0 degree to 180 degree, # then back to 0 degree, and repeats forever. # --- # Connection: 4x Servo ports - from GP12 to GP15 # --- # Hardware: # 1. Cytron Maker Pi RP2040 (www.cytron.io/p-MAKER-PI-RP2040) # - Any RP2040 boards should work too. # 2. TS90A Micro Servo 3-6V (www.cytron.io/p-analog-micro-servo-9g-3v-6v) # - Any servo motors within the rated voltage of 3.6-6V. # --- from machine import Pin, PWM import time # fine tune the duty cycle values to suit your servo motors MIN_DUTY = 1600 MAX_DUTY = 8400 servo1 = PWM(Pin(12)) servo1.freq(50) servo2 = PWM(Pin(13)) servo2.freq(50) servo3 = PWM(Pin(14)) servo3.freq(50) servo4 = PWM(Pin(15)) servo4.freq(50) while True: servo1.duty_u16(MIN_DUTY) servo2.duty_u16(MIN_DUTY) servo3.duty_u16(MIN_DUTY) servo4.duty_u16(MIN_DUTY) time.sleep_ms(1000) servo1.duty_u16(MAX_DUTY) servo2.duty_u16(MAX_DUTY) servo3.duty_u16(MAX_DUTY) servo4.duty_u16(MAX_DUTY) time.sleep_ms(1000)