Skip to content

About

Open-source ESP32 Lens Encoder for Virtual Production — FreeD Protocol over WiFi

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

OpenLensEncoder

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.


What it does

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.


Hardware

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.


Software Requirements

  • 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

Quick Start

1. Clone the repository

git clone https://github.com/MarcusMa-dotcom/OpenLensEncoder.git
cd OpenLensEncoder/LensEncoder

2. Configure WiFi and FreeD destination

Open 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 PC

The graphics engine must listen on UDP port 1112 (standard FreeD port).

3. Flash the ESP32

  1. Open LensEncoder/LensEncoder.ino in Arduino IDE
  2. Select Board: ESP32 Dev Module
  3. Select the correct Port
  4. Click Upload

4. Wire the encoders

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

5. Calibrate the encoders

Calibration teaches the firmware the physical travel range of each lens ring. Calibration data is saved to NVS flash and survives reboots.

Zoom encoder:

  1. Turn the lens zoom ring to the minimum focal length (widest angle).
  2. Press the Zoom encoder push-button once → minimum endpoint saved.
  3. Turn to the maximum focal length (most tele).
  4. 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.


Project Structure

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 Protocol

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.


Encoder Value Mapping

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;

Transmission Rate

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.


Disabling WiFi (debug mode)

To test encoder readings without WiFi, comment out the define in LensEncoder.ino:

// #define ENABLE_FREED_CLIENT

Encoder values will be printed to the Serial Monitor at 115200 baud.


Roadmap / Future Improvements

  • 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

License

MIT License — free to use, modify, and distribute for any purpose.


Acknowledgements

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.

About

Open-source ESP32 Lens Encoder for Virtual Production — FreeD Protocol over WiFi

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages