Camera always fails to capture fb (tested with different lenses, power supplies, sketches)

FBMinis
Posts: 15
Joined: Sun Jan 17, 2021 1:08 pm

Camera always fails to capture fb (tested with different lenses, power supplies, sketches)

Postby FBMinis » Tue Oct 31, 2023 8:15 pm

This AI-Thinker ESP32-CAM was kept in its sealed bag until yesterday, when I decided to start a new project.

I was working on something with a different board, so I used the same test setup and sketch to power it ON for the first time. Its fails in every sketch when
esp_camera_fb_get()()
is called.

I tried a different lens with the same result. I tried the lens on another board and it work normally. I also tried with a different power supply, making sure all connection were ok.

This board doesn't show any problem when running other sketches that don't use the lens, like Wifiscan or test PSRAM.

As an example, the following sketch capture and saves a photo to spiffs on every other board I have but this one. The OV2640 seems to be properly initialized, as no error pops up. Also, I tried and i2c scanner sketch and it detected the lens module.

I'm using Arduino 1.8.13 and the latest ESP32 2.0.11

Code: Select all

#include "esp_camera.h"
//#include "soc/soc.h"           // Disable brownour problems
//#include "soc/rtc_cntl_reg.h"  // Disable brownour problems
#include <SPIFFS.h>
#include <FS.h>

int count = 0;

// Photo File Name to save in SPIFFS
#define FILE_PHOTO "/photo.jpg"

// OV2640 camera module pins (CAMERA_MODEL_AI_THINKER)
#define PWDN_GPIO_NUM     32
#define RESET_GPIO_NUM    -1
#define XCLK_GPIO_NUM      0
#define SIOD_GPIO_NUM     26
#define SIOC_GPIO_NUM     27
#define Y9_GPIO_NUM       35
#define Y8_GPIO_NUM       34
#define Y7_GPIO_NUM       39
#define Y6_GPIO_NUM       36
#define Y5_GPIO_NUM       21
#define Y4_GPIO_NUM       19
#define Y3_GPIO_NUM       18
#define Y2_GPIO_NUM        5
#define VSYNC_GPIO_NUM    25
#define HREF_GPIO_NUM     23
#define PCLK_GPIO_NUM     22

void setup() {
  // Serial port for debugging purposes
  Serial.begin(115200);
  Serial.println("Formating SPIFFS");
  if (!SPIFFS.format()) {
    Serial.println("An Error has occurred while formating SPIFFS");
    ESP.restart();
  }
  else {
    delay(500);
    Serial.println("SPIFFS formated successfully");
  }

  if (!SPIFFS.begin(true)) {
    Serial.println("An Error has occurred while mounting SPIFFS");
    ESP.restart();
  }
  else {
    delay(500);
    Serial.println("SPIFFS mounted successfully");
  }

  // Turn-off the 'brownout detector'
  //  WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);

  // OV2640 camera module
  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.pixel_format = PIXFORMAT_GRAYSCALE;

  if (psramFound()) {
    config.frame_size = FRAMESIZE_VGA;
    config.jpeg_quality = 12;
    config.fb_count = 1;
  } else {
    config.frame_size = FRAMESIZE_VGA;
    config.jpeg_quality = 12;
    config.fb_count = 1;
  }
  // Camera init
  esp_err_t err = esp_camera_init(&config);
  if (err != ESP_OK) {
    Serial.printf("Camera init failed with error 0x % x", err);
    ESP.restart();
  }
}

void loop() {
  do {
    count++;
    Serial.print("------ -Test nbr "); Serial.println(count);
    capturePhotoSaveSpiffs();
    delay(5000);
  } while (count < 10);
  Serial.println("------ -The end ");
  while (true) {};
}

// Check if photo capture was successful
bool checkPhoto( fs::FS &fs ) {
  File f_pic = fs.open( FILE_PHOTO );
  unsigned int pic_sz = f_pic.size();
  return ( pic_sz > 100 );
}

