本站改版新增arduino频道
arduino 步进电机
#define IN_1 13 #define IN_2 12 #define IN_3 14 #define IN_4 27 // 设置延时,这个时间不能设置太小,否则电机来不及响应 int delay_time = 5; void setup() { // 设置输出模式 pinMode(IN_1, OUTPUT); pinMode(IN_2, OUTPUT); pinMode(IN_3, OUTPUT); pinMode(IN_4, OUTPUT); // 单四拍模式 for (int i=0;i<256;i++) { digitalWrite(IN_1, 1); digitalWrite(IN_2, 0); digitalWrite(IN_3, 0); digitalWrite(IN_4, 0); delay(delay_time); digitalWrite(IN_1, 0); digitalWrite(IN_2, 1); digitalWrite(IN_3, 0); digitalWrite(IN_4, 0); delay(delay_time); digitalWrite(IN_1, 0); digitalWrite(IN_2, 0); digitalWrite(IN_3, 1); digitalWrite(IN_4, 0); delay(delay_time); digitalWrite(IN_1, 0); digitalWrite(IN_2, 0); digitalWrite(IN_3, 0); digitalWrite(IN_4, 1); delay(delay_time); } delay(1000); // 双四拍模式 for (int i=0;i<256;i++) { digitalWrite(IN_1, 1); digitalWrite(IN_2, 1); digitalWrite(IN_3, 0); digitalWrite(IN_4, 0); delay(delay_time); digitalWrite(IN_1, 0); digitalWrite(IN_2, 1); digitalWrite(IN_3, 1); digitalWrite(IN_4, 0); delay(delay_time); digitalWrite(IN_1, 0); digitalWrite(IN_2, 0); digitalWrite(IN_3, 1); digitalWrite(IN_4, 1); delay(delay_time); digitalWrite(IN_1, 1); digitalWrite(IN_2, 0); digitalWrite(IN_3, 0); digitalWrite(IN_4, 1); delay(delay_time); } delay(1000); // 双四拍模式,顺时针转 180° for (int i=0;i<256;i++) { digitalWrite(IN_1, 1); digitalWrite(IN_2, 1); digitalWrite(IN_3, 0); digitalWrite(IN_4, 0); delay(delay_time); digitalWrite(IN_1, 1); digitalWrite(IN_2, 0); digitalWrite(IN_3, 0); digitalWrite(IN_4, 1); delay(delay_time); digitalWrite(IN_1, 0); digitalWrite(IN_2, 0); digitalWrite(IN_3, 1); digitalWrite(IN_4, 1); delay(delay_time); digitalWrite(IN_1, 0); digitalWrite(IN_2, 1); digitalWrite(IN_3, 1); digitalWrite(IN_4, 0); delay(delay_time); } delay(1000); // 八拍模式 for (int i=0;i<256;i++) { digitalWrite(IN_1, 1); digitalWrite(IN_2, 0); digitalWrite(IN_3, 0); digitalWrite(IN_4, 0); delay(delay_time); digitalWrite(IN_1, 1); digitalWrite(IN_2, 1); digitalWrite(IN_3, 0); digitalWrite(IN_4, 0); delay(delay_time); digitalWrite(IN_1, 0); digitalWrite(IN_2, 1); digitalWrite(IN_3, 0); digitalWrite(IN_4, 0); delay(delay_time); digitalWrite(IN_1, 0); digitalWrite(IN_2, 1); digitalWrite(IN_3, 1); digitalWrite(IN_4, 0); delay(delay_time); digitalWrite(IN_1, 0); digitalWrite(IN_2, 0); digitalWrite(IN_3, 1); digitalWrite(IN_4, 0); delay(delay_time); digitalWrite(IN_1, 0); digitalWrite(IN_2, 0); digitalWrite(IN_3, 1); digitalWrite(IN_4, 1); delay(delay_time); digitalWrite(IN_1, 0); digitalWrite(IN_2, 0); digitalWrite(IN_3, 0); digitalWrite(IN_4, 1); delay(delay_time); digitalWrite(IN_1, 1); digitalWrite(IN_2, 0); digitalWrite(IN_3, 0); digitalWrite(IN_4, 1); delay(delay_time); } // 步进电机运行完后断点,不然会发热 digitalWrite(IN_1, 0); digitalWrite(IN_2, 0); digitalWrite(IN_3, 0); digitalWrite(IN_4, 0); } void loop() { } 第三方库 CheapStepper,代码如下:#include #define IN_1 13 #define IN_2 12 #define IN_3 14 #define IN_4 27 // 定义对象 CheapStepper stepper (IN_1, IN_2, IN_3, IN_4); bool clockwise = true; void setup() { // 设置转速 stepper.setRpm(10); // 通过步数旋转 stepper.moveTo(clockwise, 2048); delay(1000); // 通过角度旋转 stepper.moveDegrees(clockwise, 90); } void loop() { // put your main code here, to run repeatedly: } ------ 著作权归极客侠 GeeksMan所有 基于GPL 3.0协议 原文链接:https://docs.geeksman.com/esp32/Arduino/20.esp32-arduino-step-motor.html
Copyright © 2014 ESP56.com All Rights Reserved
晋ICP备14006235号-22 晋公网安备14108102001165号
执行时间: 0.0096180438995361 seconds