I am working on a project that involves using an ESP32-CAM ov2640 camera to capture grayscale images and save them to an SD card, and also save the image information in an array to generate a CSV file that is also saved to the SD card (this is to later use an AI for image recognition but I am not there yet). However, when I run the code provided below, instead of getting error information, I get a series of messages on the serial monitor that I don't understand, apparently the board is rebooting but that is not in the code.
#include "esp_camera.h"
#include "soc/soc.h" // Disable brownour problems
#include "soc/rtc_cntl_reg.h" // Disable brownour problems
#include "driver/rtc_io.h"
#include "img_converters.h" // see https://github.com/espressif/esp32-camera/blob/master/conversions/include/img_converters.h
#include "FS.h"
#include "SD.h"
// Definicion de pines 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
// 4 for flash led or 33 for normal led
#define LED_GPIO_NUM 4
// Define las dimensiones de la matriz
#define WIDTH 160 // Cambia esto al ancho deseado de la imagen
#define HEIGHT 120 // Cambia esto a la altura deseada de la imagen
void captureImage(byte dataMatrix[][WIDTH]);
void saveDataToCSV(const char* filename, byte dataMatrix[][WIDTH], int rows, int cols);
void setup() {
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
Serial.begin(115200);
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_GRAYSCALE;
config.frame_size = FRAMESIZE_QQVGA; // FRAMESIZE_ + QVGA|CIF|VGA|SVGA|XGA|SXGA|UXGA
config.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
config.fb_location = CAMERA_FB_IN_PSRAM;
config.jpeg_quality = 4;
config.fb_count = 1;
if (psramFound()) {
config.jpeg_quality = 10;
config.fb_count = 2;
config.grab_mode = CAMERA_GRAB_LATEST;
} else {
Serial.println("ps RAM no encontrada");
return;
}
// Inicializa la tarjeta SD
if (!SD.begin()) {
Serial.println("Failed to initialize SD card");
return;
}
uint8_t cardType = SD.cardType();
if (cardType == CARD_NONE) {
Serial.println("No SD Card attached");
return;
}
//Inicializar camara
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x", err);
return;
}
// Matriz para almacenar los datos de la imagen
byte imageMatrix[HEIGHT][WIDTH];
camera_fb_t * fb = NULL;
// Tomar foto
fb = esp_camera_fb_get();
if (!fb) {
Serial.println("Camera capture failed");
return;
}
// Copia los datos de la imagen a la matriz
for (int row = 0; row < HEIGHT; row++) {
for (int col = 0; col < WIDTH; col++) {
imageMatrix[row][col] = fb->buf[row * WIDTH + col];
}
}
// Guarda la imagen en un archivo JPEG en la tarjeta SD
File file = SD.open("/captured.jpg", FILE_WRITE);
if (!file) {
Serial.println("Failed to open file for writing");
} else {
file.write(fb->buf, fb->len);
Serial.println("Image saved as captured.jpg on SD card");
file.close();
}
// Liberar la memoria de la captura de imagen
esp_camera_fb_return(fb);
// Llama a la función para guardar los datos en un archivo CSV
saveDataToCSV("/data.csv", imageMatrix, HEIGHT, WIDTH);
Serial.println("Todo funciono");
}
void loop() {}
void saveDataToCSV(const char *filename, byte dataMatrix[][WIDTH], int rows, int cols) {
File dataFile = SD.open(filename, FILE_WRITE);
if (dataFile) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
dataFile.print(dataMatrix[i][j]);
if (j < cols - 1) {
dataFile.print(",");
} else {
dataFile.println();
}
}
}
dataFile.close();
Serial.println("Datos guardados en el archivo CSV con éxito");
} else {
Serial.println("Error al abrir el archivo CSV");
}
}
The messages I get look like this:
rst:0x8 (TG1WDT_SYS_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:1184
load:0x40078000,len:13260
load:0x40080400,len:3028
entry 0x400805e4
To upload the programming I use a USB adapter, and it doesn't seem to me to be the one with the problem as with it I was able to upload the "CameraServer" example code and use it. It looks like this.