ULN2003步進電機驅動板+ 5V步進電機 步進馬達

ULN2003步進電機驅動板+ 5V步進電機 步進馬達

裏頭講得非常清楚 
整理一下基本:

接線是 
UNL2003  Mini   
Pin 1           D1
Pin 2           D2
Pin 3           D3
Pin 4           D4
Vcc    +5V    另外接電源
Gnd    -----    另外接電源
************************
一相激磁:1000-0100-0010-0001
二相激磁:1100-0110-0011-1001
一/二相激磁:1000-1100-0100-0110-0010-0011-0001-1001
****************************************************
int motorPin_1 = 5;//D1 8; IN1
int motorPin_2 = 4;//D2 9; IN2
int motorPin_3 = 0;//D3 10;IN3
int motorPin_4 = 2;//D4 11;IN4     //定義pin對應名稱
int delayTime = 6;    //設定速度(毫秒)
int Tr_flag = 0;
void setup() {
pinMode(motorPin_4, OUTPUT);    //定義pin輸出
pinMode(motorPin_3, OUTPUT);
pinMode(motorPin_2, OUTPUT);
pinMode(motorPin_1, OUTPUT);
Serial.begin(9600);
}

void loop() {
Serial.println("TR");
Tr_flag = 0;
for (int i = 0; i < 512; i++)
{
 T12();
}
Serial.println("TL");
Tr_flag = 1;
for (int i = 0; i < 512; i++)
{
 T12();
}

Serial.println("Wait");
delay(1000);
}

void St(int p1, int p2, int p3, int p4)
{
if (Tr_flag == 0)
{  //順時針 
digitalWrite(motorPin_1, p1);
digitalWrite(motorPin_2, p2);
digitalWrite(motorPin_3, p3);
digitalWrite(motorPin_4, p4);
}
else{  //逆時針  反送就好
digitalWrite(motorPin_4, p1);
digitalWrite(motorPin_3, p2);
digitalWrite(motorPin_2, p3);
digitalWrite(motorPin_1, p4);
}
}
// 一相激磁:1000-0100-0010-0001
void T1()//這是1相
{
St(HIGH, LOW, LOW, LOW);
delay(delayTime);
St(LOW, HIGH, LOW, LOW);
delay(delayTime);
St(LOW, LOW, HIGH, LOW);
delay(delayTime);
St(LOW, LOW, LOW, HIGH);
delay(delayTime);
}
//二相激磁:1100-0110-0011-1001
void T2()//這是2相
{
St(HIGH, HIGH, LOW, LOW);
delay(delayTime);
St(LOW, HIGH, HIGH, LOW);
delay(delayTime);
St(LOW, LOW, HIGH, HIGH);
delay(delayTime);
St(HIGH, LOW, LOW, HIGH);
delay(delayTime);
}
//一/二相激磁:1000-1100-0100-0110-0010-0011-0001-1001
void T12()//這是1+2相
{
St(HIGH, LOW, LOW, LOW);
delay(delayTime);
St(HIGH, HIGH, LOW, LOW);
delay(delayTime);
St(LOW, HIGH, LOW, LOW);
delay(delayTime);
St(LOW, HIGH, HIGH, LOW);
delay(delayTime);
St(LOW, LOW, HIGH, LOW);
delay(delayTime);
St(LOW, LOW, HIGH, HIGH);
delay(delayTime);
St(LOW, LOW, LOW, HIGH);
delay(delayTime);
St(HIGH, LOW, LOW, HIGH);
delay(delayTime);
}

//************************
這顆有註明是  步进角度:5.625 x 1/64
所以  360 / (5.625 / 64)=4096 步 /8 =512週期
測試結果:
用512 個週期轉完1圈
1相
   順時動不了 逆時OK 可能扭力不夠
2相
   順時逆時OK
1+2相
   順時逆時OK 
所以1或2相 1步=(1+2相)2步
****************************************
正常還是用(1+2相)來驅動比較不會有問題
*********************************************************************
另一種用 Stepper lib
要有 Stepper lib 已經內建好可用
但是注意
接這顆 28BYJ48
設定要  Stepper myStepper(stepsPerRevolution, mPin_1, mPin_3, mPin_2, mPin_4);//2 3 顛倒
而且 用 D1 Mini 不知為啥 驅動不了 或是動得不正常
但是用 Nano 就是 OK
***************************
#include <Stepper.h>
const int stepsPerRevolution = 2048;  //定義步進馬達每圈的步數
//steps:代表馬達轉完一圈需要多少步數。如果馬達上有標示每步的度數,
//將360除以這個角度,就可以得到所需要的步數(例如:360/3.6=100)。(int)
//5 4 0 2
int mPin_1 =8; // 5;//D1 8; IN1
int mPin_2 =9; // 4;//D2 9; IN2
int mPin_3 =10; // 0;//D3 10;IN3
int mPin_4 =11; // 2;//D4 11;IN4     //定義pin對應名稱
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, mPin_1, mPin_3, mPin_2, mPin_4);//2 3 顛倒
void setup() {
  // set the speed at 60 rpm:
  myStepper.setSpeed(5);
  // initialize the serial port:
  Serial.begin(9600);
}
void loop() {
  // step one revolution  in one direction:
  Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
  delay(500);
  // step one revolution in the other direction:
  Serial.println("counterclockwise");
  myStepper.step(-stepsPerRevolution);
  delay(500);
}
***************************************************************










留言

熱門文章