Electronics From Zero Search/

Explainer · Intermediate · about 35 minutes

Motors and H-bridges

A DC motor turns current into movement, but it needs far more current than a microcontroller pin can give, it spins backwards only when its supply is reversed, and it kicks back when switched off. Flip the four switches of an H-bridge, control speed with PWM, and see how a driver chip does it for you.

How a DC motor behaves

Current through the motor’s coils makes a magnetic force that turns it. The more current, the more turning force (torque). As it spins, the motor also works as a generator, making a voltage that opposes the supply: the back-EMF.

  • Speed follows voltage: the motor speeds up until its back-EMF nearly matches the supply.
  • Current follows load: hold the shaft back and it slows, the back-EMF falls, and more current flows.
  • Stalled (not turning), only the coil’s resistance limits the current: the stall current, often ten times the running current.

V = I × R + back-EMF

At start-up the motor is not yet turning, so for a moment it draws its stall current. A small 6 V hobby motor with 3 Ω coils draws 2 A when stalled: a hundred times what an Arduino pin can safely supply (about 20 mA).

To reverse a DC motor, reverse the voltage across it. To control its speed, switch it on and off fast with PWM: it responds to the average voltage.

The H-bridge

Four switches around the motor make an H shape. Close a diagonal pair and current flows one way; close the other pair and it flows the other way. Click the switches, or choose a setting.

H-bridge circuit

In a real bridge the switches are transistors (MOSFETs), each with a diode across it. The diodes give the motor’s current a path when the switches open, so the motor’s kick-back cannot damage them: see the flyback example in the sandbox. Driver chips also add a short delay between turning one switch off and its partner on, so a side is never shorted.

Speed control with PWM

speed at each duty cycle your setting

Average voltage—
Speed—
Motor current—
From the supply—

Model: an illustrative small 6 V hobby motor (3 Ω coils, about 10 900 rpm with no load, 2 A when stalled), PWM fast enough that the motor sees the average voltage, and a steady load. Real motors also need extra current to break free from standstill, so they start at a higher duty cycle than this shows.

Driving a motor from an Arduino

One direction only: a logic-level MOSFET (or a transistor) switches the motor’s ground side, with a flyback diode across the motor, stripe towards +. PWM on the gate controls the speed. See the transistor switch explainer.

Both directions: use an H-bridge driver chip or module (such as the DRV8833, TB6612FNG or the older L298N). It contains the four switches, their diodes and the protection, and takes two logic inputs per motor:

IN1IN2Motor
HIGH (or PWM)LOWForward
LOWHIGH (or PWM)Reverse
LOWLOWCoast (free-wheels to a stop)
HIGHHIGHBrake (stops quickly)

This is the DRV8833-style table; some modules, such as the TB6612FNG and L298N, add an enable or PWM pin. Check your module’s datasheet. The L298N loses about 2 V or more inside, so the motor gets less than the supply.

// Two-input motor driver on pins 9 and 10 (both PWM on an Uno)
const int IN1 = 9, IN2 = 10;

void setup() {
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
}

void loop() {
  analogWrite(IN1, 180); digitalWrite(IN2, LOW);   // forward, about 70%
  delay(2000);
  digitalWrite(IN1, LOW); digitalWrite(IN2, LOW);  // coast
  delay(1000);
  digitalWrite(IN1, LOW); analogWrite(IN2, 180);   // reverse
  delay(2000);
  digitalWrite(IN1, HIGH); digitalWrite(IN2, HIGH); // brake
  delay(1000);
}

Servos and stepper motors

Hobby servos contain a motor, gears, a position sensor and their own driver. You send a pulse about every 20 ms; its width, roughly 1 to 2 ms, sets the angle. On an Arduino the Servo library makes the pulses. Power servos from a separate 5–6 V supply: they draw large current spikes.

Stepper motors move in exact steps (often 200 per turn), so they can go to a position without a sensor: they are used in 3D printers. They need a driver that energises their coils in sequence, such as an A4988 module or, for small 5 V steppers, a ULN2003 board.

Check your understanding