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

Hi there,

So , Finally some progress, won’t be long now.

Here is what I got.
using this test code, and the BSP 2.0.14 I can see the Lens move out and in and on the serial monitor if you move it it is in AF mode, check out the status coming back… ALL GOOD!

Try it on yours I’ll upload a video, in the mean time. :+1:

#include "esp_camera.h"
#include "ESP32_OV5640_AF.h"

// #elif defined(CAMERA_MODEL_XIAO_ESP32S3)
#define PWDN_GPIO_NUM     -1
#define RESET_GPIO_NUM    -1
#define XCLK_GPIO_NUM     10
#define SIOD_GPIO_NUM     40
#define SIOC_GPIO_NUM     39
#define Y9_GPIO_NUM       48
#define Y8_GPIO_NUM       11
#define Y7_GPIO_NUM       12
#define Y6_GPIO_NUM       14
#define Y5_GPIO_NUM       16
#define Y4_GPIO_NUM       18
#define Y3_GPIO_NUM       17
#define Y2_GPIO_NUM       15
#define VSYNC_GPIO_NUM    38
#define HREF_GPIO_NUM     47
#define PCLK_GPIO_NUM     13

OV5640 ov5640 = OV5640();

void setup() {
  Serial.begin(9600);

  camera_config_t config;
  config.ledc_channel = LEDC_CHANNEL_0;
  config.ledc_timer = LEDC_TIMER_0;
  config.pin_d0 = Y2_GPIO_NUM;
  config.pin_d1 = Y3_GPIO_NUM;
  config.pin_d2 = Y4_GPIO_NUM;
  config.pin_d3 = Y5_GPIO_NUM;
  config.pin_d4 = Y6_GPIO_NUM;
  config.pin_d5 = Y7_GPIO_NUM;
  config.pin_d6 = Y8_GPIO_NUM;
  config.pin_d7 = Y9_GPIO_NUM;
  config.pin_xclk = XCLK_GPIO_NUM;
  config.pin_pclk = PCLK_GPIO_NUM;
  config.pin_vsync = VSYNC_GPIO_NUM;
  config.pin_href = HREF_GPIO_NUM;
  config.pin_sscb_sda = SIOD_GPIO_NUM;
  config.pin_sscb_scl = SIOC_GPIO_NUM;
  config.pin_pwdn = PWDN_GPIO_NUM;
  config.pin_reset = RESET_GPIO_NUM;
  config.xclk_freq_hz = 20000000;
  config.pixel_format = PIXFORMAT_JPEG;
  config.frame_size = FRAMESIZE_VGA;
  config.jpeg_quality = 10;
  config.fb_count = 2;

  esp_err_t err = esp_camera_init(&config);
  if (err != ESP_OK) {
    Serial.printf("Camera init failed with error 0x%x", err);
    return;
  }

  sensor_t* sensor = esp_camera_sensor_get();
  ov5640.start(sensor);

  if (ov5640.focusInit() == 0) {
    Serial.println("OV5640_Focus_Init Successful!");
  }

  if (ov5640.autoFocusMode() == 0) {
    Serial.println("OV5640_Auto_Focus Successful!");
  }
}

void loop() {

  uint8_t rc = ov5640.getFWStatus();
  Serial.printf("FW_STATUS = 0x%x\n", rc);

  if (rc == -1) {
    Serial.println("Check your OV5640");
  } else if (rc == FW_STATUS_S_FOCUSED) {
    Serial.println("Focused!");
  } else if (rc == FW_STATUS_S_FOCUSING) {
    Serial.println("Focusing!");
  } else {
  }

  camera_fb_t* fb = esp_camera_fb_get();

  if (!fb) {
    Serial.println("Camera capture failed");
    esp_camera_fb_return(fb);
    return;
  }

  if (fb->format != PIXFORMAT_JPEG) {
    Serial.println("Non-JPEG data not implemented");
    esp_camera_fb_return(fb);
    return;
  }

  // Draw Image on the display or Send Image to the connected device!
  // With (fb->buf, fb->len);
  esp_camera_fb_return(fb);
}

I’m using the LIB from here;

here is the serial output, put your hand in front and you’ll see it AF like a BOSS :muscle: :stuck_out_tongue_winking_eye:

OV5640_Focus_Init Successful!
OV5640_Auto_Focus Successful!
FW_STATUS = 0x10
Focused!
FW_STATUS = 0x10
Focused!
FW_STATUS = 0x10
Focused!
FW_STATUS = 0x20
FW_STATUS = 0x20
FW_STATUS = 0x20
FW_STATUS = 0x20
FW_STATUS = 0x20
FW_STATUS = 0x20
FW_STATUS = 0x20
FW_STATUS = 0x20
FW_STATUS = 0x20
FW_STATUS = 0x20
FW_STATUS = 0x20
FW_STATUS = 0x20

here is video…LOL :grin:

HTH
GL :slight_smile: PJ :v:

1 Like