Arduino 下用A4988或TMC2209驅(qū)動(dòng)42步進(jìn)電機(jī)
在DIY黑膠唱機(jī)的過程中,準(zhǔn)備用一個(gè)42步進(jìn)電機(jī)帶動(dòng)唱盤,需要恒定的每分鐘33.33轉(zhuǎn)的轉(zhuǎn)速。
記錄一下折騰的過程。
用洞洞板制作的驅(qū)動(dòng)電路:


驅(qū)動(dòng)板接線圖

先拿價(jià)格便宜很多的A4988做實(shí)驗(yàn)


按照接線圖在面包板上把線接好。
Ardunio代碼如下:
bool PULSE_STATE = true;
// A4988引腳連接Arduino引腳編號(hào)
const int dirPin ? = 2; ? // Direction
const int stepPin ?= 3; ? // Step
const int sleepPin = 4; ? // Sleep
const int resetPin = 5; ? // Reset
const int ms3Pin ? = 6; ? // Ms3
const int ms2Pin ? = 7; ? // Ms2
const int ms1Pin ? = 8; ? // Ms1
const int enPin ? ?= 9; ? // Enable
void setup() {
? // 設(shè)置引腳模式
? pinMode(stepPin,OUTPUT);
? pinMode(dirPin,OUTPUT);
? pinMode(sleepPin,OUTPUT);
? pinMode(resetPin,OUTPUT); ?
? pinMode(ms3Pin,OUTPUT); ?
? pinMode(ms2Pin,OUTPUT); ?
? pinMode(ms1Pin,OUTPUT);
? pinMode(enPin,OUTPUT); ?
?
? // 初始化引腳狀態(tài)
? digitalWrite(sleepPin, HIGH); ?
? digitalWrite(resetPin, HIGH);
? digitalWrite(enPin, LOW);
? digitalWrite(ms1Pin, LOW);
? digitalWrite(ms2Pin, LOW);
? digitalWrite(ms3Pin, LOW); ?
? // 初始化電機(jī)步進(jìn)模式為全步進(jìn)
? //TMC2209 64細(xì)分
? digitalWrite(ms1Pin, LOW);
? digitalWrite(ms2Pin, HIGH);
? //Clockwise 順時(shí)針旋轉(zhuǎn)
? digitalWrite(dirPin, 1);
?
? cli(); ? ? ? ? ? ? ? ? ? ? ?//stop interrupts for till we make the settings
? /*1. First we reset the control register to amke sure we start with everything disabled.*/
? TCCR1A = 0; ? ? ? ? ? ? ? ? // Reset entire TCCR1A to 0
? TCCR1B = 0; ? ? ? ? ? ? ? ? // Reset entire TCCR1B to 0
? TCNT1 ?= 0;
? // turn on CTC mode
? TCCR1B |= (1 << WGM12);
? /*2. We set the prescalar to the desired value by changing the CS10 CS12 and CS12 bits. */ ?
?
? TCCR1B |= B00000001; ? ? ? ?
? /*3. We enable compare match mode on register A*/
?
? TIMSK1 |= (1 << OCIE1A);
? ?
? OCR1A = 1125; ? ? ? ? ? ? //Finally we set compare register A to this value ?
? sei(); ? ? ? ? ? ? ? ? ? ? //Enable back the interrupts
}
void loop() {
? // put your main code here, to run repeatedly:
}
?
ISR(TIMER1_COMPA_vect){
?
? PULSE_STATE = !PULSE_STATE; ? ? ?
? digitalWrite(stepPin,PULSE_STATE); ?
}
代碼主要使用了TIMER1定時(shí)器。需要計(jì)算發(fā)送到電機(jī)的脈沖頻率。用的Arduino NANO 主頻是26MHz, 普通42步進(jìn)電機(jī)的步距角是1.8度,轉(zhuǎn)一圈都要200個(gè)脈沖,A4988芯片最大可使用的細(xì)分是16細(xì)分,如果用16細(xì)分,轉(zhuǎn)一圈都要200*16=3200個(gè)脈沖。我的目標(biāo)是每三分鐘轉(zhuǎn)100轉(zhuǎn),需要的脈沖頻率是100*3200/(3*60)=1777.77Hz
A4988驅(qū)動(dòng)芯片噪音比較大,步進(jìn)電機(jī)運(yùn)轉(zhuǎn)時(shí)震動(dòng)比較大。試驗(yàn)成功之后,換上了高大上的TMC2209芯片,電機(jī)低速運(yùn)轉(zhuǎn)時(shí)超級(jí)安靜,震動(dòng)極小。TMC2209內(nèi)部支持256細(xì)分,計(jì)算脈沖頻率的時(shí)候需要重新計(jì)算一下。上面代碼中使用的是TMC2209芯片的版本。
用洞洞板制作的時(shí)候,用了一片LM7809穩(wěn)壓芯片給NANO主板供電,但當(dāng)使用24伏電壓時(shí),LM7809芯片發(fā)熱比較嚴(yán)重。后續(xù)準(zhǔn)備換一個(gè)DCDC模塊。
使用這個(gè)步進(jìn)電機(jī)的唱機(jī)演示:
