Speed with PWM

Control DC motor speed with pulse width modulation, choose a sensible frequency and measure your motor's dead zone.

By Kevin McAleer,    6 Minutes

Page last updated November 24, 2026


Course Cover Image


By the end of this lesson you will be able to set motor speed with PWM duty cycle, and measure your own motor’s dead zone.

So far the motor has two speeds: off and flat out. To control speed, we cannot lower the battery voltage from code, but we can switch the driver on and off so quickly that the motor feels an average. That trick is pulse width modulation, or PWM, and the Pico has 16 PWM channels ready to go.

Duty cycle

PWM repeats a cycle many times a second. In each cycle the output is high for a fraction of the time. That fraction is the duty cycle.

# One PWM cycle at 1 kHz
freq_hz = 1000
period_ms = 1000 / freq_hz        # 1.0 ms per cycle

duty_percent = 25
on_time_ms = period_ms * duty_percent / 100   # 0.25 ms on, 0.75 ms off

# The motor's average voltage, with a 6 V battery
average_v = 6.0 * duty_percent / 100          # 1.5 V on average
print(on_time_ms, average_v)

The motor’s inertia smooths the pulses out, so 25% duty feels like a slow, gentle push.

MicroPython on the Pico expresses duty as a 16-bit number from 0 to 65535, using duty_u16():

# Convert between percent and the Pico's duty_u16 range
def percent_to_duty(percent):
    return int(percent * 65535 / 100)

print(percent_to_duty(0))     # 0      always off
print(percent_to_duty(25))    # 16383
print(percent_to_duty(50))    # 32767
print(percent_to_duty(100))   # 65535  always on

Choosing a frequency

The frequency is how many cycles per second. Too low and the motor visibly stutters and whines. Too high and the driver spends its time switching instead of delivering power.

Frequency What you notice
Below about 200 Hz Jerky, vibrating motor
500 Hz to 2 kHz Smooth, audible whine on the L298N
10 kHz to 20 kHz Silent to most people, great for the DRV8833

We use 1 kHz for the L298N, which switches slowly, and 10 kHz for the DRV8833, which does not mind.

Finding the dead zone

Here is the surprise: a motor does not move at 1% duty. It needs a minimum push to overcome friction and gearbox drag. Below that, you only hear a whine. This is the dead zone, and every motor and battery combination has its own.

Run this on the Pico with the left motor wired to the L298N. Wheel off the floor, please. Watch carefully, and note when the wheel first begins to turn:

from machine import Pin, PWM
from time import sleep_ms

in1 = Pin(0, Pin.OUT)
in2 = Pin(1, Pin.OUT)
ena = PWM(Pin(2))
ena.freq(1000)

in1.value(1)   # forward
in2.value(0)

try:
    for percent in range(0, 101, 5):
        duty = int(percent * 65535 / 100)
        ena.duty_u16(duty)
        print("duty", percent, "percent ->", duty)
        sleep_ms(700)
finally:
    ena.duty_u16(0)

On a typical TT motor running from 4xAA through an L298N, the wheel starts somewhere between 25% and 45%. Write your number down. We will call it deadzone.

Mapping speed past the dead zone

The pipeline from a speed number to a pin looks like this:

flowchart LR
    S["speed 0 to 100"] --> M["skip the dead zone"]
    M --> P["duty in percent"]
    P --> U["duty_u16, 0 to 65535"]
    U --> E["PWM pin, for example ENA"]

We want a “speed” of 1 to feel like the slowest useful speed, and 100 to be flat out. So we map the speed range onto duty values above the dead zone:

def speed_to_duty(speed, deadzone=30):
    """Map speed 0..100 onto duty_u16, skipping the dead zone."""
    if speed <= 0:
        return 0
    percent = deadzone + (100 - deadzone) * speed / 100
    return int(percent * 65535 / 100)

# With a dead zone of 30 percent:
print(speed_to_duty(0))     # 0      stopped
print(speed_to_duty(1))     # 20119  just above where the motor starts
print(speed_to_duty(50))    # 42597  65 percent duty
print(speed_to_duty(100))   # 65535  full power

Walk through the middle line: speed 50 is half way between the dead zone (30) and the maximum (100), which is 30 + 70 x 0.5 = 65 percent duty. That mapping is what stops Scout from creeping at speed 10 and then suddenly lurching at speed 40.

Try it Yourself

  1. Run the dead zone script and write down the percentage at which your motor starts. Try again with the wheel on the floor. Does the number change?
  2. Change ena.freq(1000) to 100, then 20000, and listen. What happens to the sound?
  3. Challenge: find the dead zone separately in forward and reverse. Are they the same?

Common Issues

  • Problem: A ValueError about the duty value, or the motor behaves oddly at high values.
  • Solution: Keep duty between 0 and 65535. Clamp speeds before you convert them.
  • Why: Pico PWM duty is a 16-bit value, and out-of-range numbers are not accepted.

  • Problem: Two pins in the same PWM slice ignore my second frequency.
  • Solution: Set the same frequency for any pins in a slice, for example GP0 and GP1 together.
  • Why: Each Pico PWM slice has one counter shared by its two output channels, so the last frequency you set wins for both.

  • Problem: The motor whines but will not turn at low speed.
  • Solution: That is the dead zone. Raise the minimum duty with the deadzone value.
  • Why: Friction needs a minimum torque to overcome before the shaft turns.

Check your understanding

Quick quiz 1 of 3

What range of numbers does duty_u16() accept on the Pico?

Quick quiz 2 of 3

What is the dead zone?

Quick quiz 3 of 3

With a dead zone of 30 percent, what duty percent does speed 50 give?

< Previous Next >

Was this lesson helpful?

Course complete

Congratulations, you finished Motors and motor drivers!

That's all 17 lessons done. Let people know, it's a great way to inspire someone else to start.

Share card: I completed Motors and motor drivers on KevsRobots
Post Bluesky LinkedIn Facebook


What are you looking for?
Watch Videos Get Ideas Learn Something Read a Review Read the Blog Search
... Z Z z