HC-SR04 STM32l476 Interfacing:Object Detector Device

Overview

HC-SR04 is a non-contact ultrasonic ranging module that can be used for object detection and range measurements. The HS-SR04 is an ultrasonic sensor that works on the principle of pulse-echo ultrasonic ranging. The modules include ultrasonic transmitters, receivers and a control circuit.




HS-SR04 Working Principle:

  • The HC-SR04 emits a 40 KHz ultrasonic pulse from its trigger pin.
  • The sound wave bounces off the target and returns as an echo to the sensor.
  • The Echo pin on the HC-SR04 module turns high, indicating that the sound wave has reached the target and the echo has been received.
  • Distance is calculated by formulae


      Distance =  (Echo high level time×velocity of sound (340M/S) / 2
or
Echo high-level time/58.0


                                                        Image Source: https://cdn.sparkfun.com/datasheets/Sensors/Proximity/HCSR04.pdf

  • The Timing diagram is shown above. To begin ranging, simply apply a short 10uS pulse to the trigger input, and the module will send an 8-cycle burst of ultrasound at 40 kHz and raise its echo.


The HC-SR04 is a simple and inexpensive means to properly measure distances, and it is often used in applications such as robots, obstacle avoidance systems, Range finders, Object Detection, and automation systems. For further information about the HC-SR04 refers to the datasheet, links are provided in the reference below.

Example code is built using the MBED OS platform and the Nucleo-l476RG board to demonstrate how the ultrasonic sensor HS-SR04 can be interfaced with the Micro Controller.


MBBED OS

MBED OS is an open-source operating system created primarily for IoT devices and embedded systems. It is based on the ARM Cortex-M microprocessor architecture and includes a collection of libraries and tools for developing, deploying, and supporting IoT applications. Overall, MBED OS offers a complete solution for developing and deploying IoT applications, allowing developers to focus on application development rather than low-level hardware and system software problems.


Hardware details


  • Board:NUCLEOL476RGV(STM32L476 development board)
  • Sensor Module: HS-SR04


Pin Connections:



Firmware Design

The trigger pulses in the following code are generated in regular intervals in accordance with the timeout specified in the code. The object's distance is printed on  Nucleo Virtual COM Port if it exceeds the threshold value.




Please Refer to the GitHub link below for the source code:-


Test Results:




References:





Comments