ESP32(MicroPython) 步进电机串口控制

本程序实现了通过串口输入步数或角度实现步进电机的精确控制,每次控制结束后可输入下一条指令继续控制。输入正数控制顺时针旋转,负数控制逆时针旋转;角度控制按一圈4096步来计算,如果您的电机参数不同,请在移植时进行调整。

按步数控制:

'''
接线说明:电机模块-->ESP32 IO
         (IN1-IN4)-->(15,2,0,4)
         
         电机模块输出-->28BYJ-48步进电机
         (5V)-->红线
         (O1)-->依次排序                  
'''
#导入Pin模块
from machine import Pin
import time

#定义步进电机控制对象
motor_a=Pin(15,Pin.OUT,Pin.PULL_DOWN)
motor_b=Pin(2,Pin.OUT,Pin.PULL_DOWN)
motor_c=Pin(0,Pin.OUT,Pin.PULL_DOWN)
motor_d=Pin(4,Pin.OUT,Pin.PULL_DOWN)
    
#步进电机发送脉冲函数
def step_motor_send_pulse(step,fx):
    temp=step
    if fx==0:
        temp=7-step
    if temp==0:
        motor_a.value(1)
        motor_b.value(0)
        motor_c.value(0)
        motor_d.value(0)
    elif temp==1:
        motor_a.value(1)
        motor_b.value(1)
        motor_c.value(0)
        motor_d.value(0)
    elif temp==2:
        motor_a.value(0)
        motor_b.value(1)
        motor_c.value(0)
        motor_d.value(0)
    elif temp==3:
        motor_a.value(0)
        motor_b.value(1)
        motor_c.value(1)
        motor_d.value(0)
    elif temp==4:
        motor_a.value(0)
        motor_b.value(0)
        motor_c.value(1)
        motor_d.value(0)
    elif temp==5:
        motor_a.value(0)
        motor_b.value(0)
        motor_c.value(1)
        motor_d.value(1)
    elif temp==6:
        motor_a.value(0)
        motor_b.value(0)
        motor_c.value(0)
        motor_d.value(1)
    elif temp==7:
        motor_a.value(1)
        motor_b.value(0)
        motor_c.value(0)
        motor_d.value(1)
        
while True:
    fx1=0
    step1=0
    s=0  
    i=int(input("输入步数(步数为正时顺时针转,反之逆时针转)"))
    if i<0 :
        fx1=1
        i=-i
    while True:
        step_motor_send_pulse(step1,fx1)
        step1+=1
        s+=1
        if s==i :
            break
        if step1==8:
            step1=0
        time.sleep_ms(1)

按角度控制:

'''
接线说明:电机模块-->ESP32 IO
         (IN1-IN4)-->(15,2,0,4)
         
         电机模块输出-->28BYJ-48步进电机
         (5V)-->红线
         (O1)-->依次排序                  
'''
#导入Pin模块
from machine import Pin
import time

#定义步进电机控制对象
motor_a=Pin(15,Pin.OUT,Pin.PULL_DOWN)
motor_b=Pin(2,Pin.OUT,Pin.PULL_DOWN)
motor_c=Pin(0,Pin.OUT,Pin.PULL_DOWN)
motor_d=Pin(4,Pin.OUT,Pin.PULL_DOWN)
    
#步进电机发送脉冲函数
def step_motor_send_pulse(step,fx):
    temp=step
    if fx==0:
        temp=7-step
    if temp==0:
        motor_a.value(1)
        motor_b.value(0)
        motor_c.value(0)
        motor_d.value(0)
    elif temp==1:
        motor_a.value(1)
        motor_b.value(1)
        motor_c.value(0)
        motor_d.value(0)
    elif temp==2:
        motor_a.value(0)
        motor_b.value(1)
        motor_c.value(0)
        motor_d.value(0)
    elif temp==3:
        motor_a.value(0)
        motor_b.value(1)
        motor_c.value(1)
        motor_d.value(0)
    elif temp==4:
        motor_a.value(0)
        motor_b.value(0)
        motor_c.value(1)
        motor_d.value(0)
    elif temp==5:
        motor_a.value(0)
        motor_b.value(0)
        motor_c.value(1)
        motor_d.value(1)
    elif temp==6:
        motor_a.value(0)
        motor_b.value(0)
        motor_c.value(0)
        motor_d.value(1)
    elif temp==7:
        motor_a.value(1)
        motor_b.value(0)
        motor_c.value(0)
        motor_d.value(1)
        
while True:
    fx1=0
    step1=0
    s=0  
    i=int(input("输入角度(步数为正时顺时针转,反之逆时针转)"))
    if i<0 :
        fx1=1
        i=-i
    i=i/365*4096    
    while True:
        step_motor_send_pulse(step1,fx1)
        step1+=1
        s+=1
        if s>=i :
            break
        if step1==8:
            step1=0
        time.sleep_ms(1)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路易斯720

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值