
How to implement a multi class neural network with STM32F103?
Global electronic component supplier AMPHEO PTY LTD: Rich inventory for one-stop shopping. Inquire easily, and receive fast, customized solutions and quotes.
Implementing a multi-class neural network on an STM32F103 (Cortex-M3, 72 MHz, no FPU, limited RAM/Flash) is challenging due to its limited resources, but it is possible for small networks and low-dimensional inputs (e.g., 2D/3D sensor inputs).
Here's how you can do it step-by-step:
1. Design a Small Neural Network on PC
Use Python with TensorFlow/Keras to design and train a model:
2. Convert Model to C (Quantized if Needed)
Options:
-
STM32Cube.AI (official ST tool)
-
Converts Keras/TensorFlow models to C code optimized for STM32
-
Integrates with STM32CubeMX
-
-
uTensor, CMSIS-NN, or TensorFlow Lite for Microcontrollers (TFLM)
-
Lightweight inference engines for ARM Cortex-M cores
-
On STM32F103, use integer quantization (8-bit) to reduce memory and increase speed:
3. Integrate with STM32 Project
A. Using STM32Cube.AI:
-
Install STM32CubeMX + STM32Cube.AI plugin
-
Import
.h5
model -
Enable X-CUBE-AI middleware
-
Generate code
-
Use
aiRun()
to pass input data and get output class
B. Manual (for tiny models):
If doing it manually:
-
Extract weights and biases
-
Implement forward pass in C:
4. Run and Test
-
Flash to STM32 using STM32CubeIDE
-
Send input via UART or use ADC/sensors
-
Print the predicted class
Optimization Tips
-
Use fixed-point arithmetic (Q7, Q15 types)
-
Use CMSIS-DSP for matrix operations
-
Use CMSIS-NN if available — optimized for Cortex-M
-
Keep model size tiny: e.g., 2–1–3 or 2–4–3 architecture
Resources
Related Articles
- ·STM32 four precision control methods for stepper motors
- ·What is the lowest power STM32 MCU? how to choose?
- ·Blue Pill vs Black Pill: What’s the Difference and How to Choose?
- ·Why can STM32 stand out from many 32-bit microcontrollers?
- ·How to distinguish fake chips?
- ·How do I secure an MCU from hacking or tampering?
- ·What are the advantages and disadvantages of using SoCs in embedded systems?
- ·Comparison of ARM vs. RISC-V MCUs
- ·How to achieve serial communication between STM32 and ESP8266?