10 Battle-Tested Prompts for IoT and Embedded: Arduino, ESP32, Raspberry Pi

Introduction

If you’re building IoT devices — from a simple temperature logger to a multi-sensor home automation hub — you’ve probably noticed that half the time goes into wiring, debugging, and rewriting boilerplate code. The other half? Figuring out how to prompt LLMs (like ChatGPT or Claude) to actually help you with embedded C, MicroPython, or MQTT configuration.

I’ve been there. Over the past two years, I’ve curated a set of prompts that reliably produce working firmware outlines, sensor readouts, and debugging strategies for Arduino, ESP32, and Raspberry Pi. These are not generic “write code for me” requests — they are structured, context-rich prompts that save hours.

Below are 10 prompts I use daily in my IoT workflow. Each comes with a real usage example, the exact prompt text, and why it works.

1. Generate a Sensor Readout Loop (Arduino)

When to use: You need a reliable, non-blocking loop that reads a sensor (e.g., DHT22) and prints to Serial or sends data.

Prompt:

Write an Arduino sketch for ESP32 that reads a DHT22 sensor every 5 seconds without using delay(). Use millis() for timing. Print temperature and humidity to Serial. Include error handling for sensor read failures. Assume DHT data pin = 4.

Why it works: It specifies the board, sensor, timing method, and error handling. The delay() avoidance is critical for IoT devices that need to handle WiFi or button interrupts.

Example output (simplified):

#include <DHT.h>
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
unsigned long previousMillis = 0;
const long interval = 5000;

void setup() {
  Serial.begin(115200);
  dht.begin();
}

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    float h = dht.readHumidity();
    float t = dht.readTemperature();
    if (isnan(h) || isnan(t)) {
      Serial.println("Sensor read failed!");
      return;
    }
    Serial.print("Temp: "); Serial.print(t);
    Serial.print(" °C, Humidity: "); Serial.println(h);
  }
}

2. MQTT Publish-Subscribe Template (ESP32)

When to use: You need a secure MQTT connection with TLS, subscribing to a topic and publishing sensor data.

Prompt:

Create an ESP32 Arduino sketch that connects to WiFi (SSID: MyIoT, password: secret), then connects to broker.hivemq.com on port 8883 with TLS. Subscribe to topic "home/commands". When a message "toggle" arrives, toggle built-in LED (pin 2). Also publish "online" every 60 seconds to topic "home/status". Use PubSubClient library.

Why it works: It pins down broker, port, TLS, subscription behavior, and periodic publishing — all in one request.

Note: HiveMQ’s public broker is free and works for prototyping. For production, consider a private broker or AWS IoT Core.

3. Debug a Non-Functional WiFi Connection

When to use: Your ESP32 can’t connect to WiFi and you’ve tried everything.

Prompt:

I have an ESP32 that fails to connect to WiFi. It prints "Connecting to WiFi..." infinitely. I’ve checked SSID and password. The router is 2.4 GHz. I use WiFi.begin() in setup(). List 10 possible causes, from most common to rarest, with code fixes for each.

Why it works: It forces the AI to think systematically — power supply, channel, MAC filtering, static IP, etc. I once discovered that my board had a defective antenna after the AI suggested checking signal strength via WiFi.RSSI().

4. Convert Arduino Code to MicroPython (Raspberry Pi Pico)

When to use: You have a working Arduino sketch but want to port it to a Raspberry Pi Pico running MicroPython.

Prompt:

Convert the following Arduino code to MicroPython for Raspberry Pi Pico. The original reads a DHT11 sensor, blinks an LED on pin 25 when humidity > 70%. Use machine.Pin and dht module. Keep the same logic but use MicroPython idioms (no curly braces). Here's the Arduino code: [paste code].

Why it works: It gives the AI both the source and target constraints. I’ve used this to migrate a greenhouse monitor from an Uno to a Pico W in under an hour.

5. Write a PID Controller for Temperature Regulation

When to use: You’re building an oven reflow controller or a fermentation chamber.

Prompt:

Write an Arduino PID controller for an ESP32 that maintains a setpoint of 60°C using a DS18B20 sensor and a relay-controlled heater. Output PWM to a MOSFET (pin 5). Use the Arduino PID library. Include anti-windup and a manual override function. Print current temp, setpoint, and output every second.

Why it works: It specifies sensor, actuator, library, and advanced features (anti-windup, manual override). The AI will generate a functional PID loop, not just a sketch.

6. Parse JSON from an API (ESP32)

When to use: You want your device to fetch weather data or control commands from a REST API.

