====== Controlling a DC Motor ======
This scenario demonstrates how to control a DC motor's rotation direction and speed using Arduino with the Pololu TB6612FNG Dual Motor Driver.
===== Prerequisites =====
* Familiarize yourself with Arduino hardware reference.
* Understand basic PWM concepts.
* Familiarity with the Pololu TB6612FNG motor driver.
===== Hardware Connections =====
| Motor Driver Pin | Arduino Pin |
|------------------|-------------|
| AIN1 | 12 |
| AIN2 | 11 |
| PWMA | 3 (PWM) |
**Motor specifications:**
* Voltage: 12 V DC
* Speed: 5 rpm
* Torque: 20 N·cm
* Shaft Diameter: 4 mm
===== Suggested Knowledge Resources =====
* Arduino programming fundamentals
* [[taltech:arduino:hardware|Arduino Uno Hardware Reference]]
===== Task =====
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.
===== Steps =====
=== Step 1: Define Pins ===
const int AIN1 = 12;
const int AIN2 = 11;
const int PWMA = 3;
=== Step 2: Setup ===
Configure pin modes:
void setup() {
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(PWMA, OUTPUT);
}
=== Step 3: Control Loop ===
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
}
===== Validation =====
Observe the motor rotating in both directions with clear speed differences. Verify pauses between rotations.
===== Troubleshooting =====
If the motor doesn’t rotate or behaves erratically:
* Check power supply (motor needs 12V DC).
* Confirm motor driver connections.
* Verify Arduino pin assignments match the code.