• MicroPython
    2 min | 5857

    #MicroPython: Portable time-lapse camera using an ESP32-CAM

    MicroPython | 2 min | 5857


    This tutorial extends the article MicroPython: Taking photos with an ESP32 and in this case, the ESP32-CAM running MicroPython takes a photo every 5 seconds and saves it on the microSD card. The photos can be then combined into a time-lapse video! :)

    The MicroPython official version doesn't support I2S interface. However, tsaarni wrote a driver for the OV2640 and forked the MicroPython repository to include it. Thus, you need to follow the instructions in this article: MicroPython: Taking photos with an ESP32 to install the right firmware.

    Hardware and Software

    In the following table, you will find the hardware and software that you can use on this tutorial:

    There are two new articles about this topic:Check out those, before you keep reading this article and instructions.

    DIY

    After flashing the MicroPython firmware, clone the repository lemariva/uPyCam typing:

    git clone -b timelapse-camera https://github.com/lemariva/uPyCam.git

    then rename the file boot.sample.py to boot.py and configure the Wi-Fi access settings:

    # wlan access
    ssid_ = ""
    wpa2_pass = ""

    The ESP32-CAM uses Wi-Fi to connect to an NTP-server and to get the actual time. Additionally, you can check, if everything is working because the code enables an FTP server.

    Use VSCode with the PyMakr extension to upload all the files to the ESP32. If you need some help, follow the instructions from this tutorial.

    Then, connect your ESP32-CAM to a 5V battery and enjoy the results! :)

    If you need a datasheet of the ESP32-CAM, here is one available, I needed to define the pins that are connected to the MicroSD slot.

    Error on saving photos

    Some of the photos were saved with error on the MicroSD card, and I got an error when I tried to export the video sequence. So, I wrote some code lines to repair the photos. The error that I got was the following: Premature end of JPEG file.

    Create a file photo_repair.py with the following code:

    import os
    import cv2
    import shutil
    from os import listdir
    from os.path import isfile, join
    
    basedir = os.path.abspath(os.getcwd())
    
    onlyfiles = [f for f in listdir(basedir) if isfile(join(basedir, f))]
    new_folder = "numerated" 
    
    for idx, filename in enumerate(onlyfiles):   
        base, extension = os.path.splitext(filename)
        if extension == ".jpg":
            print(filename)
            img = cv2.imread(filename)
            cv2.imwrite(filename, img)

    Then, install the dependencies (opencv) using pip:

    pip install opencv-python

    and run the code typing python photo_repair.py.

    Conclusions

    This article presents an example code to use the ESP32-CAM as a time-lapse camera using MicroPython. The module takes a photo every 5 seconds and saves it on the SD. Then, using x264vfw and VirtualDub is possible to generate a time-lapse video. Some of the photos are saved on the microSD with errors and I included a some code lines to repair them.


    Comments

    Mohamed Ali. A 03.07.2020

    I want built camera for blind people. Blind person , with help of ESP 32 , CAM and SD card will take photo, this photo will be compared database in SD card . This will inform over voice what is the in-front of him. Please send necessary microphython code. Mohamed Ali Chennai, India []

    Colin 04.05.2020

    This article says nothing of how to mount the sdcard. It appears that it is writing to the filesystem.

    Stan92 04.12.2020

    Thanks for sharing...

    Amy 05.12.2020

    Hi, I gotthe camera working but I just cannot get the sd card respond. Is it working? import machine, sdcard, os

    try:
        # sd mount
        spi = machine.SPI(1, baudrate=100000, phase=0, polarity=0, sck=machine.Pin(14), mosi=machine.Pin(15), miso=machine.Pin(2))
        sd = sdcard.SDCard(spi, machine.Pin(13))
    except AssertionError as e:
        print(e)
        machine.reset()
    -------
    MPY: soft reboot
    I (5749) spi_master: Allocate TX buffer for DMA
    I (5759) spi_master: Allocate TX buffer for DMA
    I (5759) spi_master: Allocate TX buffer for DMA
    I (5759) spi_master: Allocate TX buffer for DMA
    I (5759) spi_master: Allocate TX buffer for DMA
    Traceback (most recent call last):
      File "boot.py", line 32, in 
      File "boot.py", line 22, in 
      File "sdcard.py", line 48, in __init__
      File "sdcard.py", line 76, in init_card
    OSError: no SD card

    MicroPython v1.10-128-g584bc5b2a on 2019-09-01; ESP32 module with ESP32 Type "help()" for more information.