take_picture.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * This example takes a picture every 5s and print its size on serial monitor.
  3. */
  4. // =============================== SETUP ======================================
  5. // 1. Board setup (Uncomment):
  6. // #define BOARD_WROVER_KIT
  7. #define BOARD_ESP32CAM_AITHINKER
  8. /**
  9. * 2. Kconfig setup
  10. *
  11. * If you have a Kconfig file, copy the content from
  12. * https://github.com/espressif/esp32-camera/blob/master/Kconfig into it.
  13. * In case you haven't, copy and paste this Kconfig file inside the src directory.
  14. * This Kconfig file has definitions that allows more control over the camera and
  15. * how it will be initialized.
  16. */
  17. /**
  18. * 3. Enable PSRAM on sdkconfig:
  19. *
  20. * CONFIG_ESP32_SPIRAM_SUPPORT=y
  21. *
  22. * More info on
  23. * https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/kconfig.html#config-esp32-spiram-support
  24. */
  25. // ================================ CODE ======================================
  26. #include <esp_event_loop.h>
  27. #include <esp_log.h>
  28. #include <esp_system.h>
  29. #include <nvs_flash.h>
  30. #include <sys/param.h>
  31. #include <string.h>
  32. #include "freertos/FreeRTOS.h"
  33. #include "freertos/task.h"
  34. #include "esp_camera.h"
  35. // WROVER-KIT PIN Map
  36. #ifdef BOARD_WROVER_KIT
  37. #define CAM_PIN_PWDN -1 //power down is not used
  38. #define CAM_PIN_RESET -1 //software reset will be performed
  39. #define CAM_PIN_XCLK 21
  40. #define CAM_PIN_SIOD 26
  41. #define CAM_PIN_SIOC 27
  42. #define CAM_PIN_D7 35
  43. #define CAM_PIN_D6 34
  44. #define CAM_PIN_D5 39
  45. #define CAM_PIN_D4 36
  46. #define CAM_PIN_D3 19
  47. #define CAM_PIN_D2 18
  48. #define CAM_PIN_D1 5
  49. #define CAM_PIN_D0 4
  50. #define CAM_PIN_VSYNC 25
  51. #define CAM_PIN_HREF 23
  52. #define CAM_PIN_PCLK 22
  53. #endif
  54. // ESP32Cam (AiThinker) PIN Map
  55. #ifdef BOARD_ESP32CAM_AITHINKER
  56. #define CAM_PIN_PWDN 32
  57. #define CAM_PIN_RESET -1 //software reset will be performed
  58. #define CAM_PIN_XCLK 0
  59. #define CAM_PIN_SIOD 26
  60. #define CAM_PIN_SIOC 27
  61. #define CAM_PIN_D7 35
  62. #define CAM_PIN_D6 34
  63. #define CAM_PIN_D5 39
  64. #define CAM_PIN_D4 36
  65. #define CAM_PIN_D3 21
  66. #define CAM_PIN_D2 19
  67. #define CAM_PIN_D1 18
  68. #define CAM_PIN_D0 5
  69. #define CAM_PIN_VSYNC 25
  70. #define CAM_PIN_HREF 23
  71. #define CAM_PIN_PCLK 22
  72. #endif
  73. static const char *TAG = "example:take_picture";
  74. static camera_config_t camera_config = {
  75. .pin_pwdn = CAM_PIN_PWDN,
  76. .pin_reset = CAM_PIN_RESET,
  77. .pin_xclk = CAM_PIN_XCLK,
  78. .pin_sscb_sda = CAM_PIN_SIOD,
  79. .pin_sscb_scl = CAM_PIN_SIOC,
  80. .pin_d7 = CAM_PIN_D7,
  81. .pin_d6 = CAM_PIN_D6,
  82. .pin_d5 = CAM_PIN_D5,
  83. .pin_d4 = CAM_PIN_D4,
  84. .pin_d3 = CAM_PIN_D3,
  85. .pin_d2 = CAM_PIN_D2,
  86. .pin_d1 = CAM_PIN_D1,
  87. .pin_d0 = CAM_PIN_D0,
  88. .pin_vsync = CAM_PIN_VSYNC,
  89. .pin_href = CAM_PIN_HREF,
  90. .pin_pclk = CAM_PIN_PCLK,
  91. //XCLK 20MHz or 10MHz for OV2640 double FPS (Experimental)
  92. .xclk_freq_hz = 20000000,
  93. .ledc_timer = LEDC_TIMER_0,
  94. .ledc_channel = LEDC_CHANNEL_0,
  95. .pixel_format = PIXFORMAT_JPEG, //YUV422,GRAYSCALE,RGB565,JPEG
  96. .frame_size = FRAMESIZE_VGA, //QQVGA-UXGA Do not use sizes above QVGA when not JPEG
  97. .jpeg_quality = 12, //0-63 lower number means higher quality
  98. .fb_count = 1 //if more than one, i2s runs in continuous mode. Use only with JPEG
  99. };
  100. static esp_err_t init_camera()
  101. {
  102. //initialize the camera
  103. esp_err_t err = esp_camera_init(&camera_config);
  104. if (err != ESP_OK)
  105. {
  106. ESP_LOGE(TAG, "Camera Init Failed");
  107. return err;
  108. }
  109. return ESP_OK;
  110. }
  111. void app_main()
  112. {
  113. init_camera();
  114. while (1)
  115. {
  116. ESP_LOGI(TAG, "Taking picture...");
  117. camera_fb_t *pic = esp_camera_fb_get();
  118. // use pic->buf to access the image
  119. ESP_LOGI(TAG, "Picture taken! Its size was: %zu bytes", pic->len);
  120. vTaskDelay(5000 / portTICK_RATE_MS);
  121. }
  122. }