What is a DC motor? Why an MCU cannot drive it directly
Atlasbot · 16/8/2026 · 11 min read
DC motor (DC motor) is the simplest and cheapest type of electric motor - supplying power in one direction means rotating, reversing the polarity means reversing the direction of rotation. This is often the first choice when making a wheel for a mobile robot. But there is a very common misunderstanding among newbies: thinking that you can connect the motor directly to the GPIO pin of the microcontroller like connecting an LED.
Key concepts
Inside a DC motor there are coils and magnets — when current flows through the coil, it creates a magnetic field that interacts with the magnet, creating force that causes the shaft to rotate. To rotate faster, a larger current or voltage is needed; If you want to reverse direction, you need to reverse the direction of the current.
The problem is that an MCU's GPIO pin can only provide a few tens of milliamps (mA), while a small DC motor requires several hundred mA to several amps when starting up or under load — connecting it directly will almost certainly damage the GPIO pin or the entire chip.
H-bridge — H-bridge circuit solves both problems
A motor driver circuit (usually using a H-bridge configuration, including 4 transistors/MOSFETs arranged in an H shape around the motor) stands between the MCU and the motor, solving two things simultaneously:
- Current Amplification — The MCU only needs to send a small current control signal, the H-bridge supplies the actual large current to the motor from a separate source
- Reverse rotation direction — by turning on the crossed pair of transistors in an H shape, the current through the motor reverses without the MCU needing to reverse the power polarity
In short: The MCU never "runs" the motor directly — it only sends control signals (logic level + PWM) to the driver, the driver is what actually supplies the large current to the motor from its own source.
How it works
MCU (control signal, small current)
│ IN1, IN2, PWM
▼
H-Bridge Driver (eg: L298N, TB6612) ◄── Separate motor power (eg: 12V)
│ large, reversible current
▼
DC motor rotates
The rotation speed is controlled by PWM (Pulse Width Modulation) — instead of providing a fixed voltage, the MCU turns on/off the control signal very quickly (thousands of times/second), the ratio of "on" time to total cycle (duty cycle) determines the actual average voltage applied to the motor:
// Duty cycle 75% → speed about 75% of maximum speed
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, 750); // thang 0-1000
HAL_GPIO_WritePin(IN1_Port, IN1_Pin, GPIO_PIN_SET); // Direction of rotation is positive
HAL_GPIO_WritePin(IN2_Port, IN2_Pin, GPIO_PIN_RESET);
Technical details
Formulas
The average voltage applied to the motor is proportional to the duty cycle — the fundamental formula for all speed control using PWM:
V(average) = duty cycle (%) × V(source)
Example: 12V source, duty cycle 75%
V(average) ≈ 0.75 × 12V = 9V
Another important number is starting current/stall current — when the motor is blocked from rotating (eg: the robot crashes into an obstacle), only the (very small) coil resistance remains to block the current, causing the current to spike compared to normal running:
I(stall) = V(source) / R(coil)
For example: 12V source, small motor coil resistance = 6Ω
I(stall) = 12V / 6Ω = 2A (much higher than idle current ~100-200mA)
Parameters
The current of a small DC motor varies greatly depending on the operating state — the driver must choose according to the shaft jam current (worst case), not the normal running current:
| Status | Typical current (small gear reduction motor) |
|---|---|
| No load (free rotation) | ~60 – 200mA |
| Download normally | ~300mA – 1A |
| Stuck | 1.5 – 3A or more |
Popular driver: L298N can withstand a maximum of ~2A/channel (usually it is recommended to use below this level due to heat generation); TB6612 has higher performance, ~1.2A continuous/channel, short-term peak to ~3.2A.
Register / Bit / Byte
Duty cycle PWM on the STM32 is determined by 2 timer registers: ARR (Auto-Reload Register — decides the PWM cycle/frequency) and CCR (Capture/Compare Register — decides the pulse width, which is the "on" part):
Duty cycle (%) = (CCR / ARR) × 100%
VD: ARR = 1000, CCR = 750
Duty cycle = 750 / 1000 × 100% = 75%
The command line __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, 750) in the Principle of Operation section actually only writes the value 750 into the CCR register — the hardware timer automatically generates PWM pulses at the correct rate without needing the CPU to continuously intervene.
Practical applications
The 2-wheel mobile robot (differential drive) uses 2 DC motors, each with a separate H-bridge channel - independently controlling the speed and direction of each wheel, allowing the robot to go straight, turn, or rotate in place just by coordinating the duty cycle of both sides. Many modern drivers also have a current sense pin - monitoring an abnormal sudden increase in current (near the stall current value calculated above) is a way to detect if the robot is stuck/colliding with an obstacle without the need for a separate collision sensor, although the reliability is lower than a dedicated sensor.
Practical example
#include <stdio.h>
int main() {
float v_nguon = 12.0;
float duty[] = {0.25, 0.50, 0.75, 1.00};
for (int i = 0; i < 4; i++) {
float v_tb = duty[i] * v_nguon;
printf("Duty %.0f%% -> V trung binh = %.2fV\n", duty[i]*100, v_tb);
}
return 0;
}
Output:
Duty 25% -> V trung binh = 3.00V
Duty 50% -> V trung binh = 6.00V
Duty 75% -> V trung binh = 9.00V
Duty 100% -> V trung binh = 12.00V
The result increases linearly with duty cycle exactly as the formula stated - this is the basis for reverse calculation: if a motor speed corresponding to a specific voltage range is needed, it is possible to estimate the duty cycle that needs to be set (although in reality it depends on the load, not absolutely linear).
Common mistakes
- Don't consider stall current when choosing a driver - just look at the normal running current (a few hundred mA) and choose a driver close to that threshold, until the robot hits an obstacle the current spikes to a stall current level that can burn the driver - always choose a driver with a rated current in excess of the stall current, not just the normal operating current.
- Set both IN1/IN2 pins high — this is an invalid state for many types of H-bridges (some drivers interpret it as "braking" the motor, others can cause an internal short circuit) — always look up the correct IN1/IN2 status table in the datasheet of the specific driver being used.
- The power supply to the driver is not enough current when the motor starts — the starting current is much higher than the stable running current (near the stall current value in the first moment) which can cause a voltage drop in the entire system if the power is not enough, causing the robot to automatically restart (brownout) at the right time to start moving.
- Forget the protection diode when making your own separate driver circuit — as mentioned in the Diode article, the motor coil creates a reverse voltage pulse when the current is suddenly interrupted, which can destroy the transistor/MOSFET in the H-bridge if there is no protection (most commercially available H-bridge modules have it built-in).
Conclusion
DC motors are simple in principle but require the right H-bridge driver for safe control — understanding the duty cycle/average voltage relationship and always taking into account the shaft jam current when choosing a driver is fundamental knowledge before assembling any robot wheel. The next article in the section is about the Encoder — a sensor that measures the actual speed/position of the motor shaft, a required step to move from "blind" (open-loop) control to control with precise feedback.
Get new articles and lessons by email
When a new technical article or course module is published, Atlas Robotics sends you a short email. No spam.
Unsubscribe any time with the link in each email. See our Privacy policy.