Prompt:

Write an ESP32 sketch that fetches JSON from https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41¤rent_weather=true using WiFiClientSecure. Parse the JSON with ArduinoJson library. Extract current temperature and windspeed. Print them to Serial. Handle HTTP errors and JSON parsing failures gracefully.

Why it works: It uses a real, free API (Open-Meteo, no key needed) and forces error handling.

7. Deep Sleep Optimization (Battery-Powered ESP32)

When to use: You need your device to run on batteries for months.

Prompt:

Write an ESP32 sketch that reads a soil moisture sensor (analog pin 34), takes an average of 10 readings, wakes from deep sleep every 30 minutes, sends data via MQTT, then goes back to deep sleep. Use RTC memory to store calibration values across sleep cycles. Calculate estimated battery life for a 2000 mAh LiPo.

Why it works: It combines deep sleep, ADC reading, MQTT, RTC memory, and power estimation — a complete battery-optimized template.

8. Debug I2C Communication (Raspberry Pi + Arduino)

When to use: Your I2C devices aren’t showing up or are returning garbage.

Prompt:

My ESP32 is master, and an Arduino Nano is slave on I2C. The Nano has address 0x08. When I send a byte from ESP32, the Nano receives it but returns wrong data. List 7 debugging steps, including code fixes for both sides, and how to use a logic analyzer to check SDA/SCL.

Why it works: It asks for a structured debugging approach. I’ve used this to find a missing pull-up resistor on SDA.

9. OTA Firmware Update (ESP32)

When to use: You want to update firmware over WiFi without a USB cable.

Prompt:

Write an ESP32 Arduino sketch that supports OTA updates via ArduinoOTA library. Include a fallback mechanism: if the new firmware fails to boot, revert to the previous version after 3 failed attempts. Use preferences library to store boot count. Print OTA progress to Serial.

Why it works: It includes a boot-count fallback — a critical safety feature rarely covered in basic examples.

10. Automate Home Assistant MQTT Discovery

When to use: You want your ESP32 sensor to appear automatically in Home Assistant.

Prompt:

Write an ESP32 sketch that publishes MQTT discovery messages for Home Assistant. Create a temperature sensor entity with device class "temperature", unit "°C", and a unique ID. Use the DHT22 sensor. Publish config to "homeassistant/sensor/esp32temp/config" and state to "homeassistant/sensor/esp32temp/state". Include availability topic.

Why it works: It follows the exact MQTT discovery protocol that Home Assistant expects. The AI generates the correct JSON config payload.

How to Adapt These Prompts

  • Be specific about hardware: Always mention board (ESP32, Arduino Uno, Pi Pico), sensor model, and pin numbers.
  • Specify libraries: If you use a particular library (e.g., ArduinoJson, PubSubClient), name it.
  • Add constraints: Non-blocking, error handling, power efficiency, etc.
  • Include context: The more you tell the AI about your system (e.g., “running on batteries,” “needs to handle WiFi disconnects”), the better the output.

Real-World Case: Greenhouse Monitor

I used prompts #1, #2, and #7 to build a battery-powered greenhouse monitor. The device:
- Reads temperature, humidity, and soil moisture every 30 minutes
- Sends data via MQTT to a Raspberry Pi running Node-RED
- Goes into deep sleep between readings

Result: Battery life exceeded 3 months on a single 18650 cell. The AI-generated code required only minor pin adjustments. The total development time dropped from 3 days to 4 hours.

Tools and Resources

  • Arduino IDE 2.x — free, supports ESP32 via board manager
  • PlatformIO — better for complex projects, excellent library management
  • MQTT Explorer — desktop tool to debug MQTT messages
  • Home Assistant — open-source home automation platform
  • Open-Meteo API — free weather API, no key required

For connecting your IoT devices to cloud services like AWS IoT Core, Azure IoT Hub, or custom dashboards, consider using a middleware platform. ASI Biont supports connecting your embedded devices to multiple cloud services through a unified API — see details at asibiont.com/courses.

Conclusion

Prompts are not magic — they are structured instructions that reduce ambiguity. The 10 prompts above have saved me dozens of hours of boilerplate coding and debugging. Copy them, adapt them to your hardware, and you’ll go from idea to working prototype faster than ever.

The key insight: treat an LLM like a junior engineer who knows everything but needs precise instructions. Give it board type, sensor model, pin numbers, library names, and error handling requirements. The output will be production-ready — or close enough that you only fix wiring.

Now go build something that connects to the world.

← All posts

Comments