Want to try easy AI on ESP32 but think you need Python, a GPU or a big dataset? You don’t. In this tutorial you will build a voice-controlled light that understands two spoken commands – for example “light on” and “light off”, or Bangla words like “batti jalao” and “batti nevao”. It runs fully offline on an ESP32-S3, with just one microphone module and one relay. Most people finish it in an afternoon.

Easy AI on ESP32 tutorial: offline voice controlled light
Easy AI on ESP32: a voice-controlled light that works without internet

What you will build

The ESP32 listens to the room in 1-second chunks. A tiny neural network (a keyword spotting model) decides whether it heard “light on”, “light off”, background noise, or some other word. If it is confident, it switches a relay. Nothing is sent to the cloud – the AI decision happens on the chip in a fraction of a second.

Easy AI on ESP32 keyword spotting flow: microphone, MFCC, TinyML model, relay
The whole AI pipeline runs on the ESP32-S3

Why is this the easiest real AI project? Because the hard parts – feature extraction (MFCC) and training – are done for you by the free Edge Impulse studio in a web browser. You only record your voice, click Train, and download an Arduino library. If you want the bigger picture first, read our overview: ESP32 AI Projects: 4 Ways to Add AI to Your ESP32.

Parts list (only 3 modules)

PartWhyNotes
ESP32-S3 dev board with PSRAM (e.g. N16R8)Runs the model; its vector instructions speed up AI mathsClassic ESP32 can also work for small keyword models, but S3 is easier
INMP441 I²S microphone moduleClean digital audio, no analog noise problemsTDK specifies 61 dBA SNR and a 1.62–3.63 V supply – power it from 3.3 V, never 5 V
Relay module (opto-isolated, 5 V) or an LEDSwitches the lightTest with an LED first. Many relay modules are active-LOW
USB cable + 5 V supplyPowerA weak cable causes random resets

Not sure which board or mic to buy? Our ESP32 AI hardware guide explains every option in detail.

Wiring

Easy AI on ESP32 wiring diagram: ESP32-S3 with INMP441 I2S microphone and relay module
Only 8 wires: 5 for the microphone, 3 for the relay
  • INMP441 VDD → 3V3, GND → GND, and tie L/R → GND (selects the left channel).
  • INMP441 SCK → GPIO4, WS → GPIO5, SD → GPIO6.
  • Relay IN → GPIO10, VCC → 5V, GND → GND.
  • Keep the microphone wires short (under 15 cm) and away from the relay coil.

Safety: if you switch a 230 V lamp, the mains side of the relay must be inside a closed box, with proper wire and terminals. Practise with an LED or a 12 V lamp first. Our article on low-cost smart home relay modules shows suitable boards.

Step-by-step: train and run the AI

Step 1: Create an Edge Impulse project and record voice samples

  1. Create a free Edge Impulse account and a new project, for example light_switch.
  2. Go to Data acquisition. The easiest way is to record with your phone or laptop microphone – connect your phone by scanning the QR code shown in the studio.
  3. Create four labels: light_on, light_off, noise (fan, traffic, TV) and unknown (other random words).
  4. Record at least 50 one-second samples per label. Ask 3–4 family members or classmates to help – different voices make the model much more reliable.
  5. Edge Impulse splits long recordings into 1-second clips for you (use Split sample).

Step 2: Design the impulse and train

  1. Create impulse: time series input, window size 1000 ms, frequency 16000 Hz.
  2. Add an Audio (MFCC) processing block and a Classification learning block. Save.
  3. Click Generate features, then Train with the default settings.
  4. Check the confusion matrix. 85–95% accuracy is normal for a first try. If light_on and light_off get confused, record more samples or choose two words that sound more different.

Step 3: Download the Arduino library

Go to Deployment → Arduino library → Build. In the Arduino IDE (with the ESP32 board package version 3.x installed) choose Sketch → Include Library → Add .ZIP Library and select the downloaded file. Edge Impulse documents this flow in its Arduino library guide.

The ESP32 code

Create a new sketch and paste the code below. Change the #include line to match your Edge Impulse project name. In Tools, select your ESP32-S3 board and enable PSRAM.

#include <ESP_I2S.h>
#include <light_switch_inferencing.h>   // name comes from your Edge Impulse project

#define I2S_SCK    4
#define I2S_WS     5
#define I2S_SD     6
#define RELAY_PIN  10
#define THRESHOLD  0.80f                // only act when the model is 80% sure

I2SClass i2s;
static int16_t audio[EI_CLASSIFIER_RAW_SAMPLE_COUNT];   // 1 s of audio (16000 samples)

// Edge Impulse asks for audio in small pieces through this callback
static int get_audio(size_t offset, size_t length, float *out) {
  numpy::int16_to_float(&audio[offset], out, length);
  return 0;
}

