晶片L293D可控制直流馬達及步進馬達,另有已製好的電機擴充板(如上圖)
使用2個L293D晶片,加上74HC595N移位暫存器,直接插上UNO板,
可控制4個直流馬達或2個5線步進馬達,同時控制2個伺服舵機,
並可透過PWM控制直流馬達轉速,馬達電力超過0.5A,務必搭配外部電源使用
使用前需先下載安裝外部程式庫,以下擇一載點,
https://github.com/adafruit/Adafruit-Motor-Shield-library
https://github.com/adafruit/AccelStepper
https://drive.google.com/file/d/0B_hzaKBiDSLgeWxaYWhJdDlIVWM/view?usp=sharing
解壓後將整個目錄放在Arduino目錄libraries之中,原始參考,
https://learn.adafruit.com/adafruit-motor-shield/library-install
預設位置為C:\Program Files\Arduino\libraries\AF_Motor↓
Before you can use the Motor shield, you must install the AF_Motor Arduino library - this will instruct the Arduino how to talk to the Adafruit Motor shield, and it isn't optional!
First, grab the library from github
Uncompress the ZIP file onto your desktop
Rename the uncompressed folder AFMotor
Check that inside AFMotor is AFMotor.cpp and AFMotor.h files. If not, check the steps above
Place the AFMotor folder into your arduinosketchfolder/libraries folder. For Windows, this will probably be something like MY Documents/Arduino/libraries for Mac it will be something likeDocuments/arduino/libraries. If this is the first time you are installing a library, you'll need to create the libraries folder. Make sure to call it libraries exactly, no caps, no other name.
Check that inside the libraries folder there is the AFMotor folder, and inside AFMotor isAFMotor.cpp AFMotor.h and some other files
Quit and restart the IDE. You should now have a submenu called File->Examples->AFMotor->MotorParty
測試馬達(M3)
上傳以下程式後可開啟監控視窗,說明接腳座M3,頻率可設定為64K,8K,2K,1K,
PWM視馬達而定,約150~255控制轉速,讓馬達正轉2秒停2秒,再反轉2秒停2秒,
同時發送訊息到監控視窗↓
#include <AFMotor.h>
AF_DCMotor motor(3, MOTOR12_8KHZ); //接腳座及頻率
void setup() {
Serial.begin(9600);
Serial.println("Motor test!");
motor.setSpeed(200); //可調轉速約150~到255
}
void loop() {
Serial.print("tick---");
motor.run(FORWARD); //正轉
delay(2000);
Serial.println("stop");
motor.run(RELEASE); //停止
delay(2000);
Serial.print("tock---");
motor.run(BACKWARD); //反轉
delay(2000);
Serial.println("stop");
motor.run(RELEASE); //停止
delay(2000);
}
也可開啟範例檔
測試馬達
// Adafruit Motor shield library
// copyright Adafruit Industries LLC, 2009
// this code is public domain, enjoy!
#include <AFMotor.h>
AF_DCMotor motor(3);
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Motor test!");
// turn on motor
motor.setSpeed(200);
motor.run(RELEASE);
}
void loop() {
uint8_t i;
Serial.print("tick");
motor.run(FORWARD);
for (i=0; i<255; i++) {
motor.setSpeed(i);
delay(10);
}
for (i=255; i!=0; i--) {
motor.setSpeed(i);
delay(10);
}
Serial.print("tock");
motor.run(BACKWARD);
for (i=0; i<255; i++) {
motor.setSpeed(i);
delay(10);
}
for (i=255; i!=0; i--) {
motor.setSpeed(i);
delay(10);
}
Serial.print("tech");
motor.run(RELEASE);
delay(1000);
}