Arduino library for using serial enabled ultrasonic distance modules (eg. US-100)
There are two common types of cheap ultrasonic distance modules listed below, which are both very similar from a hardware perspective. They both have an ultrasonic transmitter and a matching receiver, with a small amount of logic on board to talk to the connected microcontroller (such as an Arduino). They both claim similar specifications (ranging distance from 20-4500mm, with resolution of a few mm, and a measuring angle of 15 degrees)
This library assumes that, unlike the rather simplistic first-attempt examples of how to use these devices, the devices will be used in a complex environment where the controller cannot block for any significant time and where accuracy and precision of measurements matter (ie. both the right measurement is returned, and that measurement has small variation if repeated).
The cheapest and most common devices are the family of devices that use trigger/echo pins to make measurements. HC-SR04 is a common example sold on ebay, though there are a large number of devices out there. Sometimes a different model number just indicates a different design, manufacturer, or Chinese clone, while sometimes there are actual differences in the behaviour.
The devices operate by the microcontroller signaling to the device to perform a measurement by toggling the trigger line briefly (10s of microseconds), and then waiting for the echo pin to be toggled to indicate the ultrasonic pulse has been returned. Measuring the time the echo takes allows us to infer the distance to the nearest object.
The problem here is twofold:
It may be possible to use these devices with pin-change interrupts, but that would not necessarily fix all the issues.
Though these are the cheapest devices by far (£1.39 including postage from China, as of July 2016) I cannot recommend using them, given the relative cost and far superior performance of the serial models.
This library does not handle these devices - the NewPing library already exists for that and is very good.
The less common and more expensive devices are the family of devices that use a TTL serial communication to make measurements. US-100 is a common example sold on ebay.
These cost fractionally more than the Trigger/Echo models (£2.32 including postage from China, as of July 2016), but I cannot imagine a usage scenario where this difference in cost will be an issue, given the far superior performance of the US-100 and similar models.
There are two main benefits to these models:
These devices are not without foibles however! This library aims to hide most of these from the user. The most obvious weaknesses are:
Note: US-100 in particular can operate in either Trigger/Echo mode or Serial mode. Thought if you've got the device I think you'd be mad to use it in the Trigger/Echo mode for the reasons already given!
Download the zip file (link below) then install as you would any other library (instructions below).
Two examples are provided:
#define PS_INCLUDE_SOFTWARESERIAL
before the #ifdef PS_INCLUDE_SOFTWARESERIAL
check. If anyone has better ideas on how to do this, I'm all ears.