# Cypress Developer CommunityTM

## Ultrasonic Distance Sensor HC-SRO4

Summary: 21 Replies, Latest post by Bob Marlowe on 24 Oct 2016 12:24 AM PDT
User
7646 posts

Here is a simple project, attached, for using distance sensing module on PSOC 3.

Could easily be ported to  PSOC 4 or 5.

Regards, Dana.

Attachments:
User
407 posts

My project  HCSR0-04.zip  has several features:

1. I decided not to use the float. The oscillator frequency is chosen so that the counter gave the result immediately in millimeters.

2. Polling frequency sensor the maximum possible: Impulse Trig generated 10 ms after the end of the echo.

3. There is averaging result with setting the amount of averaging points and shutdown threshold filter with sharp changes in the distance.

4. I had to increase the sensitivity of the sensor (see diagram here)

User
9284 posts

Measuring distances with sonic sensors in a range of up to 4m with a precision of 1mm as your programs show does not take into account that the sonic speed changes with temperature (not pressure and a little bit by humidity).

Since there is a calibratable temperature measure within the PSoC chips it would not be too complicated to take temperature into account

The equation is c = 331,5 + 0,596 · T where the temperature T is measured in °C.

So for 20°C we get c = 331,5 + 0,596 · 20  which results in 343,42m/s

I did not skip the units in the equation for ignorance, but I was afraid that this could stress the forum-sw too much.

Bob

User
33 posts

The accuracy has a number of dependencies, some discussed in the notes pages of

the project -

1) Temp

2) Humidity

3) Transport medium

4) Supply

5) Non linearities

6) Beam shape, mechanical accuracies in transducers

7) Material reflectivity

8) Xtal freq variations

9) Dark matter local group denisty, so many others....................

These can be addressed by a number of simple and complex corrections, but then

sensor is not at all characterized, let alone its module interface, costs ~ \$ 3, and is meant for,

generally speaking, consumer grade applications. But use of least squares/power

curve fit to take out non linearities, discussed in notes page of project at manufacturing

test could take out a lot of these issues. As well as medium tables for velocity correction.

So much correction, so little time.

Regards, Dana.

User
33 posts

"1. I decided not to use the float. Thehttp://www.cypress.com/?app=forum&id=2232&rID=93819&message=posted&actio... oscillator frequency is chosen so that the counter gave the result immediately in millimeters."

Thats an excellent idea for PSOC 4 application because of code load of sprintf or other float to string

APIs. But you sacrifice ability to deasily display fractional format.

2. Polling frequency sensor the maximum possible: Impulse Trig generated 10 ms after the end of the echo.

Yes, I had a delay of 100 mS, since I have no module data that indicates what min I could use. I

too think 100 mS is too long, but w/o data not sure I can reliably change it.

3. There is averaging result with setting the amount of averaging points and shutdown threshold filter with sharp changes in the distance.

Yes, averaging would remove interface and environmental noise. But you sacrifice latency.

4. I had to increase the sensitivity of the sensor (see diagram here)

The TL074 has a GBW of 3 Mhz, typical, at a G = 75 the 3 db BW is ~ 40 Khz, what you want.

However thats only a typ value, the 3 Mhz, probably has a GBW variation on the order of 40%

or more. Net of this is device to device the sensitivity might be more aggravated at higher G.

Unfortunately I cannot find a schematic of module I used, and further evaluation, enhancement

of its design, w/o manually recreating it, not on my radar.

Regards, Dana.

User
407 posts

I dreamed

to make a model locator (as toy).

Project for unipolar stepper motor TEAC 14769070B   STEPPER.zip   was made (here)

but there is no time to gather design and come up with an algorithm to scan and display to GLCD.

User
357 posts

here is EMil's blog post about this sensor with details of how he mods it to improve it.

User
33 posts

Thanks for the post Zeta, very useful info.

Regards, Dana.

User
357 posts

Unfortunately I cannot find a schematic of module I used, and further evaluation, enhancement

of its design, w/o manually recreating it, not on my radar.

Regards, Dana.

Emil reversed the HC-SR04 modules and has complete schematic

-->

User
1 post

Different approach to same device, from a newb;  http://www.spiked3.com/?p=605

I do not do any conversion to distance math on the psoc, since I do not really care what the distance is, only that it exceeds a minimum (collision immenent).

But I feel this is the proper approach to reading the information from the SR04s (again coming at it as a newb).

Attachments:
User
9284 posts

There is no need to abandon floats in a PSoC4 environment, the provided libs are small enough. Using trigonometric functions can eat up some codespace, but could be done better with some own approximations of the required precision (you may need milimeters, but not with a nanometer precision...). Even sqtr() or 1.0/sqrt(x) find a neat approximation using very few code here: en.wikipedia.org/wiki/Fast_inverse_square_root

EXCEPT

The sprint() functions are at this time breaking code-space, but you find some libraries that are a lot smaller, just google for them.

Bob

PS: I managed to program an AHRS system with 9 degrees of freedom and acting 6 servo motors using floats for the Mahony algorithms and a frequency of 200 Hz into a CY8C4245 chip. Flash full to the rim, but fitted