📜 ⬆️ ⬇️

Time-lapse camera on STM32L

image

According to Wikipedia, Time-lapse or slow motion is a motion picture with a frequency lower than the standard shooting frequency and a projection of 24 frames per second.
Gifka time-lapse
image

Having started studying STM32 microcontrollers and writing “HellowWorld” with a flashing LED, I realized that in order to better understand how STM32 works, I need to implement something more complicated using more microcontroller peripherals. So the idea of ​​creating a time-lapse camera.

The camera developed by me will take photos about once every 5 seconds and save them to the SD card in jpeg format. Then they must be combined on a computer into a video file.

To create the camera, I used the following components:
')
1) STM32L-DISCOVERY debug board. It is equipped with an STM32L152RBT6 microcontroller specially designed for the implementation of low-power devices, which would be useful for realizing the power supply of the camera from the battery.

image

2) Module LinkSprite JPEG Color Camera , working on the UART interface. This module is a ready-made camera, which produces photos in JPEG format via UART. Configuration and control is also carried out via this interface.

image
LinkSprite module features:


3) SD card connection module from LC STUDIO, the microcontroller will communicate with it via the SPI interface.

image

4) Any “usb to uart” adapter can also come in handy for the initial study of the LinkSprite camera module (for this you need the LS-Y201 program) and for outputting debug information from the microcontroller to the computer. (I did not find beautiful photos of my adapter on the Internet, so I photographed myself):

image

As they say, we will take the integral in parts:

First, let's look at how LinkSprite camera works:

1) We will assemble the circuit according to the figure (The camera module does not have a wire label, but there is a description in the LinkSprite manual, so if I could not figure it out in my drawing, then refer to it).

image

This construction should turn out (For myself, I marked all the wires from the camera module). One wire remains unconnected, if you believe the information on the site, then this is an analog video output.

image

2) Now we connect the adapter to the computer and run the LS-Y201 program . It is necessary to select the Com port of the adapter (The special feature is that you can only select from 1 to 4, so if you have a larger adapter, you will have to reconfigure it). Next, click the Open button. After that, the Com port will open and you can receive photos from the camera. To do this, use the buttons Single Shot (Makes a single photo), Continues Shot (Takes a photo until the Stop Shot button is pressed), Save Pictures (Saves the taken photo to a computer disk), Reset (Reset the camera to its original settings), Exit from the program).

image

In the program settings, you can select a snapshot resolution of 320x240 or 640x480 ( Image Style ), transfer mode of 512 bytes or the entire frame ( Transfer Mode ), transfer rate ( Rate , set from 9600 to 115200, while the Com port speed will also need to be changed) Photo compression ratio ( Compress Rate ); Photo saving folder ( Path ).

The exchange between the computer and the camera is carried out according to the protocol described in “LinkSprite JPEG Color Camera
Serial UART Interface ”.

There are the following commands:


With the camera module more or less sorted out, now we collect the STM32 camera.

image

You should get such a construction from a pile of wires.

image

Enough for work, but you can cram everything into some kind of case. I used a cardboard battery box.

image

Consider now the microcontroller program.

The microcontroller is set to a maximum frequency of 32 MHz from an embedded 16 MHz generator using a PLL. UART1 is used to work with the camera module. UART2 is used for output to the debugging port (for UART operation, interrupts and ring buffers are used, how all this is configured is described in detail in this article UART (USART) on STM32L (STM32) ). SD card is connected via SPI2. I also use the RTC timer to save photos with reference to time and date.
To work with peripherals, I used the library STM32L1xx_StdPeriph. Her description is in the folder StLib.
All functions of the microcontroller interaction with the camera module, SD card and USB to UART adapter are described in MyLib.

The FatFs library is used to work with Fat, its files are located in the fatfs folder.
The main.h file contains all the basic settings of the project, such as the description of the microcontroller's legs used, the description of the parameters of the peripherals used.

