Prerequisites
Before you begin, make sure you have:Hardware Ready
- ESP32-S2 or ESP32-S3 development board with USB host support
- USB camera (UVC compatible)
- USB cable for connecting camera to ESP32
- Power supply (camera may need external power)
If you haven’t installed the library yet, see Installation.
Your First UVC Camera Application
Let’s create a simple application that captures video frames from a USB camera.Step 1: Create a New Sketch
Create a new Arduino sketch and include the library:Step 2: Define the Frame Callback
Create a callback function that will be called when each new frame arrives:Step 3: Complete Setup Code
Here’s the completesetup() function:
Step 4: Upload and Test
- Connect your ESP32 board to your computer
- Select the correct board and port in Arduino IDE
- Upload the sketch
- Connect a USB camera to the ESP32’s USB host port
- Open Serial Monitor (115200 baud)
Expected Output
You should see output like this in the Serial Monitor:Understanding the Configuration
Let’s break down the key configuration parameters:Accepts any resolution the camera supports. You can also specify exact values like 640, 480, etc.
Sets the frame rate to 15 FPS. Other options:
FRAME_INTERVAL_FPS_5, FRAME_INTERVAL_FPS_10, FRAME_INTERVAL_FPS_20, FRAME_INTERVAL_FPS_30Two buffers for double-buffering USB transfers. Size must be >= one frame size. 55KB is a safe default for most resolutions.
Buffer to store the complete assembled frame. Size must be >= one frame size.
Buffer Size Guidelines
Choose buffer sizes based on your resolution:| Resolution | Frame Size (MJPEG) | Recommended Buffer |
|---|---|---|
| 320x240 | ~15-20 KB | 25 KB |
| 640x480 | ~30-40 KB | 55 KB |
| 800x600 | ~50-60 KB | 80 KB |
| 1280x720 | ~80-100 KB | 120 KB |
Next Steps
Advanced Features
Learn about suspend/resume, dynamic resolution changes, and advanced configuration
Audio Streaming
Add USB audio (microphone/speaker) to your application
Memory Optimization
Optimize buffer allocation and memory usage
API Reference
Explore the complete API documentation
Troubleshooting
Camera not detected
Camera not detected
Possible causes:
- Camera not powered properly
- Incorrect USB wiring
- Camera not UVC compatible
- Check USB D+/D- connections (GPIO 19/20)
- Ensure camera has sufficient power
- Try a different camera
- Check serial output for error messages
Assertion failed: _xferBufferA != NULL
Assertion failed: _xferBufferA != NULL
Cause: Not enough heap memory available for buffer allocation.Solutions:
- Reduce buffer sizes
- Use ESP32-S3 with PSRAM
- Check other memory allocations in your code
No frames received
No frames received
Possible causes:
- Camera requires specific resolution/format
- Insufficient USB bandwidth
- Timing issues
- Try specifying exact resolution instead of
FRAME_RESOLUTION_ANY - Reduce frame rate
- Add longer delay after
start()beforeconnectWait()