# Using a Camera Module with the B-U585I-IOT02A Discovery Board

**URL:** <https://forum.digikey.com/t/using-a-camera-module-with-the-b-u585i-iot02a-discovery-board/27245>\
**Category:** Development Boards, Kits, Programmers\
**Tags:** cameras, stmicroelectronics, evaluation-boards, tech-tips\
**Created:** [December 12, 2022, 8:36pm UTC](https://forum.digikey.com/t/using-a-camera-module-with-the-b-u585i-iot02a-discovery-board/27245 "2022-12-12T20:36:10Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matt\_Mielke](https://sea2.discourse-cdn.com/digikey/user_avatar/forum.digikey.com/matt_mielke/32/71373_2.png) [@Matt\_Mielke](https://forum.digikey.com/u/Matt_Mielke)\
**Post date:** [December 12, 2022, 8:36pm UTC](https://forum.digikey.com/t/using-a-camera-module-with-the-b-u585i-iot02a-discovery-board/27245/1 "2022-12-12T20:36:11Z")

</div>

# Introduction

 ![mb1379](https://global.discourse-cdn.com/digikey/original/3X/1/8/1813d4790c0531f4de600ac991a368f36fe18299.jpeg) ![b-u585i-iot02a_scaled](https://global.discourse-cdn.com/digikey/original/3X/3/7/37a6db576f5c6b6d337d832e86b8dc36550357e7.jpeg)

The latest addition to the collection of IoT discovery boards from STMicroelectronics, the [B-U585I-IOT02A](https://www.digikey.com/en/products/detail/stmicroelectronics/B-U585I-IOT02A/15218435) features the STM32U585AII6Q MCU. Falling into the ultra-low-power category of STM32 MCUs, the [STM32U5](https://www.digikey.com/en/product-highlight/s/stmicroelectronics/stm32u5-series-ultra-low-power-mcus) series boasts enhanced performance and security features over its peers. Taking advantage of these capabilities, this discovery board includes an overabundance of components which can be used to develop any number of multi-functional, secure, and efficient IoT applications. These include both WiFi and BLE communication modules, external memories, and an assortment of environmental sensors.

The B-U585I-IOT02A also includes a camera module expansion connector, allowing the connection of an external camera module. In particular, the MB1379 camera module included with the [B-CAMS-OMV](https://www.digikey.com/en/products/detail/stmicroelectronics/B-CAMS-OMV/13677506) bundle is fully compatible with the discovery board and is based on the popular OV5640 5-Mpixel CMOS image sensor. The tutorial that follows demonstrates how to interface with this module starting from an empty project.

* * *

Note, however, that the silkscreen for this connector (Figure 1) mistakenly indicates that the MB1183 camera module should be used. This is incorrect for several reasons:

1. The MB1183 module is not available for individual purchase. It can only be acquired by purchasing an ST EVAL board which includes this module (see Table 4 in AN5020). As the EVAL boards are considerably more expensive than either the Discovery or NUCLEO boards, it may be cost-prohibitive to obtain this module.

2. The MB1183 module requires that 1.8V be supplied on pins 1 and 2 of the camera module connector but these pins are connected to test points on the B-U585I-IOT02A (i.e., they are left floating). So, while it is possible to use the MB1183 module with this discovery board, a 1.8V voltage regulator must first be added to provide the proper voltage. Figure 2 shows an example of how this could be done using a small SOT-23-3 device (the [AP2138N-1.8TRG1DICT-ND](https://www.digikey.com/en/products/detail/diodes-incorporated/AP2138N-1-8TRG1/4470802) regulator is shown).

3. The MB1183 module is based on the S5K5CAGA CMOS image sensor, which is poorly supported by ST. Drivers _are_ available from the STM32U5 MCU package and on GitHub, similar to the drivers for the OV5640 sensor. However, the S5K5CAGA drivers are outdated and lack much of the functionality provided by the OV5640 drivers. 

You may also find references to the MB1066 camera module (e.g., in AN5020). The pinout of this module is **not** compatible with the camera module connector on the B-U585I-IOT02A.

 ![b-u585i-iot02a_cameraSilkscreen](https://global.discourse-cdn.com/digikey/original/3X/2/7/27d81ab330045c977cbb162b70c43c1b1d7307b2.jpeg)  
**Figure 1:** The incorrect silkscreen marking for the camera module connector

 ![mb1183Regulator](https://global.discourse-cdn.com/digikey/original/3X/e/c/ece4799af9ce7f9cd743eaf7188bba1156aa60a9.jpeg)  
**Figure 2:** A small voltage regulator (SOT-23-3 package) soldered to the B-U585I-IOT02A to provide 1.8V to Pin 1 on CN7 (required to use the MB1183 camera module)

* * *

# Requirements

To follow along with this tutorial, the following software and hardware are required.

- [STM32CubeIDE](https://www.st.com/en/development-tools/stm32cubeide.html) (version 1.11.0)

- The [B-U585I-IOT02A](https://www.digikey.com/en/products/detail/stmicroelectronics/B-U585I-IOT02A/15218435) discovery board

- The [B-CAMS-OMV](https://www.digikey.com/en/products/detail/stmicroelectronics/B-CAMS-OMV/13677506) bundle

- A Micro-B USB cable (e.g., [993-1294-ND](https://www.digikey.com/en/products/detail/phihong-usa/IP-USB1-C10-S/4935860))

# Procedure

This procedure demonstrates how to configure and initialize the STM32U5 so it is capable of controlling the camera module and reading images from it. It also shows how to add the driver for the camera itself (the OV5640) as well as provides an additional abstraction layer to further simplify the coding. If you don’t care about the CubeMX configuration and would rather get started with a working example, the BSP example in the [STM32CubeU5 MCU package](https://www.st.com/en/embedded-software/stm32cubeu5.html) is what you are looking for.

## Step 1. Create the STM32CubeIDE Project

Of course, you can skip this step if you are starting from an existing STM32U5 project.

1. Start the STM32 project wizard by choosing **File** \> **New** \> **STM32 Project**.

2. Within the _STM32 Project Target Selection_ tool, select the _Board Selector_ tab and choose the B-U585I-IOT02A board (use the board filters on the left-hand side to narrow the search) as shown in Figure 3. Click **Next \>**.

3. Provide a Project Name (e.g., “b-u585i-iot02a\_camera”) and and click **Finish**. During project creation, you should be presented with several prompts. Answer them as follows:

## Step 2. STM32CubeMX Configuration

1. Under the _Pinout & Configuration_ tab, select the **ICACHE** component from the component list on the left-hand side of the perspective and change the _Mode_ to “1-way (direct mapped cache)” (Figure 4).

2. Select the **I2C1** peripheral from the component list. In the Mode panel, change the _I2C_ option to “I2C” and verify that doing so causes pins PB8 and PB9 in the Pinout view to turn green (they should already be supplied with user labels). Optionally, change the _I2C Speed Mode_ to “Fast Mode” in the Configuration panel under the _Parameter Settings_ tab. These steps are presented in Figure 5.

3. From the component list, select the **DCMI** peripheral.

4. Use the Pinout view to set pin PI1 as GPIO\_Input and pins PI2 and PI3 as GPIO\_Output as indicated in Figure 9.

5. Select the **GPIO** peripheral from the component list and configure the PI1, PI2, and PI3 pins to match the signal table shown in Figure 10.

6. From the component list, select **GPDMA1**.

7. From the component list, select **LINKEDLIST**.

8. Select the _Project Manager_ tab and switch to the _Code Generator_ options. Check the box next to “Generate peripheral initialization as a pair of ‘.c/.h’ files per peripheral”. This is demonstrated in Figure 16.

9. Save the \*.ioc file ( **File** \> **Save** ). When prompted “Do you want to generate Code?”, click **Yes**.

## Step 3: Initialize Linked-List

Though we configured the Linked-List in Step 2(g), not all of the initialization code is automatically generated when we save the .ioc file. The Linked-List queue still needs to be associated with channel 12 of the GPDMA by adding the below code snippets to the `main.c` file.

1. In the `main.c` file, add the directive `#include "linked_list.h"` to the `USER CODE Includes` section.

2. In the `USER CODE PV` section, add the following line of code:

3. In the `USER CODE 2` section, add the following lines of code:

## Step 4: Add the Camera Driver

1. First, the OV5640 driver must be obtained/located. It is included in the [STM32CubeU5 MCU package](https://www.st.com/en/embedded-software/stm32cubeu5.html) and is also available from the [stm32-ov5640 GitHub repository](https://github.com/STMicroelectronics/stm32-ov5640). However, because we used STM32CubeIDE to create the project, the STM32CubeU5 MCU package was automatically downloaded and saved to the local STM32Cube Repository. If you’re not sure where the local repository is located on your machine, simply select **Window** \> **Preferences** and select _Firmware Updater_ from the STM32Cube category. The firmware installation repository path is then listed as shown in Figure 18. We’ll use this location in Step 4(d) below.

2. In the Project Explorer, right-click on the Drivers directory and select **Import…** (Figure 19).

3. From the General category, choose **File System** (Figure 20). Click **Next \>**.

4. Navigate to the Drivers directory of the STM32CubeU5 MCU Package (located in Step 4(a) above). Select the ov5640 driver by checking the box next to it (see Figure 21). Click **Finish**.

5. When new header files are added to the project, the path to the folder that contains them must be on the list of include paths to ensure the compiler can find them. In the Project Explorer, right-click on the just added ov5640 directory and choose **Add/remove include paths…** (Figure 22). When the _Select configurations to modify_ dialog box appears, ensure the boxes next to both the “Debug” and “Release” options are checked and click **OK**.

## Step 5: Add the BSP Driver

1. Download the contents of the [stripped-down B-U585I-IOT02A BSP files repository](https://github.com/eewiki/b-u585i-iot02a_bsp_camera) and copy the B-U585I-IOT02A directory into the `<project_name>/Drivers/BSP` directory.

2. Similar to Step 4(e), add this new directory to the list of include paths. That is, right-click on the B-U585I-IOT02A directory in the Project Explorer and choose **Add/remove include paths…**. Then click **OK**. At this point, the project should contain the driver files shown in Figure 23.

3. Finally in the `main.c` file, add the directive `#include "b_u585i_iot02a_camera.h"` to the `USER CODE Includes` section.

# Example Usage

Following the above procedure for configuring/initializing the STM32U5, adding the image sensor driver, and including the BSP abstraction layer allows images to easily be captured from the connected camera module daughterboard. As an example of doing so, the following code initializes the camera, captures a size 640x480 image, and saves it to a data buffer located in SRAM. Because the B-U585I-IOT02A does not include a display, the next section provides one of several possible methods for reading the image from SRAM and viewing it on your PC.

1. Open the `main.c` file in STM32CubeIDE. To the `USER CODE PV` section, add the following lines of code:

2. To the `USER CODE 0` section, add the following lines of code:

3. To the `USER CODE 2` section, add the following lines of code:

The project should build successfully ( **Project** \> **Build Project** ). Notice that the RAM usage is quite high because we are storing raw RGB565 images where each pixel requires 2 bytes of storage. Thus, 640x480 pixels requires 600 KiB of memory (out of the 768 KiB of available SRAM).

## View the Captured Image

The most straightforward way to read data from device memory without the installation of additional software and/or writing additional code is to use the STM32CubeIDE debugging tools.

1. Plug the MB1379 camera module into CN7 camera connector on the B-U585I-IOT02A discovery board. Connect the USB cable between the PC and the discovery board’s CN8 ST-LINK USB connector.

2. Debug the STM32CubeIDE project (i.e., **Run** \> **Debug** ). If this is the first time launching the application, the _Edit launch configuration properties_ dialog box should appear. If it does, simply click **OK**.

3. Once the application is loaded and the debug session begins, the program should suspend at the beginning of the `main()` function. Proceed with the program execution by choosing **Run** \> **Resume**.

4. When the green user LED (LD7) on the B-U585I-IOT02A turns on, the image capture is complete. Suspend the debug session by choosing **Run** \> **Suspend**.

5. As shown in Figure 24, open the _Memory_ view ( **Window** \> **Show View** \> **Memory** ) and click the green plus sign to add a new address to monitor. In the dialog box that appears, type “CameraBuf” into the text field and click **OK**.

6. Click the _Export_ icon in the _Memory_ view. In the _Export Memory_ dialog box that appears, change the _Format_ option to “RAW Binary”. For the _Length_ option, enter the length of the CameraBuf array. Recall that we defined CameraBuf as `uint32_t CameraBuf[640*480/2];`. Therefore,

7. To convert the raw RGB565 image to a usable format, the popular ffmpeg tool can be used. An alternative solution using GIMP is also provided below. Assuming it is installed on your system, open your preferred terminal application and use ffmpeg to convert the RGB565 image to a PNG image using the below command. The output should appear as shown in Figure 26.

8. As an alternative to the ffmpeg method, the GNU Image Manipulation Program (GIMP) can be used to view and manipulate the exported raw RGB565 image instead. Simply save the image with the .data extension (see Figure 25 above) and open it with GIMP. A dialog box like the one shown in Figure 28 should appear. Modify the Image settings as shown and click **Open**.
