AI as an Engineering Tool
Accelerating Arduino Development with Generative AI
(Source: fencifd/stock.adobe.com)
In pure software projects where no hardware interaction is required, large language models (LLMs) like ChatGPT and Gemini are helping programmers accelerate their development like never before. This assistance often comprises completion suggestions as the developer is writing code or troubleshooting advice for bugs and syntax errors—elements of programming that take time. The text-generation capabilities of these models seem to go hand in hand with software development, but, fortunately, these models can help with hardware-based projects in many ways as well. In particular, for platforms like Arduino, which is popular for hobby projects and proof-of-concept hardware products alike, LLMs can accelerate the development process beyond just producing snippets of helpful code.
Begin the Sketch with AI
A key part of Arduino projects is the sketch, but as any engineer will tell you, that’s just one part of the process. Two of the other main steps when building a project with Arduino are (1) reading documentation about specific boards and components to understand pin assignments and requirements for use and (2) planning the overall software flow and architecture. After those initial steps, the groundwork for the project continues, which often entails troubleshooting unexpected or obscure error messages. Alongside writing the code for the sketch, tests can be written and gaps in functionality can be identified. Fortunately, with an artificial intelligence (AI) companion in the development process, each of these phases can be streamlined. The “companion” role is important; although these AI models seem to perform miracles in terms of the sophisticated outputs they can produce, they are not always correct and often miss important nuances when trying to solve a large, complex problem with code. Hence, working with an engineer or similarly experienced technologist is vital to test and verify that the code works as expected and that the project is a success. The type of LLM is important, too; even though ChatGPT is the most well-known, others like Claude, Gemini, and Copilot are worth considering for their coding abilities.
Read the Documentation
Arduino projects are not just about writing the sketch code. Before the software programming can start, preparation needs to happen. Specifically, one needs to read documentation and specifications to understand how to interact with the chosen components, as well as to recognize expected inputs and outputs and any limits or ranges. Depending on the type of component, these documents may be quite long and tedious to examine, so having an AI sidekick to streamline the process is beneficial. Many LLMs can accept files like those provided as datasheets in order to answer questions about the specifications, functions, and requirements (Figure 1). Some models are also multimodal and can understand, to some extent, the graphics and circuit board diagrams in these files. Similarly, generative AI can accelerate understanding and learning about unfamiliar functions related to new components. This is particularly useful when dealing with libraries for new modules being used in a project for the first time.
Figure 1: ChatGPT demonstrating its ability to ingest a component datasheet (the Adafruit TSL2591 High Dynamic Range Digital Light Sensor) and write the basic code necessary to use it. (Source: Author)
Brainstorm and Structure
AI can also help with understanding what a high-level flow might look like, especially if multiple components are interacting. In cases where data from many sensors should be read prior to making complex, multifaceted decisions impacting other electronic elements, brainstorming the pseudocode flow or investigating what the software layout should look like may be useful before coding anything. This could also include determining whether different sections of code should be spread across different files and how those might interact. If the project is not particularly complex, one can kickstart it quickly by asking the model to generate the most basic loops needed and then proceed.
Help Write the Code
As previously mentioned, LLMs are good at generating code but are not perfect, especially when asked to produce long and complex outputs. With that in mind, a few steps will accelerate development by avoiding delays from having to fix errors. First, request small snippets at a time by concisely outlining inputs, outputs, and expected functionality. For example, writing precise engineering style comments and the opening line of a function for the AI to complete can help guide better code production. This is especially true for writing common segments of code like blinking light-emitting diodes, outputting pulse width modulation signals, turning encoders, or readings from popular sensors.
Second, ask the AI to modify sections of code, such as those from existing sketches, and make them fit with the current project. This approach can also be used to streamline code, turning parts into repeatable functions and ensuring that code generation requests describe hardware constraints, like the exact microcontroller (e.g., Arduino Uno versus ESP32) and any power, memory, or pin limitations. This ensures that the code is optimized for the board you’re using and avoids functions that are incompatible with certain architectures..
Verifying that the model hasn’t made mistakes is equally important when using it to generate code, so be sure to review the code and test with online simulators like Tinkercad. Additionally, the AI can produce debugging code lines to simplify the process; for instance, if a piece of its code is unclear, it tends to succeed at explaining specific lines when asked. Lastly, the model can be probed if bugs or errors are occurring, which is especially useful when it already has the generated code as context (Figure 2).
Figure 2: Given the instructions on the left, ChatGPT is able to rewrite portions of code as functions, so the main loop is less cluttered on the right. (Source: Author)
Conclusion
Powerful, modern AI models in the form of LLMs are accelerating software development like never before, but they also have a place in boosting hardware projects. This is especially true for tasks like writing Arduino sketches. Not only can these models help developers produce code for these systems, but they can also assist in other areas of the project, such as understanding documentation and unfamiliar component libraries and brainstorming overall software flow before implementation. Like any software tool, AI models are not perfect and cannot replace the people working on hardware projects, but AI’s current capabilities as an engineering companion are ideal to help accelerate the development process.