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.
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.
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.
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.
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.
Once you have PWM working, a few refinements make a big difference to how your robot feels.
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.
Leave A Comment