Part

DC motor

Needs a transistor. The shaft shows how fast it is going and which way.

See every part
  • 2 pins
  • 6 settings
  • In 26 projects
  • In 7 lessons
Drawn live by the editor's own code, at the size you see it.
At a glance

The DC motor in brief

What it is, and every setting the properties panel gives it.

A small brushed DC motor, the yellow gearbox kind, has two terminals and nothing else: put a voltage across them and it turns, turn the voltage round and it turns the other way. There is no driver inside the part, because a board pin cannot run a motor. A pin gives about twenty milliamps, and the default motor stalled is 6 ohms of winding that wants most of an amp, which is why the circuit around it, a transistor or an H bridge, is the real lesson. Mokxi models the winding in series with a back EMF that rises with speed, so a spinning motor draws far less than a stalled one, and the speed follows the voltage with a lag. Friction is modeled too: from rest the default motor does not start until 1.35 V is across it, the dead band every robot sketch’s minimum speed is for. The winding has no inductance, so leaving out the flyback diode does no harm here, though it would on a bench.

Family
Outputs
Pins
2
model
custom (or TT 1:48, TT 1:90, N20 100:1, N20 50:1 LP, FA-130, 130 6V)
voltage
6
rated voltage, 6 by default.
rpm
200
rated speed at that voltage, 200.
resistance
6
winding resistance, 6 ohms, what a small gearbox motor measures across its terminals when still.
inertia
120
the mechanical lag, in milliseconds, 120 for a gearbox motor with a wheel on it.
back_emf
0.85
the fraction of rated voltage the motor generates at full speed, 0.85.

A circuit built around it, running and explained: the DC motor simulator.

Pins

2 pins

Named as the editor names them, so a wire lands where you expect it.

Pin
What it does
1
One terminal.
2
The other terminal.
How to use it

How to drive a DC motor from an Arduino

A pin cannot run a motor: it gives about 20 mA, and a small gearbox motor wants most of an amp to start. So pin 9 drives a 2N2222 through a 1 k resistor, the transistor’s collector pulls the motor’s low side to ground, and the motor’s other terminal goes to 5 V. analogWrite(9, 0 to 255) sets the speed, and at 100 out of 255 the shaft still will not start from rest.

Put a 1N4007 across the motor, band toward 5 V. On the bench it catches the kick the coil gives when the transistor turns off. The motor here has no inductance, so the diode is in the circuit for the bench rather than the simulation. To run the motor both ways, use an H bridge such as the L293D or the L298N.

Part pin
Board pin
Transistor base, through 1 k
Pin 9
Transistor emitter
GND
Motor terminal 1
5 V
Motor terminal 2
Transistor collector
1N4007, band toward 5 V
Across the motor

On a 3.3 V board such as an ESP32 or a Pico, use a smaller base resistor, about 470 ohms, which gives the base at least the current 1 k gives it from 5 V.

Arduino Uno: motor on a transistorlive0.000 s 0.00x
Click to open it in the editor
This is the simulator itself, running here. Click anything to open it in the editor.
Set the speed with PWM
const int DRIVE = 9;   // a PWM pin, into the base through 1 k

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

void loop() {
  analogWrite(DRIVE, 255);   // full speed
  delay(2000);
  analogWrite(DRIVE, 200);   // slower
  delay(2000);
  analogWrite(DRIVE, 0);     // off: it coasts to a stop
  delay(2000);
}
How it is modeled

What is true about the DC motor, here

In its help article’s own words: what the simulation gets right, and where it stops.

Modeled

The standard brushed-motor model: the winding resistance in series with a back EMF proportional to speed, so a motor already up to speed draws far less current than a stalled one, and the speed chases the applied voltage with a first-order lag set by inertia. The part presents itself to the circuit exactly as it loads a driver (stalled it is 6 ohms pulling most of an amp off a 6 V supply, spinning it pulls a tenth of that), so a transistor stage sees the real difference between starting and running current.

Not modeled

No mechanical load on the shaft, no inductive kick when current stops (which is what a flyback diode across a real motor is for, and which this part will happily let you leave out with no consequence), no brush noise.

The winding has no inductance, which is why there is no kick to clamp: the motor is a resistance in series with a back EMF and nothing else. A real gearbox motor's winding is a few millihenries, and that is the whole reason a flyback diode exists, so a circuit that would destroy a transistor on the bench runs cleanly here.

The mechanics are one lag and a friction. inertia is a single time constant in milliseconds. The friction is the free-running current ((1 - back_emf) of the rated voltage over the winding, 150 mA for the defaults), taken as a constant torque against the motion, so half the rated voltage gives rather less than half the speed (41% for the defaults), and from rest the motor does not start until the stalled current is one and a half times that: 1.35 V across the default motor. On a 5 V PWM through an H bridge, analogWrite below about 70 hums and does not turn, which is the dead band every robot sketch's minimum speed is there for. Let go of a terminal and it coasts on friction alone, most of a second from full speed; short the two together (an H bridge's brake) and the back EMF stops it in a fraction of that. The friction figure and the 1.5 are assumptions, not datasheet numbers. There is no cogging, no gear backlash, no load and no stall current limit other than the winding's own resistance. The five properties are round numbers for "a small yellow gearbox motor", not a measurement of any one part.

From DC motor, in full.

Projects

See the DC motor in a project

26 built-in projects use it. Each opens in the editor, already wired.

Boards

The boards it is shown on

Each of these 2 boards has a built-in project with the DC motor in it.

Libraries

Arduino libraries for the DC motor

Built in, so the sketch compiles with nothing to install. Each page has the calls, the wiring and an example to run.

Wire up the DC motor

Open the editor and push it into the breadboard. It is free, and it runs on your own machine.