Search code examples
esp32arduino-esp32

Is there a way to reduce the amont of SPI PSRAM memory used up during esp_camera_init on ESP32-Cam


Below code:-

Serial.printf("Before camera config ... SPIRam Total heap %d, SPIRam Free Heap %d", ESP.getPsramSize(), ESP.getFreePsram())
cam_err = esp_camera_init(&config);
Serial.printf("After camera config ... SPIRam Total heap %d, SPIRam Free Heap %d", ESP.getPsramSize(), ESP.getFreePsram())

Will give the following printed:-

Before camera config ... SPIRam Total heap   4194252, SPIRam Free Heap   4194252
After  camera config ... SPIRam Total heap   4194204, SPIRam Free Heap   1314204

So of 4MB, 1.3 MB remains free after esp_camera_init. Is there a possibility such that this can be reduced, and have relatively larger amount of free memory in PSRAM.

I am using AI-Thinker board ESP32-Cam.

(I am following this code: https://github.com/jameszah/ESP32-CAM-Video-Recorder-junior)


Solution

  • Yes, you can reduce the amount of SPI PSRAM used by the camera.

    The answer is in the config argument to esp_camera_init(), the contents of which you didn't include in your question.

    The camera driver allocates frame buffers in PSRAM. You control the number of frame buffers (.fb_count) and the size of the frame buffers (.frame_size) in the config structure. If you want to use less PSRAM, reduce those numbers.

    Reducing the number of frame buffers will reduce the speed at which the camera can capture frames. Reducing the frame size will obviously reduce the resolution of the capture.

    You can also configure the camera to use non-PSRAM DRAM for the frame buffer, but there's precious little of it to start with and you won't be able to make this work for anything but low resolutions. The .fb_location attribute controls this. Set it to CAMERA_FB_IN_DRAM in order to use regular RAM but again, don't expect it to work for anything but the lowest resolutions.

    Yes, the camera uses a lot of storage. Video storage needs go up rapidly with resolution.

    The ESP32 camera code is open source, so you can read the code for esp_camera_init() to learn what it does, and the code for cam_dma_config(), which allocates the frame buffers.