AI on Arduino and ESP32: 3 ways to add AI to your projects

The three ways to add AI to an Arduino or ESP32 project, what each board can manage, and a 10-minute sketch where the UNO learns from your own examples.
  • Level:Beginner
  • Duration: 30 min
  • Ages 12+
  • Platform: Arduino

Tools for this lesson

Helpful for this build. Open a tool to see tips and more projects that use it.

AI on Arduino and ESP32 – EEMENTOR STEM™ guide

Quick answer: you can add AI to Arduino and ESP32 projects in three ways. 1. Learn on the board: even an Arduino UNO can learn simple patterns from your examples. 2. Run a trained model: an ESP32 can run a small machine learning model, for example to recognise gestures or sounds, trained on your computer with a tool like Edge Impulse. 3. Ask a cloud AI: an ESP32 can send a question over Wi-Fi to an AI like ChatGPT or Claude and show the answer. Below you build the first kind in 10 minutes, then find a free lesson for each.

What you need

  • For the sketch in this guide, from the UNO R3 Starter Kit: the UNO R3 board and USB cable, the DHT11 module, 2 push buttons, 1 green LED, 1 red LED, 2 resistors (220 Ω), breadboard and jumper wires
  • For trained models and cloud AI: the ESP32 IoT Starter Kit, because these need more memory and Wi-Fi
  • The free Arduino IDE with the DHT sensor library by Adafruit

Steps: choose your kind of AI

  1. Learn on the board (UNO or ESP32). The board stores your examples and decides by comparing. Good for a few sensor readings, like "is this comfortable?". Try it with the sketch below.
  2. Run a trained model (ESP32). You collect data, train a model on a computer, and copy the finished model onto the board. Good for gestures, sounds and motion. It works without the internet once it's trained.
  3. Ask a cloud AI (ESP32 with Wi-Fi). The board sends text, like a sensor reading and a question, to an AI service and gets an answer back. Good for chatting, explaining and writing. It needs the internet and an API key, and the AI services charge for use.
  4. Pick the smallest kind that does the job. On-board learning is fast, free and private. Use a cloud AI only when you need language.
Three ways to add AI: learn on the board with an UNO or ESP32, run a trained model on an ESP32 without internet, or ask a cloud AI like ChatGPT or Claude over Wi-Fi.
The three kinds of AI, from smallest to biggest.

Free AI lessons for each kind

Kind of AI Board Try this lesson
Learn on the board UNO Teach an artificial neuron to tell light from dark, a colour detector that learns from examples, a servo that learns to find the light
Learn on the board ESP32 Let the ESP32 find groups on its own with k-means, predict tomorrow's room temperature, a machine that notices when a fan sounds "wrong"
Run a trained model ESP32 A magic wand that recognises gestures with Edge Impulse (the model runs on the ESP32), rock-paper-scissors against an AI (the model runs in your browser and the ESP32 plays)
Ask a cloud AI ESP32 An AI chat buddy on the LCD, a talking plant, ChatGPT vs Claude blind test
The Arduino UNO R3 has 2 KB of RAM, 16 MHz and no Wi-Fi, so it can learn on the board; the ESP32 has 520 KB of RAM, up to 240 MHz and Wi-Fi, so it can do all three kinds of AI.
Why the trained models and cloud AI lessons use the ESP32.

Full code: the UNO learns what "comfortable" means to you

This sketch uses k-nearest neighbours, one of the simplest machine learning methods. Press the "comfy" button when the room feels nice and the "stuffy" button when it doesn't. The UNO saves the temperature and humidity with your label. To decide about a new reading, it finds the 3 saved examples closest to it and lets them vote.

// The UNO learns from your examples whether the room is comfy or stuffy (k-nearest neighbours).
#include <DHT.h>

DHT dht(7, DHT11);
const int COMFY_BUTTON = 2, STUFFY_BUTTON = 3;
const int GREEN_LED = 9, RED_LED = 10;

const int MAX_EXAMPLES = 30;
float exTemp[MAX_EXAMPLES], exHum[MAX_EXAMPLES];
bool exStuffy[MAX_EXAMPLES];
int count = 0;

void addExample(float t, float h, bool stuffy) {
  int i = count < MAX_EXAMPLES ? count++ : random(MAX_EXAMPLES);   // when full, replace a random one
  exTemp[i] = t; exHum[i] = h; exStuffy[i] = stuffy;
  Serial.print(stuffy ? "Learned STUFFY at " : "Learned COMFY at ");
  Serial.print(t, 1); Serial.print(" C, "); Serial.print(h, 0); Serial.println(" %");
}

