diff --git a/libraries/AnalogUVSensor/.arduino-ci.yml b/libraries/AnalogUVSensor/.arduino-ci.yml index ff5659b9..e7cb4633 100644 --- a/libraries/AnalogUVSensor/.arduino-ci.yml +++ b/libraries/AnalogUVSensor/.arduino-ci.yml @@ -2,6 +2,10 @@ compile: # Choosing to run compilation tests on 2 different Arduino platforms platforms: - uno - - leonardo - - due - - zero + # - due + # - zero + # - leonardo + - m4 + - esp32 + # - esp8266 + # - mega2560 \ No newline at end of file diff --git a/libraries/AnalogUVSensor/AnalogUVSensor.cpp b/libraries/AnalogUVSensor/AnalogUVSensor.cpp index fc90c311..a84380cc 100644 --- a/libraries/AnalogUVSensor/AnalogUVSensor.cpp +++ b/libraries/AnalogUVSensor/AnalogUVSensor.cpp @@ -1,12 +1,14 @@ // // FILE: AnalogUVSensor.h // AUTHOR: Rob Tillaart -// VERSION: 0.1.0 +// VERSION: 0.1.1 // DATE: 2021-09-25 // PURPOSE: AnalogUVSensor library // // HISTORY: // 0.1.0 2021-09-25 initial version +// 0.1.1 2021-10-17 update build-ci, readme.md +// refactor, plotter example #include "AnalogUVSensor.h" @@ -34,6 +36,7 @@ void AnalogUVSensor::begin(uint8_t analogPin, float volts, uint16_t maxADC) float AnalogUVSensor::read(uint8_t times) { uint32_t sum = 0; + if (times == 0) times = 1; for (int i = 0; i < times; i++) { sum += analogRead(_analogPin); @@ -50,7 +53,8 @@ float AnalogUVSensor::mV2index(uint16_t milliVolt) if (milliVolt < 227) return 0.0 + (1.0 * milliVolt - 50.0) / (227.0 - 50.0); // linear interpolation between 1..11 // formula derived with spreadsheet. - return 0.0104865310 * milliVolt - 1.289154988; + if (milliVolt < 1200) return 0.0104865310 * milliVolt - 1.289154988; + return 12; } diff --git a/libraries/AnalogUVSensor/AnalogUVSensor.h b/libraries/AnalogUVSensor/AnalogUVSensor.h index 9c038ffa..2ce96472 100644 --- a/libraries/AnalogUVSensor/AnalogUVSensor.h +++ b/libraries/AnalogUVSensor/AnalogUVSensor.h @@ -2,7 +2,7 @@ // // FILE: AnalogUVSensor.h // AUTHOR: Rob Tillaart -// VERSION: 0.1.0 +// VERSION: 0.1.1 // DATE: 2021-09-25 // PURPOSE: AnalogUVSensor library // @@ -12,7 +12,7 @@ #include "Arduino.h" -#define ANALOG_UVSENSOR_LIB_VERSION (F("0.1.0")) +#define ANALOG_UVSENSOR_LIB_VERSION (F("0.1.1")) class AnalogUVSensor diff --git a/libraries/AnalogUVSensor/README.md b/libraries/AnalogUVSensor/README.md index 2783e2d8..6ea5cb17 100644 --- a/libraries/AnalogUVSensor/README.md +++ b/libraries/AnalogUVSensor/README.md @@ -1,7 +1,16 @@ + +[![Arduino CI](https://github.com/RobTillaart/AnalogUVSensor/actions/workflows/arduino_test_runner.yml/badge.svg)](https://github.com/RobTillaart/AnalogUVSensor/actions/workflows/arduino_test_runner.yml) +[![Arduino-lint](https://github.com/RobTillaart/AnalogUVSensor/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/AnalogUVSensor/actions/workflows/arduino-lint.yml) +[![JSON check](https://github.com/RobTillaart/AnalogUVSensor/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/AnalogUVSensor/actions/workflows/jsoncheck.yml) +[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/AnalogUVSensor/blob/master/LICENSE) +[![GitHub release](https://img.shields.io/github/release/RobTillaart/AnalogUVSensor.svg?maxAge=3600)](https://github.com/RobTillaart/AnalogUVSensor/releases) + + # AnalogUVSensor Arduino library for analogue UV sensor + ## Description ![Foo](https://www.tinytronics.nl/shop/image/cache/catalog/products/product-003601/uv-light-sensor-module-200-370nm-80x80w.jpg) @@ -28,42 +37,55 @@ The sensor has an analogue output that is roughly linear with the UV-index (sunl | 1.170> | 11 | From this table the formula is derived (spreadsheet) which is pretty linear between UV 1 and 11. +Between 0 and 1 there seems to be a curve / other linear relation. ### Notes -Note: The sensor is not calibrated and the table is indicative for sunlight (assumption!) +Note: The sensor is not calibrated and the table is indicative for sunlight (warning assumption!) and therefore not suitable for e.g. medical or industrial usage. -Note: UV radiation will not go through glass, so inside you will most likely always read zero. -This may help to calibrate the zero level of the sensor. +Note: UV radiation will not go through normal glass, so inside (a house) you will most likely always read zero. +This may of course help to calibrate the zero level of the sensor. Note: depending on the light source used, the table above is incorrect. -If one wants to use other values one need to fix this in the **mv2index()** function. +If one wants to use other values one need to fix the formula in the **mv2index()** function. + +Note: the output of the sensor is typically 0 .. 1.1 volt. To increase precision one might configure +the ADC used to match this voltage range. E.g. Arduino UNO can be set to use an internal 1.1 volt reference. +See Arduino manual **setAnalogReference()** + -### Sensitivity +### Wavelength sensitivity The sensor is sensitive for wavelengths from 200 - 370 nm, so mostly in UVB and UVA region and less in the UVC. +There is no information about the detailed wavelength sensitivity, so it is not possible to compensate. +However one could assume that there is a normal (or other) distribution over the 200 - 370 nm range and create +a compensation table based upon the assumed distribution. -https://en.wikipedia.org/wiki/Ultraviolet +More about UV see - https://en.wikipedia.org/wiki/Ultraviolet ## Interface - **AnalogUVSensor()** Constructor. - **void begin(uint8_t analogPin, float volts = 5.0, uint16_t maxADC = 1023)** -set the parameters of the sensor, analogPin, volts and maxADC to specify the internal ADC. Volts and maxADC have a default so these can be omitted. -Note that one needs to reset these parameters if the settings of the internal ADC are +set the parameters of the sensor, analogPin, volts and maxADC to specify the internal ADC. +Volts and maxADC have a default so these can be omitted if these match. +Note: one needs to reset these parameters if the settings of the internal ADC are changed e.g. to INTERNAL_1V1 to change the accuracy. - **float read(uint8_t times = 1)** Returns the UV index. Read the analogue sensor one (or more) times to average the reading. -This can improve the accuracy of the reading. +This can improve the accuracy of the reading. +If times == 0 it is set to 1. - **float mV2index(uint16_t milliVolt)** Returns the UV index. -The conversion formula from milliVolt to the UV index used internally by the **read()** function. +The conversion formula from milliVolt to the UV index is used internally by the **read()** function. This function can also be called with a voltage measured with an external ADC. -- **char index2color(float index)** Converts an index to the first letter of Green, -Yellow, Orange, Red or Purple. Can be used as indication on a user interface. +THe function will return a value between 0.0 and 12.0. +- **char index2color(float index)** Converts an UV index to the first letter of **G**reen, +**Y**ellow, **O**range, **R**ed or **P**urple. +Can be used as indication on a user interface. This function can also be called with an index from an other UV index sensor. @@ -73,9 +95,10 @@ Works only if setPowerPin is called properly. - **void setPowerPin(uint8_t powerPin, bool invert = false)** Can be used to control the power of the sensor e.g. for low power mode. -Optionally one can set the invert flag to **true** to invert the **HIGH LOW** levels e.g. when the analogue sensor is switched through a MOSFET. -- **void switchOff()** should switch off the power of the sensor. -- **void switchOn()** should switch on the power of the sensor. +Optionally one can set the invert flag to **true** to invert the **HIGH LOW** levels +e.g. when the analogue sensor is switched through a MOSFET. +- **void switchOff()** switch off the power of the sensor. +- **void switchOn()** switch on the power of the sensor. ### Future @@ -85,7 +108,8 @@ Optionally one can set the invert flag to **true** to invert the **HIGH LOW** le - investigate response time to stabilize e.g. does it react fast on clouds. - investigate with different light sources (UVled, TL, sunlight). - investigate time needed to first (stable) measurement. -- **index2time()** calculate max exposure time? dangerous? +- **index2time()** calculate max exposure time? dangerous advice? +- **uint32_t index2RGB(float index)** full colour RGB mapping. ## Sponsor diff --git a/libraries/AnalogUVSensor/examples/analog_uv_sensor/analog_uv_sensor.ino b/libraries/AnalogUVSensor/examples/analog_uv_sensor/analog_uv_sensor.ino index 72dc75d4..7721b2f6 100644 --- a/libraries/AnalogUVSensor/examples/analog_uv_sensor/analog_uv_sensor.ino +++ b/libraries/AnalogUVSensor/examples/analog_uv_sensor/analog_uv_sensor.ino @@ -1,7 +1,6 @@ // // FILE: UV_sensor.ino // AUTHOR: Rob Tillaart -// VERSION: 0.1.0 // PURPOSE: demo // DATE: 2021-09-21 diff --git a/libraries/AnalogUVSensor/examples/uvi_color/uvi_color.ino b/libraries/AnalogUVSensor/examples/uvi_color/uvi_color.ino index e5e83a40..79f49245 100644 --- a/libraries/AnalogUVSensor/examples/uvi_color/uvi_color.ino +++ b/libraries/AnalogUVSensor/examples/uvi_color/uvi_color.ino @@ -1,7 +1,6 @@ // // FILE: demUV.ino // AUTHOR: Rob Tillaart -// VERSION: 0.1.0 // PURPOSE: demo UV sensor // DATE: 2021-09-25 @@ -25,7 +24,7 @@ void setup() void loop() { - float uvi = AUV.read(); + float uvi = AUV.read(3); // average 3 readings Serial.print("UVI: "); Serial.print(uvi, 1); Serial.print("\t"); diff --git a/libraries/AnalogUVSensor/examples/uvi_demo/uvi_demo.ino b/libraries/AnalogUVSensor/examples/uvi_demo/uvi_demo.ino index 4a77797b..dfaa3f5a 100644 --- a/libraries/AnalogUVSensor/examples/uvi_demo/uvi_demo.ino +++ b/libraries/AnalogUVSensor/examples/uvi_demo/uvi_demo.ino @@ -1,7 +1,6 @@ // // FILE: uvi_demo.ino // AUTHOR: Rob Tillaart -// VERSION: 0.1.0 // PURPOSE: demo UV sensor // DATE: 2021-09-25 @@ -24,7 +23,7 @@ void setup() void loop() { - float uvi = AUV.read(); + float uvi = AUV.read(5); // average 5 readings Serial.print("UVI: "); Serial.println(uvi, 1); delay(1000); diff --git a/libraries/AnalogUVSensor/examples/uvi_demo_low_power/uvi_demo_low_power.ino b/libraries/AnalogUVSensor/examples/uvi_demo_low_power/uvi_demo_low_power.ino index 25308815..e60c6da2 100644 --- a/libraries/AnalogUVSensor/examples/uvi_demo_low_power/uvi_demo_low_power.ino +++ b/libraries/AnalogUVSensor/examples/uvi_demo_low_power/uvi_demo_low_power.ino @@ -1,7 +1,6 @@ // // FILE: uvi_demo_low_power.ino // AUTHOR: Rob Tillaart -// VERSION: 0.1.0 // PURPOSE: demo UV sensor // DATE: 2021-09-25 @@ -21,7 +20,7 @@ void setup() AUV.begin(A0, 5.0, 1023); - AUV.setPowerPin(4); + AUV.setPowerPin(4); // connect power of sensor to pin 4 AUV.switchOff(); } @@ -29,7 +28,7 @@ void setup() void loop() { AUV.switchOn(); - float uvi = AUV.read(); + float uvi = AUV.read(); // default is 1 read AUV.switchOff(); Serial.print("UVI: "); diff --git a/libraries/AnalogUVSensor/examples/uvi_plotter/uvi_plotter.ino b/libraries/AnalogUVSensor/examples/uvi_plotter/uvi_plotter.ino new file mode 100644 index 00000000..97b755f6 --- /dev/null +++ b/libraries/AnalogUVSensor/examples/uvi_plotter/uvi_plotter.ino @@ -0,0 +1,47 @@ +// +// FILE: uvi_plotter.ino +// AUTHOR: Rob Tillaart +// PURPOSE: demo UV sensor +// DATE: 2021-10-17 + +// Use the Arduino IDE -> Tools -> Serial plotter +// to get a graph. + +#include "AnalogUVSensor.h" + +AnalogUVSensor AUV; +float lastRead = 0; + +void setup() +{ + Serial.begin(115200); + // Serial.println(__FILE__); + // Serial.print("ANALOG_UVSENSOR_LIB_VERSION: "); + // Serial.println(ANALOG_UVSENSOR_LIB_VERSION); + + AUV.begin(A0, 5.0, 1023); + lastRead = AUV.read(5); // average 5 readings + + // print the header for the plotter. + Serial.println("UVI \tLAST \tDELTA"); +} + + +void loop() +{ + float uvi = AUV.read(5); // average 5 readings + float delta = uvi - lastRead; + + Serial.print(uvi, 1); + Serial.print('\t'); + Serial.print(lastRead, 1); + Serial.print('\t'); + Serial.print(delta, 1); + Serial.print('\n'); + + lastRead = uvi; + delay(1000); +} + + +// -- END OF FILE -- diff --git a/libraries/AnalogUVSensor/examples/uvi_test/uvi_test.ino b/libraries/AnalogUVSensor/examples/uvi_test/uvi_test.ino index b652b835..d3e7e4ba 100644 --- a/libraries/AnalogUVSensor/examples/uvi_test/uvi_test.ino +++ b/libraries/AnalogUVSensor/examples/uvi_test/uvi_test.ino @@ -1,7 +1,6 @@ // // FILE: uvi_test.ino // AUTHOR: Rob Tillaart -// VERSION: 0.1.0 // PURPOSE: demo UV sensor // DATE: 2021-09-25 diff --git a/libraries/AnalogUVSensor/library.json b/libraries/AnalogUVSensor/library.json index 2e0eb54f..38c24c41 100644 --- a/libraries/AnalogUVSensor/library.json +++ b/libraries/AnalogUVSensor/library.json @@ -15,7 +15,7 @@ "type": "git", "url": "https://github.com/RobTillaart/AnalogUVSensor.git" }, - "version": "0.1.0", + "version": "0.1.1", "license": "MIT", "frameworks": "arduino", "platforms": "*" diff --git a/libraries/AnalogUVSensor/library.properties b/libraries/AnalogUVSensor/library.properties index a06a4c3b..c92d7503 100644 --- a/libraries/AnalogUVSensor/library.properties +++ b/libraries/AnalogUVSensor/library.properties @@ -1,5 +1,5 @@ name=AnalogUVSensor -version=0.1.0 +version=0.1.1 author=Rob Tillaart maintainer=Rob Tillaart sentence=AnalogUVSensor library for Arduino.