3D打印机、雕刻机、云台里的电机能一小步一小步走得准,背后基本是"A4988这颗红板加NEMA17"的组合。它内置译码器,Arduino只给两个信号就能玩转,这篇把引脚、限流、接线到两段例程一次说清。

为什么选它:两根信号线带一台电机
A4988是双极步进电机的细分驱动芯片,内置翻译器,控制端只需要STEP和DIR两根线:DIR决定方向,STEP每来一个脉冲走一步。不用自己写相序表、不用管换相时序,这正是它比老式四线直驱省事的地方。
细分支持五档:整步、半步、四分之一、八分之一和十六分之一步。逻辑电压3到5.5V,每相持续电流1A(加好散热可到2A峰值),电机电源8到35V,过热关断和交叉导通保护都内置。

引脚逐个认
红板两侧共十六个脚,按功能分四组看就清楚了。供电组:VDD和GND接3到5.5V逻辑电(直接取Arduino的5V),VMOT和GND接8到35V电机电源,VMOT旁边必须并一颗至少47微法的电解电容吸收尖峰,这一步省不得。
电机组:1A、1B接一组线圈,2A、2B接另一组。控制组:STEP接3脚、DIR接4脚,这两脚内部没有上下拉,程序里绝不能悬空。Sleep低电平进休眠省电,Reset是悬空脚,不用它就把Sleep和Reset短接拉高,否则芯片一直不工作。
细分组:MS1、MS2、MS3三脚内部下拉,全悬空就是整步模式,按真值表组合拉高可得五档细分。ENABLE高电平关断输出。

上电前的限流调整,最容易被跳过也最致命
接电机之前先调限流电位器。参考电压Vref与限流的关系一般是Vref乘2等于电流值,但不同厂家板子不完全一致。实测方法更可靠:调到0.6V参考电压,按公式算出1.2A;注意整步模式下绕组电流只能到限流的70%,实际约0.84A。
拿万用表串进一相绕组实测,作者那次测出0.5A——公式和实物有偏差是常态,所以别只信公式,上电前测一次Vref、上电后串一次电流表,心里才有底。

例程一:定圈数正反转
整步模式下200脉冲正好转一圈。程序先顺时针转一圈,停一秒,再逆时针转两圈,循环往复。
/* Simple Stepper Motor Control Exaple Code
*
* by Dejan Nedelkovski, www.HowToMechatronics.com
*
*/
// defines pins numbers
const int stepPin = 3;
const int dirPin = 4;
void setup() {
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
}
void loop() {
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < 200; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
delay(1000); // One second delay
digitalWrite(dirPin,LOW); //Changes the rotations direction
// Makes 400 pulses for making two full cycle rotation
for(int x = 0; x < 400; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
delay(1000);
}delayMicroseconds的500微秒同时决定脉冲频率,也就是转速上限,调小这个值电机转得快,小到某个程度就会失步,这是步进系统的物理边界,不是程序错误。
例程二:电位器调速
把A0接一颗电位器,analogRead读出0到1023映射成300到4000微秒的脉冲间隔,旋钮一拧速度即变,这也是桌面机速度旋钮的原型。
/* Simple Stepper Motor Control Exaple Code
*
* by Dejan Nedelkovski, www.HowToMechatronics.com
*
*/
// Defines pins numbers
const int stepPin = 3;
const int dirPin = 4;
int customDelay,customDelayMapped; // Defines variables
void setup() {
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
digitalWrite(dirPin,HIGH); //Enables the motor to move in a particular direction
}
void loop() {
customDelayMapped = speedUp(); // Gets custom delay values from the custom speedUp function
// Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
digitalWrite(stepPin, HIGH);
delayMicroseconds(customDelayMapped);
digitalWrite(stepPin, LOW);
delayMicroseconds(customDelayMapped);
}
// Function for reading the Potentiometer
int speedUp() {
int customDelay = analogRead(A0); // Reads the potentiometer
int newCustom = map(customDelay, 0, 1023, 300,4000); // Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (300 to 4000)
return newCustom;
}几句提醒
一是VDD和VMOT两路电源绝不能混接,接反瞬间烧板;二是装散热片前先贴导热垫,别让金属外壳碰到引脚;三是细分档位越高越安静越平滑,但同样转速需要更高脉冲频率,UNO这种8位板高频delayMicroseconds已经接近极限,跑16细分高速时建议换AccelStepper库或用定时器出脉冲。
把限流调对、电容焊上、Sleep和Reset短接,这三件事做完,A4988基本就是插上就能跑的模块,适合所有需要在正确位置停下的项目。











电位器调速那段map到300到4000,高速端建议再加个软启动不然容易失步。
Vref乘2的公式确实不能全信,我两块板同款电位器调出来差了0.15V,串表实测才踏实。
Sleep和Reset要短接这个点救了我,之前插上电机一动不动找了半天。
47微法电容真不能省,有一次没装插电瞬间驱动板直接冒烟了。