I Let AI Generate an Arduino Sketch — Here’s What Happened

I was setting up a simple servo test rig on Saturday — the kind of thing I’ve done a dozen times on an ESP32. Potentiometer controls a servo. Basic analogRead, map, servo.write. Nothing fancy. This is the same workbench where I’ve been experimenting with gesture recognition on ESP32, so I had all the hardware within arm’s reach.

Normally I’d open the Arduino IDE and type it from memory. But this time I was curious. I opened Claude Code and typed: “Write an Arduino sketch that reads a potentiometer on A0 and moves a servo on pin 9 proportionally.”

It gave me working code in about 4 seconds.

The Good Parts

The initial sketch was genuinely good:

#include 

Servo myServo;
const int potPin = A0;
const int servoPin = 9;

void setup() {
  myServo.attach(servoPin);
  Serial.begin(9600);
}

void loop() {
  int potVal = analogRead(potPin);
  int angle = map(potVal, 0, 1023, 0, 180);
  myServo.write(angle);
  Serial.print("Pot: ");
  Serial.print(potVal);
  Serial.print(" | Angle: ");
  Serial.println(angle);
  delay(15);
}

It compiled on first upload. The servo moved. The serial monitor showed values. I almost posted a victory tweet right there.

Where It Got Interesting

Then the servo started jittering. Not constantly — just at certain positions, around 90 degrees. Classic servo jitter. I know this problem: cheap MG996R servos get noisy near center position when the PWM signal isn’t perfectly stable.

Claude’s code was *correct* but not *robust*. It didn’t account for real-world hardware quirks. Here’s what I had to add:

1. Dead zone. I added a small threshold so the servo only moves when the pot change exceeds 3 units. This kills the jitter from ADC noise.

2. Smoothing. The map() function is fine, but raw analogRead values bounce. I added a simple running average over 5 samples.

3. Serial baud consideration. The 9600 baud with delay(15) means we’re printing faster than the serial buffer can drain on some boards. The Arduino Uno handles it fine, but on a Nano clone I was getting garbled output.

All three fixes took me about 40 minutes of debugging. The AI gave me the 80% solution in 4 seconds. The last 20% — the “actually works in the real world” part — needed my hardware experience.

What This Means for Weekend Tinkering

AI code generation for Arduino is in an interesting place. For standard libraries like Servo.h, the output is solid. The AI has seen thousands of servo sketches in its training data and can reproduce the pattern perfectly.

But it doesn’t know about your specific hardware. It doesn’t know your MG996R jitters at center. It doesn’t know your Nano clone has a sketchy CH340 serial chip. That domain knowledge still lives in your head — and in the forums where someone had the same problem in 2018.

The Verdict

I’ll keep using AI to generate Arduino boilerplate. It saves me from typing the same setup() pattern for the hundredth time. But I won’t trust it to write production-ready hardware code without my review. The gap between “compiles” and “works reliably” is still wide, and that gap is exactly where experience lives.

If you’re starting out with Arduino, use AI to get unstuck — but budget half your time for debugging. The AI gives you a starting point. Hardware gives you the hard problems.

Related: Design Thinking Is 80% Theater. Here’s the 20% That Works..

Related: The $5 VPS Era Is Over. Here’s What Replaced It..


Discover more from Susiloharjo

Subscribe to get the latest posts sent to your email.

Discover more from Susiloharjo

Subscribe now to keep reading and get access to the full archive.

Continue reading