Example program for capturing photos with Unit CamS3-5MP and saving them to a microSD card.
Please insert the microSD card before powering on the device. After startup, the device will take photos at regular intervals and save them to the microSD card. The captured photos will be named using the runtime since power-on (IMG_{millis}.jpg). Upon reboot, files with the same name will be overwritten.
#include <WiFi.h>
#include "esp_camera.h"
#include "FS.h"
#include "SD.h"
#include "SPI.h"
#define SD_SPI_CS_PIN 9
#define SD_SPI_MOSI_PIN 38
#define SD_SPI_SCK_PIN 39
#define SD_SPI_MISO_PIN 40
#define CAPTURE_INTERVAL 15000
#define PWDN_GPIO_NUM -1
#define RESET_GPIO_NUM 21
#define XCLK_GPIO_NUM 11
#define SIOD_GPIO_NUM 17
#define SIOC_GPIO_NUM 41
#define Y9_GPIO_NUM 13
#define Y8_GPIO_NUM 4
#define Y7_GPIO_NUM 10
#define Y6_GPIO_NUM 5
#define Y5_GPIO_NUM 7
#define Y4_GPIO_NUM 16
#define Y3_GPIO_NUM 15
#define Y2_GPIO_NUM 6
#define VSYNC_GPIO_NUM 42
#define HREF_GPIO_NUM 18
#define PCLK_GPIO_NUM 12
#define LED_GPIO_NUM 14
unsigned long lastCaptureTime = 0;
void setup() {
Serial.begin(115200);
Serial.println("Camera + SD Card Capture Demo");
SPI.begin(SD_SPI_SCK_PIN, SD_SPI_MISO_PIN, SD_SPI_MOSI_PIN, SD_SPI_CS_PIN);
if (!SD.begin(SD_SPI_CS_PIN, SPI, 40000000)) {
Serial.println("SD Card Mount Failed");
return;
}
uint8_t cardType = SD.cardType();
if (cardType == CARD_NONE) {
Serial.println("No SD card attached");
return;
}
Serial.println("SD Card initialized.");
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_sccb_sda = SIOD_GPIO_NUM;
config.pin_sccb_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_5MP;
config.fb_location = CAMERA_FB_IN_PSRAM;
config.jpeg_quality = 63;
config.fb_count = 1;
config.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x\n", err);
return;
}
Serial.println("Camera initialized.");
}
void loop() {
unsigned long now = millis();
if (now - lastCaptureTime >= CAPTURE_INTERVAL) {
lastCaptureTime = now;
camera_fb_t* fb = esp_camera_fb_get();
if (!fb) {
Serial.println("Camera capture failed");
return;
}
String path = "/IMG_" + String(millis()) + ".jpg";
File file = SD.open(path.c_str(), FILE_WRITE);
if (!file) {
Serial.println("Failed to open file for writing");
} else {
file.write(fb->buf, fb->len);
file.close();
Serial.printf("Saved file: %s, size: %d bytes\n", path.c_str(), fb->len);
}
esp_camera_fb_return(fb);
}
}
The Unit CamS3-5MP microSD card functionality uses Arduino's built-in SD
library. For more related APIs, please refer to the documents below: