Problem measuring distance with HC-SR04

14 views (last 30 days)
Im using an HC-SR04 Ultrasonic sensor to measure distance with an arduino UNO board in simulink. The problem is, when i try to measure distance with the sensor it only measures 0.
I recently changed my computer to Linux (Pop_Os) and ive been having a lot of problems since then.

Answers (1)

Swathi
Swathi on 22 May 2023
Hello Alejandro,
While using the HC-SR04 ultrasonic sensor to measure distance from an obstacle or object while using an Arduino Uno board in Simulink, I presume that you would be using the 'Ultrasonic Sensor' block from the Sensor library of the Simulink Support Package for Arduino Hardware.
In the case of the sensor producing only zeroes, which I believe is not OS or platform dependent, here are a few possible reasons that could be causing the issue:
  1. If the object is placed beyond the range of sensor detection.
  2. The Sample time parameter is not equal to the model base rate.
  3. When the block Sample time and model base rate are not greater than the total time required by the sensor to transmit and receive the sound wave. More information on this can be found at - Calculate the Time of a Sound Wave.
  4. When the model is not run on target hardware or if External Mode is not being used.
Here are some things to try:
  • Check if the Uno board's digital pin values, to which the Trig and Echo pins of the sensor are connected, have been correctly mentioned in the block parameters.
  • Check the voltage of the supply to the sensor and ensure the Vcc pin of the sensor is connected to 5 volts, which is recommended for reliable performance.
I hope this helps.
Swathi.D.

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!