Skip to content

Interfacing the Water Level Sensor with the STM32-F103RB Nucleo Board

Introduction

In this guide, we will learn how to interface a water level sensor and LCD with the STM32-F103RB Nucleo Board. The water level sensor is a crucial component for monitoring the water level in tanks, reservoirs, or any other liquid containers. By interfacing the water level sensor with the STM32-F103RB Nucleo Board and displaying the data on an LCD screen, we can easily monitor and manage water levels in various applications.

Problem Statement

The objective of this project is to interface a water level sensor and LCD with the STM32-F103RB Nucleo Board and display information on the screen. By accomplishing this, you will gain hands-on experience in using the ADC of the board.

Requirements

Follow the circuit diagram below to properly connect the water level sensor to the STM32-F103RB :

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

Circuit Diagram

Follow the circuit diagram below to connect the LED to the Nucleo board:

  1. Connect the RS (Register Select) pin of the LCD to GPIO 4 (D4) of the board.
  2. Connect the EN (Enable) pin of the LCD to GPIO 5 (D5) of the board.
  3. Connect the D4 pin of the LCD to GPIO 18 (D18) of the board.
  4. Connect the D5 pin of the LCD to GPIO 19 (D19) of the board.
  5. Connect the D6 pin of the LCD to GPIO 21 (D21) of the board.
  6. And connect the D7 pin of the LCD to GPIO 22 (Pin 22) of the board.
  7. Also connect the Sig pin from the sensor to the PA0 Pin of the board, and other power pins from 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 "water_level" 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.

  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.

  5. To set up PA0 as ADC Input, Open the Pinout and Configuration menu in the STM32 Cube IDE. Select ADC1 from the available options.

  6. In the ADC1 Mode tab, locate the IN0 option and click on it to enable it. This will configure PA0 as the ADC input. Keep the parameter settings in the config tab to their default values, as they are suitable for most applications.

 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.

  3. Finally, configure the PA1 as GPIO_Input, keeping all the other configuration for the pin to 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 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.

  3. Download the "lcd_1602.c" and "lcd_1602.h" file from this link and add it to your project. Ensure that the project structure includes the added header and source files. And your project structure should look as below:

 STM 32
  1. After adding the header and source file, copy the below code into your main.c file.

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

c
#include "main.h"



/* Private includes ----------------------------------------------------------*/

/* USER CODE BEGIN Includes */

#include "lcd_1602.h"

#include <stdio.h>

#include <string.h>

/* USER CODE END Includes */



ADC_HandleTypeDef hadc1;

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_ADC1_Init(void);

static void MX_TIM1_Init(void);





int main(void)

{

    /* USER CODE BEGIN 1 */

    int ADC_VAL;

    char val[20];

    /* USER CODE END 1 */

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

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

    HAL_Init();



    /* Configure the system clock */

    SystemClock_Config();



    /* Initialize all configured peripherals */

    MX_GPIO_Init();

    MX_USART2_UART_Init();

    MX_ADC1_Init();

    MX_TIM1_Init();

    /* USER CODE BEGIN 2 */

    HAL_TIM_Base_Start(&htim1);

    lcd_init();

    lcd_send_string("Intialising");

    HAL_Delay(1000);

    lcd_clear();

    /* USER CODE END 2 */

    /* Infinite loop */

    /* USER CODE BEGIN WHILE */

    while (1)

    {

        /* USER CODE END WHILE */



        /* USER CODE BEGIN 3 */

        HAL_ADC_Start(&hadc1);

        HAL_ADC_PollForConversion(&hadc1, 1000);

        ADC_VAL = HAL_ADC_GetValue(&hadc1);

        HAL_ADC_Stop(&hadc1);

        sprintf(val, "%d", ADC_VAL);



        lcd_put_cur(0, 0);

        lcd_send_string("value:=");

        lcd_put_cur(8, 0);

        lcd_send_string(val);

        HAL_Delay(1000);

        lcd_clear();

    }

    /* 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, the STM32-F103RB Nucleo Board will continuously monitor the water level. As the water level changes, the readings will reflect the current water level in the container or reservoir being monitored.

Conclusion

In this guide, we have learned how to interface a water level sensor with the STM32-F103RB Nucleo Board. By reading the sensor data and displaying it on an LCD screen, we can accurately monitor the water levels in tanks or containers. This project enables efficient water management and helps prevent overflow or depletion. The interfacing process and code provided in this guide allow for easy implementation of the project. By experimenting with different water levels, users can observe the corresponding readings and understand the behavior of the water level sensor.