To check my wiring, I tried using the jumpers on the driver board to set the modes rather than Arduino pins. The same results were seen. Here is the new setup:
D1 = tied to LOW via jumper
D2 = tied to HIGH via jumper
INV = tied to arduino GND
EN = tied to arduino Vdd
SLEW = tied to arduino GND
FORWARD MOTION:
IN1 = PWM from pin 5 on the Arduino board
IN2 = LOW from pin 4 on the Arduino board
REVERSE MOTION:
IN1 = LOW from pin 5 on the Arduino board
IN2 = PWM from pin 4 on the Arduino board
I have enclosed a simple diagram of my circuit. It uses an Arduino Mega.
The code for 3 simple sketches is listed below:
“PWM_Single” is very simple and commands a 16% percent duty cycle. The “loop” section of the sketch is not used. The motor turns at 84%. An 84% duty cycle command results in a 16% duty cycle at the motors.
PWM_Single2" is the same as above, except I have moved the pwm commands to the “loop” section of the sketch. The results are the same… an 84% duty cycle results from a 16% command.
“PWM_Single3” commands forward at 16% for 1 second, then coast for a second, then reverse at 16% for a second, then coast for a second. In this case, after an initial fast rotation transient, the motors turned at the proper 16%!
The observed behavior is strange in that sometimes the proper commanded duty cycle is sent to the motors and other times, the opposite is sent.
CODE
PWM_single:
/* This sketch commands the motor to drive at a 16% duty cycle.
Running this sketch resulted in the motor driving very fast.
When I change the duty cycle command to 84%, the motor ran slow.
*/
int M1D1 = 22;
int M1D2 = 24;
int EN = 26;
int SLEW = 27;
int M1IN2 = 4; // arduino digital pwm pin
int M1IN1 = 5; // arduino digital pwm pin
int INV = 6;
void
setup()
{
// Serial.begin(9600); // setup serial
pinMode(EN,OUTPUT);
pinMode(M1D1,OUTPUT);
pinMode(M1D2,OUTPUT);
pinMode(SLEW,OUTPUT);
pinMode(INV,OUTPUT);
digitalWrite(M1D1,LOW);
digitalWrite(M1D2,HIGH);
digitalWrite(SLEW, LOW);
digitalWrite(INV, LOW);
digitalWrite(EN, HIGH);
analogWrite(M1IN2,214); // a 16% duty cycle command (40/255) results in fast rotation.
// when I changed the command to analogWrite(M1IN1, 214), the motors run slow.
digitalWrite(M1IN1, LOW); // this side tied to gnd.
}
void loop()
{
}
PWM_Single2:
/* This sketch moved the pwm commands inside the loop of the sketch.
The results were the same.
*/
int M1D1 = 22;
int M1D2 = 24;
int EN = 26;
int SLEW = 27;
int M1IN2 = 4; // arduino digital pwm pin
int M1IN1 = 5; // arduino digital pwm pin
int INV = 6;
void
setup()
{
// Serial.begin(9600); // setup serial
pinMode(EN,OUTPUT);
pinMode(M1D1,OUTPUT);
pinMode(M1D2,OUTPUT);
pinMode(SLEW,OUTPUT);
pinMode(INV,OUTPUT);
digitalWrite(M1D1,LOW);
digitalWrite(M1D2,HIGH);
digitalWrite(SLEW, LOW);
digitalWrite(INV, LOW);
digitalWrite(EN, HIGH);
}
void loop()
{
analogWrite(M1IN2,214); // a 16% duty cycle command (40/255) results in fast rotation.
// when I changed the command to analogWrite(M1IN1, 214), the motors run slow.
digitalWrite(M1IN1, LOW); // this side tied to gnd.
}
PWM_single3:
/* This sketch commands forward 16% for 1 second, then coast 1 sec, then reverse 16%
for 1 second, then coast for 1 sec. In this case, there is an initial fast rotation
transient followed by proper operation... meaning that a 16% command of analogWrite(M1IN1, 40)
results in low speed rotation.
*/
int M1D1 = 22;
int M1D2 = 24;
int EN = 26;
int SLEW = 27;
int M1IN2 = 4; // arduino digital pwm pin
int M1IN1 = 5; // arduino digital pwm pin
int INV = 6;
void
setup()
{
// Serial.begin(9600); // setup serial
pinMode(EN,OUTPUT);
pinMode(M1D1,OUTPUT);
pinMode(M1D2,OUTPUT);
pinMode(SLEW,OUTPUT);
pinMode(INV,OUTPUT);
digitalWrite(M1D1,LOW);
digitalWrite(M1D2,HIGH);
digitalWrite(SLEW, LOW);
digitalWrite(INV, LOW);
digitalWrite(EN, HIGH);
}
void loop()
{
analogWrite(M1IN1,40); // forward 16%
digitalWrite(M1IN2, LOW);
delay(1000);
digitalWrite(M1IN1, LOW); // coast
delay(1000);
analogWrite(M1IN2, 40); // reverse 16%
delay(1000);
digitalWrite(M1IN2, LOW); // coast
delay(1000);
}
Freescale Pinouts.pdf (162 KB)