GY-63_MS5611/libraries/rain/README.md

175 lines
6.6 KiB
Markdown
Raw Normal View History

2022-12-04 11:55:37 -05:00
[![Arduino CI](https://github.com/RobTillaart/RAIN/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![Arduino-lint](https://github.com/RobTillaart/RAIN/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/RAIN/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/RAIN/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/RAIN/actions/workflows/jsoncheck.yml)
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/RAIN/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/RAIN.svg?maxAge=3600)](https://github.com/RobTillaart/RAIN/releases)
# RAIN
2022-12-06 05:26:48 -05:00
RAIN is an Arduino library for a rain sensor (analog).
2022-12-04 11:55:37 -05:00
## Description
2023-01-20 11:42:39 -05:00
A rain sensor like the FC-37, YL-83, HM-RD a.o. is a relative simple device.
It measures the resistance between wires when these are put in a liquid, water or in the ground.
2022-12-06 05:26:48 -05:00
The device converts the resistance to a voltage typical 0 .. 5 Volt.
2022-12-06 10:57:55 -05:00
The more the wires are covered by the liquid, the lower the voltage.
0.0 Volt is WET, and a high voltage is DRY.
2022-12-04 11:55:37 -05:00
2023-01-20 11:42:39 -05:00
The breakout (LM393 comparator) I used to test also has a digital output,
2022-12-06 10:57:55 -05:00
which goes LOW if a threshold (to be set with a potentiometer on the breakout) is reached.
2022-12-04 11:55:37 -05:00
2023-01-20 11:42:39 -05:00
The library is EXPERIMENTAL as it needs more testing.
2022-12-06 05:26:48 -05:00
(changes of the interface are definitely possible).
2022-12-04 11:55:37 -05:00
2023-01-20 11:42:39 -05:00
2022-12-06 10:57:55 -05:00
## Hardware connection
Typical connection
```
Processor LM393 SENSOR FC-37
+-------------+ +----------+ +---------------------+
| | | | | |
| GND |----->| GND | | |
| powerPin |----->| 5V |-----| |
| analogIn |<-----| AO | | |
| | | |-----| |
| digital in |<-----| DO | | |
| | | | | |
+-------------+ +----------+ +---------------------+
```
2022-12-04 11:55:37 -05:00
## Interface
2023-01-20 11:42:39 -05:00
- **RAIN(uint8_t analogPort, uint8_t powerPin = 255)** constructor.
analogPort is the internal analog port to use.
2022-12-06 10:57:55 -05:00
powerPin is optional, but recommended.
2023-01-20 11:42:39 -05:00
The default 255 means **NO** powerPin selected.
- **bool begin(float maxVoltage, uint16_t maxSteps)** sets the ADC parameters.
Allows to be changed runtime, e.g. if voltage fluctuates the math can be adapted by calling **begin()** again.
2022-12-04 11:55:37 -05:00
Might be a separate **setVoltage()** is more efficient.
- **float raw(uint8_t times = 1)** makes 1 or more measurements and averages them.
returned value is the average number of ADC steps.
- **float read(uint8_t times = 1)** makes 1 or more measurements, averages them and convert the average to a voltage.
2022-12-06 10:57:55 -05:00
This voltage is returned, and also cached for **percentage()** and **getLevel()**.
#### powerControl
Will only work if the **powerPin** is set in the constructor.
- **void powerOn()** switch the sensor on.
- **void powerOff()** switch the sensor off.
2023-01-20 11:42:39 -05:00
- **void setPowerDelay(uint8_t powerDelay = 100)** delay in microseconds
in powerOn() to stabilize the rain sensor.
Default is 100 microseconds.
- **uint8_t getPowerDelay()** returns the set value.
2022-12-06 10:57:55 -05:00
The powerPin is used to switch the LM393 ON and OFF so the sensor is powered
2023-01-20 11:42:39 -05:00
only when the sensor is read.
This reduces corrosion and minimizes the usage of energy.
2022-12-06 10:57:55 -05:00
Note: when the power is OFF, the digital-out cannot be used e.g. for interrupts.
So check what your project needs.
2023-01-20 11:42:39 -05:00
Note: the powerOn() default delays for 100 us to give the LM393 time to stabilize.
Adjust this time if you want to optimize performance, power consumption or corrosion.
Values can be 0 .. 255.
2022-12-06 10:57:55 -05:00
2022-12-04 11:55:37 -05:00
#### Analysis
2022-12-06 10:57:55 -05:00
- **void setDryReference(float dryRef)** used to calibrate the voltage when the sensor is dry.
Use **read()** to read / calibrate the voltage when the sensor is dry.
If not explicitly set, the max ADC voltage os used.
- **void getDryReference()** returns the set value.
2022-12-04 11:55:37 -05:00
- **float percentage()** returns the last **read()** to a percentage.
Note one needs to call read() again to get a new value as this uses a cached value.
2022-12-06 05:26:48 -05:00
- **float delta()** returns the delta voltage compared to previous read.
It give the first derivative of the signal. How fast does it rise.
2022-12-06 10:57:55 -05:00
- **bool setLevel(uint8_t nr, uint16_t millivolts)** allows a user to set 4 voltage levels in milliVolts.
2022-12-06 05:26:48 -05:00
- **uint8_t getLevel()**
2023-01-20 11:42:39 -05:00
Returns the level of the current cached voltage.
2022-12-04 11:55:37 -05:00
See example.
2022-12-06 10:57:55 -05:00
The library allows the user to set 4 thresholds or levels for the **getLevel()** function.
2023-01-20 11:42:39 -05:00
These 4 levels + 1 zero level can help to control behaviour of a system at a certain level.
Typical levels are almost empty, almost full and full.
The level do not need to be on a linear mapping like 20% steps, if your project need
2022-12-06 05:26:48 -05:00
other levels you can define these.
Note it is possible to adjust the levels runTime with **setLevel()**
2022-12-04 11:55:37 -05:00
#### MultiMap
2023-01-20 11:42:39 -05:00
For a continuous mapping one can use the **MultiMap** library.
It allows to map the voltage to any other useful unit as it can handle
2022-12-06 05:26:48 -05:00
even non-linearities well.
2022-12-04 11:55:37 -05:00
See https://github.com/RobTillaart/MultiMap
## Operation
2022-12-06 10:57:55 -05:00
The examples show the basic working of the functions of the library.
2023-01-20 11:42:39 -05:00
The rain sensor can be used in different types of projects.
2022-12-06 10:57:55 -05:00
Every project has a typical orientation of the sensor.
| project | orientation | measurement |
|:-----------------|:----------------:|:-------------:|
| rain sensor | angle e.g. 45° | polling
| leak detection | horizontal | interrupt - digital OUT
| water level | vertical | polling
2023-01-20 11:42:39 -05:00
For other applications it depends.
2022-12-04 11:55:37 -05:00
## Future
#### Must
2023-01-20 11:42:39 -05:00
2022-12-04 11:55:37 -05:00
- update documentation
- links etc.
#### Should
2023-01-20 11:42:39 -05:00
2022-12-06 05:26:48 -05:00
- optimizations
2022-12-06 10:57:55 -05:00
- a lot of floats...==> more uint16_t millivolts? (0.2.0)
2023-01-20 11:42:39 -05:00
- getLevel() split into level() + getLevel(nr); // semantics
- breaking change == 0.2.0
- level(0) should be settable too
2022-12-04 11:55:37 -05:00
#### Could
2023-01-20 11:42:39 -05:00
2022-12-06 05:26:48 -05:00
- add unit-tests
- **float readExt(float voltage)** for external ADC
2023-01-20 11:42:39 -05:00
- See **ACS712** library how this can be done.
2022-12-06 05:26:48 -05:00
- investigate: **getLevel()** should it do a read()?
- **setForcedRead(bool flag)** + getter
- investigate "a scale of wetness"
- investigate
- different salinity
- different liquids? which?
- how linear is the device?
2022-12-04 11:55:37 -05:00
2022-12-06 10:57:55 -05:00
#### Won't (unless requested)
2023-01-20 11:42:39 -05:00
2022-12-06 05:26:48 -05:00
- example with multiMap
- see multiMap library.
2022-12-06 10:57:55 -05:00
- **incrLevel(nr, amount = 1)** + **decrLevel(nr, amount = 1)**
to allow easier runtime tuning
2023-01-20 11:42:39 -05:00
- investigate level-changed "event"
- user should poll ==> keeps the lib simple.
- make the number of levels configurable
- dynamic array allocation.?
2022-12-04 11:55:37 -05:00