How to deploy your AI model on mainstream STM32 ¶
This tutorial details the steps to generate an AI application running on a mainstream STM32, embedding an internal flash memory and without AI accelerator. For this tutorial, choice is made to use a STM32U5 microcontroller, in which AI inference is performed in software on its Cortex-M33 core. This tutorial uses the B-U585I-IOT02A as the reference board.
Objective ¶
Generate a working AI inference application for the B-U585I-IOT02A by converting your AI model into C-code with STM32Cube AI Studio, configuring board peripherals and generating firmware project in STM32CubeMX, integrating model files and flashing the board.
Important
For a fast model evaluation on your board, STM32Cube AI Studio can directly generate a complete firmware project, ready to build and flash. Steps described in Parts 2 and 3 of this tutorial aim to detail required project configurations to implement in your own application.
Summary ¶
Part 1 — Generate AI model C-code with STM32Cube AI Studio
Getting the model
Create a project in STM32Cube AI Studio
Generate AI model code in STM32Cube AI Studio
Part 2 — Create the base firmware project with STM32CubeMX
Create a project from the B-U585I-IOT02A board selector
Configure Connectivity: USART1
Configure Clock
Configure Project Manager: appli structure, STM32CubeIDE
Generate code and open the firmware project
Part 3 — Deploy AI model in firmware project with STM32CubeIDE
Copy AI files to the firmware project
Configure the project settings: include paths, link library, source location
Edit
main.c: add AI init and process callsFlash and run
0. Prerequisites ¶
Component
Minimum version
Purpose
6.17.0
Project configuration and code generation
1.8.0
STM32U5 MCU package (HAL drivers)
2.0.0
IDE and compiler
1.2.0
AI model validation and C code generation
4.0.0
AI model validation and C code generation (core component)
You also need a B-U585I-IOT02A board with a micro-USB cable.
Part 1 — Generate AI model C-code with STM32Cube AI Studio ¶
1. Getting the model ¶
For this tutorial, we will download an Arc Fault Detection model in
.tfliteformat from the STM32 Model Zoo. The STM32 Model Zoo is an open-source repository of pre-trained, STM32-optimized AI models for various applications including predictive maintenance, audio, and more. Each model comes with training scripts and configuration files.The chosen model is a 1D convolutional neural network that detects arc faults in electrical installations from time-series current samples, post-training quantized to INT8 for efficient deployment on STM32.
Download it from the STM32 Model Zoo: st_conv_time_1channel_512_int8.tflite
2. Create a project in STM32Cube AI Studio ¶
At this step, we will create an STM32Cube AI Studio project to convert our
.tflitemodel to C code.STM32Cube AI Studio converts your model (
.tfliteor.onnx) into C code optimized for STM32 microcontrollers. AI model inferences run entirely in software on the Cortex-M33 core. The Application variant produces not only the raw network files but also the middlewares and the application source code to run the model.
Open STM32Cube AI Studio.
Create a new project clicking + Project on the top right.
Select From Scratch project creation option.
Set a name for your project (
Test_CubeAIStudio_ArcFault_U5in this tutorial). Click Next.Set the target development board: B-U585I-IOT02A. Click Next.
Select the toolchain you will use for building your AI project. For this tutorial, select STM32CubeIDE. Click Create Project. Your STM32Cube AI project is now created:
![]()
3. Generate AI model code in STM32Cube AI Studio ¶
On the right bar in Model section, click Model file field to select your model (
st_conv_time_1channel_512_int8.tflite).You will now validate your model to check its compatibility for C-code conversion and memory size. For this, click Run:
![]()
When the validation is successful, click the gear icon on the top right to open Board settings. Check the default settings. The UART interface will be used to set the communication between the board and the host PC. Then, click Save.
UART interface:
USART1Baudrate:
115200RX pin:
PA10TX pin:
PA9![]()
Click Generate code to generate your neural network embedded C code that you will add to your application.
![]()
Select the Application variant, keep Generate C code, then click Generate. On top of the AI part, the Application variant adds a top level code running an inference each 5 seconds and outputting its execution time.
![]()
After the generation, click Download your C model. A .zip archive (named
Test_CubeAIStudio_ArcFault_U5-run-1-model-network-ApplicationTemplate-code.zipin this tutorial) is produced containing C-code model files and dependencies. Model integration in firmware project will be done in Part 3 of this tutorial.
Part 2 — Create the base firmware project with STM32CubeMX ¶
In this part we will use STM32CubeMX to initialize board peripherals and generate the firmware project for STM32CubeIDE.
A ready-to-use .ioc file with all the settings described below is available for download. You can use it directly in STM32CubeMX instead of configuring the project from scratch. All the steps of this Part 2 are nonetheless detailed below so you can understand how to proceed by yourself and adapt the configuration to your own application.
1. Create a project from the B-U585I-IOT02A board selector ¶
Starting from the board selector pre-fills all board-specific hardware and software settings allowing to run your AI application. Only the needed peripherals will be enabled.
Open STM32CubeMX and click Access to Board Selector.
![]()
In the Commercial Part Number field, enter B-U585I-IOT02A. Select the item in the Board List.
![]()
Click Start Project.
When asked to initialize all peripherals with their default mode, click No.
Note
Declining auto-initialization avoids enabling peripherals that are not needed and that could create pin conflicts.
When asked for the TrustZone feature, select without TrustZone activated and click OK.
Note
TrustZone is a security feature that allows to separate the application into two domains: secure and non-secure. It is not needed for the simple AI inference application described in this tutorial.
2. Configure Connectivity: USART1 ¶
USART1 is used to print inference results to a serial terminal through Virtual COM Port (115200 bauds).
In Connectivity, select USART1.
Set the mode as Asynchronous.
![]()
3. Configure Clock ¶
Set the system clock to the maximum frequency for best inference throughput.
Switch to the Clock Configuration tab and check that Cortex System timer clock is set at maximum frequency ( 160 MHz):
![]()
4. Configure Project Manager: appli structure, STM32CubeIDE ¶
Switch to the Project Manager tab.
Set a Project Name (
Test_CubeAIStudio_ArcFault_U5in this tutorial).Set Application Structure to Basic.
Set Toolchain / IDE to STM32CubeIDE.
![]()
5. Generate code and open the firmware project ¶
Click Generate Code, then click Open Project in the information window. It imports and opens your firmware project in STM32CubeIDE.
Part 3 — Deploy AI model in firmware project with STM32CubeIDE ¶
In this part, we will integrate the AI model C-code generated in Part 1 into the firmware project generated in Part 2. We will also configure the project settings and edit the main codes to run inferences on the STM32N6570-DK board.
1. Copy AI files to the firmware project ¶
In your file explorer, unzip the archive generated by STM32Cube AI Studio in Part 1 of this tutorial (named
Test_CubeAIStudio_ArcFault_U5-run-1-model-network-ApplicationTemplate-code.zip). The folder contains three folders:
ai_generated_network/— low-level model C code (network weights, operator calls)
AI/App/— application-level code (init, process, I/O wrappers)
Middlewares/ST/AI/— AI software inference runtime libraryIn STM32CubeIDE, in the firmware project (named
Test_CubeAIStudio_ArcFault_U5in this tutorial):
Directly in
Test_CubeAIStudio_ArcFault_U5, create a folder namedAI.In this new AI folder, copy paste .c/.h network files from
Test_CubeAIStudio_ArcFault_U5-run-1-model-network-ApplicationTemplate-code/ai_generated_network:
network.c
network.h
network_data.c
network_data.h
network_details.hIn
Src, copy paste .c files located inTest_CubeAIStudio_ArcFault_U5-run-1-model-network-ApplicationTemplate-code/AI/App:
app_x-cube-ai.c
network_weights.c
user_init.cIn
Inc, copy paste .h files located inTest_CubeAIStudio_ArcFault_U5-run-1-model-network-ApplicationTemplate-code/AI/App:
app_config.h
app_x-cube-ai.h
bsp_ai.h
network_weights.h
user_init.hDirectly in
Test_CubeAIStudio_ArcFault_U5, copy paste Middlewares folder fromTest_CubeAIStudio_ArcFault_U5-run-1-model-network-ApplicationTemplate-code.The resulting project tree should look like:
2. Configure the project settings: include paths, link library, source location ¶
You will have to declare include paths to expose the generated API and runtime headers to the compiler. The runtime library is a pre-compiled static library that implements the software inference engine; it is not generated from source and must be linked explicitly.
Open project properties (Project → Properties → C/C++ Build → Settings → Tool Settings):
Include paths (
MCU/MPU GCC Compiler → Include paths):../AI ../Middlewares/ST/AI/Inc ../Middlewares/ST/AI/Misc/IncLibraries (
MCU/MPU GCC Linker → Libraries):
Libraries — add:
:NetworkRuntime<version>_CM33_GCC.a, replacing<version>with the actual version number of the library (e.g.:NetworkRuntime1200_CM33_GCC.a)Library search path — add:
../Middlewares/ST/AI/LibNote
Check the
Middlewares/ST/AI/Libfolder to find the actual runtime library filename.Still in project properties, switch to C/C++ General → Paths and Symbols → Source Location and add the following folders:
AI
MiddlewaresClick Apply and Close.
3. Edit
main.c: add AI init and process calls
¶
Open
Src/main.cand add:
In
/* USER CODE BEGIN Includes */:#include "app_x-cube-ai.h"In
/* USER CODE BEGIN 2 */:// Load model weights into memory and configure the AI runtime STM32CubeAI_Studio_AI_Init();In
/* USER CODE BEGIN WHILE */:// Run one complete inference per main-loop iteration and output the result over USART1. STM32CubeAI_Studio_AI_Process();You can now build the project (Project → Build Project).
4. Flash and run ¶
You can use STM32CubeIDE ’s built-in run/debug functionality to flash and run the application.
Connect the board to your PC using the micro-USB cable on the STLK USB port.
Right-click the project → Run As → STM32 C/C++ Application.
When the launch configuration dialog opens, click OK. STM32CubeIDE flashes the binary and starts the application.
Open a serial terminal and connect to the board through Virtual COM Port (speed: 115200 bauds). Inference output is displayed on the serial console every 5s (inference time and number of CPU cycles).
Next Steps ¶
Validate model accuracy — in STM32Cube AI Studio, run an on-target validation to compare C model performance metrics against the original model and measure inference time.
Explore other AI models — download pre-trained STM32-optimized models from the STM32 Model Zoo
Customize the application — add sensor input for a full end-to-end inference pipeline
Troubleshooting ¶
Build error — missing headers: Verify that all files from
ai_generated_network/were copied toAIand that../AIis in the include paths and added to source locations.Runtime library not found at link time: Verify that
../Middlewares/ST/AI/Libis set as a Library search path in the project linker settings, and that the library filename listed in Libraries matches the actual.afile inMiddlewares/ST/AI/Lib.``STM32CubeAI_Studio_AI_Init`` undefined reference: Confirm all files from
Test_CubeAIStudio_ArcFault_U5-run-1-model-network-ApplicationTemplate-code/AI/App/(app_x-cube-ai.c, etc.) were copied toSrcandIncand that../AIis in the include paths and added to source locations.Board not detected by STM32CubeIDE: Verify the USB cable is connected to the STLK USB port and that the ST-LINK firmware is up to date (use STM32CubeProgrammer to update it if needed).
Application does not print inference results: Confirm the correct COM port is selected in the serial terminal and that the baud rate is set to 115200.
Assets ¶
Arc_Fault_Detection_U5.ioc - download the
.iocconfiguration file and open it with STM32CubeMX.AI model (
.tflite) — obtain the model from the STM32 Model Zoo. This tutorial was validated with STM32Cube AI Studio v1.2.0.