Ultrasonic Distance Sensor HC-SRO4

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

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.

0 Likes
21 Replies
EvPa_264126
Level 7
Level 7
500 replies posted 250 replies posted 100 likes received

   

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)

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes
Anonymous
Not applicable

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

   

😎 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.

0 Likes
Anonymous
Not applicable

"1. I decided not to use the float. Thehttp://www.cypress.com/?app=forum&id=2232&rID=93819&message=posted&action=reply 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.

0 Likes
EvPa_264126
Level 7
Level 7
500 replies posted 250 replies posted 100 likes received

   

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.

0 Likes
Anonymous
Not applicable

 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    
   
        
0 Likes
Anonymous
Not applicable

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

0 Likes
Anonymous
Not applicable

Thanks for the post Zeta, very useful info.

   

 

   

Regards, Dana.

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

 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).

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Interesting approach if you do not need actual distance for

   

display or other. Many applications do not use distance but there

   

is no need to display it. More than once in small devices I have been

   

up against FLASH limits, so integer math or simple approximations

   

very useful. You can also find approximations to sprintf() since

   

not unusual you are using only a small part of its overall capability,

   

which of course leads to unnecessary and wasteful bloat.

   

 

   

Regards, Dana.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

When running against flash-limits there is another approach to reduce code: Using Optimization

   

This may need some preparation beforehand since the debugging capabilities get worse under optimized code (imagine a variable held in a register cannot be inspected and you must know where it is kept).

   

Additionally errors as forgetting to declare a variable as "volatile" will pop up now and spoil your program until found and corrected.

   

When some parts of the program are debugged it is a good practice to make a module out of that (a .h and a .c file) if not already done so and then set the optimization level for that .c file as required (right click on the file and select "Build Properties"). So you may select for every file (even the generated ones) the optimization level individually.

   

 

   

Bob

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Many "optimization" techniques other than just compiler settings

   

(albeit that is important also) -

   

 

   

1) Architecture choice

   

2) Coding methods

   

3) Compiler specifics

   

4) Arithmetic choices

   

5) Data flow scheme

   

6) Library reduction/extraction

   

7) HW vs SW choices

   

😎 Circuit design, eg, cal code needed

   

9) ....

   

 

   

But back to the original point, floating point math and sprintf() great

   

when you can afford it, eliminate and rexamine the problem when you

   

can't.

   

 

   

Regards, Dana.

0 Likes
ShVy_264716
Level 4
Level 4
50 replies posted 25 replies posted 10 replies posted

Hi Dana

   

I checked the project "Ultrasonic" shared by you, but I am afraid it is not working. While debugging, I came to know that the "DistOutHCSRO4" remains HIGH constantly, so the code gets stuck into while loop and hence I am only getting the string "Distance = " on LCD. No number is displayed on LCD. Can you please help me out with this project?

   

Thanks in advance.

   

 

   

Regards

   

Shaunak

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

I no longer have this setup so help limited.

   

 

   

But confirm that trigger TrigHCSRO4 is being generated from PSOC

   

to the HCSR04. This project is really quite simple since HCSR04 is

   

controlling most of the interface. And that is triggered by the TrigHCSRO4

   

coming from PSOC.

   

 

   

One think I noticed in another HCSR04 datasheet is a delay of 1 sec after

   

power up before any triggers are sent. Thats not in my code, so consider

   

adding that somewhere after LCD_Start() and before the for() loop.

   

 

   

   

 

   

Regards, Dana.

0 Likes
ShVy_264716
Level 4
Level 4
50 replies posted 25 replies posted 10 replies posted

Hi Dana

   

I tried the project shared by you again, but I am not able to get the distance. I checked the PSoC4 project and my sensor HCSR04 with CRO. My PSoC4 and the sensor give proper results. I think the problem lies in not using the Timer component properly or the Timer_ReadCounter() API. Did you work successfully on this project? Everybody is welcome to share views on this.

   

 

   

Regards

   

Shaunak

0 Likes
ShVy_264716
Level 4
Level 4
50 replies posted 25 replies posted 10 replies posted

Hi Dana

   

I got it working by putting integer %d in sprintf instead of float %f. I did not know that float does not work with sprintf.

   

 

   

Regards

   

Shaunak

0 Likes
lock attach
Attachments are accessible only for community members.
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Here is a current version, minor mod to allow sensor a second to start up.

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

Did you use a timer? I couldn't find it in the top design.

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Hello Guys,

   

I am trying to get a HC SR04 sensor to work with my PSOC5LP​. I am sort of on the right track just not getting the right results. Can you guys take a look at my project. Ive tried using a timer with UDB and also the fixed mode.  

0 Likes
lock attach
Attachments are accessible only for community members.
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Samuel, welcome in the forum.

   

Normally we do not supply complete solutions, but to operate the SR04 safely there are some facts to remember.

   

The attached project runs on a CY8KIT-043, output goes to an emulated com-port via usb. Use PuTTY or any other terminal program.

   

 

   

Have fun

   

Bob

0 Likes