// Ask the 3 nearest examples to vote. Humidity is divided by 3 so 3 % counts like 1 C.
bool predictStuffy(float t, float h) {
  bool used[MAX_EXAMPLES] = {false};
  int votes = 0, k = min(3, count);
  for (int n = 0; n < k; n++) {
    int best = -1; float bestDist = 1e9;
    for (int i = 0; i < count; i++) {
      float dt = t - exTemp[i], dh = (h - exHum[i]) / 3.0;
      float dist = dt * dt + dh * dh;
      if (!used[i] && dist < bestDist) { bestDist = dist; best = i; }
    }
    used[best] = true;
    if (exStuffy[best]) votes++;
  }
  return votes * 2 > k;   // more than half say stuffy
}

void setup() {
  Serial.begin(9600);
  dht.begin();
  pinMode(COMFY_BUTTON, INPUT_PULLUP);
  pinMode(STUFFY_BUTTON, INPUT_PULLUP);
  pinMode(GREEN_LED, OUTPUT);
  pinMode(RED_LED, OUTPUT);
  randomSeed(analogRead(A0));
  Serial.println("Press COMFY or STUFFY to teach me");
}

void loop() {
  float t = dht.readTemperature(), h = dht.readHumidity();
  if (isnan(t) || isnan(h)) { Serial.println("DHT11 error"); delay(2000); return; }

  if (digitalRead(COMFY_BUTTON) == LOW) { addExample(t, h, false); delay(500); }
  if (digitalRead(STUFFY_BUTTON) == LOW) { addExample(t, h, true); delay(500); }

  if (count > 0) {
    bool stuffy = predictStuffy(t, h);
    digitalWrite(GREEN_LED, stuffy ? LOW : HIGH);
    digitalWrite(RED_LED, stuffy ? HIGH : LOW);
    Serial.print(t, 1); Serial.print(" C, "); Serial.print(h, 0);
    Serial.println(stuffy ? " %  -> I think: stuffy" : " %  -> I think: comfy");
  }
  delay(1000);
}

Teach it in different conditions: breathe near the sensor or hold it in your hand for "stuffy", open a window for "comfy". After 6 to 10 examples it starts to agree with you.

k-nearest neighbours: comfy and stuffy examples as dots on a temperature and humidity chart; the 3 examples closest to a new reading vote, and the majority wins.
How the sketch decides, with example data.

Wiring

Breadboard wiring diagram: comfy button on UNO pin 2, stuffy button on pin 3, green LED on pin 9 and red LED on pin 10 through 220 Ω resistors, DHT11 on pin 7.
Breadboard view. Check each connection against the wiring table below.
From To
DHT11 S, +, − UNO pin 7, 5V, GND
"Comfy" button UNO pin 2 and GND (across the centre gap)
"Stuffy" button UNO pin 3 and GND (across the centre gap)
Green LED long leg 220 Ω resistor, then UNO pin 9 (short leg to GND)
Red LED long leg 220 Ω resistor, then UNO pin 10 (short leg to GND)

The buttons need no resistors: INPUT_PULLUP uses resistors inside the UNO.

FREE

Build this circuit in EEMENTOR Lab

Wire it yourself in your browser before you touch a real wire. No sign-up, nothing to install.

The ESP32 and the parts for this lesson laid out in EEMENTOR Lab, ready to wireBuild it yourself
  1. The ESP32 and this lesson’s parts are already placed. Wire them using the wiring table above. Pins with no wire show red.
  2. Missing a part? Add it from the parts list on the left.
  3. Press Run, then try the inputs: click, hold or slide them under Selected.
  4. Then try this lesson’s sketch: paste it into the Code tab and press Compile & Run.

EEMENTOR Lab is free and in beta. Results are a guide; a real build can differ slightly. About the Lab

Common problems

  • Nothing lights up: the LEDs stay off until you've taught at least one example. Press a button.
  • It always says the same thing: you've taught only one label, or all your examples are at almost the same temperature. Teach both labels in clearly different conditions.
  • One press adds several examples: hold the button for a moment and let go; the half-second pause stops repeats. Shorter presses help too.
  • "DHT11 error": check S goes to pin 7 and that the DHT sensor library is installed.
  • It forgets everything when unplugged: the examples live in RAM. To keep them, save them to the UNO's EEPROM, a good next challenge.

FAQ

Can an Arduino UNO really run AI?

Simple AI, yes. The UNO has only 2 KB of RAM, so it can't run big models, but it can learn from examples with methods like k-nearest neighbours or a single artificial neuron.

What is the difference between AI and machine learning?

AI is the big idea of machines doing things that seem smart. Machine learning is one way to get there: instead of writing every rule, you give examples and the program finds the pattern.

Can the ESP32 run ChatGPT or Claude?

Not on the board itself; those models are far too big. The ESP32 sends your question over Wi-Fi to the AI company's servers and shows the answer. You need an API key, and the services charge for use.

Do I need the internet for AI on an ESP32?

Only for cloud AI. Learning on the board and trained models (like the Edge Impulse magic wand) run on the ESP32 with no internet.

Which is better for AI, the ESP32 or the Arduino UNO?

The ESP32: it has far more memory, a faster processor and Wi-Fi, so it can do all three kinds. The UNO is great for learning how simple AI works.

Related lessons