Tiva Lab 10: Using Ultrasonic Sensor for Distance Determination


Required Reading Materials

Background Information

Ultrasonic sensor is a great tool for all kinds of projects that need distance measurements. Obstacle avoidance is one of the examples.

Ultrasonic sensor HC-SR04 is used in this lab to measure distance in range og 2 cm to 400 cm with accuracy of 3 mm. The sensor module consists of the control circuit and two ultrasonic sensors — one is transmitter, another is receiver.

How Ultrasonic Sensors Work

An ultrasonic burst of energy is emitted from the transducer. This is known as a ping. The sound waves travel until reflected off of an object. The echoed sound wave returns to the transducer. The echo may be of smaller amplitude, but the carrier frequency should be the same as the ping. An external timer records the time of flight (the time that the sound waves take to travel to and from the object), which can be converted to distance when considering the speed of sound in air. As the transmitted sound waves propagate from the transducer, they spread over a greater range. In other words, the sound waves propagate from the transducer in the shape of a cone of angle q.

Limitations of Sonar Sensors

Sonar sensors are not ideal devices. They are limited to resolution, range, and the size of object they can detect. The external timing circuits of some sonar sensor systems are subject to false echoes. Values returned by the sensor may not match the actual distance of the object. One solution is to take an average of your readings. For example, ping three times and take an average. This method seems to reduce the effects of false triggers.

In this lab, you will learn Timer Input Capture and Universal Asynchronous Receiver transmitter (UART) communication methods. You will use Timer Input Capture to measure an input pulse width that is directly proportional to the distance measured by an ultrasonic sensor, and then send the result to the PC through the UART.

Required Components List

hc sr04 ultrasonic range finder s HC-SR04 Ultrasonic Sensor Module x 1
breadboard s Breadboard x 1
Resistor 64 One 200Ω or 220Ω resistor x 1

Circuit / Schematic Diagram and Sample Firmware Code


Write down the following configuration information into your report.


Distance Determination

SONAR (Sound Navigation and Ranging) uses ultrasonic signal to determine the presence and distance of objects. The HC-SR04 sensor module includes ultrasonic transmitter, receiver and control circuit. The ultrasonic transmitter emits 40 KHz sound pulses, and the receiver receives the return echo signal. The idea behind distance estimation is that objects reflect sound, and that the time between emitting a sound pulse and receiving an echo can be translated into distance given the speed of sound through air.

The HC-SR04 sensor documentation provides detailed description of how it operates, but a few points are worth emphasizing here:

  • The HC-SR04 sensor uses two signal pins, one is for trigger and another is for receiving echo signal. The "trigger" pin is connected to the output pin of GPIO, and the "echo" pin is attached to the Input Capture pin of Timer with a 200Ω (or 220Ω) resistor to protect the Tiva LaunchPad.
  • The HC-SR04 sensor must be "triggered" in order to enable pulsing. Therefore, you need to set the GPIO output pin high and then low. The sensor documentation discusses how long the trigger pulse should be.
  • The echo signal received from the sensor will rise and then fall on the "echo" pin. The distance between these two edges is directly proportional to the round-trip distance between the sensor and the object. Input capture when properly configured to Edge-Time mode will identify the edges of the signal and set TIMERn_TAR_R to the current counter value when the events/edges are detected. The difference in counter values represents the signal's pulse width as time. The actual amount of time can be determined when the system clock frequency is used to compute the amount of time each counter value represents.
  • After you calculate the value, send the distance in centimeters (cm) to your PC through UART.


  1. In UART protocol, how many bit are NECESSARILY added into a data package for the interfacing to be successful? What is the function of those bits?
  2. Assume the system clock is 12 MHz. What is the baud rate if UART0_IBRD_R equals 2 and UART0_FBRD_R equals 32?
  3. Assume the system clock is 32 MHz. What values should you put in UART0_IBRD_R and UART0_FBRD_R to make a baud rate of 115200 bit/sec?
  4. A UART is configured as 1000,8N1, what is the bandwidth in bytes/sec?
  5. Two devices are using the UART protocol to exchange data. Describe what happens if device A sends data using a baud rate that is twice as fast as the received by device B?
  6. Two devices are using the UART protocol to exchange data. Describe what happens if device B receives data using a baud rate that is twice as fast as the sent by device A?