How to Create a Library for sensors Part 1: Getting the Required information

In this series guide, we’ll explore building a dedicated, lightweight library to interface with the ADXL345 accelerometer sensor using the I2C protocol on the STM32F4 microcontroller. Creating a custom library from scratch not only provides greater control over sensor configuration and data handling but also allows for easy integration across multiple projects and applications. This hands-on approach will equip you with a strong foundation for developing driver libraries for various I2C-based peripherals in the future.

In part 1, we shall cover the following:

  • Gathering the required information.
  • Connection.
  • STM32CubeIDE Setup.

1. Gathering the Required Information:

Since this guide is focused on creating custom library for adxl345, we shall start from the datasheet of the sensor. The datasheet of the sensor can be found here.

From the datasheet, we can find the following:

From the features section, we can find the following:

  • 10-bit resolution with ability to reach 13-bit for higher g.
  • Works from 2.0 to 3.6V which is the operating voltage of STM32F4 of 3.3V.
  • Can work with SPI and I2C mode. In this guide, we are focusing on I2C interface.
  • It has ability to generate interrupt when new data is available.

From the I2C section of the ADXL345, we can find the address for read and write and also the operating frequency.

  • Can work with standard speed of 100KHz and fast mode of 400KHz. This guide shall focus on using the fast mode.
  • Address of 0x53 when the SDO/ALT to ground.

Since the module used in this guide the address is 0x53, the library we shall build will have the ability to accept the other address in case this address will conflict with the other slave device connected to the I2C bus.

For now, this is the basic information to get the device connected to our STM32F4.

2. Connection:

Since the board used in this guide is STM32F411RE Nucleo-64.

The board features Arduino Uno pinout, D14 and D15 features I2C pins as shown below:

These pins can be found in the pinout in the user manual of STM32 Nucleo-64 boards (MB1136) as following:

  • PB8(D15) is SCL which is Serial Clock.
  • PB9(D14) is SDA which is Serial Data.

Hence, the connection as following:

3. STM32CubeIDE Setup:

Open STM32CubeIDE after selecting the workspace and create new project as following:

Select the MCU:

Give the project a name:

Make sure the Targeted Project Type is STM32Cube, Language is C and binary type is Executable.

After that, set PB8 and PB9 for I2C1 or I2C3, here we shall use I2C1 as following:

Also, set PA2 and PA3 for Serial2 since they are linked with VCP of onboard ST-Link.

Enable UART as following:

Next Enable I2C1 as following:

From parameter Settings of I2C1 enable the fast mode as following:

From Clock Configuration tab, set the frequency to maximum of 100MHz as following:

From Project Manager tab, select Code Generation and enable Generate peripheral initialization as pair of .c/.h files per peripheral as following:

Thats all for the configuration.

Save the project and this will generate the project.

In part 2, we start developing the library.

Happy coding 😉

Add Comment

Your email address will not be published. Required fields are marked *