Skip to content

Interfacing IR sensor with the STM32-F103RB Nucleo Board

Introduction

In this guide, we will explore how to interface an IR (infrared) sensor with the Nucleo Board. The IR sensor can detect infrared radiation and is commonly used for proximity sensing, object detection, and other applications.

Problem Statement

The objective of this project is to interface an IR sensor with the STM32-F103RB Nucleo Board and a buzzer. By accomplishing this, we aim to detect the presence or absence of infrared radiation using the IR sensor and utilize the Nucleo Board to process the sensor data. The project also involves activating the buzzer to generate an audible alert based on the sensor's reading.

Requirements

To successfully complete this project, you will need the following components:

  1. STM32-F103RB Nucleo Board
  2. Buzzer
  3. IR Sensor
  4. Breadboard
  5. Jumper wires

Circuit Diagram

To interface the IR sensor and buzzer with the Nucleo Board, follow the circuit diagram below:

  1. Connect the signal pin of the Buzzer to the PA8 pin and the other power pins of buzzer to Nucleo board.
  2. Connect the signal/output pin of the IR Sensor to PC13 pin of the STM32-F103RB Nucleo Board.
  3. Also connect the other terminals of the IR sensor to the STM32-F103RB 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 "ir_proximity" and click on "Finish". After that, click on "Yes" to initialize all the peripherals in default mode.
 STM 32
  1. In the Pinout view of the STM32 Cube IDE, locate the PC13 pin. Select the "GPIO_Input" option for PC13 and name it as "IR". Locate the PA8 pin and select the "GPIO_Output" option for PA8. Name it as "BUZZ".

  2. Leave the rest of the configuration settings as default.

 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 detect the objects using IR Sensor. Follow the steps below:

  1. The Device Configuration Tool will automatically open the main.c file for you. In the project, you will find all the necessary pin and port names defined in the Core ⇾ Inc ⇾ main.h file. These definitions will help us easily reference the pins and ports in our code.

  2. Navigate to the main function in the main.c file and paste the following code:

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

c
int main(void)
{ /* 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();
    /* USER CODE BEGIN 2 */
    uint8_t IR_val = 0;
    /* USER CODE END 2 */
    /* Infinite loop */
    /* USER CODE BEGIN WHILE */
    while (1)
    {
        /* USER CODE END WHILE */
        /* USER CODE BEGIN 3 */
        IR_val = HAL_GPIO_ReadPin(IR_GPIO_Port, IR_Pin); // read the value of the button

        if (IR_val)
        {
            HAL_GPIO_WritePin(BUZZ_GPIO_Port, BUZZ_Pin, GPIO_PIN_SET); // turn on the LED
        }
        else
        {
            HAL_GPIO_WritePin(BUZZ_GPIO_Port, BUZZ_Pin, GPIO_PIN_RESET); // turn off the LED
        }
    }
    /* 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 successfully running the project, the sensor will detect the presence or absence of an object and the buzzer will turn on. To increase the range of detection, you can try rotating the potentiometer present at IR sensor

Conclusion

In this guide, you have learned to interface an IR sensor with the STM32-F103RB Nucleo Board. By configuring the GPIO pin as an input and connecting it to the IR sensor's output, you can detect and process infrared radiation using the Nucleo Board. This project opens up possibilities for various applications, such as proximity sensing, object detection, and automated systems.