The Nucleo-F401RE is perfect for a first “real” robot because it has ST-LINK built-in, plenty of timers for PWM + encoders, and Arduino-style headers for quick wiring.
Below is a practical “learn STM32 by building the robot” roadmap, with a solid hardware stack and the exact STM32 peripherals you’ll learn in the right order.
1) Pick a simple robot architecture (do this first)
Start with a 2-wheel differential drive (2 DC gear motors + 1 caster). It’s the easiest to control and debug.
Recommended BOM (robot base)
Core
- Nucleo-F401RE
- 2× DC gear motors (6–12V) + wheels
- 1× motor driver board (dual H-bridge): TB6612FNG / DRV8833 / L298N (pick one)
- Battery: 2S Li-ion/LiPo (7.4V) or 6×AA (simple)
- 5V/3.3V regulation:
- If using 2S battery: buck converter to 5V
- Nucleo can be powered by 5V on VIN/5V pin (board then makes 3.3V)
- Wires + breadboard or perfboard
Sensors (add in phases)
- Wheel encoders (quadrature) highly recommended
- IMU (I2C): e.g., MPU-6050/ICM-series (any common I2C IMU)
- Distance sensor: ultrasonic (HC-SR04) or ToF (I2C)
Debug tools (huge time saver)
- Cheap logic analyzer (for UART/I2C/SPI)
- Multimeter
2) Power rules (most robot bugs are power bugs)
- Never power motors from the Nucleo 5V pin.
- Power path should be:
- Battery → Motor driver (VM)
- Battery → Buck → 5V → Nucleo (5V/VIN)
- Common ground is mandatory: Battery GND = motor driver GND = Nucleo GND
- Add a bulk capacitor near motor driver VM: 220–1000 µF helps with brownouts.
3) Learning path that maps directly to robot features
Phase A — “It moves” (1–2 days)
Goal: Drive both motors open-loop.
You learn:
- GPIO output (direction pins)
- Timer PWM (speed control)
Motor driver signals you’ll typically use:
- AIN1/AIN2 + PWMA
- BIN1/BIN2 + PWMB
- STBY/EN (enable)
CubeMX setup
- Enable 2 PWM channels (e.g., TIM3 CH1/CH2)
- Configure 2–4 GPIOs for direction
- Start PWM and vary duty cycle
Phase B — “It moves straight” (2–5 days)
Goal: Read encoders and do speed control so both wheels match.
You learn:
- Timer Encoder Interface mode (best)
- Interrupt basics (optional)
- PID speed loop
Why encoders matter: without them, the robot drifts and turns randomly because motors aren’t identical.
Phase C — “It goes where you tell it” (1–2 weeks)
Goal: Drive distance/angle commands.
You learn:
- Differential drive kinematics
- Closed-loop control (speed + heading)
- IMU via I2C (optional but great)
Phase D — “It navigates” (ongoing)
Add:
- Obstacle avoidance (distance sensor)
- Line following (IR array)
- Remote control (Bluetooth UART)
4) Wiring example (typical dual H-bridge)
This is generic and works for TB6612/DRV8833-type drivers:
STM32 → Motor Driver
- PWM_A → PWMA
- PWM_B → PWMB
- DIR_A1, DIR_A2 → AIN1, AIN2
- DIR_B1, DIR_B2 → BIN1, BIN2
- EN/STBY → STBY/EN
- GND → GND (must share)
Power
- Battery + → VM (motor supply)
- Battery – → GND
- Buck 5V → Nucleo 5V/VIN
- Buck GND → common GND
Encoders → STM32
- Left encoder A/B → TIMx CH1/CH2 (encoder mode)
- Right encoder A/B → TIMy CH1/CH2
- Use pull-ups if encoder outputs are open collector.
Tip: Nucleo-F401RE exposes many timer pins; choose pins that support PWM outputs and encoder channels, and confirm in the Nucleo pinout.
5) CubeIDE/CubeMX peripheral “shopping list” for this robot
You’ll end up using these a lot:
- TIM PWM: 2 channels (left/right speed)
- TIM Encoder: 2 timers (left/right wheel)
- UART: debug prints + later Bluetooth control
- I2C: IMU / ToF sensor
- ADC: battery voltage monitoring
- (Optional) FreeRTOS: only after you’re comfortable without it
6) A proven firmware structure (simple, robust)
Run a fixed-rate control loop (no RTOS needed at first):
- 1 kHz (every 1 ms): read encoders, compute wheel speed
- 100–200 Hz: PID speed control update
- 20–50 Hz: navigation logic (distance/heading/obstacle)
A clean module split:
- motor.c (set PWM + direction)
- encoder.c (read counts, compute speed)
- control.c (PID)
- robot.c (drive commands: forward/turn)
- comms.c (UART CLI)
7) Your first “robot milestone plan” (no guessing, just build)
- PWM one motor forward/backward
- PWM both motors, manual speed commands over UART
- Read both encoders reliably
- Implement wheel speed PID (robot drives straight)
- Implement “drive X cm” and “turn Y degrees”
- Add sensor-based stopping (distance sensor)
- Add Bluetooth control (UART) or a simple command protocol
8) Quick parts choice advice (so you don’t get stuck)
- If your motors are small (≤1A each typical): TB6612FNG / DRV8833 class drivers are great.
- If motors are bigger: pick a higher-current driver and plan real heat dissipation.
- Get encoders early. They make the project way more predictable.

Top comments (0)