216 lines
8.4 KiB
Markdown
Raw Normal View History

2021-05-26 11:31:36 +02:00
[![Arduino CI](https://github.com/RobTillaart/SRF05/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
2021-11-18 13:52:59 +01:00
[![Arduino-lint](https://github.com/RobTillaart/SRF05/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/SRF05/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/SRF05/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/SRF05/actions/workflows/jsoncheck.yml)
2021-05-26 11:31:36 +02:00
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/SRF05/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/SRF05.svg?maxAge=3600)](https://github.com/RobTillaart/SRF05/releases)
# SRF05
2021-11-18 13:52:59 +01:00
Arduino library for the SRF05 distance sensor and compatibles.
2021-05-26 11:31:36 +02:00
## Description
2023-03-16 11:50:16 +01:00
The library allows to adjust to the speed of sound (sos).
2021-11-18 13:52:59 +01:00
Reasons to use a different value are temperature, humidity, type of gas, composition etc.
2021-05-26 11:31:36 +02:00
2023-03-16 11:50:16 +01:00
Default value for the speed of sound is set to 340 m/s. (~15°C)
#### Effect temperature and humidity
2021-05-26 11:31:36 +02:00
2021-11-18 13:52:59 +01:00
Several correction formulas for the speed of sound are available on the internet.
2021-05-26 11:31:36 +02:00
2021-11-18 13:52:59 +01:00
```
// temperature in °C
v = 331.3 + 0.606 * temperature (m/s)
v = 331.4 + 0.606 * temperature + 0.0124 * rel_humidity (m/s)
v = 20.05 * sqrt(273.16 + temperature) (m/s)
```
2021-05-26 11:31:36 +02:00
2023-03-16 11:50:16 +01:00
In fact humidity has an effect which increases with temperature so the formula is more complex.
2021-11-18 13:52:59 +01:00
See - https://forum.arduino.cc/t/ultrasonic-sensor-to-determine-water-level/64890/12
2021-05-26 11:31:36 +02:00
2021-11-18 13:52:59 +01:00
| temp | 10% | 20% | 30% | 40% | 50% | 60% | 70% | 80% | 90% | notes |
|:----:|:------:|:------:|:------:|:------:|:------:|:------:|:------:|:------:|:------:|:------|
| -30 | 313.5 | 329.5 | 312.9 | 329.6 | 313.0 | 329.6 | 313.1 | 329.7 | 329.7 | extrapolated indication
| -25 | 316.5 | 332.6 | 316.0 | 332.7 | 316.1 | 332.7 | 316.2 | 332.8 | 332.9 | extrapolated indication
| -20 | 319.5 | 335.6 | 319.1 | 335.7 | 319.2 | 335.9 | 319.3 | 336.0 | 336.0 | extrapolated indication
| -15 | 322.5 | 330.5 | 322.2 | 330.6 | 322.3 | 330.6 | 322.4 | 330.7 | 330.7 | extrapolated indication
| -10 | 325.5 | 333.6 | 325.3 | 333.7 | 325.4 | 333.7 | 325.5 | 333.8 | 333.9 | extrapolated indication
| -5 | 328.5 | 336.6 | 328.4 | 336.7 | 328.5 | 336.9 | 328.6 | 337.0 | 337.0 | extrapolated indication
2021-05-26 11:31:36 +02:00
| 0 | 331.5 | 331.5 | 331.5 | 331.6 | 331.6 | 331.6 | 331.7 | 331.7 | 331.7 |
| 5 | 334.5 | 334.6 | 334.6 | 334.7 | 334.7 | 334.7 | 334.8 | 334.8 | 334.9 |
| 10 | 337.5 | 337.6 | 337.7 | 337.7 | 337.8 | 337.9 | 337.9 | 338.0 | 338.0 |
| 15 | 340.5 | 340.6 | 340.7 | 340.8 | 340.9 | 341.0 | 341.1 | 341.2 | 341.2 |
| 20 | 343.5 | 343.6 | 343.7 | 343.9 | 344.0 | 344.1 | 344.2 | 344.4 | 344.5 |
| 25 | 346.4 | 346.6 | 346.8 | 347.0 | 347.1 | 347.3 | 347.5 | 347.6 | 347.8 |
| 30 | 349.4 | 349.6 | 349.9 | 350.1 | 350.3 | 350.5 | 350.8 | 351.0 | 351.2 |
2021-11-18 13:52:59 +01:00
| 35 | 352.4 | 352.6 | 353.0 | 353.2 | 353.5 | 353.7 | 354.1 | 354.4 | 354.6 | extrapolated indication
| 40 | 355.4 | 355.6 | 356.1 | 356.3 | 356.7 | 356.9 | 357.4 | 357.8 | 358.0 | extrapolated indication
| 45 | 358.4 | 358.6 | 359.2 | 359.4 | 359.9 | 360.1 | 360.7 | 361.2 | 361.4 | extrapolated indication
| 50 | 361.4 | 361.6 | 362.3 | 362.5 | 363.1 | 363.3 | 364.0 | 364.6 | 364.8 | extrapolated indication
| 55 | 364.4 | 364.6 | 365.4 | 365.6 | 366.3 | 366.5 | 367.3 | 368.0 | 368.2 | extrapolated indication
| 60 | 367.4 | 367.6 | 368.5 | 368.7 | 369.5 | 369.7 | 370.6 | 371.4 | 371.6 | extrapolated indication
(table based upon https://www.engineeringtoolbox.com/air-speed-sound-d_603.html)
2021-05-26 11:31:36 +02:00
The library has several ways to improve the quality of the measurements.
2023-03-16 11:50:16 +01:00
E.g. by taking the average or the median of multiple readings.
This can be set with the mode commands.
2021-05-26 11:31:36 +02:00
The library allows to set a correction factor to compensate for the timing of
the **pulseIn()** function. This has in the end the same effect as changing the
2023-03-16 11:50:16 +01:00
speed of sound however it is technically more correct to keep the two separated.
2021-05-26 11:31:36 +02:00
## Interface
2023-03-16 11:50:16 +01:00
```cpp
#include "SRF05.h"
```
#### Constructor
2021-05-26 11:31:36 +02:00
- **SRF05(const uint8_t trigger, const uint8_t echo, const uint8_t out = 0)** constructor to set the trigger and echo pin.
2023-03-16 11:50:16 +01:00
It is not clear what the purpose of the OUT pin is, effectively it is not used yet.
2021-05-26 11:31:36 +02:00
2023-03-16 11:50:16 +01:00
#### Configuration
2021-05-26 11:31:36 +02:00
- **void setSpeedOfSound(float sos)** adjust the speed of sound.
2023-03-16 11:50:16 +01:00
See table above.
2021-05-26 11:31:36 +02:00
- **float getSpeedOfSound()** return set value.
2023-03-16 11:50:16 +01:00
- **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%.
2021-11-18 13:52:59 +01:00
Should not be used to correct the speed of sound :)
2023-03-16 11:50:16 +01:00
Returns false if factor <= 0.
2021-11-18 13:52:59 +01:00
- **float getCorrectionFactor()** returns the current correction factor.
2021-05-26 11:31:36 +02:00
2023-03-16 11:50:16 +01:00
#### Operational mode
2022-11-25 19:11:37 +01:00
Normally a single read should be OK.
2023-03-16 11:50:16 +01:00
- **void setModeSingle()** read a single time.
This is the default and typical the fastest.
2022-11-25 19:11:37 +01:00
- **void setModeAverage(uint8_t count)** read count times and take the average.
2023-03-16 11:50:16 +01:00
Note: between the reads there is a delay of 1 millisecond.
- **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.
- **void setModeRunningAverage(float alpha)** use a running average algorithm
with a weight alpha. Value for alpha depends on your application.
2021-11-18 13:52:59 +01:00
- **uint8_t getOperationalMode()** returns the operational mode 0..3.
2023-03-16 11:50:16 +01:00
See table below.
2021-11-18 13:52:59 +01:00
2023-03-16 11:50:16 +01:00
| operational mode | value | Notes |
|:-------------------------|:-------:|:-------:|
| SRF05_MODE_SINGLE | 0 | |
| SRF05_MODE_AVERAGE | 1 | |
| SRF05_MODE_MEDIAN | 2 | |
| SRF05_MODE_RUN_AVERAGE | 3 | |
| | other | error |
2021-05-26 11:31:36 +02:00
2023-03-16 11:50:16 +01:00
#### Get distance
2021-05-26 11:31:36 +02:00
- **uint32_t getTime()** returns distance in microseconds.
2022-11-25 19:11:37 +01:00
- **uint32_t getMillimeter()** returns distance in millimetre.
- **float getCentimeter()** returns distance in centimetre.
2021-05-26 11:31:36 +02:00
- **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).
2023-03-16 11:50:16 +01:00
#### Experimental - setTriggerLength
2021-05-26 11:31:36 +02:00
2023-03-16 11:50:16 +01:00
Since 0.1.4 two experimental functions are added to tune the length
of the trigger signal.
The idea is that shorter triggers can be used with harder surfaces
or short distances. Longer trigger for longer distances.
The effects and value of adjusting trigger length needs investigation.
Experiences are welcome.
- **void setTriggerLength(uint8_t length = 10)** default length == 10 us.
- **uint8_t getTriggerLength()** returns set length.
#### Experimental - calibration
Put the sensor at exactly 1.00 meter from a wall, and based
2022-11-25 19:11:37 +01:00
upon the timing it will give an estimate for the speed of sound.
2023-03-16 11:50:16 +01:00
0.1.2 version seems to be accurate within 5 %.
2021-05-26 11:31:36 +02:00
2023-03-16 11:50:16 +01:00
- **float determineSpeedOfSound(uint16_t distance)** distance is between
sensor and the wall - not forth and back.
The distance is averaged over 16 measurements.
2021-05-26 11:31:36 +02:00
2023-03-16 11:50:16 +01:00
Function can be used to compensate for temperature and humidity.
2021-11-18 13:52:59 +01:00
2021-05-26 11:31:36 +02:00
2023-03-16 11:50:16 +01:00
#### Performance
2021-05-26 11:31:36 +02:00
2021-11-18 13:52:59 +01:00
Assumes default speed of sound of 340 m/sec.
2023-03-16 11:50:16 +01:00
| distance (cm) | time (us) |
|:-------------:|----------:|
| 1 | 29.4 |
| 2 | 58.8 |
| 5 | 147.1 |
| 10 | 294.1 |
| 20 | 588.2 |
| 50 | 1470.6 |
| 100 | 2941.2 |
| 200 | 5882.4 |
| 300 | 8823.5 |
| 400 | 11764.7 |
to be elaborated.
2021-05-26 11:31:36 +02:00
## Operational
2022-11-25 19:11:37 +01:00
See examples.
2021-05-26 11:31:36 +02:00
2021-11-18 13:52:59 +01:00
## Future
2021-05-26 11:31:36 +02:00
2023-03-16 11:50:16 +01:00
#### Must
- investigate purpose/possibilities of the OUT pin.
#### Should
2022-11-25 19:11:37 +01:00
- add examples
- DHT22 and the formula for SOS
2023-03-16 11:50:16 +01:00
- add **float calcSOS(float temp, float humidity = 0)**
- investigate
- should **setSpeedOfSound(float sos)** return bool if sos <=0 ?
- value of **setTriggerLength()**
#### Could
2022-11-25 19:11:37 +01:00
- set default SOS to an SOS from the table instead of 340.
2023-03-16 11:50:16 +01:00
- add example to determine the correction factor?
- delay(1) in average configurable?
2021-11-18 13:52:59 +01:00
2023-03-16 11:50:16 +01:00
#### Wont
2021-05-26 11:31:36 +02:00
2022-11-25 19:11:37 +01:00
- print feet as 3'2" or 3-7/8 feet (is that needed in this lib)
- in printHelpers ?
2023-03-16 11:50:16 +01:00
- fix magic conversion numbers.
- add ```float lastValue()``` ?
- not all paths update this variable.
- add ```float delta()``` difference with last value.
- how to handle different units? or only time?