天天看點

樹莓派入門(八)之樹莓派與A4988 驅動42步進電機

  在樹莓派入門(三)裡面我介紹了如何驅動步進電機,當時驅動的步進電機是那種比較簡單精度不是那麼高的電機,今天我們驅動的是比較複雜的步進電機。當然,這裡我還是以arduino為例,但是會給出樹莓派的代碼。

樹莓派入門(八)之樹莓派與A4988 驅動42步進電機

 ​這個畫圖軟體叫(fritzing)隻能進行繪圖,不能進行仿真。

其中MS1 , MS2 , MS3 跳線說明:(例子裡是低電平,懸空或接地線,使用全步進模式)分别是全步進,1/2步進,1/4步進,1/8步進,1/16步進模式。

步進電機走一步是1.8度,一圈就是200步。例如使用1/16步進,則需要走3200步才等于一圈。

上圖中的那三根懸空的就是MS1,MS2,MS3。

樹莓派入門(八)之樹莓派與A4988 驅動42步進電機

下圖是A4988的圖,有的可能是綠色的,但是都無所謂。 

樹莓派入門(八)之樹莓派與A4988 驅動42步進電機

下面我給出arduino的代碼,看起來還是挺簡單的,感興趣的同學可以再去網上查查更多的資料。

int x;
 
 
void setup()
{
  pinMode(6,OUTPUT); // Enable
  pinMode(5,OUTPUT); // Step
  pinMode(4,OUTPUT); // Dir
  digitalWrite(6,LOW); // Set Enable low
}
 
 
void loop()
{
  
  digitalWrite(4,HIGH); // Set Dir high
  
  for(x = 0; x < 200; x++) // Loop 200 times
  {
      digitalWrite(5,HIGH); // Output high
      delayMicroseconds(800); // Wait 1/2 a ms
      digitalWrite(5,LOW); // Output low
      delayMicroseconds(800); // Wait 1/2 a ms
    }
  delay(1000); // pause one second
  
  digitalWrite(4,LOW); // Set Dir low
  
  for(x = 0; x < 200; x++) // Loop 2000 times
  {
      digitalWrite(5,HIGH); // Output high
      delayMicroseconds(800); // Wait 1/2 a ms
      digitalWrite(5,LOW); // Output low
      delayMicroseconds(800); // Wait 1/2 a ms
    }
    delay(1000); // pause one second
}
           

 接下來給出樹莓派的代碼

#include <stdio.h>
#include <wiringPi.h>
​
void init();
void loop();
int main()
{
  init();
  loop();
  
  return 0;
}
 
void init()
{
  wiringPiSetup();
  pinMode(6,OUTPUT); // Enable
  pinMode(5,OUTPUT); // Step
  pinMode(4,OUTPUT); // Dir
  digitalWrite(6,LOW); // Set Enable low
}
 
 
void loop()
{
  
  digitalWrite(4,HIGH); // Set Dir high
  
  for(x = 0; x < 200; x++) // Loop 200 times
  {
      digitalWrite(5,HIGH); // Output high
      delayMicroseconds(800); // Wait 1/2 a ms
      digitalWrite(5,LOW); // Output low
      delayMicroseconds(800); // Wait 1/2 a ms
   }
  delay(1000); // pause one second
  
  digitalWrite(4,LOW); // Set Dir low
  
  for(x = 0; x < 200; x++) // Loop 2000 times
  {
      digitalWrite(5,HIGH); // Output high
      delayMicroseconds(800); // Wait 1/2 a ms
      digitalWrite(5,LOW); // Output low
      delayMicroseconds(800); // Wait 1/2 a ms
   }
    delay(1000); // pause one second
}
           

樹莓派的代碼和arduino的代碼很像吧,确實如此,值得注意的就是,樹莓派的那幾個引腳是我随便寫的,你們實驗的時候要注意看看你們用的是幾号引腳,不要一股腦跟着我寫了。

歡迎關注我們的公衆号,本人知識能力有限,如果文章中有錯誤的地方歡迎向我回報或者留言,十分感謝!

樹莓派入門(八)之樹莓派與A4988 驅動42步進電機