Open-source ESP32 Lens Encoder for Virtual Production — FreeD Protocol over WiFi
A low-cost, open-source lens encoder system that reads Zoom and Focus positions from a broadcast or photo lens using two incremental rotary encoders, and transmits the data wirelessly to a graphics engine (Unreal Engine, Aximmetry, Ross Voyager, Ventuz, etc.) via the industry-standard FreeD UDP protocol.
Designed and developed as a bachelor's thesis at Hochschule Mittweida, 2023.
In virtual production and Augmented Reality (AR) broadcast workflows, the graphics engine must know the exact lens parameters of the real camera — zoom (focal length) and focus (focus distance) — to render the correct perspective and depth of field for virtual elements.
Professional systems (ncam, Mo-Sys, Stype …) cost tens of thousands of euros. OpenLensEncoder achieves the same data stream for under €30 in components:
Camera Lens → Rotary Encoder → ESP32 ──WiFi──► FreeD UDP port 1112
(Zoom + Focus) (this firmware) Unreal Engine / Live Link
Aximmetry / Ross / Ventuz
The FreeD packet contains a standard 29-byte UDP message with zoom, focus and checksum fields. Camera position (pan/tilt/roll/xyz) is set to zero — the lens encoder only provides the optical data; a separate tracking system handles camera motion.
| Component | Quantity | Notes |
|---|---|---|
| ESP32 Dev Module | 1 | Any ESP32 with WiFi |
| Incremental Rotary Encoder | 2 | With push-button (SW pin); 0.5° resolution recommended |
| Mod 0.8 timing gear | 2 | 32 mm / 40 teeth (see WIRING.md) |
| Lanparte-compatible follow-focus belt | 1–2 | Connects gear to lens ring |
| Resin-printed mounting bracket | 1 | Elegoo Mars 3 or equivalent |
Total cost: approx. €25–35 depending on sourcing.
- Arduino IDE 2.x (or PlatformIO)
- ESP32 Arduino Core — add board URL in preferences:
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json - AiEsp32RotaryEncoder library — install via Library Manager (search:
ESP32 rotary) - Preferences library — included in the ESP32 Arduino core
git clone https://github.com/MarcusMa-dotcom/OpenLensEncoder.git
cd OpenLensEncoder/LensEncoderOpen LensEncoder.ino and edit:
#define WIFI_SSID "your-network-name"
#define WIFI_PASSWORD "your-wifi-password"
#define FREED_DESTINATION_IP "192.168.1.100" // IP of the graphics engine PCThe graphics engine must listen on UDP port 1112 (standard FreeD port).
- Open
LensEncoder/LensEncoder.inoin Arduino IDE - Select Board:
ESP32 Dev Module - Select the correct Port
- Click Upload
See docs/WIRING.md for the complete pin assignment.
Default GPIO mapping:
| Signal | GPIO |
|---|---|
| Zoom CLK (A) | 21 |
| Zoom DT (B) | 22 |
| Zoom SW | 23 |
| Focus CLK (A) | 16 |
| Focus DT (B) | 17 |
| Focus SW | 18 |
| Focus VCC | 19 |
Calibration teaches the firmware the physical travel range of each lens ring. Calibration data is saved to NVS flash and survives reboots.
Zoom encoder:
- Turn the lens zoom ring to the minimum focal length (widest angle).
- Press the Zoom encoder push-button once → minimum endpoint saved.
- Turn to the maximum focal length (most tele).
- Press the button again → maximum saved. Encoder is calibrated.
Focus encoder: repeat the same process for the focus ring (near → far).
Press the button a third time on any encoder to clear calibration and start over.
OpenLensEncoder/
├── LensEncoder/
│ ├── LensEncoder.ino # Main Arduino sketch (setup + loop)
│ ├── RotaryEncoder.h # Custom encoder class declaration
│ ├── RotaryEncoder.cpp # Encoder logic, calibration, NVS persistence
│ ├── FreeDClient.h # FreeD UDP client declaration
│ └── FreeDClient.cpp # FreeD packet construction and WiFi transmission
├── docs/
│ └── WIRING.md # GPIO wiring diagram and lens coupling details
├── .gitignore
└── README.md
FreeD is a broadcast-industry standard protocol developed by Vinten Radamec for transmitting real-time camera tracking data over UDP.
Each packet is 29 bytes at destination port 1112:
| Field | Bytes | Description |
|---|---|---|
D1 |
1 | Message type (always 0xD1) |
CA |
1 | Camera ID |
PH PM PL |
3 | Pan angle (24-bit signed, big-endian) |
TH TM TL |
3 | Tilt angle |
RH RM RL |
3 | Roll angle |
XH XM XL |
3 | X position |
YH YM YL |
3 | Y position |
HH HM HL |
3 | Height (Z position) |
ZH ZM ZL |
3 | Zoom |
FH FM FL |
3 | Focus |
SH SL |
2 | Spare / user-defined (16-bit) |
CK |
1 | Checksum |
Checksum formula:
CK = (0x40 − byte1 − byte2 − … − byte28) mod 256
Zoom and Focus values range from 0 to 4095 (12-bit) after calibration mapping.
In Unreal Engine, FreeD data is received via the Live Link plugin:
Plugins → Live Link → Add Source → FreeD.
After calibration, raw encoder counts are mapped linearly to the range [0, 4095]:
mapped = map(rawValue, rotary_min, rotary_max, 0, 4095)
If the physical rotation direction is inverted (CCW yields increasing raw values), the firmware automatically compensates:
if (rotary_min < 0)
mapped = 4095 - mapped;
The main loop runs at 25 Hz (delay(40) ms), matching the 1080/25i broadcast standard.
For tighter frame-accurate sync, the delay() can be replaced with an ISR triggered
by a hardware sync signal extracted from the video stream using a
Texas Instruments LMH1981 Multi-Format Video Sync Separator IC.
To test encoder readings without WiFi, comment out the define in LensEncoder.ino:
// #define ENABLE_FREED_CLIENTEncoder values will be printed to the Serial Monitor at 115200 baud.
- OLED display — show calibration status and current zoom/focus values on-device
- Gyroscope integration — add IMU (e.g. MPU-6050) for Roll/Pitch/Yaw tracking
- Fujinon direct interface — read 10–14 bit encoder signals from the lens 20-pin HIROSE connector directly, bypassing external gears (see WIRING.md)
- Iris / Aperture — third encoder or direct ADC for f-stop data via FreeD spare bytes
- Hardware sync — ISR-based packet transmission locked to LMH1981 video sync output
- Web UI — browser-based calibration and configuration over mDNS
MIT License — free to use, modify, and distribute for any purpose.
This project was developed as part of a Diplom thesis at the Faculty of Applied Computer and Biosciences, Hochschule Mittweida, 2023.
Special thanks to the communities at various forums and Discord servers whose expertise helped shape the technical implementation.