ESP32-CAM拍照并保存到MicroSD卡
了解如何使用ESP32-CAM板拍照并使用Arduino IDE将其保存到microSD卡中。按下ESP32-CAM RESET按钮时,它会醒来并拍照,并将其保存在microSD卡中。
我们将使用名称为AI-Thinker模块的ESP32-CAM开发板,但其它模块也应通过在代码中进行正确的引脚分配来工作。
ESP32-CAM板是其结合了ESP32-S芯片,OV2640相机,microSD卡插槽和几个GPIO引脚。
这是市面上可以买到的ESP32CAM,很便宜,一般几十块钱可以拿下。
所需零件
要遵循本教程,您需要以下组件:
1.带OV2640的ESP32-CAM
2.MicroSD卡
3.FTDI编程器
4.母对母连接线
5.用于ESP32-CAM的移动电源或5V电源
注:在购买的时候也可以购买ESP32CAM烧录底座,可以节省很多麻烦
项目概述
以下是有关该项目工作原理的快速概述。
1.ESP32-CAM处于深度睡眠模式
2.按RESET按钮唤醒开发板
3.相机拍照
4.照片以以下名称保存在microSD卡中:pictureX.jpg,其中X对应于图片编号
5.图片编号将保存在ESP32闪存中,以便在RESET期间不会被擦除,我们可以跟踪拍摄的照片数量。
接下来第一步就是先把TF卡格式化:
格式化MicroSD卡
我们建议做的第一件事是格式化microSD卡。您可以使用Windows格式化程序工具或任何其它microSD格式化程序软件。
1. 将microSD卡插入计算机。转到 我的电脑, 然后右键单击SD卡。选择 格式 ,如下图所示。
2. 弹出一个新窗口。选择 FAT32,按 开始 以初始化格式化过程,然后按照屏幕上的说明进行操作。
注意:根据产品规格,ESP32-CAM只应支持4 GB SD卡。但是,通过测试,对2GB和16GB的存储卡也适用。
接下来:
这一步比较麻烦,
详情可以自己搜搜,主要就是要在arduino中下载esp32的库。
拍摄并保存照片
将以下代码复制到您的Arduino IDE中:
该代码首先包括使用相机所需的库。我们还包括与microSD卡交互所需的库:
#include "esp_camera.h"
#include "Arduino.h"
#include "FS.h" // SD Card ESP32
#include "SD_MMC.h" // SD Card ESP32
#include "soc/soc.h" // Disable brownour problems
#include "soc/rtc_cntl_reg.h" // Disable brownour problems
#include "driver/rtc_io.h"
#include // read and write from flash memory
和 EEPROM 库将永久数据保存在闪存中。
#include
定义要在闪存中访问的字节数。在这里,我们将仅使用一个字节,该字节最多可以生成256个图片编号。
#define EEPROM_SIZE 1
然后,定义AI-THINKER摄像机模块的插针。
// Pin definition for 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
注意:您可能需要根据所使用的板来更改引脚定义。错误的引脚分配将导致无法启动摄像机。
初始化一个名为的int变量 pictureNumber 将生成照片名称的图片:picture1.jpg,picture2.jpg等。
int pictureNumber = 0;
我们所有的代码都在 setup() 。该代码仅在ESP32唤醒时运行一次(在这种情况下,当您按板载的RESET按钮时)。
定义相机设置:
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;
对于具有PSRAM的相机,请使用以下设置(例如本教程中使用的设置)。
if(psramFound()){
config.frame_size = FRAMESIZE_UXGA; // FRAMESIZE_ + QVGA|CIF|VGA|SVGA|XGA|SXGA|UXGA
config.jpeg_quality = 10;
config.fb_count = 2;
}
初始化相机:
// Init Camera
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x", err);
return;
}
初始化microSD卡:
//Serial.println("Starting SD Card");
if(!SD_MMC.begin()){
Serial.println("SD Card Mount Failed");
return;
}
uint8_t cardType = SD_MMC.cardType();
if(cardType == CARD_NONE){
Serial.println("No SD Card attached");
return;
}
以下几行用相机拍摄照片:
camera_fb_t * fb = NULL;
// Take Picture with Camera
fb = esp_camera_fb_get();
if(!fb) {
Serial.println("Camera capture failed");
return;
}
#include "esp_camera.h"
#include "Arduino.h"
#include "FS.h" // SD Card ESP32
#include "SD_MMC.h" // SD Card ESP32
#include "soc/soc.h" // Disable brownour problems
#include "soc/rtc_cntl_reg.h" // Disable brownour problems
#include "driver/rtc_io.h"
#include // read and write from flash memory
// define the number of bytes you want to access
#define EEPROM_SIZE 1
// Pin definition for 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
int pictureNumber = 0;
void setup() {
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
Serial.begin(115200);
//Serial.setDebugOutput(true);
//Serial.println();
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;
if(psramFound()){
config.frame_size = FRAMESIZE_UXGA; // FRAMESIZE_ + QVGA|CIF|VGA|SVGA|XGA|SXGA|UXGA
config.jpeg_quality = 10;
config.fb_count = 2;
} else {
config.frame_size = FRAMESIZE_SVGA;
config.jpeg_quality = 12;
config.fb_count = 1;
}
// Init Camera
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x", err);
return;
}
//Serial.println("Starting SD Card");
if(!SD_MMC.begin()){
Serial.println("SD Card Mount Failed");
return;
}
uint8_t cardType = SD_MMC.cardType();
if(cardType == CARD_NONE){
Serial.println("No SD Card attached");
return;
}
camera_fb_t * fb = NULL;
// Take Picture with Camera
fb = esp_camera_fb_get();
if(!fb) {
Serial.println("Camera capture failed");
return;
}
// initialize EEPROM with predefined size
EEPROM.begin(EEPROM_SIZE);
pictureNumber = EEPROM.read(0) + 1;
// Path where new picture will be saved in SD Card
String path = "/picture" + String(pictureNumber) +".jpg";
fs::FS &fs = SD_MMC;
Serial.printf("Picture file name: %s\n", path.c_str());
File file = fs.open(path.c_str(), FILE_WRITE);
if(!file){
Serial.println("Failed to open file in writing mode");
}
else {
file.write(fb->buf, fb->len); // payload (image), payload length
Serial.printf("Saved file to path: %s\n", path.c_str());
EEPROM.write(0, pictureNumber);
EEPROM.commit();
}
file.close();
esp_camera_fb_return(fb);
// Turns off the ESP32-CAM white on-board LED (flash) connected to GPIO 4
pinMode(4, OUTPUT);
digitalWrite(4, LOW);
rtc_gpio_hold_en(GPIO_NUM_4);
delay(2000);
Serial.println("Going to sleep now");
delay(2000);
esp_deep_sleep_start();
Serial.println("This will never be printed");
}
void loop() {
}
示范
上传代码后,从GND移除连接GPIO 0的连接线。
以115200的波特率打开串行监视器。按ESP32-CAM复位按钮。它应该初始化并拍照。拍照时,它会打开闪光灯(GPIO 4)
检查Arduino IDE串行监视器窗口,看一切是否按预期工作。如您所见,图片已成功保存在microSD卡中。
以上就基本完成了。