DC motor speed and direction
Serial at 9600 baudTap a part to see what it does.
Tap the parts to see what each one does. A teaching simulation, not a circuit simulator.
Build it step by step
0 of 5 donePlace the driver
TB6612 breakout across the centre gap.
Two supplies, one ground
Battery to VM, Uno 5V to VCC and STBY, and join every ground.
Motor and capacitor
Motor to AO1 and AO2 with a 100 nF capacitor across its terminals.
Knob and button
Pot wiper to A0, button from D2 to GND.
Upload and turn
Turn the knob slowly, then press the button.
How do you build a DC motor speed and direction?
A knob sets the speed of a TT gear motor through a TB6612FNG driver and a button reverses it, with a gentle stop before every change of direction. You need Arduino Uno R3, TB6612FNG Motor Driver, TT gear motor 3 to 6 V, Potentiometer 10 kΩ, Tactile push button 6 mm, Ceramic Capacitor 100 nF and Breadboard 830 Tie Points. It takes 5 steps and about 55 minutes.
- Level
- Intermediate
- Time
- About 55 min
- Parts
- 7
Wiring
| From | To | Why |
|---|---|---|
| 4 × AA pack + | TB6612 VM | Motor supply (about 6 V) |
| Pack − | TB6612 GND and Uno GND | Shared ground |
| Uno 5V | TB6612 VCC and STBY | Logic power, driver enabled |
| Uno D5 / D7 / D8 | PWMA / AIN1 / AIN2 | Speed and direction |
| AO1 / AO2 | Motor terminals | Motor A |
| Pot wiper | Uno A0 (ends to 5V and GND) | Speed knob |
| Button | Uno D2 to GND | Reverse |
Code
No libraries. Pin 5 gives about 980 Hz PWM on the Uno, fine for TT motors.
Runs on a virtual Arduino Uno in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
const int PWMA = 5, AIN1 = 7, AIN2 = 8, BTN = 2;
bool forward = true, last = HIGH;
void drive(int speed) {
digitalWrite(AIN1, forward); digitalWrite(AIN2, !forward);
analogWrite(PWMA, speed);
}
void setup() {
pinMode(PWMA, OUTPUT); pinMode(AIN1, OUTPUT); pinMode(AIN2, OUTPUT);
pinMode(BTN, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
int speed = map(analogRead(A0), 0, 1023, 0, 255);
bool now = digitalRead(BTN);
if (last == HIGH && now == LOW) {
for (int s = speed; s >= 0; s -= 15) { drive(s); delay(20); } // ease to a stop
forward = !forward;
Serial.println(forward ? "Forward" : "Reverse");
}
last = now;
drive(speed);
delay(20);
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Knob at zero | Motor stops completely |
| Knob at full | Motor runs at full speed, driver barely warm |
| Press the button | Motor slows to a stop, then runs the other way |
If it does not work
Nothing moves
STBY must be HIGH and VM must have battery voltage.
Uno resets when the motor starts
Motor powered from the Uno: use the battery pack for VM.
Motor hums at low speed
Normal below about 20 %: the motor needs a minimum push.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the DC motor speed and direction?
Arduino Uno R3, TB6612FNG Motor Driver, TT gear motor 3 to 6 V, Potentiometer 10 kΩ, Tactile push button 6 mm, Ceramic Capacitor 100 nF and Breadboard 830 Tie Points. Ceramic Capacitor 100 nF: Across the motor terminals.
How long does the DC motor speed and direction take to build?
About 55 minutes for a intermediate maker, in 5 steps.
How do I know the DC motor speed and direction works?
Knob at zero: Motor stops completely. Knob at full: Motor runs at full speed, driver barely warm. Press the button: Motor slows to a stop, then runs the other way.
Nothing moves: what should I check?
STBY must be HIGH and VM must have battery voltage.
Uno resets when the motor starts: what should I check?
Motor powered from the Uno: use the battery pack for VM.
Motor hums at low speed: what should I check?
Normal below about 20 %: the motor needs a minimum push.
