在本篇文章中,我们将学习如何使用摇杆和Arduino开发板控制步进电机的知识。在这里,我们将使用Arduino UNO开发板和PS2游戏杆控制步进电机的速度和旋转方向。
我们使用带有内部驱动器的单极5V DC步进电机28BYJ-48。因此,不需要任何步进电机驱动。相反,我们将使用ULN2003A达林顿双电机驱动器IC。 PS2游戏杆由两个10k欧姆的电位器组成,即一个用于X轴,另一个用于Y轴。它还包括一个按钮。但是我们将仅使用X轴来控制步进电机的运动。
所需的组件 ● Arduino UNO开发板 ● 28BYJ-48 5V DC步进电机 ● ULN2003芯片 ● PS2游戏杆 ● 5V DC电源
什么是步进电机 
步进电动机(stepper motor)是一种无刷直流电动机,它将完整的全周长分为多个相等的步长。然后可以命令电机的位置移动并保持在其中一个步进,而无需任何位置传感器进行反馈(开环控制器),只要在转矩和速度方面仔细选择适合电动机尺寸的电动机即可。
步进电机28-BYJ48的连接方式 步进电动机28-BYJ48是一个具有5引线线圈布置的单极电动机。必须按特定顺序为四个线圈通电。红线连接到+ 5V电压,其余四根线将被拉到接地以触发相应的线圈。我们使用Arduino开发板以特定顺序为这些线圈通电,并使电动机执行所需的步数。 
步进电机需要借助驱动器芯片才能进行操作。原因是控制器需要较高的电流才能运行,但是仅步进电机将无法从其I / O引脚提供足够的电流来使电机运行。因此使用ULN2003作为驱动模块,电流被放大了。 
连接电路图 要使用摇杆和Arduino控制步进电机,请如下图所示组装电路。
步数计算 在将代码上传到Arduino板上之前需要进行步数计算
在Arduino中,我们将以4步顺序操作电机,因此需要计算步幅角(Stride Angle)。 步幅角= 5.625° Arduino步骤顺序= 4 所需步骤顺序= 8 步数角度= 5.625 * 2 = 11.25 每转的步数= 360 /步角= 360 / 11.25 =每转32步。
源代码/程序: - #include <Stepper.h>
- #define STEPS 32
- // define stepper motor control pins
- #define IN1 7
- #define IN2 6
- #define IN3 5
- #define IN4 4
- // initialize stepper library
- Stepper stepper(STEPS, IN4, IN2, IN3, IN1);
- // joystick pot output is connected to Arduino A0
- #define joystick A0
- void setup()
- {
- }
- void loop()
- {
- // read analog value from the potentiometer
- int val = analogRead(joystick);
- // if the joystic is in the middle ===> stop the motor
- if( (val > 500) && (val < 523) )
- {
- digitalWrite(IN1, LOW);
- digitalWrite(IN2, LOW);
- digitalWrite(IN3, LOW);
- digitalWrite(IN4, LOW);
- }
- else
- {
- // move the motor in the first direction
- while (val >= 523)
- {
- // map the speed between 5 and 500 rpm
- int speed_ = map(val, 523, 1023, 5, 500);
- // set motor speed
- stepper.setSpeed(speed_);
- // move the motor (1 step)
- stepper.step(1);
- val = analogRead(joystick);
- }
- // move the motor in the other direction
- while (val <= 500)
- {
- // map the speed between 5 and 500 rpm
- int speed_ = map(val, 500, 0, 5, 500);
- // set motor speed
- stepper.setSpeed(speed_);
- // move the motor (1 step)
- stepper.step(-1);
- val = analogRead(joystick);
- }
- }
- }
复制代码 |