An UNO pin can give about 20 milliamps, enough for an LED. A small motor wants ten times more, and as it stops and starts it sends voltage spikes back into the circuit. Connect a motor straight to a pin and you can damage the board. The answer is a transistor: an electronic switch that a tiny current from the pin can turn on, while a much bigger current flows through it to the motor. In this lesson you use the TIP120 transistor from the kit, protect it with a diode, and control the motor's speed with a knob. This is the same idea that drives the fans in a computer and the motors in toys and drones.
What you'll learn
- Why an UNO pin can't drive a motor on its own
- How an NPN transistor switches a big current with a small one
- Why a motor needs a protection (flyback) diode
- How PWM controls a motor's speed
- Why a motor needs a "kick" to start, and how to give it one in code
What you need
- UNO R3 board and USB cable (UNO R3 Starter Kit)
- 3–6 V DC motor
- TIP120 transistor
- 1N4007 diode
- 1 resistor, 560 Ω
- 1 capacitor, 100 µF
- 10 kΩ potentiometer
- A small strip of tape to make a flag on the motor shaft
- Breadboard and jumper wires (UNO R3 Starter Kit)
Meet the parts
The TIP120 is an NPN transistor with three legs: B (base), C (collector) and E (emitter). Hold it with the printed side facing you and the legs pointing down: from left to right they are B, C, E. A small current into the base lets a big current flow from the collector to the emitter. The TIP120 is really two transistors in one package (a Darlington pair), so a few milliamps into the base can switch more than an amp.
The 1N4007 diode is a small black cylinder with a silver stripe. Current can only flow through it one way: towards the stripe. The stripe end is called the cathode.
The 100 µF capacitor sits across the 5V and GND rails as a small reserve tank. When the motor starts and suddenly pulls a lot of current, the capacitor helps out so the UNO's 5V doesn't dip. Its long leg is +, and the striped side is −.
Wiring

Build it step by step
Plug in all the parts first, then add the wires in this order. In each picture the new wires are bright and the ones from earlier steps are faded. Click a picture to see it larger.





| From | To |
|---|---|
| UNO 5V | Bottom + rail |
| UNO GND | Bottom − rail |
| TIP120, printed side facing you | B in column 30, C in column 31, E in column 32 (row f) |
| UNO pin 9 | Column 26, row j |
| Resistor 560 Ω | Column 26, row h to column 30, row h (the base) |
| Column 32, row j (the emitter) | − rail |
| Diode 1N4007: plain end | Column 31, row h |
| Diode 1N4007: striped end | Column 36, row h |
| Column 36, row j | + rail |
| Motor wire 1 | Column 36, row i |
| Motor wire 2 | Column 31, row i |
| Capacitor 100 µF: long leg (+) | Column 45, row f, with column 45, row j to the + rail |
| Capacitor 100 µF: striped leg (−) | Column 46, row f, with column 46, row j to the − rail |
| Potentiometer | Legs in columns 50, 51 and 52 (row f) |
| Column 50, row j | + rail |
| Column 51, row j (the wiper) | UNO A0 |
| Column 52, row j | − rail |
The diode's stripe must point to the + rail. Put it in the other way round and it connects 5V straight to GND through the transistor as soon as the motor switches on.
Stick a small flag of tape on the motor shaft so you can see how fast it turns. Hold the motor or tape it to the table: it jumps when it starts.
Power: the motor gets its current from the UNO's USB connection. If you can, power the UNO from a USB phone charger or a power bank rather than a laptop. Never hold the shaft still at high speed: a stalled motor pulls much more current. If you stop it with a finger, only do it at low speed and only for a second.
The sketch
const int MOTOR_PIN = 9; // PWM pin to the transistor's base
const int KNOB_PIN = A0;
const int MIN_PWM = 60; // below this the motor only hums, so switch it off
const int KICK_MS = 80; // full power for a moment to get it turning
int lastPwm = 0, shownPwm = -1;
void setup() {
Serial.begin(9600);
pinMode(MOTOR_PIN, OUTPUT);
Serial.println("Turn the knob to set the speed");
}
void loop() {
int pwm = map(analogRead(KNOB_PIN), 0, 1023, 0, 255);
int offBelow = lastPwm == 0 ? MIN_PWM : MIN_PWM - 5; // a small gap stops it flickering on and off
if (pwm < offBelow) pwm = 0; // a dead zone at the bottom of the knob
if (lastPwm == 0 && pwm > 0) { // starting from standstill
analogWrite(MOTOR_PIN, 255); // kick-start
delay(KICK_MS);
}
analogWrite(MOTOR_PIN, pwm);
lastPwm = pwm;
if (abs(pwm - shownPwm) >= 5 || (pwm == 0 && shownPwm != 0)) { // only show real changes
shownPwm = pwm;
Serial.print("PWM ");
Serial.print(pwm);
Serial.print(" = ");
Serial.print(pwm * 100L / 255);
Serial.println(" % power");
}
delay(20);
}
Try it
- Turn the knob all the way down, then upload the sketch and open the Serial Monitor at 9600 baud.
- Turn the knob up slowly. Nothing happens at first: that's the dead zone. Then the motor jumps to life, and the Serial Monitor shows the power in percent.
- Turn the knob higher and lower and watch the flag. The motor speeds up and slows down smoothly.
- Change
KICK_MSto0and upload again. Turn the knob fully down, then slowly up just past the dead zone. Without the kick, the motor may only hum and not start. SetKICK_MSback to80and it starts every time. - After a minute at full speed, touch the TIP120 carefully with one fingertip. It is warm, but not hot: it only has to carry the small motor's current.
What you should see