I would like to focus on these lines:
#define DEBUG //     UART2 #define ENABLE_POWER_SAVING //      //#define JPEG_TO_UART //   jpg  UART2 

They can help with debugging and camera performance checks.

Consider the interaction algorithm of the microcontroller with the camera module.

The following functions are used to configure the camera module:
1. ResetCameraAtDifferentSpeeds () - initially you need to restart the camera module. For this, a reset command is sent to it, since it is not known at what speed the camera was set up before, we try to reset it on all available (from 19200 to 115200). In case of a successful reset, the camera on boot issues a sequence ending with “init end”. After that, you need to sustain an interval of 2-3 seconds and you can continue to configure the camera.
2. Camera_ChBaudRate (4) - for faster frame transfer, you can increase the exchange rate on the UART or vice versa decrease it to eliminate transmission errors. By default, the camera is set to 38400. (4 is the speed of 115200)
3. Camera_ChPictureSize (2) - further the resolution of the photos formed by the camera is set. It can be 160x120, 320x240 or 640x480. (2- resolution 640x480)
4. Camera_ChCompression (0x36) - sets the compression ratio of photos (from 0x00 to 0xFF), the larger it is, the smaller they will be.
5. Camera_PowerSaving (1) - well, in the end, you can switch the camera to energy saving mode to reduce power consumption. (1- switch to energy saving mode)

All these actions are described in the files init_JPEGCamera.h and init_JPEGCamera.c located in the project folder MyLib.

After setting up the camera, you can receive photos from it, for this you need to perform the following sequence:
1. Camera_PowerSaving (0) - if the camera was in power saving mode, take it out of this mode .. (0 - output from power saving mode)
2. Camera_TakePicture () - send a command to form a photo.
3. Camera_ReadData () - read the generated photo from the camera.
4. Camera_StopPictures () - send a command to complete the formation of the photo.
5. Camera_PowerSaving (1) - if necessary, transfer the camera to a power saving mode.

The description of these functions is located in the functions_JPEGCamera.h and functions_JPEGCamera.c files located in the project folder MyLib.

Now consider the algorithm of interaction with the SD card.
I use the FatFS library, according to this article Connecting an SD card over SPI to STM32F4xx and FatFS

Connecting the card is carried out using two functions:
1. Using the function disk_initialize, the map is initialized.
2. Next, you need to mount it f_mount .

After successfully completing these steps, you can perform various operations with the card, such as:
1. f_open - opening a file
2. f_close - closing the file;
3. f_mkdir - create a directory;
4. f_chdir - select the directory;
5. f_write - write to file;
6. f_read - reading from the file.

I described the work with the camera and the SD card, now let's consider the work of the program as a whole:
1. Initialize the SD card;
2. Initialize the camera module;
3. Open the file time.txt (the file should contain one line of format 01/01/ 2014_12 : 00 ), which stores the time for the RTC timer countdown;
4. Read the time and date from the file and configure the RTC;
5. Close the file time.txt;
6. Create a directory for storing photos "LinkSpritePhoto";
7. When you press the PA0 button on the board, we take a photo and save it to the card (While the photo is being taken, the blue LED is on);
8. In case of non-fulfillment of any of the functions, we drop into an infinite loop and flash with a green LED.

To start the camera in continuous mode, you need to uncomment the following lines in main.c:

 //EnabledButtonStart = 101; //        //Delay(300); //      

And comment out this line:

 EnabledButtonStart = 0; //        

Then when turned on, the camera will begin to write photos continuously.

In general, I made a description of how this camera works, and now it is possible to show the result of the work.



Gif for those who do not play the video
image

For some reason, all the frames go into yellowing, perhaps the lens without IR correction.

The camera provides information on the UART to the computer.

image

Saves frames to the SD card.

image

Well, the conclusion of all that I have done:


The list of links needed to repeat everything I did:

As well as a link for downloading the project STM32Camera.rar

Source: https://habr.com/ru/post/243655/


All Articles