PSoC4: UltraSonic Sensor based Distance Measurement

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.
ShVy_264716
Level 4
Level 4
50 replies posted 25 replies posted 10 replies posted

Hi All

   

I am currently working on UltraSonic Sensor based Distance Measurement. I am using the sensor HCSR04 for that. With help of Danaaknight, I have designed the attached project, but it is not working as I am not getting the distance. The PSoC4 pins of trigger, echo and the sensor's results are checked on CRO. They are tested OK. I think the problem lies in measuring the time period on the Echo pin. Everybody is welcome to give solution.

   

 

   

Regards

   

Shaunak

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

Hi Shaunak,

   

are you facing a problem with the display, the formatting of the result or the values you compute?

   

Which hardware does the project run on?

   

Some comments:

   

It is always advisable to have a main loop running at a constant frequency instead of a loop that is governed by external hardware. In this case: the smaller the distance, the faster the loop will run. To avoid this and to shorten software, use a PWM that generates a 10µs pulse at the desired frequency of 40Hz, trigger the sensor with that pulse, clear/load the timer and so on.

   

Your documentation is very good, although I didn't find (maybe overlooked) the underlying hardware specs (board: Kit or custom)

   

Your program structure may be better using an initialization part, a sensor handler, a display handler and a handler for calculating distance. For more accuracy: Largest error influence comes from temperature, wikipedia helds a formula for that and the chip has got a raw measurement capability. Especially in your country where environment temps are comparably high the difference betweein conditioned inside and on the street may be remarkable.

   

 

   

Bob

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

Hi Bob

   

Thank you for the reply. I just debugged the code and found that the code gives me distance when I "watched" the variable in debugging environment. It means the LCD output is not coming properly. I checked other projects with LCD, they are running well.

   

 

   

Regards

   

Shaunak

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

Anything on the LCD? Is the "Distance =" showing? -> Hardware issue.

   

else

   

There is some peculiar about "print float in PSoC4", have a keyword search (at top of this page) for that and take the newer infos from Robyn.

   

 

   

Bob

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

Hi Bob

   

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
ETRO_SSN583
Level 9
Level 9
100 sign-ins 5 likes given 1000 replies posted

You must enable the nano library to get the sprintf() working -

       

 

   

http://www.cypress.com/?id=4&rID=87354 newlib-nano

           

 

   

 

   

Regards, Dana.

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

Dana

   

I did the steps given on the above link to get sprintf working with float, but I am afraid it is still not working.

   

 

   

Regards

   

Shaunak

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

I am using the Creator 3.2 early access and there is in the project settings an item to enable float formatting. This option costs you 14kB of flash. If that is too much, you may find some libraries using quite less program memory. Alternatively convert the float yourself by converting the fractional part separately as integer with the required precision

   

Intpart = (uint)MyFloat;

   

FracPart = (uint)((MyFloat-Intpart)*1000);

   

sprintf(Buffer,"%d.%d",Intpart,FracPart);

   

Will give your MyFloat converted with 3 decimal places. You must care for the sign separately.

   

 

   

Bob

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

Hi Bob

   

This method works quite well. Thank you.

   

 

   

Regards

   

Shaunak

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

You are always welcome, Shaunak!

   

 

   

Bob

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

I posted a minor update to project in PSOC 3 forum,

   

eg. a delay to allow sensor to start up. Seems to

   

be working fine with or without delay as original project

   

did not have.

   

 

   

Attached pin chart for Pioneer board in case you need it.

   

 

   

Regards, Dana.

0 Likes
ETRO_SSN583
Level 9
Level 9
100 sign-ins 5 likes given 1000 replies posted

If you want to avoide sprintf( ) altogether -

   

 

   

    

   

          

   

http://stackoverflow.com/questions/2302969/how-to-implement-char-ftoafloat-num-without-sprintf-libra...

   

 

   

Not sure of its general capability or limitations.

   

   

 

   

Regards, Dana.

0 Likes