Micropython学习交流群 学习QQ群:786510434 提供多种固件下载和学习交流。

Micropython-扇贝物联 QQ群:31324057 扇贝物联是一个让你与智能设备沟通更方便的物联网云平台

Micropython学习交流群 学习QQ群:468985481 学习交流ESP8266、ESP32、ESP8285、wifi模块开发交流、物联网。

Micropython老哥俩的IT农场分享QQ群:929132891 为喜欢科创制作的小白们分享一些自制的计算机软硬件免费公益课程,由两位多年从事IT研发的中年大叔发起。

Micropython ESP频道

ESP32(MicroPython)串口控制舵机


ESP32(MicroPython)串口控制舵机

主程序 


'''

         橙色(信号线)-->(17)

         红色(电源正)-->(5V)

         褐色(电源负)-->(GND)         

'''

 

#导入Pin模块

from machine import Pin

import time

from servo import Servo

 

#定义SG90舵机控制对象

my_servo = Servo(Pin(17))

 

#程序入口

if __name__=="__main__":

 

    while True:

        a=int(input("舵机角度(0-180)"))

        my_servo.write_angle(a)        

servo.py


from machine import PWM

import math

 

# originally by Radomir Dopieralski http://sheep.art.pl

# from https://bitbucket.org/thesheep/micropython-servo

 

class Servo:

    """

    A simple class for controlling hobby servos.

    Args:

        pin (machine.Pin): The pin where servo is connected. Must support PWM.

        freq (int): The frequency of the signal, in hertz.

        min_us (int): The minimum signal length supported by the servo.

        max_us (int): The maximum signal length supported by the servo.

        angle (int): The angle between the minimum and maximum positions.

    """

    def __init__(self, pin, freq=50, min_us=600, max_us=2400, angle=180):

        self.min_us = min_us

        self.max_us = max_us

        self.us = 0

        self.freq = freq

        self.angle = angle

        self.pwm = PWM(pin, freq=freq, duty=0)

 

    def write_us(self, us):

        """Set the signal to be ``us`` microseconds long. Zero disables it."""

        if us == 0:

            self.pwm.duty(0)

            return

        us = min(self.max_us, max(self.min_us, us))

        duty = us * 1024 * self.freq // 1000000

        self.pwm.duty(duty)

 

    def write_angle(self, degrees=None, radians=None):

        """Move to the specified angle in ``degrees`` or ``radians``."""

        if degrees is None:

            degrees = math.degrees(radians)

        degrees = degrees % 360

        total_range = self.max_us - self.min_us

        us = self.min_us + total_range * degrees // self.angle

        self.write_us(us)

文章知识点与官方知识档案匹配,可进一步学习相关知识

————————————————

版权声明:本文为CSDN博主「路易斯720」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/weixin_74155302/article/details/130998810



推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 ESP56.com All Rights Reserved

执行时间: 0.010506153106689 seconds