The transistor’s job is to act as a switch; the Arduino doesn’t need to touch the 6V external power supply shown in this circuit, but can just flip the “switch” on pin 5 to affect the motor on the left, or pin 8 to affect the motor on the right.
This circuit uses a ULN2803 transistor, which is actually a single IC (chip) with 8 different transistors in it.
HIGH signal from the Arduino will turn the motor on, and a
LOW signal will turn it off. Notice that the motors are always connected to power (they are plugged right into the 6V supply), so what the transistor does in this case is “turn on and off” their connection to ground. It’s a funny logic, but it works!
The diodes attached parallel to the motors are there for the protection of the transistors. They are necessary because when power is suddenly shut off to a coil (motors have big coils in them), that coil has a tendency to kick with a high voltage for a brief period. The diodes give that kick somewhere to go so it doesn’t damage anything. You don’t need diodes like these if you’re powering something without a coil, such as an LED strip.
Sample code to drive the motors
Sample Arduino sketch to play with switching two loads on and off: