步进电机与Arduino的连线参考的http://www.arduino.cn/thread-19050-1-1.html中
给出的连线。
连线示意图如下:
![Arduino使用步进电机 - gc_2299 - gc_2299的博客 Arduino使用步进电机 - gc_2299 - gc_2299的博客](http://img1.ph.126.net/7s4Y15OiXBv96eBDEmfurQ==/6631684591723146364.png)
实际连线图如下:
![Arduino使用步进电机 - gc_2299 - gc_2299的博客 Arduino使用步进电机 - gc_2299 - gc_2299的博客](http://img2.ph.126.net/QCKpVlahg67dIBbFfAJjbA==/6631756059978956096.jpg)
使用Stepper类库中自带的示例,并简单修改了其中的一些参数。
#include<Stepper.h>
const int stepsPerRevolution = 500;
// 初始化步进电机要使用的Arduino的引脚编号
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
void setup()
{
// 设置转速,单位r/min
myStepper.setSpeed(60);
// 初始化串口
Serial.begin(9600);
}
void loop()
{
// 顺时针一次旋转
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(500);
// 逆时针一次旋转
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
delay(500);
}
为了能够明显的看到步进电机在旋转,将一个小车的轮子放在步进电机上。这样就可以很明显的看到车轮随着步进电机的旋转而正转和反转。下面是视频。