Robil
Meta to cut 10,000 jobs in second...
Elevating Your Office Attire...
Beauty and Wellness Secrets...
31°C, New York
Robil

Weekly Updates

Let's join our newsletter!

Do not worry we don't spam!

Robil

Controlling Wheel Speed with Pulse Width Modulation

Why simple on/off switching won't do

When you first connect a motor to a battery, it does one thing: it spins at full speed. For a robot that only needs to go forwards at one pace, that might be fine. But as soon as you want gentle turns, slow approaches to a wall, or smooth acceleration, you hit a problem. A simple switch gives you 0% or 100% power, nothing in between. You could add a resistor to drop the voltage, but that wastes energy as heat and gives you a fixed speed that changes as the battery drains. What you really want is a way to dial the power up and down electronically. That is where pulse width modulation, usually shortened to PWM, comes in.

What PWM actually does

PWM is a technique for switching a motor's power supply on and off very quickly. Instead of a steady voltage, you send a square wave. During the "on" time, the motor receives full battery voltage. During the "off" time, it receives nothing. If you repeat this fast enough, the motor's own inertia and inductance smooth out the pulses. The motor behaves as if it were receiving a lower, constant voltage.

The key number is the duty cycle – the percentage of each cycle that the signal is on. A 25% duty cycle means the motor is powered for a quarter of the time. A 75% duty cycle means three-quarters. The average voltage the motor sees is simply the battery voltage multiplied by the duty cycle. So with a 6 V battery pack, a 50% duty cycle gives an average of 3 V. That average voltage determines the motor's speed under light loads.

Choosing a sensible PWM frequency

The frequency – how many on/off cycles happen per second – matters more than you might think. If you switch too slowly, you will hear a buzzing or whining sound from the motor windings, and the robot will move in visible jerks. If you switch too quickly, the motor driver electronics may waste energy as heat during each transition, and radio interference can become a nuisance.

For small hobby motors in home robots, a frequency between 1 kHz and 20 kHz is a good starting range. Many beginners settle around 5 kHz to 8 kHz because it is above the most annoying part of the audible range for many adults, yet still gentle on cheap driver chips. If you can hear a high-pitched whistle from your robot, try raising the frequency. If the driver gets hot, try lowering it. There is no single perfect number, but anything in that range will work for most wheeled robots.

How to generate PWM in your robot

You have two main options: use a microcontroller's built-in PWM hardware, or buy a motor driver module that accepts a simple direction and speed signal. Most beginner-friendly microcontrollers have several PWM-capable pins. You configure a timer to produce a square wave, then change the duty cycle in your code.

  • Direct pin drive – only suitable for very small motors that draw less than about 20 mA. Never connect a motor directly to a microcontroller pin; you will damage it.
  • Motor driver module – the usual choice. It takes your low-power PWM signal and uses it to switch a higher current to the motor. Look for one that supports the voltage and current of your motors.
  • H-bridge – the circuit inside most driver modules that lets you reverse the motor by swapping which side is switched. PWM then controls speed in either direction.

In your code, you will typically set a pin as an output, start a PWM signal at your chosen frequency, and then write a value from 0 to 255 (or 0 to 100%) to set the duty cycle. Many microcontroller environments have a simple function or library call for this. Check the documentation for your board to find which pins support hardware PWM – software PWM can jitter and cause uneven motion.

Practical tips for smooth wheel control

Once you have PWM working, a few refinements make a big difference to how your robot feels.

  • Start with a dead band. Most motors will not turn at all below a certain duty cycle – often 10% to 20%. Find the lowest value that reliably starts your wheel from rest, and never command below it when you want movement.
  • Accelerate gradually. Instead of jumping from 0% to 60%, step the duty cycle up over a few hundred milliseconds. This reduces wheel slip, protects your gearbox, and looks much more polished.
  • Compensate for battery sag. As batteries discharge, the same duty cycle gives less speed. If your robot needs consistent speed, measure the battery voltage and scale your PWM value accordingly.
  • Mind the stall current. If a wheel jams, the motor will draw a huge current. Your driver and battery must be able to handle it, or you risk overheating. A fuse or current-limiting driver is cheap insurance.

PWM is one of those ideas that seems abstract until you try it. Once you have a wheel that creeps forwards slowly, then accelerates smoothly, you will wonder how you ever managed with simple switches. It is the foundation for almost every clever movement your home robot will ever make.

author
Emily Clarke

Robil shares practical, down-to-earth guidance on home robotics and automation for uk beginners for readers across the UK.

Leave A Comment
Popular Guides