// Capture Photo and Save it to SPIFFS
void capturePhotoSaveSpiffs( void ) {
  camera_fb_t * fb = NULL; // pointer
  bool ok = 0; // Boolean indicating if the picture has been taken correctly

  do {
    // Take a photo with the camera
    Serial.println("Taking a photo...");

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

    // Photo file name
    Serial.printf("Picture file name: % s\n", FILE_PHOTO);
    File file = SPIFFS.open(FILE_PHOTO, FILE_WRITE);

    // Insert the data in the photo file
    if (!file) {
      Serial.println("Failed to open file in writing mode");
    }
    else {
      file.write(fb->buf, fb->len); // payload (image), payload length
      Serial.print("The picture has been saved in ");
      Serial.print(FILE_PHOTO);
      Serial.print(" - Size: ");
      Serial.print(file.size());
      Serial.println(" bytes");
    }
    // Close the file
    file.close();
    esp_camera_fb_return(fb);

    // check if file has been correctly saved in SPIFFS
    ok = checkPhoto(SPIFFS);
  } while ( !ok );
}

Code: Select all

18:48:30.694 -> ets Jul 29 2019 12:21:46
18:48:30.694 -> 
18:48:30.694 -> rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
18:48:30.694 -> configsip: 0, SPIWP:0xee
18:48:30.694 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
18:48:30.694 -> mode:DIO, clock div:1
18:48:30.694 -> load:0x3fff0030,len:1344
18:48:30.694 -> load:0x40078000,len:13864
18:48:30.694 -> load:0x40080400,len:3608
18:48:30.694 -> entry 0x400805f0
18:48:31.294 -> Formating SPIFFS
18:48:39.274 -> SPIFFS formated successfully
18:48:39.874 -> SPIFFS mounted successfully
18:48:40.074 -> ------ -Test nbr 1
18:48:40.074 -> Taking a photo...
18:48:44.074 -> Camera capture failed
18:48:49.075 -> ------ -Test nbr 2
18:48:49.075 -> Taking a photo...
18:48:53.075 -> Camera capture failed
18:48:58.075 -> ------ -Test nbr 3
18:48:58.075 -> Taking a photo...
18:49:02.072 -> Camera capture failed
18:49:07.065 -> ------ -Test nbr 4
18:49:07.065 -> Taking a photo...
18:49:11.065 -> Camera capture failed
18:49:16.066 -> ------ -Test nbr 5
18:49:16.066 -> Taking a photo...
18:49:20.066 -> Camera capture failed
18:49:25.056 -> ------ -Test nbr 6
18:49:25.056 -> Taking a photo...
18:49:29.056 -> Camera capture failed
18:49:34.056 -> ------ -Test nbr 7
18:49:34.056 -> Taking a photo...
18:49:38.056 -> Camera capture failed
18:49:43.066 -> ------ -Test nbr 8
18:49:43.066 -> Taking a photo...
18:49:47.067 -> Camera capture failed
18:49:52.067 -> ------ -Test nbr 9
18:49:52.067 -> Taking a photo...
18:49:56.071 -> Camera capture failed
18:50:01.057 -> ------ -Test nbr 10
18:50:01.057 -> Taking a photo...
18:50:05.047 -> Camera capture failed
18:50:10.047 -> ------ -The end
Attachments
photo_2023-10-31_19-42-05.jpg
photo_2023-10-31_19-42-05.jpg (153.1 KiB) Viewed 5993 times
photo_2_2023-10-31_10-59-53.jpg
photo_2_2023-10-31_10-59-53.jpg (121.88 KiB) Viewed 5993 times
photo_1_2023-10-31_10-59-53.jpg
photo_1_2023-10-31_10-59-53.jpg (132.74 KiB) Viewed 5993 times

epietrowicz
Posts: 1
Joined: Tue Nov 21, 2023 2:59 pm

Re: Camera always fails to capture fb (tested with different lenses, power supplies, sketches)

Postby epietrowicz » Tue Nov 21, 2023 3:01 pm

I'm sorry, I do not have a solution. I am running into this exact same behavior. The example sketch works as expected, but triggering individual captures always fails for me after the first capture.

Did you make any progress on solving this?

Who is online

Users browsing this forum: No registered users and 53 guests