Search

PSoC 4: Measuring Distance with HCSR-04 and CY8CKIT-049 4200

Made by Shahariar Hossain

About the project

Pulse High measurement (proportional to distance) of HCSR-04 (Echo pin) UltraSonic Ranging module using Timer component of PSoC 4

Project info

Difficulty:

Platforms:

Estimated time:

1 hour

Published:

13th May 2019

Items used in this project


Hardware components

Jumper Wire Kit x 1

Story


With Arduino, this is no big deal, right ?

Just digitalWrite "high" the trigger pin for a few micro second then pulseIn the echo pin to measure the pulse duration in time, convert that time to distance !

How do you do that in PSoC 4 ?Do some math to get the time and print on the LCD !

So How does it work ?

The Trigger (also the LED pin for a short BLUE flash) named Output Pin of PSoC, which is connected to the Trigger Input of the the Ultrasonic Module, is made HIGH for 10 microsecond on every 500 millisecond to Trigger the Module for sending ultrasonic waves. This is done from the C code, which means this part is dealt by the CPU (ARM M0 in PSoC).

Note that, both Trigger and LED are controlled from software.

Then the Echo Pin of the Module will generate a High Pulse, the width of the pulse depends on the measured distance. To measure this duration, a Hardware Timer component of PSoC is used.

PSoC Creator

Configuring Timer Component

From, HC 04 Ultrasonic Module's datasheet,

Pulse High Duration on Echo (in uS)/58 = 1 cm

Maximum Measurable Length 4 meters = 400 cm will be represented with a 400 x 58 = 23200 uS High Pulse. So, Timer Period (here 65535) must be > 23200.

Check the above image -

Counter mode : Up ( counts from 0 towards 65535 )

Run mode : 1 shot ( once stopped, won't start unless started again )

Compare/Capture : Capture ( capture the counts when Timer stops )

Interrupt : Capture Count ( an Interrupt will assert when Count is capture )

Timer Starts (start counting) & Reloads(from 0) on the Rising Edge of Ultrasonic Module's Echo Pin then Stop(stop counting) & Count(counts the ticks from start to stop) on the Falling Edge, then the Interrupt occurs( In the ISR number of counts is converted into time, distance is updated).

The Timer will start again, when a new Echo Pulse comes !

If you want to make following projects using PSoC 4:

  • 1. Obstacle Avoiding Robots
  • 2. Scanning Object Shape
  • 3. Detecting Motion at a given distance

This mini project will be handy ! Using another Timer in continuous mode, the Trigger Pin can be periodically fired, thus eliminating CyDelay from code to free up the CPU !

Schematics


Connections

Ultrasonic%20Ranger.cydsn.zip

Schematic

wiring

Code


main c

Leave your feedback...