How it works

The transistor as a switch. When pin 9 is HIGH, a small current flows from the pin through the 560 Ω resistor into the base: (5 V − 1.4 V) ÷ 560 Ω = about 6 mA. That is safe for the pin, and enough to switch the TIP120 fully on, so the motor current flows from the + rail, through the motor, into the collector and out of the emitter to GND. When pin 9 is LOW, no base current flows and the transistor switches off. The resistor matters: without it the pin would push far too much current into the base.
PWM sets the speed. analogWrite() doesn't make a lower voltage. It switches the pin on and off about 490 times a second, and the number from 0 to 255 sets how long each "on" part lasts. At 128 the motor gets full power half of the time. The motor is too heavy to follow such fast changes, so it settles at a speed in between. The faint whine you may hear is that 490 Hz switching.
The diode protects the transistor. A motor is full of coils of wire, and a coil hates sudden changes in current. Every time the transistor switches off, hundreds of times a second, the coil tries to keep the current flowing and makes a voltage spike that can be much higher than 5 V. The diode gives that current a safe loop back through the motor, so the spike never reaches the transistor. That's why it's called a flyback diode. You will find one next to almost every motor, relay and solenoid.
Why the kick? A motor that stands still needs more force to start turning than to keep turning, because of friction and the magnets inside. A low PWM value is enough to keep it going, but not to start it. The sketch gives the motor full power for 80 ms whenever it starts from zero, then drops to the speed you chose.
The TIP120 isn't a perfect switch: about 1 V is lost across it, so the motor gets about 4 V. For bigger motors, engineers often use a MOSFET instead, which loses much less.
Try this next
- Add a push button that switches between "knob" and "breeze" mode, where the speed rises and falls slowly by itself like a gust of wind.
- Use the same transistor circuit to dim a strip of LEDs or switch a small water pump. Anything that needs more current than a pin can give works the same way.
- Compare with the ESP32 web fan lesson: its L293D chip contains transistors and diodes too, and it can also run the motor backwards.
- Use the motor as a fan, and control it with the DHT11: the warmer the room, the faster it spins.
Troubleshooting
-
The UNO restarts when the motor starts: the motor pulls too much current from the USB port. Check the 100 µF capacitor, use a USB charger or a powered USB hub, and try a gentler
KICK_MS. - The motor never turns: check that the TIP120's printed side faces you, with B on the 560 Ω resistor and E on the − rail. Then check that the motor's wires are in columns 31 and 36.
- The motor runs at full speed all the time: a wire connects column 31 straight to the − rail, the base is wired to + instead of through the 560 Ω resistor, or the transistor is damaged. Check the wiring against the table, then try another TIP120.
- The transistor gets very hot: unplug at once. The diode may be the wrong way round: its stripe must face the + rail (column 36).
- The speed jumps around: the potentiometer's wiper (middle leg) must go to A0, and its outer legs to + and −.