0.3.0 SRF05

This commit is contained in:
Rob Tillaart 2024-02-10 13:14:07 +01:00
parent ed5c6c6d1d
commit 3200ea3e98
8 changed files with 186 additions and 68 deletions

View File

@ -6,6 +6,16 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.3.0] - 2024-02-08
- add support for single pin operation (echo == trigger) Kudos to RobertPHeller!
- add **float getYards()** wrapper.
- add **void setSampleInterval(uint16_t microSeconds = 1000)** (for average & median)
- add **uint16_t getSampleInterval()**
- reduced pulseIn timeout to 200000 (30+ meter)
- minor edits.
----
## [0.2.0] - 2024-01-28
- add **float calculateSpeedOfSound(float temperature, float humidity)**
- redo lookup table SOS temperature humidity
@ -44,7 +54,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- minor edits
## [0.1.1] - 2021-11-18
- update build-CI,
- update build-CI
- update readme.md
- minor edits

View File

@ -18,15 +18,19 @@ Arduino library for the SRF05 distance sensor and compatibles.
This library implements a API for a PING type of sensor.
It is expected to work for quite a range of them.
THe current version of the library only uses the SRF04 compatibility mode which uses a
separate TRIGGER and ECHO pin.
It does not (yet) implement the SRF05 mode in which TRIGGER and ECHO are the same.
Till version 0.2.0 of the library, it only supported the SRF04 compatibility mode which uses a
separate **TRIGGER** and **ECHO** pin.
An important feature of this library is that it allows to adjust to the speed of sound (SOS).
Version 0.3.0 implements support for the SRF05 mode in which **TRIGGER** and **ECHO** are the same.
One need to connect the MODE pin to GND to select this mode in the device.
Note that not all PING type of sensors will support the single pin mode.
See also Connection section below.
An important feature of this library is that it allows to adjust the speed of sound (SOS).
Reasons to use a different value for the speed of sound is that it varies depending on
temperature, humidity, composition of the air, air pressure, other type of gas, etc.
Default value for the speed of sound is set to 340 m/s. (air, ~15°C, sea level pressure)
Default value for the speed of sound is set to **340 m/s**. (air, ~15°C, sea level pressure)
Since the version 0.2.0 the library has an interpolation formula to calculate the speed of sound
given a temperature and humidity.
@ -40,6 +44,12 @@ the **pulseIn()** function. This has in the end the same effect as changing the
speed of sound however it is technically more correct to keep the two separated.
#### Connection
How to connect:
- https://www.robot-electronics.co.uk/htm/srf05tech.htm
#### Effect temperature and humidity
Several correction formulas for the speed of sound are available on the internet
@ -64,31 +74,31 @@ and wind speed. The latter is a bit compensated for, as the acoustic pulse will
(table redone completely in 0.2.0)
Temperature in Celsius, Humidity in %, constant pressure == 1013 mBar.
Temperature in Celsius, Humidity in %, constant pressure == 1013 mBar, SOS in m/s.
| temp | 0% | 10% | 20% | 30% | 40% | 50% | 60% | 70% | 80% | 90% | 100% |
|:----:|:------:|:------:|:------:|:------:|:------:|:------:|:------:|:------:|:------:|:------:|:------:|
| -40 | 306.22 | 306.23 | 306.24 | 306.25 | 306.26 | 306.26 | 306.27 | 306.28 | 306.29 | 306.30 | 306.31 |
| -35 | 309.49 | 309.50 | 309.50 | 309.51 | 309.52 | 309.52 | 309.53 | 309.54 | 309.54 | 309.55 | 309.56 |
| -30 | 312.72 | 312.73 | 312.73 | 312.74 | 312.74 | 312.75 | 312.75 | 312.76 | 312.76 | 312.77 | 312.78 |
| -25 | 315.92 | 315.93 | 315.93 | 315.94 | 315.94 | 315.95 | 315.95 | 315.96 | 315.96 | 315.97 | 315.98 |
| -20 | 319.09 | 319.10 | 319.10 | 319.11 | 319.12 | 319.12 | 319.13 | 319.14 | 319.14 | 319.15 | 319.16 |
| -15 | 322.22 | 322.23 | 322.24 | 322.25 | 322.26 | 322.27 | 322.28 | 322.29 | 322.30 | 322.31 | 322.32 |
| -10 | 325.33 | 325.34 | 325.36 | 325.37 | 325.39 | 325.40 | 325.42 | 325.43 | 325.45 | 325.46 | 325.47 |
| -5 | 328.40 | 328.42 | 328.44 | 328.47 | 328.49 | 328.51 | 328.53 | 328.56 | 328.58 | 328.60 | 328.62 |
| 0 | 331.45 | 331.48 | 331.51 | 331.54 | 331.57 | 331.61 | 331.64 | 331.67 | 331.70 | 331.73 | 331.76 |
| 5 | 334.47 | 334.52 | 334.56 | 334.61 | 334.65 | 334.70 | 334.74 | 334.79 | 334.83 | 334.88 | 334.93 |
| 10 | 337.46 | 337.52 | 337.58 | 337.64 | 337.70 | 337.76 | 337.82 | 337.88 | 337.94 | 338.00 | 338.06 |
| 15 | 340.43 | 340.52 | 340.61 | 340.70 | 340.79 | 340.88 | 340.97 | 341.06 | 341.15 | 341.24 | 341.33 |
| 20 | 343.37 | 343.49 | 343.62 | 343.74 | 343.87 | 343.99 | 344.12 | 344.24 | 344.37 | 344.49 | 344.61 |
| 25 | 346.29 | 346.46 | 346.63 | 346.80 | 346.97 | 347.14 | 347.31 | 347.48 | 347.65 | 347.82 | 347.99 |
| 30 | 349.18 | 349.41 | 349.64 | 349.87 | 350.10 | 350.32 | 350.55 | 350.78 | 351.01 | 351.24 | 351.47 |
| 35 | 352.04 | 352.35 | 352.65 | 352.96 | 353.27 | 353.57 | 353.88 | 354.19 | 354.49 | 354.80 | 355.11 |
| 40 | 354.89 | 355.29 | 355.70 | 356.10 | 356.50 | 356.91 | 357.31 | 357.71 | 358.12 | 358.52 | 358.92 |
| 45 | 357.71 | 358.24 | 358.76 | 359.29 | 359.82 | 360.34 | 360.87 | 361.40 | 361.92 | 362.45 | 362.98 |
| 50 | 360.51 | 361.19 | 361.87 | 362.55 | 363.23 | 363.92 | 364.60 | 365.28 | 365.96 | 366.64 | 367.32 |
| 55 | 363.29 | 364.16 | 365.04 | 365.91 | 366.78 | 367.66 | 368.53 | 369.40 | 370.28 | 371.15 | 372.02 |
| 60 | 366.05 | 367.16 | 368.27 | 369.38 | 370.49 | 371.59 | 372.70 | 373.81 | 374.92 | 376.03 | 377.14 |
| -40 | 306.22 | 306.23 | 306.24 | 306.25 | 306.26 | 306.26 | 306.27 | 306.28 | 306.29 | 306.30 | 306.31 |
| -35 | 309.49 | 309.50 | 309.50 | 309.51 | 309.52 | 309.52 | 309.53 | 309.54 | 309.54 | 309.55 | 309.56 |
| -30 | 312.72 | 312.73 | 312.73 | 312.74 | 312.74 | 312.75 | 312.75 | 312.76 | 312.76 | 312.77 | 312.78 |
| -25 | 315.92 | 315.93 | 315.93 | 315.94 | 315.94 | 315.95 | 315.95 | 315.96 | 315.96 | 315.97 | 315.98 |
| -20 | 319.09 | 319.10 | 319.10 | 319.11 | 319.12 | 319.12 | 319.13 | 319.14 | 319.14 | 319.15 | 319.16 |
| -15 | 322.22 | 322.23 | 322.24 | 322.25 | 322.26 | 322.27 | 322.28 | 322.29 | 322.30 | 322.31 | 322.32 |
| -10 | 325.33 | 325.34 | 325.36 | 325.37 | 325.39 | 325.40 | 325.42 | 325.43 | 325.45 | 325.46 | 325.47 |
| -5 | 328.40 | 328.42 | 328.44 | 328.47 | 328.49 | 328.51 | 328.53 | 328.56 | 328.58 | 328.60 | 328.62 |
| 0 | 331.45 | 331.48 | 331.51 | 331.54 | 331.57 | 331.61 | 331.64 | 331.67 | 331.70 | 331.73 | 331.76 |
| 5 | 334.47 | 334.52 | 334.56 | 334.61 | 334.65 | 334.70 | 334.74 | 334.79 | 334.83 | 334.88 | 334.93 |
| 10 | 337.46 | 337.52 | 337.58 | 337.64 | 337.70 | 337.76 | 337.82 | 337.88 | 337.94 | 338.00 | 338.06 |
| 15 | 340.43 | 340.52 | 340.61 | 340.70 | 340.79 | 340.88 | 340.97 | 341.06 | 341.15 | 341.24 | 341.33 |
| 20 | 343.37 | 343.49 | 343.62 | 343.74 | 343.87 | 343.99 | 344.12 | 344.24 | 344.37 | 344.49 | 344.61 |
| 25 | 346.29 | 346.46 | 346.63 | 346.80 | 346.97 | 347.14 | 347.31 | 347.48 | 347.65 | 347.82 | 347.99 |
| 30 | 349.18 | 349.41 | 349.64 | 349.87 | 350.10 | 350.32 | 350.55 | 350.78 | 351.01 | 351.24 | 351.47 |
| 35 | 352.04 | 352.35 | 352.65 | 352.96 | 353.27 | 353.57 | 353.88 | 354.19 | 354.49 | 354.80 | 355.11 |
| 40 | 354.89 | 355.29 | 355.70 | 356.10 | 356.50 | 356.91 | 357.31 | 357.71 | 358.12 | 358.52 | 358.92 |
| 45 | 357.71 | 358.24 | 358.76 | 359.29 | 359.82 | 360.34 | 360.87 | 361.40 | 361.92 | 362.45 | 362.98 |
| 50 | 360.51 | 361.19 | 361.87 | 362.55 | 363.23 | 363.92 | 364.60 | 365.28 | 365.96 | 366.64 | 367.32 |
| 55 | 363.29 | 364.16 | 365.04 | 365.91 | 366.78 | 367.66 | 368.53 | 369.40 | 370.28 | 371.15 | 372.02 |
| 60 | 366.05 | 367.16 | 368.27 | 369.38 | 370.49 | 371.59 | 372.70 | 373.81 | 374.92 | 376.03 | 377.14 |
**Notes on table**
@ -107,6 +117,11 @@ This function returns a speed of sound with an overall error margin less than 1%
lower than 0.5% compared to the numbers above.
#### Related
- https://www.robot-electronics.co.uk/htm/srf05tech.htm (good description of the sensor)
## Interface
```cpp
@ -115,36 +130,49 @@ lower than 0.5% compared to the numbers above.
#### Constructor
- **SRF05(const uint8_t trigger, const uint8_t echo, const uint8_t out = 0)** constructor to set the trigger and echo pin.
It is not clear what the purpose of the OUT pin is, effectively it is not used yet.
- **SRF05(const uint8_t trigger, const uint8_t echo = 0)** constructor to set the trigger and echo pin.
The echo pin can be left out, or set to the same as the trigger pin.
In total one can do it in three ways.
```cpp
const int trigger = 7;
const int echo = trigger;
SRF05 SRF(trigger, echo);
SRF05 SRF(trigger, trigger);
SRF05 SRF(trigger);
```
To select single pin mode in the hardware the MODE pin of the device must be connected to GND.
For the two pin (compatibility) mode the MODE pin must be left floating.
#### Configuration
- **void setSpeedOfSound(float sos)** adjust the speed of sound in meters per second (m/s).
- **void setSpeedOfSound(float sos = 340)** adjust the speed of sound in meters per second (m/s).
See table above.
The function has **no range check** and accepts even negative values.
This will cause a negative sign in the distances which can be handy sometimes when you have
This will cause a negative sign in the distances which can be useful sometimes when you have
two sensors in opposite directions.
- **float getSpeedOfSound()** return set value (m/s)
- **bool setCorrectionFactor(float factor = 1)** adjust the timing by a few percentage e.g. to adjust clocks.
Typical values are between 0.95 and 1.05 to correct up to 5%.
Should not be used to correct the speed of sound :)
Returns false if factor <= 0.
Returns false if factor is negative, <= 0.
- **float getCorrectionFactor()** returns the current correction factor.
#### Operational mode
Normally a single read should be OK.
Normally a single read should be sufficient, but the library has more modi.
- **void setModeSingle()** read a single time.
This is the default and typical the fastest.
- **void setModeAverage(uint8_t count)** read count times and take the average.
Note: between the reads there is a delay of 1 millisecond.
Note: between the reads there is a default delay of 1000 microseconds.
- **void setModeMedian(uint8_t count)** read count times and take the median.
count must between 3 and 15 otherwise it is clipped.
Note: between the reads there is a delay of 1 millisecond.
Note: between the reads there is a default delay of 1000 microseconds.
- **void setModeRunningAverage(float alpha)** use a running average algorithm
with a weight alpha. Value for alpha depends on your application.
Alpha must be larger than zero and smaller or equal to one. Alpha == <0..1]
@ -152,15 +180,18 @@ Lower alpha averages great for static distances, a higher alpha is better
suited for changing distances.
- **uint8_t getOperationalMode()** returns the operational mode 0..3.
See table below.
- **void setSampleInterval(uint16_t microSeconds = 1000)** changes the interval used
in **SRF05_MODE_AVERAGE** and **SRF05_MODE_MEDIAN**.
Default is 1000 us == 1 milliSecond. Maximum is 65535 microseconds.
- **uint16_t getSampleInterval()** return set interval.
| Operational mode | Value | Notes |
|:-------------------------|:-------:|:-------:|
| SRF05_MODE_SINGLE | 0 | |
| SRF05_MODE_AVERAGE | 1 | |
| SRF05_MODE_MEDIAN | 2 | |
| SRF05_MODE_RUN_AVERAGE | 3 | |
| | other | error |
| SRF05_MODE_SINGLE | 0 |
| SRF05_MODE_AVERAGE | 1 | default sample interval 1000 us
| SRF05_MODE_MEDIAN | 2 | default sample interval 1000 us
| SRF05_MODE_RUN_AVERAGE | 3 |
| | other | error
If other modi are needed, please open an issue and I see if it fits.
Of course one can create more elaborated processing of measurements
@ -177,6 +208,7 @@ around this one.
- **float getMeter()** returns distance in meter.
- **float getInch()** returns distance in inches. (1 inch = 2.54 cm).
- **float getFeet()** returns distance in feet. (1 feet = 12 inch).
- **float getYards()** returns distance in yards. (1 yard = 3 feet = 36 inch).
#### Experimental - setTriggerLength
@ -262,10 +294,7 @@ See examples.
- DHT22 and the formula for SOS
- investigate effect of wind (speed of air) on the speed of sound.
- investigate
- should **setSpeedOfSound(float sos)** return bool if sos <=0 ?
- value of **setTriggerLength()**
- investigate switching between single pin (SRF05) mode and dual pin (SRF04) mode.
- need a separate constructor.
- investigate "guard time" between reads of 50 ms (20x /sec max).
@ -274,12 +303,16 @@ See examples.
- set default SOS to an SOS from the table instead of 340.
- function **begin(T, H)** ?
- add example to determine the correction factor?
- delay(1) in average configurable?
- add uint16_t **PulseTimeOut** = 200; // milliSeconds?
- void setPulseTimeOut(uint16_t milliSeconds = 200);
- uint16_t getPulseTimeOut();
#### Wont
- should **setSpeedOfSound(float sos)** return bool if sos <=0 ?
- print feet as 3'2" or 3-7/8 feet (is that needed in this lib)
- in printHelpers ?
- see printHelpers lib **printFeet(float feet)**
- fix magic conversion numbers.
- add ```float lastValue()``` ?
- not all paths update this variable.

View File

@ -1,7 +1,7 @@
//
// FILE: SRF05.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.2.0
// VERSION: 0.3.0
// DATE: 2021-05-17
// PURPOSE: Arduino library for the SRF05 distance sensor (and compatibles)
// URL: https://github.com/RobTillaart/SRF05
@ -19,10 +19,12 @@ SRF05::SRF05(const uint8_t trigger, const uint8_t echo)
_trigger = trigger;
_echo = echo;
_mode = SRF05_MODE_SINGLE;
if (echo == 0)
{
_echo = _trigger;
}
pinMode(_trigger, OUTPUT);
digitalWrite(_trigger, LOW);
pinMode(_echo, INPUT);
}
@ -31,6 +33,7 @@ void SRF05::setSpeedOfSound(float speedOfSound)
_speedOfSound = speedOfSound;
}
float SRF05::getSpeedOfSound()
{
return _speedOfSound;
@ -42,13 +45,13 @@ bool SRF05::setCorrectionFactor(float factor)
if (factor <= 0) return false;
_correctionFactor = factor;
return true;
};
}
float SRF05::getCorrectionFactor()
{
return _correctionFactor;
};
}
//////////////////////////////////////////////////
@ -81,7 +84,6 @@ void SRF05::setModeMedian(uint8_t count)
void SRF05::setModeRunningAverage(float alpha)
{
_mode = SRF05_MODE_RUN_AVERAGE;
_count = 1;
_alpha = alpha;
@ -114,7 +116,7 @@ uint32_t SRF05::getTime()
for (uint8_t s = 0; s < _count; s++)
{
sum += _read();
delay(1);
delayMicroseconds(_sampleInterval);
}
return round(sum / _count);
}
@ -125,7 +127,7 @@ uint32_t SRF05::getTime()
for (uint8_t s = 0; s < _count; s++)
{
samples[s] = _read();
delay(1);
delayMicroseconds(_sampleInterval);
}
_insertSort(samples, _count);
if (_count & 0x01) return samples[_count / 2];
@ -170,6 +172,12 @@ float SRF05::getFeet()
}
float SRF05::getYards()
{
return _speedOfSound * getTime() * 0.54681e-6;
}
// EXPERIMENTAL
// distance in meters (single trip)
float SRF05::determineSpeedOfSound(float distance, uint8_t count)
@ -179,9 +187,9 @@ float SRF05::determineSpeedOfSound(float distance, uint8_t count)
while (count--)
{
sum += _read();
delay(1);
delayMicroseconds(_sampleInterval);
}
// sos = distance travelled forth and back in micrometer
// sos = distance travelled forth and back in micrometer
// divided by time in microseconds.
float sos = (count * distance * 2e6) / sum;
return sos;
@ -208,7 +216,7 @@ uint32_t SRF05::lastTime()
float SRF05::calculateSpeedOfSound(float temperature, float humidity)
{
// interpolate
// interpolate
// column RNH = 0%, from formula.
float sos = 331.45 * sqrt(1 + temperature/273.15);
// column RH = 100%, interpolation from spreadsheet
@ -229,11 +237,16 @@ float SRF05::calculateSpeedOfSound(float temperature, float humidity)
//
uint32_t SRF05::_read()
{
// Send pulse
pinMode(_trigger, OUTPUT);
digitalWrite(_trigger, HIGH);
delayMicroseconds(_triggerLength);
digitalWrite(_trigger, LOW);
uint32_t duration = pulseIn(_echo, HIGH, 300000);
if (_correctionFactor == 1)
// Wait for echo
pinMode(_echo, INPUT);
uint32_t duration = pulseIn(_echo, HIGH, 200000); // was 300000 (50+ meter)
if (_correctionFactor == 1.0)
{
return duration;
}
@ -241,6 +254,7 @@ uint32_t SRF05::_read()
}
// for median
void SRF05::_insertSort(uint32_t * array, uint8_t size)
{
uint8_t t, z;
@ -260,5 +274,5 @@ void SRF05::_insertSort(uint32_t * array, uint8_t size)
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -2,7 +2,7 @@
//
// FILE: SRF05.h
// AUTHOR: Rob Tillaart
// VERSION: 0.2.0
// VERSION: 0.3.0
// DATE: 2021-05-17
// PURPOSE: Arduino library for SRF05 distance sensor
// URL: https://github.com/RobTillaart/SRF05
@ -10,7 +10,7 @@
#include "Arduino.h"
#define SRF05_LIB_VERSION (F("0.2.0"))
#define SRF05_LIB_VERSION (F("0.3.0"))
const uint8_t SRF05_MODE_SINGLE = 0;
@ -22,7 +22,7 @@ const uint8_t SRF05_MODE_RUN_AVERAGE = 3;
class SRF05
{
public:
explicit SRF05(const uint8_t trigger, const uint8_t echo);
explicit SRF05(const uint8_t trigger, const uint8_t echo = 0);
// configuration
@ -40,6 +40,9 @@ public:
void setModeMedian(uint8_t count);
void setModeRunningAverage(float alpha);
uint8_t getOperationalMode();
// interval of average and median mode
void setSampleInterval(uint16_t microSeconds = 1000);
uint16_t getSampleInterval();
// get distance
@ -49,6 +52,7 @@ public:
float getMeter(); // m
float getInch(); // inch = 2.54 cm
float getFeet(); // feet = 12 inch
float getYards(); // yard = 3 feet = 36 inch
// Experimental - calibration
@ -66,6 +70,7 @@ public:
void setTriggerLength(uint8_t length = 10);
uint8_t getTriggerLength();
// TIMING
uint32_t lastTime();
@ -83,10 +88,12 @@ private:
float _alpha = 1.0;
float _value = 0;
float _correctionFactor = 1;
uint8_t _triggerLength = 10;
uint8_t _triggerLength = 10; // microSeconds
float _speedOfSound = 340; // 15°C 0%RH Sea level
uint32_t _lastTime = 0;
uint16_t _sampleInterval = 1000; // microSeconds
uint32_t _read();
void _insertSort(uint32_t * array, uint8_t size);
};

View File

@ -0,0 +1,49 @@
//
// FILE: SRF05_demo_single_pin.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo distance sensor
// URL: https://github.com/RobTillaart/SRF05
#include "SRF05.h"
const int trigger = 7;
const int echo = trigger;
// SRF05 SRF(trigger, echo); // alternative
// SRF05 SRF(trigger, trigger); // alternative
SRF05 SRF(trigger);
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println(__FILE__);
Serial.print("SRF05_LIB_VERSION: ");
Serial.println(SRF05_LIB_VERSION);
Serial.println();
SRF.setCorrectionFactor(1.035);
}
void loop()
{
Serial.println(SRF.getTime());
delay(100);
Serial.println(SRF.getMillimeter());
delay(100);
Serial.println(SRF.getCentimeter(), 1);
delay(100);
Serial.println(SRF.getMeter(), 2);
delay(100);
Serial.println(SRF.getInch(), 1);
delay(100);
Serial.println(SRF.getFeet(), 2);
delay(1000);
Serial.println();
}
// -- END OF FILE --

View File

@ -17,14 +17,19 @@ setModeMedian KEYWORD2
setModeRunningAverage KEYWORD2
getOperationalMode KEYWORD2
setSampleInterval KEYWORD2
getSampleInterval KEYWORD2
getTime KEYWORD2
getMillimeter KEYWORD2
getCentimeter KEYWORD2
getMeter KEYWORD2
getInch KEYWORD2
getFeet KEYWORD2
getYards KEYWORD2
determineSpeedOfSound KEYWORD2
setTriggerLength KEYWORD2
getTriggerLength KEYWORD2

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/SRF05.git"
},
"version": "0.2.0",
"version": "0.3.0",
"license": "MIT",
"frameworks": "*",
"platforms": "*",

View File

@ -1,5 +1,5 @@
name=SRF05
version=0.2.0
version=0.3.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for SRF05 distance sensor