-
Notifications
You must be signed in to change notification settings - Fork 22
Using motor driver with Arduino
mktk1117 edited this page Jul 28, 2016
·
1 revision
The motor driver's name is MD03A and its datasheets are available here
First, you have to connect the motors, power, and motor driver to Arduino. Connection would be like this.
Input the PWM signals to PWM pins of motor driver. The driver controls the speed of motors with these pins.
Input the direction signals to IN_A and IN_B pins. The driver decides the directions of each motors.
Then, you have to make an Arduino sketch to control the motor driver.
A simple code would be like below
// setting of pins
int pwmPin0 = 5;
int INaPin0 = 2;
int INbPin0 = 4;
int pwmPin1 = 11;
int INaPin1 = 12;
int INbPin1 = 13;
void setup() {
// set pin mode output
pinMode(pwmPin0, OUTPUT);
pinMode(INaPin0, OUTPUT);
pinMode(INbPin0, OUTPUT);
pinMode(pwmPin1, OUTPUT);
pinMode(INaPin1, OUTPUT);
pinMode(INbPin1, OUTPUT);
}
void loop() {
int pwm_value = 20;
// rotate the motor0 clockwise
// input PWM signal
analogWrite(pwmPin0, pwm_value);
// input direction signals
digitalWrite(INaPin0, HIGH);
digitalWrite(INbPin0, LOW);
// rotate the motor1 clockwise
// input PWM signal
analogWrite(pwmPin1, pwm_value);
// input direction signals
digitalWrite(INaPin1, HIGH);
digitalWrite(INbPin1, LOW);
delay(100);
}
Then, making some functions would be convinient.
void control_motor(int speed, int pwmPin, int INaPin, int INbPin){
if(speed > 0){
analogWrite(pwmPin, speed);
digitalWrite(INaPin, HIGH);
digitalWrite(INbPin, LOW);
}
else if(speed < 0){
analogWrite(pwmPin, -speed);
digitalWrite(INaPin, LOW);
digitalWrite(INbPin, HIGH);
}
else{
digitalWrite(INaPin, LOW);
digitalWrite(INbPin, LOW);
}
}
This can be used like this.
control_motor(10, pwmPin0, INaPin0, INbPin0);
control_motor(-20, pwmPin1, INaPin1, INbPin1);
In the case of 6 wheel car, functions to go straight and turn can be made like below.
void straight(int speed){
control_motor(speed, pwmPin0, INaPin0, INbPin0);
control_motor(speed, pwmPin1, INaPin1, INbPin1);
control_motor(speed, pwmPin2, INaPin2, INbPin2);
control_motor(speed, pwmPin3, INaPin3, INbPin3);
control_motor(speed, pwmPin4, INaPin4, INbPin4);
control_motor(speed, pwmPin5, INaPin5, INbPin5);
}
void turn(int speed, int direction){
if(direction >= 0){
control_motor(speed, pwmPin0, INaPin0, INbPin0);
control_motor(-speed, pwmPin1, INaPin1, INbPin1);
control_motor(speed, pwmPin2, INaPin2, INbPin2);
control_motor(-speed, pwmPin3, INaPin3, INbPin3);
control_motor(speed, pwmPin4, INaPin4, INbPin4);
control_motor(-speed, pwmPin5, INaPin5, INbPin5);
}
else{
control_motor(-speed, pwmPin0, INaPin0, INbPin0);
control_motor(speed, pwmPin1, INaPin1, INbPin1);
control_motor(-speed, pwmPin2, INaPin2, INbPin2);
control_motor(speed, pwmPin3, INaPin3, INbPin3);
control_motor(-speed, pwmPin4, INaPin4, INbPin4);
control_motor(speed, pwmPin5, INaPin5, INbPin5);
}
}