Go Down

Topic: NewPing Library: HC-SR04, SRF05, SRF06, DYP-ME007, Parallax PING))) - v1.5 (Read 128 times) previous topic - next topic

teckel


Hello,

I would like to ask if it is possibly to read the cm with decimal like 10.4 cm e.t.c.

Thank in advance.

Best regards,
Tasos


Yes, simply use ping() which will return the milliseconds and then divide by whatever number you want to convert ms to cm (defaults to 57).  The library uses integers to save program space (saves about 1k by avoiding floating point).  Also, the accuracy isn't really better than a cm and there's other variables like temperature that will change the speed of sound and mess up your calculations.

However, keep in mind that doing any of this will really be of no good as the sensor is not accurate enough to actually give you a consistent reading of 10.4.  If you did use a floating point number for ms to cm conversion, the ping results would probably jump around from 10.9 to 10.1, with everything still.  So, I'm sure sure what use you'd really get from it.  All you'd really be doing is wasting 1k of program space and getting a bunch of different readings and not knowing which one was right.

Basically, I'd suggest leaving it as-is and accepting that the sensor is really only accurate to 1cm or 1in.  And, enjoy the extra speed and 1k of program space you saved by not using floating point calculations on things that are not accurate below an integer anyway.  NewPing is designed to be small, fast, and accurate.  Making it floating point would not help the accuracy and it would make it slower and larger.  So, it would hurt the library in every way.  But, you're free to use floating point if you wish by using the ping() method along with your own conversion (after also monitoring the temperature).

Let me know if you actually find this useful, I've yet to hear of it being at all useful or more accurate.

Tim
Arduino - Teensy - Raspberry Pi
My libraries: NewPing - LCDBitmap - toneAC - NewTone - TimerFreeTone

michinyon

Well that's a good point,  but it is still likely to jump around between 10  and 11 cm.

gandalf50


Interesting!  My ultrasonic sensors only use a couple mA each, while the Arduino Uno uses 60 mA (I'd guess the Mega uses more).  I wouldn't think that just the sensors would put your USB or the voltage regulator over the top.  Have you measured what kind of current it's using?  It just seems very unlikely that it's within a couple mA of going over the top and the sensors draw enough current to do it.  I guess it's possible, just doesn't seem likely.

Tim


Well it did it for me at least. Hopefully it helps someone else as well.

The operating voltage for the HC-SR04 should apperently be 4.50 to 5.0V. But I did some quick measurements which shows that that's not the case for my sensors:

With Ethernet shield:
1. Power via USB port
1.1. With LCD screen connected as well, at 3 HC-SR04 ultrasonic sensors: 4.55 V
1.2. Without LCD Screen: 4.60 V

2. Power from power outlet
2.1. With LCD screen connected as well, at 3 HC-SR04 ultrasonic sensors: 4.98 V
2.2. Without LCD Screen: 4.98 V

Without Ethernet shield:
3. Power via USB port
3.1. With LCD screen connected as well, at 3 HC-SR04 ultrasonic sensors: 4.76 V
3.2. Without LCD Screen: 4.82 V

4. Power from power outlet
4.1. With LCD screen connected as well, at 3 HC-SR04 ultrasonic sensors: 4.98 V
4.2. Without LCD Screen: 4.98 V

i.e. since it doesn't work properly for me with just the sensors and the Ethernet shield connected (using just USB), over 4.60V seems to be needed.

teckel


Well that's a good point,  but it is still likely to jump around between 10  and 11 cm.


Not if you turn off rounding.  But, you are right, it can bounce between to different integers as well.  However, that's because the sensor is not accurate to a fraction of a cm.  It's not because the library doesn't do floating point.  Basically, it's not helpful to do floating point math.  But, feel free to do it.  Replace "ping_cm()" with "ping()/57.0" and you'll get a decimal result.  If you find this useful I'd really be amazed, but the library is designed so you can do it as shown.

Tim
Arduino - Teensy - Raspberry Pi
My libraries: NewPing - LCDBitmap - toneAC - NewTone - TimerFreeTone

teckel



Interesting!  My ultrasonic sensors only use a couple mA each, while the Arduino Uno uses 60 mA (I'd guess the Mega uses more).  I wouldn't think that just the sensors would put your USB or the voltage regulator over the top.  Have you measured what kind of current it's using?  It just seems very unlikely that it's within a couple mA of going over the top and the sensors draw enough current to do it.  I guess it's possible, just doesn't seem likely.

Tim


Well it did it for me at least. Hopefully it helps someone else as well.

The operating voltage for the HC-SR04 should apperently be 4.50 to 5.0V. But I did some quick measurements which shows that that's not the case for my sensors:

With Ethernet shield:
1. Power via USB port
1.1. With LCD screen connected as well, at 3 HC-SR04 ultrasonic sensors: 4.55 V
1.2. Without LCD Screen: 4.60 V

2. Power from power outlet
2.1. With LCD screen connected as well, at 3 HC-SR04 ultrasonic sensors: 4.98 V
2.2. Without LCD Screen: 4.98 V

Without Ethernet shield:
3. Power via USB port
3.1. With LCD screen connected as well, at 3 HC-SR04 ultrasonic sensors: 4.76 V
3.2. Without LCD Screen: 4.82 V

4. Power from power outlet
4.1. With LCD screen connected as well, at 3 HC-SR04 ultrasonic sensors: 4.98 V
4.2. Without LCD Screen: 4.98 V

i.e. since it doesn't work properly for me with just the sensors and the Ethernet shield connected (using just USB), over 4.60V seems to be needed.


I've ran ultrasonic sensors down to 3.3 volts and they still work.  I forgot I have an Internet shield, I'll see what kind of milliamps it uses.

Tim
Arduino - Teensy - Raspberry Pi
My libraries: NewPing - LCDBitmap - toneAC - NewTone - TimerFreeTone

Go Up