Corrupted image file with esp32-s3 and OV5640 module

amisbs
Posts: 2
Joined: Tue Jul 04, 2023 3:31 pm

Corrupted image file with esp32-s3 and OV5640 module

Postby amisbs » Tue Jul 04, 2023 3:51 pm

We have been working with the ESP32-S3 dev module (ESP32-S3-WROOM-1-N8R8) in order to capture 5 megapixel images with the Arducam OV5640D (which essentially is, to our understanding, a breakout board with voltage dividers for the OV5640 camera module)
When setting the resolution to HQVGA (240x176) we get the following output:

Code: Untitled.txt Select all


ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0x1 (POWERON),boot:0x8 (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fce3808,len:0x44c
load:0x403c9700,len:0xbe4
load:0x403cc700,len:0x2a38
entry 0x403c98d4
[ 115][I][esp32-hal-psram.c:96] psramInit(): PSRAM enabled
[ 129][D][esp32-s3-minimal-ov5640-test.ino:18] printDebugMemoryInfo(): Total heap: 388856
[ 129][D][esp32-s3-minimal-ov5640-test.ino:19] printDebugMemoryInfo(): Free heap: 363272
[ 134][D][esp32-s3-minimal-ov5640-test.ino:20] printDebugMemoryInfo(): Total PSRAM: 8386295
[ 142][D][esp32-s3-minimal-ov5640-test.ino:21] printDebugMemoryInfo(): Free PSRAM: 8386295
[ 1057][I][camera_functions.ino:84] camera_capture(): [camera_functions.ino] Picture taken! Its size was: 1452 bytes
FFD8FFE000104A46494600010101000000000000FFDB0043000C08090B09080 ... 00A2800A2800A2800A2800A2800A2800A2800A2800A2800A2800A2800A2800A2803FFFD9
Sometimes the printed bytes correspond to a broken jpeg file, otherwise it is either corrupted or valid.

When setting the resolution to FRAMESIZE_QSXGA 2560x1920 we get "camera capture failed error", despite PSRAM being enabled and abundant.

Here is the arduino config:
arduino_setup.png
arduino_setup.png (67.31 KiB) Viewed 2099 times

Here is the arduino code:

main file

Code: Untitled.cpp Select all


#include "esp_camera.h"

void setup()
{
Serial.begin(115200);
printDebugMemoryInfo();
camera_init();
}

void loop()
{
camera_capture();
delay(5000);
}

void printDebugMemoryInfo()
{
log_d("Total heap: %d", ESP.getHeapSize());
log_d("Free heap: %d", ESP.getFreeHeap());
log_d("Total PSRAM: %d", ESP.getPsramSize());
log_d("Free PSRAM: %d", ESP.getFreePsram());
}
and the camera_functions.ino:

Code: Untitled.cpp Select all


#include "esp_camera.h"

#define CAM_MODULE_NAME "Arducam OV5640D"
#define CAM_PIN_PWDN -1
#define CAM_PIN_RESET -1

#define CAM_PIN_VSYNC 12
#define CAM_PIN_HREF 11
#define CAM_PIN_PCLK 10
#define CAM_PIN_XCLK 9

#define XCLK_FREQ_HZ 20000000

#define CAM_PIN_SIOD 13 //sda
#define CAM_PIN_SIOC 8 //scl

#define CAM_PIN_D0 4
#define CAM_PIN_D1 5
#define CAM_PIN_D2 6
#define CAM_PIN_D3 7
#define CAM_PIN_D4 15
#define CAM_PIN_D5 16
#define CAM_PIN_D6 17
#define CAM_PIN_D7 18

static const char* TAG = "camera_functions.ino";

static camera_config_t camera_config = {
.pin_pwdn = CAM_PIN_PWDN,
.pin_reset = CAM_PIN_RESET,
.pin_xclk = CAM_PIN_XCLK,
.pin_sscb_sda = CAM_PIN_SIOD,
.pin_sscb_scl = CAM_PIN_SIOC,

.pin_d7 = CAM_PIN_D7,
.pin_d6 = CAM_PIN_D6,
.pin_d5 = CAM_PIN_D5,
.pin_d4 = CAM_PIN_D4,
.pin_d3 = CAM_PIN_D3,
.pin_d2 = CAM_PIN_D2,
.pin_d1 = CAM_PIN_D1,
.pin_d0 = CAM_PIN_D0,
.pin_vsync = CAM_PIN_VSYNC,
.pin_href = CAM_PIN_HREF,
.pin_pclk = CAM_PIN_PCLK,

.xclk_freq_hz = XCLK_FREQ_HZ,
.ledc_timer = LEDC_TIMER_0,
.ledc_channel = LEDC_CHANNEL_0,

.pixel_format = PIXFORMAT_JPEG,
.frame_size = FRAMESIZE_HQVGA, //240x176
.jpeg_quality = 12,
.fb_count = 1,
.grab_mode = CAMERA_GRAB_LATEST//CAMERA_GRAB_WHEN_EMPTY
};

esp_err_t camera_init(){
//power up the camera if PWDN pin is defined
if(CAM_PIN_PWDN != -1){
pinMode(CAM_PIN_PWDN, OUTPUT);
digitalWrite(CAM_PIN_PWDN, LOW);
}

//initialize the camera
esp_err_t err = esp_camera_init(&camera_config);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Camera Init Failed");
return err;
}

return ESP_OK;
}

esp_err_t camera_capture(){
//acquire a frame
camera_fb_t * fb = esp_camera_fb_get();
if (!fb) {
ESP_LOGE(TAG, "Camera Capture Failed");
return ESP_FAIL;
}

ESP_LOGI(TAG, "Picture taken! Its size was: %zu bytes", fb->len);
for (int i=0; i<fb->len; i++) {
if (fb->buf[i] < 16) {
Serial.print('0');
}
Serial.print(fb->buf[i], HEX);
}
Serial.println();

//return the frame buffer back to the driver for reuse
esp_camera_fb_return(fb);
return ESP_OK;
}

amisbs
Posts: 2
Joined: Tue Jul 04, 2023 3:31 pm

Re: Corrupted image file with esp32-s3 and OV5640 module

Postby amisbs » Thu Jul 06, 2023 9:18 am

Upon further testing, we have discovered that even with the higher resolution the behaviour is similar, with the output being either "camera capture failed" or a sequence of bytes containing valid jpg markers (FF D8 FF at the beginning and FF D9 at the end) that amount to an invalid image file.

simardeep27
Posts: 1
Joined: Thu Mar 14, 2024 7:39 am

Re: Corrupted image file with esp32-s3 and OV5640 module

Postby simardeep27 » Thu Mar 14, 2024 7:42 am

Hi, can you please share your pin configuration to show how are you connecting your camera with the esp32 module. I am trying to connect a arducam 2MP plus camera to the same, but I am not able to read the frames.

Thanks

Who is online

Users browsing this forum: Google [Bot], PerplexityBot and 1 guest