1 2 Previous Next 21 Replies Latest reply on Oct 24, 2016 12:24 AM by user_1377889

    Ultrasonic Distance Sensor HC-SRO4

    user_14586677

      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.

        • 1. Re: Ultrasonic Distance Sensor HC-SRO4
          user_49271930

           

             

          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)

          • 2. Re: Ultrasonic Distance Sensor HC-SRO4
            user_1377889

            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

            • 3. Re: Ultrasonic Distance Sensor HC-SRO4
              content.librarian

              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.

              • 4. Re: Ultrasonic Distance Sensor HC-SRO4
                content.librarian

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

                • 5. Re: Ultrasonic Distance Sensor HC-SRO4
                  user_49271930

                   

                     

                  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.

                  • 6. Re: Ultrasonic Distance Sensor HC-SRO4
                    norman.chahuares.collantes

                     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    
                       
                            
                    • 7. Re: Ultrasonic Distance Sensor HC-SRO4
                      norman.chahuares.collantes

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

                      • 8. Re: Ultrasonic Distance Sensor HC-SRO4
                        content.librarian

                        Thanks for the post Zeta, very useful info.

                           

                         

                           

                        Regards, Dana.

                        • 9. Re: Ultrasonic Distance Sensor HC-SRO4
                          mike.partain

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

                          • 10. Re: Ultrasonic Distance Sensor HC-SRO4
                            user_1377889

                            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

                            • 11. Re: Ultrasonic Distance Sensor HC-SRO4
                              user_14586677

                              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.

                              • 12. Re: Ultrasonic Distance Sensor HC-SRO4
                                user_1377889

                                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

                                • 13. Re: Ultrasonic Distance Sensor HC-SRO4
                                  user_14586677

                                  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

                                     

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

                                  • 14. Re: Ultrasonic Distance Sensor HC-SRO4
                                    user_350540870

                                    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

                                    1 2 Previous Next