From 69d38b02e54359d6b751b0fb941f47e6c6e220d7 Mon Sep 17 00:00:00 2001
From: Georgi Kostov
Date: Thu, 27 Nov 2025 22:53:23 +0200
Subject: [PATCH] Exposed the camera clock setting
---
examples/AsyncCam/handlers.cpp | 3 +++
src/esp32cam/config.cpp | 12 ++++++++++++
src/esp32cam/config.hpp | 3 +++
3 files changed, 18 insertions(+)
diff --git a/examples/AsyncCam/handlers.cpp b/examples/AsyncCam/handlers.cpp
index 69fda3c..243cb94 100644
--- a/examples/AsyncCam/handlers.cpp
+++ b/examples/AsyncCam/handlers.cpp
@@ -18,6 +18,7 @@ static const char FRONTPAGE[] = R"EOT(
%vflip%
%rawGma%
%lensCorrection%
+%xclk%
@@ -138,6 +139,7 @@ rewriteFrontpage(const esp32cam::Settings& s, const String& var) {
SHOW_INT(brightness, -2, 2)
SHOW_INT(contrast, -2, 2)
SHOW_INT(saturation, -2, 2)
+ SHOW_INT(xclk, 6, 24)
SHOW_BOOL(hmirror)
SHOW_BOOL(vflip)
SHOW_BOOL(rawGma)
@@ -166,6 +168,7 @@ handleUpdate(AsyncWebServerRequest* req) {
SAVE_INT(gain);
SAVE_INT(lightMode);
SAVE_INT(specialEffect);
+ SAVE_INT(xclk);
SAVE_BOOL(hmirror);
SAVE_BOOL(vflip);
SAVE_BOOL(rawGma);
diff --git a/src/esp32cam/config.cpp b/src/esp32cam/config.cpp
index f7c864f..22c6146 100644
--- a/src/esp32cam/config.cpp
+++ b/src/esp32cam/config.cpp
@@ -112,6 +112,7 @@ CameraClass::status() const {
result.vflip = ss.vflip != 0;
result.rawGma = ss.raw_gma != 0;
result.lensCorrection = ss.lenc != 0;
+ result.xclk = sensor->xclk_freq_hz / 1000000U;
return result;
}
@@ -155,6 +156,7 @@ CameraClass::update(const Settings& settings, int sleepFor) {
CHECK_RANGE(lightMode, -1, 4);
CHECK_RANGE(specialEffect, 0, 6);
CHECK_RANGE(gain, -128, 31);
+ CHECK_RANGE(xclk, 6, 24);
UPDATE3(framesize, settings.resolution.as(), framesize_t);
UPDATE1(brightness);
@@ -184,6 +186,16 @@ CameraClass::update(const Settings& settings, int sleepFor) {
UPDATE2(wb_mode, settings.lightMode);
}
+ int prev = sensor->xclk_freq_hz / 1000000U, desired = settings.xclk;
+ if (prev != desired) {
+ int res = sensor->set_xclk(sensor, LEDC_TIMER_0, desired);
+ ESP32CAM_LOG("update xclk %d => %d %s", prev, desired,
+ res == 0 ? "success" : "failure");
+ if (res != 0) {
+ return false;
+ }
+ }
+
#undef CHECK_RANGE
#undef UPDATE4
#undef UPDATE3
diff --git a/src/esp32cam/config.hpp b/src/esp32cam/config.hpp
index 77f7e2b..2dabf41 100644
--- a/src/esp32cam/config.hpp
+++ b/src/esp32cam/config.hpp
@@ -94,6 +94,9 @@ struct Settings {
* - AGC enabled: -2,-4,-8,-16,-32,-64,-128, which corresponds to 2x ~ 128x gain.
*/
int8_t gain;
+
+ /** @brief Camera clock value in MHz. Recommended to be between 6 and 24. */
+ int8_t xclk;
/** @brief Image light mode. */
LightMode lightMode;