In this CircuitMagic tutorial you will turn a tiny ESP32-S3 camera into a smart AI camera that finds and locates objects. You train a small FOMO model in Edge Impulse to recognise apples, bottles and cups, export it as an Arduino library, and run it on the board. Once uploaded, detection runs entirely on the ESP32-S3, with no internet connection.

What is AI object detection?
A normal camera captures pixels but has no idea what is in the picture. An AI camera runs a trained model on each frame, so it can tell you what it sees and where it is. It only recognises the objects you trained it on.
Required components
- ESP32-S3 camera board with PSRAM, for example the Seeed Studio XIAO ESP32S3 Sense (used in this guide) or Espressif ESP32-S3-EYE
- OV2640 camera module (included with both boards)
- USB-C data cable (classic ESP32-CAM boards need a USB-to-serial programmer instead)
- Stable 5 V USB power supply, 1 A or more
- Computer with Arduino IDE 2.x
- Free Edge Impulse account

Why ESP32-S3 instead of a classic ESP32-CAM?
- AI vector instructions: the S3 has SIMD extensions that speed up neural-network layers.
- Faster PSRAM: many S3 boards use octal PSRAM; the classic ESP32 only supports quad PSRAM.
- Native USB: most S3 boards program over USB-C with no separate adapter.
How it works
- Camera: the OV2640 captures a 320×240 JPEG frame into PSRAM.
- Image processing: the sketch decodes it and resizes it to the model’s input, for example 96×96 pixels.
- AI model: a FOMO neural network (Faster Objects, More Objects) reads the pixels.
- Object detection: FOMO outputs a grid at 1/8 of the input size, so 12×12 for a 96×96 input. Each cell holds the chance that an object’s centre is there.
- Result: the sketch prints a label, a centre point and a confidence score for each object.
FOMO finds centres, not boxes. The Arduino library still reports a bounding_boxes array, but for FOMO each “box” is just the grid cell where it found an object’s centre. That is why the sketch prints the centre point. Skipping box regression is what makes FOMO small enough for microcontrollers.

Step 1: Arduino IDE setup
- In File › Preferences, add this board manager URL:
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json - In Tools › Board › Boards Manager, install esp32 by Espressif Systems version 2.0.17.
- Select Board: XIAO_ESP32S3, PSRAM: OPI PSRAM and USB CDC On Boot: Enabled.
Why 2.0.17 and not 3.x? Edge Impulse tests its ESP32 sketches on Arduino core 2.0.4, and many users report compile errors or a cam_hal: DMA overflow error on 3.x. The XIAO ESP32S3 needs at least 2.0.8, so 2.0.17, the last 2.x release, covers both.
Step 2: Train the model in Edge Impulse
- Create a project (for example “CircuitMagic AI Camera”) and set the labeling method to Bounding boxes (object detection).
- Collect images of apples, bottles and cups. Use your phone through Data acquisition, or save snapshots from the ESP32’s CameraWebServer example and upload them. Vary the angle, distance, lighting and background, and include frames with several objects. Start with a few dozen images per object and keep about 20% as a test set.
- Label each object in the Labeling queue by drawing a box around it. FOMO trains on the centre of each box, so boxes only need to be roughly right.
- Create the impulse: image data 96×96, an Image processing block and an Object Detection (Images) learning block. On the Image page choose Grayscale (smaller, faster) or RGB (better when colour matters), then Generate features.
- Train a FOMO model, for example FOMO (Faster Objects, More Objects) MobileNetV2 0.35. The default settings are a good first try.
- Test it in Model testing on images it has never seen. If accuracy is low, add more varied images and retrain.
- Export: in Deployment, choose Arduino library, keep the quantized (int8) model and click Build. A zip such as
CircuitMagic_AI_Camera_inferencing.zipdownloads.

