
- Suggested Learning Resources
- ACE-Lab Shop
- …
- Suggested Learning Resources
- ACE-Lab Shop
- Suggested Learning Resources
- ACE-Lab Shop
- …
- Suggested Learning Resources
- ACE-Lab Shop
Ultrasonic Sensor
Developed by Dr James E. Pickering
This exercise shows how to configure an HC-SR04 ultrasonic sensor with an Arduino Uno, acquire distance measurements in Simulink, and apply Last Good Value logic to handle invalid or intermittent sensor readings.
SensorHC-SR04 ultrasonic distance measurementRange0.02 m to 4.00 m operating rangeLogicLast Good Value filtering for invalid readings1. Learning Outcomes
After completing this exercise, learners should be able to acquire distance measurements using a digital ultrasonic sensor and implement simple logic to manage unreliable readings.
Acquire distance measurements using a digital ultrasonic sensor
Explain and demonstrate how an HC-SR04 ultrasonic sensor can be configured with Arduino and Simulink to measure object distance.
Design and implement Last Good Value logic
Build a Last Good Value logic filter to manage erroneous or intermittent readings from a digital sensor.
2. Requirements
Configure the HC-SR04 ultrasonic sensor and Arduino Uno to measure object distance within the specified operating range of 0.02 m to 4.00 m.
Select, configure, and justify an appropriate sampling interval, such as 0.01 seconds, to support responsive measurement performance.
Compare measured distances against at least three known reference distances and comment on accuracy and repeatability.
Implement a Last Good Value filter using suitable Simulink logic blocks.
Experimentally demonstrate that invalid readings hold the last valid measurement and that normal operation resumes once valid data returns.
3. Hardware Set-Up
This exercise involves connecting an HC-SR04 ultrasonic sensor to an Arduino Uno to measure distance. The ACE-Box Base + Sense can be used, but it is not required; the individual components are sufficient.
Required hardware
- Arduino Uno board supported by Simulink
- USB cable Type A to B
- Breadboard
- Ultrasonic HC-SR04 sensor
- 4 male-to-male wires
HC-SR04 ultrasonic specifications
- Distance measurement range: 0.02 to 4.00 metres
- Accuracy: +/-0.003 metres
Connect the ultrasonic sensor directly to the breadboard.
Connect the Trig signal to Arduino pin 5 and the Echo signal to Arduino pin 4.
Connect the VCC terminal of the sensor to the 5 V supply pin on the Arduino Uno.
Connect the GND terminal of the sensor to a GND pin on the Arduino Uno.
Hardware wiring for the HC-SR04 ultrasonic sensor and Arduino Uno. 4. Simulink Set-Up and Distance Measurement Testing
The Ultrasonic Sensor block is available under the Sensors tab in the Simulink Support Package for Arduino Hardware. Display and Scope blocks are available under the Sinks tab.
Add an Ultrasonic Sensor block
Double-click the Ultrasonic Sensor block and set the number of signal pins to 2.
Configure the trigger and echo pins
Configure the block to match the hardware wiring: Trig on pin 5 and Echo on pin 4.
Select a sampling interval
Set a sampling time such as 0.01 seconds, then experiment with different values to determine a suitable setting.
Add display and scope outputs
Connect Display and Scope blocks so that the measured distance can be observed in real time.
Run and test the model
Run the Simulink model and observe the measured distance to the object ahead using the Scope output.
Example Simulink model for reading the ultrasonic sensor and viewing the distance measurement. 5. Last Good Value Logic Filter and Testing
A Last Good Value filter maintains a valid output when the sensor signal becomes unreliable or invalid. It remembers the last valid input value and continues to output this value until a new valid reading is received.
Switch block
The Switch block controls the flow of data based on the sensor output and the selected validity threshold.
Memory block
The Memory block stores the last valid reading so it can be reused when the current sensor output is invalid.
Build the LGV signal flow
Arrange the Ultrasonic Sensor, Switch, Memory, Mux, Display, and Scope blocks as shown in the Simulink diagram.
Configure the Switch block
Double-click the Switch block and set the threshold logic to pass values greater than 0.1.
Add signal viewing blocks
Add a Mux and Scope blocks so that the raw and filtered signals can be viewed in real time.
Deploy and test
Deploy the model and test the operation by moving the sensor within 0.1 metres and then beyond this threshold.
Last Good Value logic filter and example scope response. 6. Additional Exercises
Exercise 1: Sampling Time Investigation
Test at least three different sampling intervals and comment on the performance of the measurements.
Exercise 2: Static Accuracy Check
Measure three known distances and compare the sensor readings against the reference values.
Exercise 3: Improved Validity Logic
Replace the 0.1 threshold with a full range check from 0.02 m to 4.00 m and compare the behaviour.
Exercise 4: Moving Target Test
Move an object towards and away from the sensor and observe signal smoothness and delay.
7. Concluding Remarks
The exercises have demonstrated practical distance measurement using an ultrasonic sensor integrated with Simulink and Arduino hardware. The exercise reinforces correct hardware configuration, real-time data acquisition, and validation of sensor signals.
The implementation of the Last Good Value filter highlights the importance of basic signal conditioning to improve measurement reliability. These principles form the foundation for closed-loop control systems and autonomous sensing applications such as parking aids.
Advancing automatic control engineering (ACE) education through global collaboration


