You can now use your project to make perfect shelves.Introduction:

This project helps you measure the angle of a surface with respect to earth's gravitational field using Hexabitz modules.

If the device measures an angle of 0.0 something degrees it means you have done a nice job at leveling the desk ^_^

The project is also about designing a real product by yourself, this product is similar to another on the market as the search engines showed.

The project is stand-alone device, inclinometer and display built into the same product.

https://hackster.imgix.net/uploads/attachments/1518087/image_xziRql6vmh.png?auto=compress%2Cformat&w=1280&h=960&fit=max

In this tutorial, I will take you through the steps that I followed to make this awesome project. I hope you will like it.

🛠️ Tools:

1. STLINK-V3MODS Programmer (H40Rx):

H40Rx is a programmer module which contains STLINK-V3MODS stand-alone debugging and programming mini probe for STM32 micro-controllers (Hexabitz modules and other MCUs).

It supports the SWD (Serial Wire Debugging) interface for the communication with any STM32 micro-controller located on an application board.

It also provides bridge interfaces to several communication protocols, allowing for instance the programming of the target through boot-loader.

https://hackster.imgix.net/uploads/attachments/1518095/fqyyyalxmauxr_i_voKff2aFUp.jpg?auto=compress%2Cformat&w=740&h=555&fit=max
2. Hexabitz IMU module (H0BR40):

H0BR4x is a 3-axis inertial measurement unit (IMU) combined with a 3-axis digital compass module based on STM32F0 MCU, LSM6DS3 IMU and LSM303AGR compass.

3. 6 Digit Seven Segment (H3BR6x):

H3BR6x is a 6 digit seven segment interface module based on STM32G0 MCU.

https://hackster.imgix.net/uploads/attachments/1518090/efvmqjywkaij_ax_TMcyMNECfr.jpg?auto=compress%2Cformat&w=740&h=555&fit=maxhttps://hackster.imgix.net/uploads/attachments/1518092/efvmijswaakcuga_St2Jlpjc6Q.jpg?auto=compress%2Cformat&w=740&h=555&fit=maxhttps://hackster.imgix.net/uploads/attachments/1518091/efvmij_wkaa9npt_2NAgr93VH1.jpg?auto=compress%2Cformat&w=740&h=555&fit=max

Measure Tilt Angle Using Accelerometer:

The H0BR4x consist of 3-axis Accelerometer with Micro Electro Mechanical (MEMs) technology. It used to detect the angle of tilt or inclination along the X, Y, and Z axes as shown in the below figures.

https://hackster.imgix.net/uploads/attachments/1518171/image_hdG8rNF98y.png?auto=compress%2Cformat&w=740&h=555&fit=maxhttps://hackster.imgix.net/uploads/attachments/1518172/hnjghjg_Hwy1nV9GXR.png?auto=compress%2Cformat&w=740&h=555&fit=max

Port Buttons/Switches API:

You can connect any mechanical button or switch to any of the array ports at the modules using Hexabitz APIs.

Check out this article for Port Buttons/Switches

Steps 📝

1) Plan the array and assemble the hardware:

We prepare the project components and plan our array design by aligning modules side-by-side. Then we solder the modules together using Hexabitz Fixture.

https://hackster.imgix.net/uploads/attachments/1518191/dfgdfgdsdfsfsdfdsf_HXA9pOvy9Z.png?auto=compress%2Cformat&w=740&h=555&fit=max

2) Writing codes with STM32CubeIDE software:

Check out this article for writing code with STM32CubeIDE.

· Fixed topology file: Inter-array communication in Hexabitz is done using a routing table stored in a special header (.h) file. This header file describes the number of modules and how they are connected to each other as well as other important information for the array; hence, it is called a topology header file. Currently, you need to make a topology file manually.

After creating the topology for the two modules (H0BR4x, H3BR6x) and determining their configurations, we add the topology for each module. See this link for help in creating a topology file.

/* BitzOS (BOS) V0.2.7 - Copyright (C) 2017-2022 Hexabitz All rights reserved
 File Name     : topology.h Description   : Array topology definition.
 */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __topology_H
#define __topology_H
#ifdef __cplusplus extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "stm32f0xx_hal.h"

#define __N    2                    // Number of array modules

// Array modules
#define _mod1    1<<3
#define _mod2    2<<3

// Topology
static uint16_t array[__N ][7] ={
    {_H0BR4, 0, 0, _mod2 | P3, 0, 0, 0},                                  // Module 1
    {_H3BR6, 0, 0,_mod1 | P3, 0, 0, 0},                                     // Module 2
};

// Configurations for duplex serial ports
#if ( _module == 1 )
    #define    H0BR4    1
    #define    _P1pol_normal    1
    #define    _P2pol_normal    1
    #define    _P3pol_normal    1
    #define    _P4pol_normal    1
    #define    _P5pol_normal    1
    #define    _P6pol_normal    1
#endif

#if ( _module == 2 )
    #define    H3BR6            1
    #define    _P1pol_normal    1
    #define    _P2pol_normal    1
    #define    _P3pol_reversed    1
    #define    _P4pol_normal    1
    #define    _P5pol_normal    1
    #define    _P6pol_normal    1

#endif


#ifdef __cplusplus
}
#endif
#endif /*__ topology_H */

/************************ (C) COPYRIGHT HEXABITZ *****END OF FILE****/

Then go to project.h as shown in the photo below and UN-comment the header.

