A microcontroller cannot set a voltage directly, so it switches on and off very fast. The fraction of time on (the duty cycle) becomes the average voltage the motor sees.
See it first
Try it
Average voltage against duty cycle
V_avg = D·V
At 25%
3 V
A straight line in theory; a real driver drops a little voltage.
One step at a time
Step by step
PWM: switching so fast it averages
Step 1 of 5
Set the frequency
Say 20 kHz: one cycle every 50 µs.
In words
An ESP32 pin gives only 0 or 3.3 V. On for 30% of the time, thousands of times a second, and the motor's inertia and coils average it out: like getting 30% of full voltage.
A microcontroller pin can supply almost no current, so a driver (an H-bridge) uses the PWM signal to switch the battery's power instead, and swaps polarity to reverse. A high enough frequency smooths the current and silences the whine.
The formula
V_avg = D × V_supply
Where you meet this in a robot
In the ESP32 workshop you send a speed to the board; the firmware turns it into the PWM duty cycle that drives the motor.