Tiva Lab 10: Using Ultrasonic Sensor for Distance Determination

Objective

 

Required Reading Materials

 

Background Information

An ultrasonic sensor is a great tool for all kinds of projects that need distance measurements. Obstacle avoidance is one example.

Ultrasonic sensor HC-SR04 is used in this lab to measure distance in the range of 2 cm to 400 cm with an accuracy of 3 mm. The sensor module consists of the control circuit and two ultrasonic sensors — one is the transmitter, and another is the 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 the 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 taking 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

EK-TM4C123GXL LaunchPad

 

Connect the ultrasonic sensor with your Tiva TM4C123G LaunchPad

  • Connect the "Trig" pin to GPIO PortF[3]
  • Connect the "Echo" pin to Timer1A on PortF[2] with a 200Ω or 220Ω resistor
  • Connect the "Vcc" pin to +5V or VBUS on the LaunchPad
  • Connect the "Gnd" pin to the GND pin on the LaunchPad

Ultrasonic 123G s

 

Table 1: Pin Configurations:

DevicePort.Pin Signal TypePCTLDirectionDrive Mode

 

Procedure

  1. Create a new folder under the EE3450 folder and name it Lab10_Ultrasonic.
  2. Launch the Keil μVisio and create a new project. Save the project to the project folder you just created in the previous step and set the project name to Lab10_Ultrasonic.
  3. Add the Common and ezTivaLIB folders to the include paths, that is under the "Options for Target" setting.
  4. Add ezTiva LIB (ez123GLIB.lib or ez1294LIB.lib) into your project, increase the stack and heap size under the "startup_TM4cXXX.s (Startup)" setting.

MyDefines.h

Add the following definitions to the MyDefines.h file:

MyDefines TIMER 0
MyDefines TIMER 0

 

Configurations

Write down the following configuration information in your report.

Example Source Code

Lab Experiments

Questions

  1. In the UART protocol, how many bits 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?