Step 3: Install the library
In the Arduino IDE choose Sketch › Include Library › Add .ZIP Library… and select the zip. Edge Impulse’s own example is under File › Examples › (your project) › esp32 › esp32_camera, but it is set up for the ESP-EYE. The sketch below already uses the XIAO ESP32S3 Sense camera pins.
Camera pins (XIAO ESP32S3 Sense)
The camera plugs into the board-to-board connector, so there is no wiring. These are the GPIOs the sketch uses:
| Camera signal | GPIO | Camera signal | GPIO |
|---|---|---|---|
| XCLK | 10 | Y9 (D7) | 48 |
| SIOD (SDA) | 40 | Y8 (D6) | 11 |
| SIOC (SCL) | 39 | Y7 (D5) | 12 |
| VSYNC | 38 | Y6 (D4) | 14 |
| HREF | 47 | Y5 (D3) | 16 |
| PCLK | 13 | Y4 (D2) | 18 |
| PWDN | not used (-1) | Y3 (D1) | 17 |
| RESET | not used (-1) | Y2 (D0) | 15 |
Complete Arduino code
Note: this sketch follows the structure of Edge Impulse’s esp32_camera example but has not yet been tested on hardware by CircuitMagic. Change the first #include to match your library’s name.
// CircuitMagic - AI object detection on an ESP32-S3 camera
// Board: XIAO ESP32S3 Sense | Core: esp32 2.0.x | PSRAM: OPI PSRAM
// Model: Edge Impulse FOMO exported as an Arduino library
#include <CircuitMagic_AI_Camera_inferencing.h> // your library's header
#include "edge-impulse-sdk/dsp/image/image.hpp"
#include "esp_camera.h"
#include "img_converters.h"
// XIAO ESP32S3 Sense camera pins
#define PWDN_GPIO_NUM -1
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 10
#define SIOD_GPIO_NUM 40
#define SIOC_GPIO_NUM 39
#define Y9_GPIO_NUM 48
#define Y8_GPIO_NUM 11
#define Y7_GPIO_NUM 12
#define Y6_GPIO_NUM 14
#define Y5_GPIO_NUM 16
#define Y4_GPIO_NUM 18
#define Y3_GPIO_NUM 17
#define Y2_GPIO_NUM 15
#define VSYNC_GPIO_NUM 38
#define HREF_GPIO_NUM 47
#define PCLK_GPIO_NUM 13
#define EI_CAMERA_RAW_FRAME_BUFFER_COLS 320
#define EI_CAMERA_RAW_FRAME_BUFFER_ROWS 240
#define EI_CAMERA_FRAME_BYTE_SIZE 3
#if !defined(EI_CLASSIFIER_SENSOR) || EI_CLASSIFIER_SENSOR != EI_CLASSIFIER_SENSOR_CAMERA
#error "This sketch needs an image model from Edge Impulse."
#endif
static bool camera_ready = false;
static uint8_t *snapshot_buf = nullptr;
static camera_config_t camera_config = {
.pin_pwdn = PWDN_GPIO_NUM,
.pin_reset = RESET_GPIO_NUM,
.pin_xclk = XCLK_GPIO_NUM,
.pin_sscb_sda = SIOD_GPIO_NUM,
.pin_sscb_scl = SIOC_GPIO_NUM,
.pin_d7 = Y9_GPIO_NUM,
.pin_d6 = Y8_GPIO_NUM,
.pin_d5 = Y7_GPIO_NUM,
.pin_d4 = Y6_GPIO_NUM,
.pin_d3 = Y5_GPIO_NUM,
.pin_d2 = Y4_GPIO_NUM,
.pin_d1 = Y3_GPIO_NUM,
.pin_d0 = Y2_GPIO_NUM,
.pin_vsync = VSYNC_GPIO_NUM,
.pin_href = HREF_GPIO_NUM,
.pin_pclk = PCLK_GPIO_NUM,
.xclk_freq_hz = 20000000,
.ledc_timer = LEDC_TIMER_0,
.ledc_channel = LEDC_CHANNEL_0,
.pixel_format = PIXFORMAT_JPEG,
.frame_size = FRAMESIZE_QVGA,
.jpeg_quality = 12,
.fb_count = 1,
.fb_location = CAMERA_FB_IN_PSRAM,
.grab_mode = CAMERA_GRAB_WHEN_EMPTY,
};
bool ei_camera_init() {
if (camera_ready) return true;
esp_err_t err = esp_camera_init(&camera_config);
if (err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x\n", err);
return false;
}
camera_ready = true;
return true;
}
// Capture a frame, decode JPEG to RGB888 and resize to the model input
bool ei_camera_capture(uint32_t img_width, uint32_t img_height, uint8_t *out_buf) {
if (!camera_ready) return false;
camera_fb_t *fb = esp_camera_fb_get();
if (!fb) {
Serial.println("Camera capture failed");
return false;
}
bool converted = fmt2rgb888(fb->buf, fb->len, PIXFORMAT_JPEG, out_buf);
esp_camera_fb_return(fb);
if (!converted) {
Serial.println("JPEG to RGB conversion failed");
return false;
}
if (img_width != EI_CAMERA_RAW_FRAME_BUFFER_COLS ||
img_height != EI_CAMERA_RAW_FRAME_BUFFER_ROWS) {
ei::image::processing::crop_and_interpolate_rgb888(
out_buf, EI_CAMERA_RAW_FRAME_BUFFER_COLS, EI_CAMERA_RAW_FRAME_BUFFER_ROWS,
out_buf, img_width, img_height);
}
return true;
}
// Feed pixels as packed 0xRRGGBB (decoder outputs BGR, so swap bytes)
static int ei_camera_get_data(size_t offset, size_t length, float *out_ptr) {
size_t pixel_ix = offset * 3;
size_t out_ix = 0;
while (length--) {
out_ptr[out_ix++] = (snapshot_buf[pixel_ix + 2] << 16) +
(snapshot_buf[pixel_ix + 1] << 8) +
snapshot_buf[pixel_ix];
pixel_ix += 3;
}
return 0;
}
void setup() {
Serial.begin(115200);
while (!Serial && millis() < 5000) { }
Serial.println("CircuitMagic AI camera - Edge Impulse FOMO");
if (!psramFound()) {
Serial.println("PSRAM not found. Set Tools > PSRAM to OPI PSRAM.");
while (true) delay(1000);
}
if (!ei_camera_init()) {
while (true) delay(1000);
}
snapshot_buf = (uint8_t *)ps_malloc(EI_CAMERA_RAW_FRAME_BUFFER_COLS *
EI_CAMERA_RAW_FRAME_BUFFER_ROWS *
EI_CAMERA_FRAME_BYTE_SIZE);
if (!snapshot_buf) {
Serial.println("Could not allocate the snapshot buffer.");
while (true) delay(1000);
}
}
void loop() {
if (!ei_camera_capture(EI_CLASSIFIER_INPUT_WIDTH, EI_CLASSIFIER_INPUT_HEIGHT, snapshot_buf)) {
delay(500);
return;
}
ei::signal_t signal;
signal.total_length = EI_CLASSIFIER_INPUT_WIDTH * EI_CLASSIFIER_INPUT_HEIGHT;
signal.get_data = &ei_camera_get_data;
ei_impulse_result_t result = { 0 };
EI_IMPULSE_ERROR err = run_classifier(&signal, &result, false);
if (err != EI_IMPULSE_OK) {
Serial.printf("run_classifier failed (%d)\n", err);
return;
}
Serial.printf("Timing: DSP %d ms, inference %d ms\n",
result.timing.dsp, result.timing.classification);
bool found = false;
for (uint32_t i = 0; i < result.bounding_boxes_count; i++) {
ei_impulse_result_bounding_box_t bb = result.bounding_boxes[i];
if (bb.value == 0) continue;
found = true;
// FOMO reports the grid cell where it found a centre
uint32_t cx = bb.x + bb.width / 2;
uint32_t cy = bb.y + bb.height / 2;
Serial.printf("%s (%.2f) at x=%u, y=%u\n", bb.label, bb.value, cx, cy);
}
if (!found) Serial.println("No objects found");
delay(100);
}
Expected output
Open Serial Monitor at 115200 baud and point the camera at your objects. Each detection prints as label (confidence) at x=.., y=... Coordinates are in model-input pixels, so 0–95 for a 96×96 model. The sketch also prints how long processing and inference take, so you can measure your board’s speed.

Troubleshooting
- “either all initializer clauses should be designated…” compile error: you are on Arduino ESP32 core 3.x. Install core 2.0.17.
- Build error mentioning esp_nn: users report that setting
EI_CLASSIFIER_TFLITE_ENABLE_ESP_NNto 0 inei_classifier_config.hbuilds, but inference is slower. Rebuilding the library with the EON Compiler off can also help. - Camera init failed: check the camera ribbon, the pin map and that PSRAM is set to OPI PSRAM.
- cam_hal: DMA overflow: reported on core 3.x. Use core 2.0.x.
- Nothing or the wrong object detected: add more varied training images (angles, light, backgrounds), retrain and export again.
- Brownout resets: use a better USB cable or a 5 V supply rated for 1 A or more.
Try next
Use the detections to drive something: sort fruit with a servo, make a robot car follow a ball, or count items on a shelf. For a project with no training at all, see our AI Door Greeter with ESP32-S3 face detection, and browse more CircuitMagic electronics projects.










