PSoC Lab 11: Ultrasonic Sensor with Timer



  • To learn how to use Timer-Capture to measure the pulse

Required Reading Material(s)

  • Datasheet


Background Information

"Ultrasonic" generally refers to a "high pitch sound is inaudible to humans." The audio range for humans is said to be between about 20 Hz and 20 KHz. In other words, ultrasonic waves have a frequency of 20 KHz or greater.

An ultrasonic sensor is an instrument that measures the distance to an object using ultrasonic sound waves. The working principle of the ultrasonic module is simple. It sends an ultrasonic pulse out at 40 KHz which travels through the air and if there is an obstacle or object, it will bounce back to the sensor. By calculating the travel time and the speed of sound, the distance can be calculated.

$L = \frac{1}{2} \times {T_{PluseWidth}} \times SoundSpeed $

At 20ºC (68ºF), UltrasonicDistanceFormula . This varies depending on temperature and humidity. 

In the PSoC 5LP, the Timer Component can be used to measure a pulse width. The Timer Component provides two useful functions to capture the digital signal:

  • trigger: The trigger input enables the timer to start counting based on configurable hardware events.
  • capture: The capture input captures the current timer count value to a capture register.


Required Components List

  HC-SR04 Ultrasonic Sensor Module  × 1
  Character LCD Module × 1


Circuit / Schematic Diagram




Related Reference Material(s)



Creating a New Project

Launch the PSoC Creator and create a New Project. In the New Project wizard, select Empty Schematic, edit the name of the Project and Workspace.


PSoC Components and TopDesign.cysch

After you create a project, the PSoC Creator will bring up the TopDesign.cysch tab, with an empty schematic. You need to add the following components to the TopDesign.cysch.








Sample Firmware Code


Building the code and Programming

You can build the project using the Build menu or Build icon PSoCCreator Build Button. The Output panel shows the build results. Any errors will stop the build process and messages identifying these problems will be displayed in the Output panel.

To program the PSoC with the newly build firmware, connect the PSoC board to the PC using the USB cable. Load the program using either the Debug➤Program selection or the following Program icon PSoCCreator Program Button. The programming status is also shown in the Output panel.

When programmed, you can test your system and observe the results.


Lab Experiments




Around five questions should be based on Required Reading Material. Two more challenging questions will require students to perform additional research for extra credit points