How PWM controls speed
DC motors run on voltage. Lower voltage = slower speed. Rather than waste power through a resistor, Pulse Width Modulation (PWM) switches the motor on and off at high frequency — typically 1–20kHz. The motor's inductance averages the pulses into an effective voltage. 50% duty cycle ≈ half speed.
H-bridge fundamentals
A motor needs to spin in both directions. An H-bridge switches four transistors (or MOSFETs) around the motor — flip which pair is active, and current flows in reverse. The bridge protects your MCU from back-EMF and handles the current the motor draws (which your MCU pins cannot).
Never connect a motor directly to a GPIO pin. Even tiny motors pull 200mA+; MCU pins are typically rated 10–40mA. You will damage the MCU.
Driver comparison: L298N vs Cytron MDD10A
| L298N | Cytron MDD10A | |
|---|---|---|
| Technology | Bipolar transistor | MOSFET (lower Rdson) |
| Voltage range | 5–46V | 6–24V |
| Current per ch | 2A continuous | 10A continuous |
| Efficiency | ~60-70% | ~95% |
| Heat at 1A | Gets warm (heatsink recommended) | Barely warm |
| Logic level | 5V (3.3V works) | 3.3V / 5V |
| Price | ~$7 | ~$30 |
| Best for | RP2350 builds, light loads | RPi5, Jetson, heavy loads |
Rule of thumb: L298N for anything under 1A average. Cytron MDD10A for 12V mobile platforms — the efficiency difference alone extends runtime by 15–20%.
Encoders: why you need them
Without encoders, you're running open-loop — telling the motor to run at 50% PWM and hoping both wheels go the same speed. They won't. Floors aren't flat. Motors aren't matched. Add encoders and you get:
- Closed-loop PID speed control: both wheels turn at the same RPM, straight lines are straight
- Odometry: integrate encoder counts over time to estimate position (dead-reckoning)
- Stall detection: if encoder stops ticking while motor is powered, you're stuck
- Wheel slip detection: encoder velocity diverges from expected — surface or load issue
Size your driver for 3× stall current, minimum. Our N20 motors stall at 0.8A — that's why the L298N (2A) works with margin. The Cytron MDD10A (10A) gives 12× headroom on the same motors. More headroom = cooler driver = longer life.