https://hackster.imgix.net/uploads/attachments/1518193/image_hUvRImEU8u.png?auto=compress%2Cformat&w=740&h=555&fit=max

Press on main.c and define sensor accelerometer variables, tilt angle variable and button state variable in the main.c file of the module IMU (H0BR40):

float x, y, z, ang2;
int button1 = 0;

Define the button and link it to its callbacks in the UserTask function like this:

AddPortButton(MOMENTARY_NO,P1); // Define a button connected to port P1
SetButtonEvents(P1, 1, 1, 0, 0, 0, 0, 0, 0, 0); // Activate a click events

These events can be linked to user callbacks to execute some tasks. Add the following callback to your code in main.c to make use of button events:

void buttonClickedCallback(uint8_t port)
{
if(port == P1)     {
button1 = 1; IND_blink(200);    }
Delay_ms(10);
}

Then we obtain the filtered and calibrated values of the accelerometer in units of g using the following API from the H0BR40 factsheet:

https://hackster.imgix.net/uploads/attachments/1518226/image_PKEwvzXPVJ.png?auto=compress%2Cformat&w=740&h=555&fit=max

In the UserTask function, we now write our repeated code that checks by the state of the button variable.

If the button is not pressed, the tilt angle appears in degrees where a message is sent to the Seven Segment (H3BR6x) module. If the button is pressed, the angle of inclination is shown in radian unit where a message is sent to the Seven Segment (H3BR6x) module too.

https://hackster.imgix.net/uploads/attachments/1518229/image_LbudbjQ1oy.png?auto=compress%2Cformat&w=740&h=555&fit=max

These values were first monitored using the STM32CubeIDE debugging function to monitor live expressions.

while(1){        SampleAccG (&x, &y, &z);        if (button1 == 0){            ang2 = (atan2(x,z))* 57.3;
            memcpy(&messageParams[0], &ang2, 4);
            messageParams[4] = 2;
            messageParams[5] = 'd';
            messageParams[6]=0;
            SendMessageToModule(2,CODE_H3BR6_SevenDisplayQuantities,7);    }        if (button1 == 1){            ang2 = atan2(x,z);            memcpy(&messageParams[0], &ang2, 4);            messageParams[4] = 2;            messageParams[5] = 'r';            messageParams[6]=0;            SendMessageToModule(2,CODE_H3BR6_SevenDisplayQuantities,7);    }
    }

In the future, we are going to build a Digital Inclinometer which can be monitored using an Android application and H23R3(BlueNRG-M2 BLE 5) module 🙂📲https://hackster.imgix.net/uploads/attachments/1518235/1667312783083_NYFCthBsuy.jpg?auto=compress%2Cformat&w=740&h=555&fit=maxThe reason for planning to using a remote display like a mobile phone is that we can monitor the values from IMU without having to look at the hardware, this would come very handy when the IMU is placed on a drone or some other inaccessible locations[4].

https://hackster.imgix.net/uploads/attachments/1518177/image_LeQYYaiX9n.png?auto=compress%2Cformat&w=740&h=555&fit=max

3) Test the System 📉 🧐 📐 🤓 ☑️

https://hackster.imgix.net/uploads/attachments/1518241/rdgydrgtdr_HFYt0hp29m.PNG?auto=compress%2Cformat&w=740&h=555&fit=maxhttps://hackster.imgix.net/uploads/attachments/1518240/dsadadadada_ODr2Hu1Sed.PNG?auto=compress%2Cformat&w=740&h=555&fit=maxYou can use DC-DC power module (H03R0) with 9V battery to power your project. Note that it's better to write the code using power from USB cable connection without DC-DC power module (to avoid power confusion and reset states). Once done, you can insert the batteries and then remove the USB cable.https://hackster.imgix.net/uploads/attachments/1518260/screenshot_-_HX6K1iwSq1.png?auto=compress%2Cformat&w=740&h=555&fit=max

I recycled a paper box to enclosure inclinometer, Because I care to the UN's Sustainable Development Goals (SDGs).

https://hackster.imgix.net/uploads/attachments/1518259/img_20221103_185557_YktZWueZQT.jpg?auto=compress%2Cformat&w=740&h=555&fit=maxhttps://hackster.imgix.net/uploads/attachments/1518257/img_20221103_182453_Gm57N89WQy.jpg?auto=compress%2Cformat&w=740&h=555&fit=max

https://hackster.imgix.net/uploads/attachments/1518256/img_20221104_100323_tLY4SaW6MN.jpg?auto=compress%2Cformat&w=740&h=555&fit=maxYou can now use your project to make perfect shelves.

https://hackster.imgix.net/uploads/attachments/1518254/fdsfsdf_bShiBSXKlp.PNG?auto=compress%2Cformat&w=740&h=555&fit=max

https://hackster.imgix.net/uploads/attachments/1518253/dsdsdadsd_1PentsGlRW.PNG?auto=compress%2Cformat&w=740&h=555&fit=maxhttps://hackster.imgix.net/uploads/attachments/1518258/img_20221103_182502_Itp2vElO0r.jpg?auto=compress%2Cformat&w=740&h=555&fit=maxHappy Doing It Yourself 😊

References:

[1] https://hexabitz.com/modules/

[2] https://hexabitz.com/wiki/

[3] https://hexabitz.com/product-category/accy/

[4] https://circuitdigest.com/microcontroller-projects/arduino-inclinometer-using-mpu6050