Skip to content
pschatzmann edited this page Nov 25, 2026 · 5 revisions

Plain frames

#include "TinyUVC.h"

UVCDevice uvc;

void setup() {
  uvc.addResolution(UVCFormat::YUY2, 160, 120, {15, 10});   // two frame rates
  uvc.addResolution(UVCFormat::MJPEG, 640, 480, 15, 60000);  // max JPEG size
  uvc.begin();
}

void loop() {
  if (uvc.isReady()) {
    UVCStreamSettings s = uvc.settings();   // what the host selected
    uvc.writeFrame(data, len, PixelFormat::RGB565_LE);
  }
}

The host picks format, resolution and frame rate; your sketch reads it with settings() or is told with setStreamCallback().

writeFrame() rejects frames that do not match the host's selection: MJPEG needs PixelFormat::MJPEG, YUY2 needs exactly width x height pixels in any raw format (RGB565 in both byte orders, RGB888, BGR888, grayscale: they are converted to YUY2 while they are sent). The data must stay valid until isReady() is true again.

With TinyCamera

#include "TinyCameraPins.h"
#include "TinyUVCCamera.h"

UVCDevice uvc;
UVCCamera camera(uvc);

void setup() {
  camera_config_t cfg = TinyCamera::defaultConfig();
  setPinsXiaoEsp32S3(cfg);
  cfg.pixel_format = PIXFORMAT_JPEG;          // -> MJPEG, RGB565 -> YUY2
  camera.addFrameSize(FRAMESIZE_QVGA);
  camera.addFrameSize(FRAMESIZE_VGA);
  camera.begin(cfg);                          // starts uvc as well
}

void loop() { camera.copy(); }

The camera is reconfigured when the host switches resolution or format.

With arduino-audio-tools

#include "AudioTools.h"
#include "AudioTools/Video/CodecJPEG.h"
#include "TinyUVCAudioTools.h"

UVCDevice uvc;
UVCVideoOutput out(uvc, PixelFormat::MJPEG);
MJPEGEncoder encoder(out);       // RGB565 in, JPEG to the webcam

The video classes of arduino-audio-tools need FreeRTOS: on RP2040/RP2350 select Tools > Operating System > "FreeRTOS SMP" and add #include "AudioTools/Concurrency/RTOS.h" after AudioTools.h (see the AudioTools examples); on STM32 this needs the STM32FreeRTOS library.

UVCVideoOutput collects the write() calls of a frame and sends it on flush() (a complete JPEG in a single write() is sent right away). setPacing(true) plays files at the frame rate the host selected, setTimeout() lets it wait for the USB transfer instead of dropping frames. UVCVideoCopy streams any VideoFrameSource.

Examples

Example What it shows
TestPatternWebcam No camera: moving color bars as YUY2 - start here
ESP32CameraWebcam ESP32-S3 camera board as MJPEG webcam with two resolutions
RP2040CameraWebcam Pico + OV7670/OV2640, RGB565 converted to YUY2
STM32CameraWebcam WeAct STM32H750 + OV7725 via DCMI
AudioToolsJpegWebcam Software JPEG compression with MJPEGEncoder -> UVCVideoOutput
AudioToolsAviWebcam Plays an MJPEG AVI from SD as webcam picture (DemuxerAVI)
desktop/webcam_simulation The whole chain on the PC, see Desktop

Clone this wiki locally