ABC of Electronics cosycom.com
ARDUINO 16 · Motors

Controlling a DC Motor with a Transistor

Spin a small DC motor using the same transistor-switch principle from Lesson cb-10, scaled up with a flyback diode for the motor's own coil.

You'll need: a small DC hobby motor, an NPN transistor rated for the motor's current, a flyback diode, a base resistor, a separate motor power supply if the motor draws more than the Uno can spare, and your Uno.

A DC motor spins continuously in one direction as long as current flows through it — no angle command, no internal control circuit, just raw current. Like the LED in Lesson cb-10, it's too much current for an Uno pin to drive directly, so the same transistor-switch pattern applies, with one addition: a motor's winding is an inductor (Lesson 8), so it needs its own flyback diode across it, for exactly the reason Lesson cb-11 gave for the relay coil.

const int motorPin = 6;

void setup() {
  pinMode(motorPin, OUTPUT);
}

void loop() {
  analogWrite(motorPin, 150); // run at partial speed
  delay(3000);
  analogWrite(motorPin, 0);   // stop
  delay(3000);
}

Using analogWrite() (Lesson ard-08) instead of a plain digitalWrite() means the transistor is being switched on and off rapidly rather than held fully on — the motor's own mechanical inertia smooths that rapid switching into a genuinely slower, steady spin, giving you real speed control, not just on/off.

If the motor draws more current than the transistor or the Uno's own power budget can handle, power the motor from a separate supply, sharing only a common ground connection between that supply and the Uno — a detail Lesson ard-19 covers in more depth.

UNO M abcofelectronics
Transistor + motor + diode Same shape as Lesson cb-10's switch, with a flyback diode added across the motor winding just as with the relay coil.
TRY THIS
A DC motor's winding needs a flyback diode across it, exactly like a relay coil in Lesson cb-11 — skipping it risks the same voltage-spike damage to your transistor.