ESP cam — Esp32 Cam simulation

An interactive Esp32 Cam circuit simulation you can run free in your browser on Velxio, by user_00.

Components

Sketch code

// ESP32-CAM live webcam preview on ILI9341 320×240 TFT
// Click "Camera" in the canvas header → grant permission → see your
// face on the TFT.
//
// Wiring (already done by this example's diagram):
//   ILI9341 ↔ ESP32-CAM
//   CS  → 15   RST → 2    D/C → 14
//   MOSI→ 13   SCK → 12   MISO unused

#include "esp_camera.h"
#include "img_converters.h"   // jpg2rgb565 — built into esp32-camera
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>

// Camera pins (AI-Thinker)
#define PWDN_GPIO_NUM     32
#define RESET_GPIO_NUM    -1
#define XCLK_GPIO_NUM      0
#define SIOD_GPIO_NUM     26
#define SIOC_GPIO_NUM     27
#define Y9_GPIO_NUM       35
#define Y8_GPIO_NUM       34
#define Y7_GPIO_NUM       39
#define Y6_GPIO_NUM       36
#define Y5_GPIO_NUM       21
#define Y4_GPIO_NUM       19
#define Y3_GPIO_NUM       18
#define Y2_GPIO_NUM        5
#define VSYNC_GPIO_NUM    25
#define HREF_GPIO_NUM     23
#define PCLK_GPIO_NUM     22

// TFT pins
#define TFT_CS    15
#define TFT_DC    14
#define TFT_RST    2
#define TFT_MOSI  13
#define TFT_SCK   12

SPIClass tftSPI(VSPI);
Adafruit_ILI9341 tft = Adafruit_ILI9341(&tftSPI, TFT_DC, TFT_CS, TFT_RST);

// 160×120 preview centered in the 320×240 TFT. After the worker
// added batched spi_batch WS messages, transferring 38 KB/frame is
// no longer the dominant cost in the emulator.
#define PREVIEW_W 160
#define PREVIEW_H 120
#define PREVIEW_X  80
#define PREVIEW_Y  60
static uint8_t rgbBuf[PREVIEW_W * PREVIEW_H * 2];

#define STATUS_REFRESH_EVERY 5

uint32_t frame_count = 0, decode_fails = 0, null_fb = 0;
unsigned long start_ms = 0;

void draw_status_bar(uint32_t bytes_in, bool decoded) {
  tft.fillRect(0, 0, 320, PREVIEW_Y, ILI9341_BLACK);
  tft.setTextSize(1);
  tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
  tft.setCursor(8, 6);
  tft.print("VELXIO ESP32-CAM live preview");
  tft.setCursor(8, 22);
  tft.printf("frame %4u   %4u B   %s",
             (unsigned)frame_count, (unsigned)bytes_in,
             decoded ? "decode OK   " : "decode FAIL ");
  unsigned long elapsed = millis() - start_ms;
  float fps = elapsed > 0 ? (1000.0f * frame_count) / (float)elapsed : 0.0f;
  tft.setCursor(8, 38);
  tft.printf("fps %4.1f   fails %u   nulls %u",
             fps, (unsigned)decode_fails, (unsigned)null_fb);
  tft.fillCircle(308, 14, 6,
    (frame_count & 1) ? ILI9341_GREEN : ILI9341_DARKGREEN);
}

void setup() {
  Serial.begin(115200);
  delay(500);
  Serial.println("=== ESP32-CAM + ILI9341 live preview ===");

  tftSPI.begin(TFT_SCK, -1, TFT_MOSI, TFT_CS);
  tft.begin();
  tft.setRotation(1);
  tft.fillScreen(ILI9341_NAVY);
  tft.setTextSize(3);
  tft.setTextColor(ILI9341_WHITE);
  tft.setCursor(20, 10); tft.print("VELXIO");
  tft.setTextSize(1);
  tft.setCursor(20, 38);
  tft.setTextColor(ILI9341_CYAN);
  tft.print("ESP32-CAM live preview");

  camera_config_t cfg = {};
  cfg.ledc_channel = LEDC_CHANNEL_0;
  cfg.ledc_timer   = LEDC_TIMER_0;
  cfg.pin_d0 = Y2_GPIO_NUM; cfg.pin_d1 = Y3_GPIO_NU

More projects by user_00