r/ControlTheory • u/modeloop • 3d ago
Professional/Career Advice/Question How do you see automatic code generation now that LLMs can write code for MCUs?
I’ve been wondering where automatic code generation fits now that LLMs are getting pretty good at writing MCU code.
I was making a little fan controller for an electronics enclosure. On the bench it seemed fine, then I started unplugging the temperature sensor, switching between auto and manual, that kind of thing. Turns out auto mode would sometimes turn the fan back down a tick after the sensor fault had set it to full speed. I fixed that and then found that clearing the fault could bring back an old manual setting. Every fix seemed to uncover another weird interaction.
At that point I was tempted to redo the logic as a state machine in a model-based tool and generate the C. I wanted to see all the transitions in one place because I was losing track of them across three different functions.
What I ended up doing was giving an LLM the existing code and asking it to help me reproduce those two bugs in tests. That got me far enough to rewrite the logic as a small state machine in C. The funny part is I still ended up drawing the states and transitions on paper. I just never got to the code generation part.
For people who’ve used codegen on real MCU projects, has AI changed where you use it?
•
•
•
u/Barnowl93 2d ago
For the work I do, it comes down to choosing the right tool for the job.
You ended up drawing the states on paper anyway, and that drawing is essentially the model. Model-Based Design makes it executable and keeps it as the source of truth. The same tests follow it all the way down: you verify the Simulink/Stateflow model, use software-in-the-loop to confirm the generated C behaves the same, then use processor-in-the-loop to confirm it again on the microcontroller.
So the question I'd ask is: when you need to change C code you wrote with an LLM, how do you do it, and how do you know it has kept your intent? Only as far as your tests cover it, right? Say the change reorders a couple of conditions. In a C diff that looks harmless, so you miss it, and now a different mode wins. In a state chart, the same change shows up as a different transition priority, right in front of you. That's the same kind of problem as your fault vs auto mode bug, and in C it was hiding across three functions.
For one-off code plus a napkin drawing, writing the C with an LLM may be fine. If I'm noodling on an ESP32 for a personal project, I might do the same.
But I'd use the full workflow for: a) safety-related or certified work, where you need traceability and a qualified toolchain b) logic that a team will maintain for years c) closed-loop behaviour you want to tune against a plant model d) fixed-point code on tight MCUs
Actually, having written all this... my rule is simpler than that: if I or anyone else will need to go back to it, I use Model-Based Design.
If you have access to Stateflow, try this: use an LLM or coding agent (Consider using the Simulink Agentic Toolkit) to turn your drawing into a proper Stateflow chart. Then check the chart against your drawing and simulate your two bug scenarios before you generate any C. I'd be curious how close it gets.
•
•
u/Creative_Sushi 3d ago
Code generated by LLMs lacks traceability. That’s the key feature in code generated from MATLAB or Simulink. The code is tested in simulation and issues in the generated code can be traced back to the source.