This scenario demonstrates how to control a DC motor's rotation direction and speed using Arduino with the Pololu TB6612FNG Dual Motor Driver.
Motor Driver Pin | Arduino Pin |
—————— | ————- |
AIN1 | 12 |
AIN2 | 11 |
PWMA | 3 (PWM) |
Motor specifications:
Implement a program that rotates the DC motor forward at full speed for 5 seconds, pauses for 2 seconds, reverses at half speed for 5 seconds, then pauses again for 2 seconds, repeatedly.
const int AIN1 = 12; const int AIN2 = 11; const int PWMA = 3;
Configure pin modes:
void setup() { pinMode(AIN1, OUTPUT); pinMode(AIN2, OUTPUT); pinMode(PWMA, OUTPUT); }
Implement motor control logic:
void loop() { // Rotate Forward at full speed digitalWrite(AIN1, HIGH); digitalWrite(AIN2, LOW); analogWrite(PWMA, 255); // Full speed delay(5000); // 5 seconds // Stop motor digitalWrite(AIN1, LOW); digitalWrite(AIN2, LOW); delay(2000); // 2 seconds pause // Rotate Reverse at half speed digitalWrite(AIN1, LOW); digitalWrite(AIN2, HIGH); analogWrite(PWMA, 128); // Half speed delay(5000); // 5 seconds // Stop motor again digitalWrite(AIN1, LOW); digitalWrite(AIN2, LOW); delay(2000); // 2 seconds pause }
Observe the motor rotating in both directions with clear speed differences. Verify pauses between rotations.
If the motor doesn’t rotate or behaves erratically: