AI for Arduino: how to write sketches with a model and not fry the board
A model can draft a sketch but cannot see your wiring. How to describe the board, check code before uploading and debug using Serial output.
5 min · · development · models
An AI model for writing Arduino code helps with routine work: reading a sensor, blinking an LED, driving a relay, working with a display. But the model has no access to your wiring or board, so it guesses pin numbers, library names and voltages. A software mistake at worst leaves the sketch not working, while a hardware mistake can damage a pin or the whole board. Below: how to set the task, what to check and how to debug.
What the model knows and what it invents
Typical constructs like pinMode, digitalRead, millis(), Serial and calls to popular libraries are common in open code, but the model does not know the details of your project. Weak spots:
- Pin numbers. If you do not name the board, the model picks "typical" pins that may not exist on your board or may have a special function.
- Library names and versions. Invented names are possible, as are functions that your library version does not have.
- Voltages and currents. The model may attach a load a pin cannot handle, or connect 5 V logic to a 3.3 V board.
- Memory. On small boards (for example the Uno with 2 KB of RAM) needless
Stringobjects and large arrays cause unstable behaviour.
How to describe the task
The more precise the input, the less invention. Include in the request:
- The board: Arduino Uno, Nano, Mega, ESP32 and so on. This determines logic levels and available pins.
- The wiring: which component is on which pin, pull-ups, resistors.
- Libraries: which ones you are willing to use (or "no external libraries").
- Behaviour: what should happen, under what conditions, how often.
- Constraints: "no
delay()", "do not useString", "debug output to Serial at 115200".
A prompt template
Board: Arduino Uno (ATmega328P, 5 V logic).
Connected:
- a button between pin 2 and GND, using INPUT_PULLUP;
- an LED on pin 9 through a 220 ohm resistor to GND.
Task: toggle the LED when the button is pressed.
Requirements:
- debounce without delay(), using millis();
- print the LED state to Serial (115200);
- no external libraries;
- save memory: strings via F().
Do not add pins or components that are not in the list. If data is missing, ask a question.An example result
const uint8_t BUTTON_PIN = 2;
const uint8_t LED_PIN = 9;
const unsigned long DEBOUNCE_MS = 30;
bool ledOn = false;
bool lastReading = HIGH;
bool stableState = HIGH;
unsigned long lastChange = 0;
void setup() {
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
Serial.println(F("Ready"));The sketch is simple, so it can be checked by eye. More complex code needs a system of checks.
How to check code before uploading to the board
- Compilation. In the Arduino IDE press Verify, or use
arduino-cli compile --fqbn arduino:avr:uno YourSketchfor the Uno. This catches syntax errors and non-existent functions. Other boards need a different--fqbn; the arduino-cli documentation will tell you which. - Pin check. Match every constant against your wiring. This matters especially for the ESP32 and boards with restrictions on certain pins.
- Library check. Verify the library name and version in the library manager. If the model uses a function that is not in the library's documentation, it made it up.
- Electrical check before power. Make sure that:
- the LED has a current-limiting resistor;
- the current drawn from a pin does not exceed what your board allows (check the microcontroller documentation: power loads such as motors and relays not directly from a pin but through a driver or transistor);
- 5 V signals are not applied to 3.3 V board inputs;
- inductive loads (relays, motors) are protected by a diode.
- First run without a load. Check the logic through Serial first, and connect powerful components afterwards.
No model replaces these points: this is where boards get "burned".
How to debug via Serial
A typical debugging loop with a model:
- Ask the model to add output of key values to the code: pin states, sensor readings, time.
- Upload the sketch and open the serial monitor at the same speed as in
Serial.begin. - Copy the output together with a description of what you expected to see and send it to the model.
- Ask it to name two or three hypotheses and a way to test each, rather than rewriting the code straight away.
An example request: "Here is the sketch and the Serial output. The LED toggles twice per press. Suggest causes and how to check each." This gets you a diagnosis rather than a random edit.
If the model supports images (in the Zukko catalogue, for example, claude-sonnet-5-5), you can attach a photo of the assembled circuit. But misreading wires is possible, so a description in words is still required.
Which model and what it costs
For sketches of 100–200 lines the context is small. Suppose a request with description and code takes 1,500 tokens and the answer takes 1,200. Zukko prices at October 18, 2026, $ per 1M tokens (input / output):
claude-sonnet-5-5(0.4 / 2): 1,500 × 0.4 / 1,000,000 + 1,200 × 2 / 1,000,000 = $0.0006 + $0.0024 = $0.003gpt-6.1-sol(0.27 / 1.32): $0.000405 + $0.001584 = $0.001989deepseek-v4.1-flash(0.013 / 0.038): $0.0000195 + $0.0000456 = $0.0000651
Five debugging iterations on claude-sonnet-5-5 cost about $0.015 if you do not accumulate a long history. We do not claim which model knows Arduino better: take two or three and check on your own sketches. Prompting tips are in 10 prompts for a programmer and few-shot prompting: an example of your earlier sketch in the request helps preserve style.
Sending a request from Python
from openai import OpenAI
client = OpenAI(base_url="https://api.zukko.pro/v1", api_key="fetch_your_key")
prompt = open("prompt.txt", encoding="utf-8").read()
response = client.chat.completions.create(
model="claude-sonnet-5-5",
messages=[
{"role": "system", "content": "You help write Arduino sketches. Do not invent pins or libraries."},
{"role": "user", "content": prompt},
],
)
print(response.choices[0].message.content)The request format is described in the chat completions documentation. Create the key in the dashboard; the balance is topped up from $1. From Russia the balance is topped up via SBP (the payment page adds an 8.5% fee to the amount), MasterCard or cryptocurrency.
FAQ
Can I trust a sketch written by an AI?
As a draft, yes. Before uploading, compile the code, check pins and libraries against your wiring and verify the electrics. It is better to do the first run without a heavy load.
Which model should I pick for microcontroller programming?
There is no universal answer. For small sketches an inexpensive model may be enough, while complex logic (interrupts, several devices on a bus) is worth trying on a stronger one. A test on your own tasks decides.
What if the model uses a library that does not exist?
Check the name in the library manager. If there is no such library, ask the model to rewrite without it or to name an alternative, and check against that library's documentation.
Does this work for the ESP32 and other boards?
Yes, but name the board explicitly: the ESP32 has different logic levels (3.3 V), a different pin map and restrictions on some pins.
zukko
Pay 25×
less
for Claude Code & ChatGPT
One fetch_* key and base URL api.zukko.pro — for Cursor, Claude Code, Codex, and any SDK.
Related articles
- Prompt Engineering Examples: 12 Ready-Made Templates for WorkTwelve prompt templates for everyday tasks: what each one says, why it is built that way, and what a single API call costs.
- A database for RAG: where to store knowledge — vector DB, search or contextRAG does not require a vector database. Four ways to store knowledge, the cost of a query for each, and a working SQLite example.
- The best AI for C# programming: choosing a model and setting up an agentWe give no ranking of the best model for C#, but we show how to compare options by price and index, set up an agent for .NET and verify the result with builds and tests.
- Claude Opus 4.6: what it is and how to move to Opus 5.5 safelyOpus 4.6, 4.7 and 4.8 are the previous generation, and the catalogue now has Opus 5 and 5.5. A four-step migration plan: model id, your own prompts, cost comparison, rollback.