OV5640 Camera for XIAO ESP32S3 Sense - How to use Autofocus?

The Seeeduino Store page with the OV5640 camera description claims the following:

The sensor comes with an auto-focusing function to help users take clear photos and videos more easily. The auto-focusing function can automatically adjust the focus distance, ensuring that the captured images and videos are always clear and sharp.

However, I cannot get it to work. Seeed Studio doesn’t seem to offer any sample code, and I also tried the “OV5640 Auto Focus for ESP32 Camera” library, modifying the pins for the XIAO, but the auto-focus functions failed.

There is an “AF_VCC” listed on the expansion board schematic, so I assume the power for the voice coil motor is being supplied to the module.

Can someone explain to me how to get the autofocus working, please, preferably with a working code sample? I’m reviewing the ESP32S3 Sense board for my YouTube channel, and wish to address this issue in the video.

Thanks!

1 Like

Hi,
The camera’s autofocus function does not require additional code to implement, the camera itself comes with an autofocus algorithm

And unbelievably, this thing has a focus motor.

Hi, I have ESP32S3 Sense with a ov2640 and using a sample micropython the camera works, but if you change the OV2640 for the OV5640 the code fails, Although the Seeed wiki indicates that the OV5640 works with the same code as the OV2640. Can you provide sample code either Micropython or Arduino IDE. Thanks

Hello,
Regarding the Arduino IDE code samples provided in this Wiki, all the Arduino IDE code shown is applicable to both camera models without any modifications.

When you mentioned the MicroPython examples, are you referring to those code samples?

Have you tried using the MicroPython code directly without any changes on your camera to see if they work?

Hi, thanks for your answer, I did try it in Arduino and the OV5640 works, but there is a problem with the camera, it displays green lines as seen in attached picture.

Regarding Micropython, I did try the examples there except for the open CV, when I swapped the camera a I got a failed response on the cam = camera.init() statement, and the program failed.

Hi! I wanted to revive this thread as I’m having trouble getting the autofocus on my OV5640 to work, and I haven’t been able to find any working code examples online.

I’ve been testing the camera using the standard CameraWebServer demo, and I’ve noticed that the focus remains fixed regardless of changes in lighting conditions, resolution, or clock frequency. I also don’t see or hear the autofocus motor moving at all during this demo or when trying other camera demos from the Seeed Studio Wiki.

I also tried the “OV5640 Auto Focus for ESP32 Camera” library mentioned above. During initialization, the autofocus motor moved briefly for the first time, but didn’t work afterwards – this leads me to think that there’s probably something missing in the camera definition in the web server demo.

Has anyone successfully implemented the autofocus functionality with this camera module?

We are sorting out the data and will send it to you when it is finished. I hope it can be fully used in the autofocus function

Hi - I have the same issue and opened a support ticket. I was told that they do not have AF working. Hoping this new driver they are working on solves the issue.

Hi! We are working on this issue and will send it out later.

Is there an ETA to this fix?

We’ll send it out in about two weeks

Hi, any updates on the autofocus fix?

The current test results can prove that the OV5640 has the function of autofocus, the condition is that you need to add additional code to drive the motor inside the motor, we are improving the code base, will be launched later!

1 Like

Hi - is there an updated ETA on this driver?

Hello! Our team is currently working hard to optimize the functions of the repository to ensure a smoother and more stable experience for users after the official launch. We will synchronize the progress of the driver update to the project wiki page as soon as it is developed and verified internally.

1 Like

Is there an updated ETA on the autofocus driver? It would be helpful if you could share some guidance on how to drive the motor as the datasheet is not entirely clear.

quote=“kaloyan, post:7, topic:272485”]
OV5640
[/quote]

Hi there,

Not sure if it’s helpful , or “While You wait” but I managed to put together some edits to the Streaming web server code to use the boot button on the Xiao S3 to manually trigger AF on the camera. Unfortunately the unit I ordered is on back order so I’m limited in testing it, I get a serial message that it’s “Triggering OV5640 autofocus manually…” as programed and I see th the I2C commands going out,
BTW, I’m talking directly to the hardware NOT the ESP_camera.h FYI.

It boots and connects to the wifi, I can open the webpage and see the camera image A text board at any distance , when pressing the boot button will AF the camera.
LMK if your capable to test it and I’ll post it.
or Wait for the Seeedineers to do there thing.:crossed_fingers:

HTH
GL :slight_smile: PJ

FYI , it’s not that hard if you can compile and run the standard web page stream example.

void loop() {
  if (digitalRead(BOOT_BUTTON_PIN) == LOW) {
    unsigned long current_time = millis();
    if (current_time - last_af_time > af_debounce_time) {
      trigger_ov5640_autofocus();
      last_af_time = current_time;
    }
  }

  delay(100);
}

void trigger_ov5640_autofocus() {
  Serial.println("Triggering OV5640 autofocus manually...");
  i2cWriteReg(OV5640_ADDR, 0x3000, 0x20);
  delay(10);
  i2cWriteReg(OV5640_ADDR, 0x3000, 0x00);
  delay(10);
  i2cWriteReg(OV5640_ADDR, 0x3022, 0x08);
  delay(500);
}

void i2cWriteReg(uint8_t addr, uint16_t reg, uint8_t data) {
  Wire.beginTransmission(addr);
  Wire.write((reg >> 8) & 0xFF);
  Wire.write(reg & 0xFF);
  Wire.write(data);
  Wire.endTransmission();
}

it’s basically this above … including Wire of course. :+1:

Typical OV5640 Useful Commands (Registers)

Register Address Purpose Example
0x3008 System control Soft reset (0x82)
0x3103 Clock selection Set input clock
0x3022 Autofocus command Start AF (0x08)
0x3000 Power down AF module Enable/disable AF
0x3400-0x3406 Manual white balance Adjust RGB gains
0x3500-0x3503 Exposure control Manual exposure settings
0x3A00 AE/AGC control Enable Auto Exposure / Gain
0x5300-0x5396 Color matrix settings Image tuning (sharpness, contrast, etc.)
0x3600-0x36FF ISP controls Low-level tweaks to internal processing
0x3810-0x3815 Cropping/windowing Zooming into sub-areas

It’s all in the Data sheet…

Thank you for sharing. We will give it a go :slightly_smiling_face:

1 Like

Hi there,

Please do and hit us back, the unit s I ordered should arrive very soon.
this will help make it very usable.

GL :slight_smile: PJ :v: