Parallax PING))) Arduino example issues
![Oiberex1](https://forums.parallax.com/uploads/userpics/384/nDSS0OIQ7BGVB.jpg)
At least one of the PING Arduino interface sketches has a number of subtle problems when the communication between the Arduino and the sensor is analyzed in detail. The problems include multiple trigger pulses of the wrong pulse width.
The example does work, but I suspect using code that doesn't actually do what it claims to do could cause problems in other circumstances.
Details here.
The example does work, but I suspect using code that doesn't actually do what it claims to do could cause problems in other circumstances.
Details here.
Comments
http://learn.parallax.com/KickStart/28015
Here is yet another way written in simpleIDE. I normally would use "ping.h" but thought this more illustrative.
I took speed of sound to be 1000 ft/sec. Here are a few actual vs measured points, all in mm.
I was just surprised to see that the Arduino code was not actually producing a single 5 usec pulse to trigger the sensor as the code was attempting to do, and that it appears the Arduino digitalWrite function is actually incapable of producing a pulse of less than about 11 usec (at least on an Arduino Uno).
The original Arduino code works, but it probably wouldn't be a very good example to use in other circumstances (say, with a less tolerant sensor ) since it is not actually producing the single 5 usec pulse as is intended.
I tested Parallax's simpletools pulse_out function and it is capable of reliably producing down to a 1 usec pulse (on my PPDB anyway.) Arduino doesn't appear to offer the equivalent of a pulse_out function.
Again, not a big deal really. All versions of the code actually produce valid distance measurements.
I guess my conclusion would be: Don't use the Arduino digitalWrite function for pulses of less that maybe 20 us. Use direct port access instead.
In the PING docs, the length of the trigger pulse is stated as 2us minimum, 5us typical, and no maximum. 5us, 10us, even 50us, it doesn't matter. There is a minimum holdoff of 750 us, and the assumption there is that any additional trigger pulses during this phase will be ignored in the PING. So any double trigger would likely to be ignored anyway, at least when using a genuine Parallax part.
It's fairly well known the digitalWrite function for the Arduino takes many cycles, as it has a lot more built-in tasks it performs, including checking if PWM is active, validating the pin number, and so on. Naturally direct port writes will be faster because all these clock cycles are saved. It's up to you to make sure you don't clobber a pin.
If you look closely at the BS2 code in the PING doc examples and KickStart, you'll see the delay value for the pulse is '5', which for a BS2 is actually 10us. The Arduino KickStart keeps the 5us value because it "looks right" based on the specs, even though the Arduino cannot produce pulses this short using digitalWrite.
There could be some cheap imported knockoffs that might double-trigger or can't handle a pulse longer than X, but that wouldn't be Parallax's concern. In any case, I doubt there would be many of these out there, as its maker would test it with an Arduino, using the most common examples available, which include the one from Arduino.