Skip to content

Interfacing the BME280 and LCD with the STM32-F103RB Nucleo Board

Introduction

In this guide, we will learn how to interface the BME280 sensor and an LCD (Liquid Crystal Display) with the STM32-F103RB Nucleo Board. By combining these components, we can acquire environmental data and display it on the LCD screen in a user-friendly format. The BME280 is a versatile environmental sensor that provides measurements for temperature, humidity, and barometric pressure.

Problem Statement

The objective of this project is to interface the BME280 sensor and an LCD with the STM32-F103RB Nucleo Board. By doing so, we can acquire temperature, humidity, and barometric pressure data from the sensor and display it on the LCD screen.

Requirements

Follow the circuit diagram below to properly connect the BME280 and LCD module to the STM32-F103RB :

  1. Nucleo-F103RB board
  2. BME280
  3. 16 x 2 LCD
  4. Potentiometer (10K Ohms)
  5. Jumper wires
  6. Breadboard

Circuit Diagram

Follow the circuit diagram below to connect the BME280 and LCD to the Nucleo board:

  1. Connect the RS (Register Select) pin of the LCD to PB0 Pin of the board.
  2. Connect the RW (Read/Write) pin of the LCD to PB1 Pin of the board.
  3. Connect the EN (Enable) pin of the LCD to PB2 Pin of the board.
  4. Connect the D4 pin of the LCD to PB4 Pin of the board.
  5. The D5 pin of the LCD to PB5 Pin of the board.
  6. Connect the D6 pin of the LCD to PB6 Pin of the board.
  7. And the D7 pin of the LCD to PB7 Pin of the board.
  8. Also connect the SD0 and SCK of the BME280 to the SDA Pin (PB11) and SCL Pin (PB10) of the Nucleo Board. And connect the power pins of the sensor to the nucleo board.
 STM 32

Project Configuration

Follow these steps to configure the .IOC file and set up GPIOs and Clock :

  1. Open the STM32 Cube IDE and start by creating a new STM32 project. Click on the board selector tab in the target selection window.

  2. Enter "NUCLEO-F103RB" as the commercial part number and click on "Next" after selecting the board.

 STM 32
  1. Enter the Project Name "bme280_lcd" and click on "Finish". After that, click on "Yes" to initialize all the peripherals in default mode.
 STM 32
  1. Pinout & Configuration: In the Pinout & Configuration menu, select "TIM1" from the Timers section. This will configure Timer 1 for generating delay in micro seconds.

  2. Clock Source: Choose "Internal Clock" as the Clock Source to utilize the internal clock of the STM32-F103RB Nucleo Board.

  3. Timer Configuration: In the Configuration part of the Timers section, set the prescaler value as "72-1". The prescaler determines the frequency of the timer clock. Here, we divide the timer clock frequency by 72.

  4. Next, specify the Counter Period as "65535". The counter period defines the overall period of the timer, this will be useful in writing a delay function in LCD.

 STM 32
  1. To configure the GPIO pins for output, click on the GPIO pin in the Pinout & Configuration tab. Select the "GPIO_Output" option from the available options. To add a user label, right-click on the GPIO pin and select the "User Label" option and enter the desired name for the label.

  2. Configure the GPIO pins highlighted in the 4th and 5th boxes in the image above as output, and rename them as shown below. These labels are predefined in the provided library

 STM 32
  1. Go to the connectivity section to enable the I2C communication. We will use I2C on pin PB10 and PB11, enable it in the Mode tab and in the configuration tab, keep the default settings.
 STM 32
  1. After finishing the Pinout Configuration, go to the Clock Configuration menu. In the HCLK field, enter "72 MHz" as the desired frequency for the HCLK (system clock). The Integrated Development Environment (IDE) will automatically search for the necessary clock sources and prescalers based on this configuration.

  2. Next, locate the gear-like icon on the toolbar and click on it. This will add the configuration settings we did in the GUI to the code.

 STM 32

Code

Now, let's proceed to understand and write the C code to interface the BME280 and LCD. Follow the steps below:

  1. Open the main.c file, which is automatically opened by the Device Configuration Tool.

  2. Include the necessary header file for the LCD module. Include the source file for the LCD module.

Download the "BME280_STM32.h", "BME280_STM32.c", "lcd_1602.c" and "lcd_1602.h" file from this link and add it to your project just like we did in previous guide's. Ensure that the project structure includes the added header and source files. And your project structure should look as below:

 STM 32
  1. Now replace the below code into your main.c file before the SystemClock_Config function.

    Note: You can download the full project code from this link.

c
#include "main.h"

#include "lcd_1602.h"

#include "BME280_STM32.h"

#include <stdio.h>

#include <string.h>

/* Private variables ---------------------------------------------------------*/

I2C_HandleTypeDef hi2c2;

TIM_HandleTypeDef htim1;

UART_HandleTypeDef huart2;





/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_USART2_UART_Init(void);

static void MX_I2C2_Init(void);

static void MX_TIM1_Init(void);



char Temperature_S[20], Pressure_S[20], Humidity_S[20];

float temperature, humidity, pressure;





int main(void) {



    /* MCU Configuration--------------------------------------------------------*/



    /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

    HAL_Init();



    SystemClock_Config();



    /* Initialize all configured peripherals */

    MX_GPIO_Init();

    MX_USART2_UART_Init();

    MX_I2C2_Init();

    MX_TIM1_Init();

    /* USER CODE BEGIN 2 */

    HAL_TIM_Base_Start(&htim1);

    BME280_Config(OSRS_2, OSRS_16, OSRS_1, MODE_NORMAL, T_SB_0p5, IIR_16);

    lcd_init();

    lcd_put_cur(0, 0);

    lcd_send_string("INTIALISING>>>>>>");

    HAL_Delay(1000);



    /* USER CODE BEGIN WHILE */

    while (1) {

        /* USER CODE END WHILE */



        /* USER CODE BEGIN 3 */

        BME280_Data measurement = BME280_Measure();

        // Access the temperature, pressure, and humidity values

        temperature = measurement.temperature;

        pressure = measurement.pressure;

        humidity = measurement.humidity;

        // Convert float to string

        sprintf(Temperature_S, "%.2f", temperature);

        sprintf(Pressure_S, "%.2f", pressure);

        sprintf(Humidity_S, "%.2f", humidity);

        // Display the values

        lcd_put_cur(0, 0);

        lcd_send_string("Temp: Pres: Hum:");

        lcd_put_cur(1, 0);

        lcd_send_string(Temperature_S);

        lcd_send_string(" ");

        lcd_send_string(Humidity_S);

        lcd_send_string(" ");



        lcd_send_string(Pressure_S);

        HAL_Delay(500);

    }

    /* USER CODE END 3 */

}
  1. After pasting the above code, ensure that you have connected your STM32-F103RB Nucleo Board to your PC/Laptop using a USB cable.

  2. Next, click on the "Run and Debug" icon in the toolbar menu of the IDE (Refer to image below ). This will initiate the build process for your project. The IDE will compile the code. Once the build process is successful, the IDE will start flashing the compiled project to the STM32-F103RB Nucleo Board.

 STM 32

Output

Upon successful execution of the project, you can observe the sensor readings on the LCD. The temperature, humidity, and pressure values will be continuously updated on the LCD screen.

Conclusion

In this guide, we have learned how to interface the BME280 sensor and an LCD with the STM32-F103RB Nucleo Board. By combining the capabilities of the BME280 sensor and the LCD, you can easily monitor environmental conditions and display the sensor readings in a user-friendly format. This opens up possibilities for creating projects related to weather monitoring, indoor climate control, and other applications where environmental data is essential.