static void record_one_second() {
  int32_t raw[256];
  size_t n = 0;
  while (n < EI_CLASSIFIER_RAW_SAMPLE_COUNT) {
    size_t count = i2s.readBytes((char *)raw, sizeof(raw)) / 4;
    for (size_t i = 0; i < count && n < EI_CLASSIFIER_RAW_SAMPLE_COUNT; i++) {
      int32_t v = raw[i] >> 14;          // INMP441 sends 24-bit data in a 32-bit slot
      audio[n++] = (int16_t)constrain(v, -32768, 32767);
    }
  }
}

void setup() {
  Serial.begin(115200);
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW);

  i2s.setPins(I2S_SCK, I2S_WS, -1, I2S_SD);         // BCLK, WS, DOUT (unused), DIN
  if (!i2s.begin(I2S_MODE_STD, EI_CLASSIFIER_FREQUENCY, I2S_DATA_BIT_WIDTH_32BIT,
                 I2S_SLOT_MODE_MONO, I2S_STD_SLOT_LEFT)) {
    Serial.println("I2S init failed - check the microphone wiring");
    while (1) delay(100);
  }
}

void loop() {
  Serial.println("Listening...");
  record_one_second();

  signal_t signal;
  signal.total_length = EI_CLASSIFIER_RAW_SAMPLE_COUNT;
  signal.get_data = &get_audio;

  ei_impulse_result_t result = { 0 };
  if (run_classifier(&signal, &result, false) != EI_IMPULSE_OK) return;

  for (size_t i = 0; i < EI_CLASSIFIER_LABEL_COUNT; i++) {
    const char *label = result.classification[i].label;
    float score = result.classification[i].value;
    Serial.printf("  %s: %.2f\n", label, score);
    if (score < THRESHOLD) continue;
    if (strcmp(label, "light_on") == 0)  digitalWrite(RELAY_PIN, HIGH);
    if (strcmp(label, "light_off") == 0) digitalWrite(RELAY_PIN, LOW);
  }
}

If your relay module is active-LOW (relay clicks when the pin is LOW), swap HIGH and LOW in the last two lines.

Testing and tuning

  • Open the Serial Monitor at 115200 baud. When you see Listening…, say the command clearly from about 1 metre.
  • Watch the scores. If the right word only reaches 0.6–0.7, lower THRESHOLD to 0.70 or record more samples in the same room.
  • If the light switches by itself (false triggers), raise the threshold to 0.90 and add more noise and unknown samples.
  • The >> 14 shift sets the microphone gain. Use >> 12 if your voice is too quiet, >> 16 if it clips.

Upgrade idea: this simple version records, then thinks, so a word spoken during the short “thinking” gap can be missed. Edge Impulse’s continuous microphone examples solve that with double buffering – try them once this version works.

Common problems

ProblemFix
All scores stay near the noise labelCheck SCK/WS/SD wiring and that L/R is tied to GND; print a few raw samples to see if they change when you speak
Compile error: ESP_I2S.h not foundUpdate the ESP32 board package to version 3.x in Boards Manager
Board resets when the relay switchesPower the relay from a separate 5 V supply or add a 470 µF capacitor; use an opto-isolated relay module
Works for you but not for othersRecord more voices: men, women, children, different distances
“Out of memory” during build or runEnable PSRAM in Tools, or reduce the model size in Edge Impulse (choose a smaller network)

For general hardware problems, see how to troubleshoot a dead circuit board and how to test components with a multimeter.

Where to go next

Once the voice light works, you have the full AI workflow in your hands: collect data → train → deploy → tune. The same steps can protect a water pump: see our AI smart pump protector with ESP32. For more ideas, browse 15 Edge AI final-year project ideas.

Frequently asked questions

Is this real AI or just voice recording?

It is real machine learning: a neural network trained on your examples runs on the ESP32 and makes the decision itself. Nothing is matched against a recording, and nothing goes to the internet.

Can I use Bangla words?

Yes. The model learns from your recordings, so any language works. Choose two short words that sound clearly different.

Can I use a normal ESP32 or ESP32-C3?

A small keyword model can run on a classic ESP32 too, but the ESP32-S3 has more memory options and faster AI maths, so it is the easier choice for beginners.

How many commands can it learn?

Start with two. Five or six commands are possible, but each one needs more samples and the accuracy drops if words sound similar.

Need a voice-controlled board for your product or project? We design custom PCBs, firmware and complete working prototypes for students and companies. Message us on WhatsApp or use the contact form. You can write in Bangla or English.


MKDas

Mithun K. Das. B.Sc. in Electrical and Electronic Engineering (EEE) from KUET. Senior Embedded Systems Designer at a leading international company. Welcome to my personal blog! I share articles on various electronics topics, breaking them down into simple and easy-to-understand explanations, especially for beginners. My goal is to make learning electronics accessible and enjoyable for everyone. If you have any questions or need further assistance, feel free to reach out through the Contact Us page. Thank you for visiting, and happy learning!

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *