A compact, modular, and extensible embedded project for building a small robotic car using an STM32 microcontroller. This repository contains firmware, hardware notes, wiring guidance, and build/flash instructions to get the vehicle driving, sensing, and responding to commands.
WhatsApp.Video.2025-10-28.at.15.08.10.mp4
STM32 Car is firmware and documentation intended to run on an STM32 microcontroller connected to motors, sensors. The project focuses on being:
- Easy to flash and test
- Modular so features (remote control) can be toggled/extended
- Portable between different STM32 MCUs with small pin mapping changes
This repository provides a starting point: motor control (PWM + H-bridge), sensor integration (ultrasonic/IR).
- Motor control using PWM (supports two DC motors via an H-bridge like L298N, TB6612, etc.)
- Basic obstacle detection (ultrasonic / IR)
- Command reading from IR Remote using an IR Receiver
- Configurable PID/simple speed control loop
- Modular source layout to add sensors / new behaviors easily
Minimum:
- STM32 development board (e.g., STM32L4xx, STM32F1xx "Blue Pill", STM32F4xx Nucleo, or similar)
- 4x DC motors (with wheels)
- Motor driver (L298N)
- Power source for motors (battery pack appropriate for your motors)
- 1x Ultrasonic distance sensor (HC-SR04) or IR distance sensor (optional)
- 1x IR Remote + 1x IR Receiver
- Wires, protoboard and 16x mounting screws (once I have time I will share them in detail here)
Always share grounds between motor power and MCU power
- /Core or /Src: main application, HAL or LL drivers, peripheral initialization
- /Inc: header files for pin mappings and configuration constants
- /Drivers: motor control layer, sensor drivers (HC-SR04, IR line)
- /App: high-level behaviors (manual control, line follow, obstacle avoidance)
- Makefile / .project: project build files (if present)
The firmware uses an application loop:
- Read sensors (distance)
- Run control logic (obstacle check)
- Update motors (PWM & direction)
- Repeat
You can build this project using several toolchains. Pick one you are comfortable with.
Suggested:
-
Visual Studio Code (make sure you have the extension STM32 for vscode already installed) OR
-
STM32CubeIDE (import existing project or create a new one and copy source)
- STM32CubeMX may help generate clock/peripheral initialization
-
Alternatively:
- arm-none-eabi-gcc toolchain
- CMake
- PlatformIO (with the appropriate board/platform)
-
Flashing tools:
- ST-LINK (STLink Utility, st-flash from OpenOCD/stlink)
- OpenOCD + GDB
- DFU (if using a DFU-capable bootloader)
Useful software:
- Python (optional for scripts)
- minicom / picocom / Tera Term / PuTTY (for serial console)
If using VScode:
- Make sure
STM32CubeIde for Visual Studio CodeandCMake Toolsis installed on your IDE - optional: select your target (usually target is selected automatically)
- ctrl + shift + p and then enter
Cmake: Build
If you use STM32CubeIDE:
- Open STM32CubeIDE.
- Import -> Existing Projects into Workspace -> select this repo folder.
- Build the project (Project > Build Project).
Note: Replace MCU/clock configuration if your board differs. The project leaves pin mappings in a header file for easy editing.
- Make sure
STM32CubeIde for Visual Studio Codeis installed in your VScode - Once installed you can start debugging using STLink (through VScode).
- On power-up, firmware initializes peripherals and generates a small buzzer sound sequence (in main_task task in
App/tasks.hpp). - Implemented Modes so far:
- Manual control: receive simple commands via IRRemote (all mapped accordingly in
Drivers/drive_sys.cpp
- Manual control: receive simple commands via IRRemote (all mapped accordingly in
- Pin mapping: edit
.mxprojectfile to set MCU pins used by motors, sensors, and UART through stm32cubemx. - Tuning:
- PID values and speed limits are in
App/drive_sys.hppor similar. - Adjust sensor thresholds for distance and line reading in
Drivers/HCSR04.hpp.
- PID values and speed limits are in
- Motor does not spin:
- Verify motor power and motor driver supply.
- Confirm PWM pins are configured and timers enabled AND CLOCK FREQUENCY IS 80MHz.
- Sensors give no reading:
- Confirm correct wiring and logic levels.
- Use an oscilloscope or logic analyzer on the TRIG/ECHO lines for HC-SR04.
- Firmware does not flash:
- Check ST-LINK connection and that the target MCU isn't held in reset.
- Ensure correct flash address and file type (ELF vs BIN).
Contributions are welcome. Please:
- Open issues describing bugs or feature requests.
- Make small, focused pull requests.
- Follow the existing code style and document any hardware changes in the README or a new wiring diagram file.
This repository is released under the MIT License. See LICENSE file for details. (If you prefer a different license, replace it and update the license header.)