Mengontrol DC Motor Menggunakan Arduino Uno dan Adafruit MotorShield

Arduino is a great starting point for electronics, and with a motor shield it can also be a nice tidy platform for robotics and mechatronics. Here is a design for a full-featured adafruit motor shield that will be able to power many simple to medium-complexity projects.

  • 2 connections for 5V ‘hobby’ servos connected to the Arduino’s high-resolution dedicated timer – no jitter!
  • Up to 4 bi-directional DC motors with individual 8-bit speed selection (so, about 0.5% resolution)
  • Up to 2 stepper motors (unipolar or bipolar) with single coil, double coil, interleaved or micro-stepping.
  • 4 H-Bridges: L293D chipset provides 0.6A per bridge (1.2A peak) with thermal shutdown protection, 4.5V to 25V
  • Pull down resistors keep motors disabled during power-up
  • Big terminal block connectors to easily hook up wires (10-22AWG) and power
  • Arduino reset button brought up top
  • 2-pin terminal block to connect external power, for seperate logic/motor supplies
  • Tested compatible with Mega, Diecimila, & Duemilanove

adafruit Motor Shield

First Step

Install the Arduino Library

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!

  1. First, grab the library from github
  2. Uncompress the ZIP file onto your desktop
  3. Rename the uncompressed folder AFMotor
  4. Check that inside AFMotor is AFMotor.cpp and AFMotor.h files. If not, check the steps above
  5. 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 librariesexactly, no caps, no other name.
  6. Check that inside the libraries folder there is the AFMotor folder, and insideAFMotor isAFMotor.cpp AFMotor.h and some other files
  7. Quit and restart the IDE. You should now have a submenu called File->Examples->AFMotor->MotorParty

 

Second Step

The motor shield can drive up to 4 DC motors bi-directionally. That means they can be driven forwards and backwards. The speed can also be varied at 0.5% increments using the high-quality built in PWM. This means the speed is very smooth and won’t vary!

Note that the H-bridge chip is not meant for driving loads over 0.6A or that peak over 1.2A so this is for small motors. Check the datasheet for information about the motor to verify its OK.

To connect a motor, simply solder two wires to the terminals and then connect them to either the M1, M2, M3, or M4. Then follow these steps in your sketch

  1. Make sure you #include <AFMotor.h>
  2. Create the AF_DCMotor object with AF_DCMotor(motor#, frequency), to setup the motor H-bridge and latches. The constructor takes two arguments.
    The first is which port the motor is connected to, 1, 2, 3 or 4.
    frequency is how fast the speed controlling signal is.
    For motors 1 and 2 you can choose MOTOR12_64KHZ, MOTOR12_8KHZ,MOTOR12_2KHZ, orMOTOR12_1KHZ. A high speed like 64KHz wont be audible but a low speed like 1KHz will use less power. Motors 3 & 4 are only possible to run at 1KHz and will ignore any setting given
  3. Then you can set the speed of the motor using setSpeed(speed) where thespeed ranges from 0 (stopped) to 255 (full speed). You can set the speed whenever you want.
  4. To run the motor, call run(direction) where direction is FORWARD,BACKWARD or RELEASE. Of course, the Arduino doesn’t actually know if the motor is ‘forward’ or ‘backward’, so if you want to change which way it thinks is forward, simply swap the two wires from the motor to the shield.

 

Kode secara lengkapnya adalah seperti ini

#include <AFMotor.h>
 
AF_DCMotor motor(1, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
 
void setup() {
 Serial.begin(9600); // set up Serial library at 9600 bps
 Serial.println("Motor test!");
 
 motor.setSpeed(255); // set the speed to 200/255
}
 
void loop() {
 Serial.print("tick");
 
 motor.run(FORWARD); // turn it on going forward
 delay(3000);
 
 Serial.print("tack");
 motor.run(RELEASE); // stopped
 delay(1000);
 
 Serial.print("tock");
 motor.run(BACKWARD); // the other way
 delay(3000);
 
 Serial.print("tack");
 motor.run(RELEASE); // stopped
 delay(1000);
}
If your step is correct, your motor in channel 1 will be running for 3 second and stop for 1 second. After that the motor will running again for 3 second but in reversed direction  and stop for next 1 second. This condition will be looping as long as you give power to the board and motor.

Source