Stepper motor drive experiment
Purpose
Stepper motor rotation
Theoretical study
The red wire of the stepper motor is VCC, and the remaining 4 are 4 phases. For stepping motor phase control, if phase A is selected, the microcontroller will give high level to the BJ1 pin of the drive board, and low level to the other BJ2/BJ3/BJ4 pins.
Schematic diagram
Code writing
//定义引脚
#include<Arduino.h>
#define A1 2
#define B1 1
#define C1 4
#define D1 5
void Phase_A();
void Phase_B();
void Phase_C();
void Phase_D();
void setup() {
// put your setup code here, to run once:
pinMode(A1,OUTPUT);
pinMode(B1,OUTPUT);
pinMode(C1,OUTPUT);
pinMode(D1,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
Phase_A();//设置相位
delay(10);//改变延时可以改变旋转速度
Phase_B();//设置相位
delay(10);//改变延时可以改变旋转速度
Phase_C();//设置相位
delay(10);//改变延时可以改变旋转速度
Phase_D();//设置相位
delay(10);//改变延时可以改变旋转速度
}
void Phase_A(){
//A1引脚高电平
digitalWrite(A1,HIGH);
digitalWrite(B1,LOW);
digitalWrite(C1,LOW);
digitalWrite(D1,LOW);
}
void Phase_B(){
//B1引脚高电平
digitalWrite(A1,LOW);
digitalWrite(B1,HIGH);
digitalWrite(C1,LOW);
digitalWrite(D1,LOW);
}
void Phase_C(){
//C1引脚高电平
digitalWrite(A1,LOW);
digitalWrite(B1,LOW);
digitalWrite(C1,HIGH);
digitalWrite(D1,LOW);
}
void Phase_D(){
//D1引脚高电平
digitalWrite(A1,LOW);
digitalWrite(B1,LOW);
digitalWrite(C1,LOW);
digitalWrite(D1,HIGH);
}