0.1.4 TSL235R

This commit is contained in:
rob tillaart 2023-02-18 15:26:18 +01:00
parent 76a45dc61a
commit 9e2ad31141
11 changed files with 130 additions and 40 deletions

View File

@ -6,7 +6,7 @@ jobs:
lint: lint:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v3
- uses: arduino/arduino-lint-action@v1 - uses: arduino/arduino-lint-action@v1
with: with:
library-manager: update library-manager: update

View File

@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v3
- uses: ruby/setup-ruby@v1 - uses: ruby/setup-ruby@v1
with: with:
ruby-version: 2.6 ruby-version: 2.6

View File

@ -10,7 +10,7 @@ jobs:
test: test:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v3
- name: json-syntax-check - name: json-syntax-check
uses: limitusus/json-syntax-check@v1 uses: limitusus/json-syntax-check@v1
with: with:

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/). and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.1.4] - 2023-02-18
- updated cf value in **calculateFactor()**
- add **#define TSL235_DEFAULT_VOLTAGE** to allow set voltage from command line.
- update readme.md
- move code to .cpp
- update GitHub actions
- update license 2023
- minor edits
## [0.1.3] - 2022-11-26 ## [0.1.3] - 2022-11-26
- Add RP2040 support to build-CI. - Add RP2040 support to build-CI.
- Add CHANGELOG.md - Add CHANGELOG.md

View File

@ -1,6 +1,6 @@
MIT License MIT License
Copyright (c) 2020-2022 Rob Tillaart Copyright (c) 2020-2023 Rob Tillaart
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal

View File

@ -28,7 +28,15 @@ Above 1 uW/cm2 1 second or shorter is OK.
Note that for longer and shorter measurements than 1 second one must Note that for longer and shorter measurements than 1 second one must
convert the value to Hz, which is the number of pulses in 1 second. convert the value to Hz, which is the number of pulses in 1 second.
The library provides functions **irradiance()** to do that for you. The library provides two **irradiance()** functions to do that for you.
#### Related
- https://github.com/RobTillaart/TSL235R pulse based irradiance variant.
- https://github.com/RobTillaart/TSL260R analog IR irradiance variant.
- https://github.com/RobTillaart/AnalogUVSensor
- https://github.com/RobTillaart/ML8511 UV sensor
## Connection ## Connection
@ -44,9 +52,19 @@ Always check the datasheet.
## Interface ## Interface
- **TSL235R(float voltage = 5.0)** constructor, optionally one can give the operational voltage ```cpp
#include "TSL235R.h"
```
#### Constructor
- **TSL235R(float voltage = TSL235_DEFAULT_VOLTAGE)** constructor, optionally one can give the operational voltage
to add a small correction (< 1.5%). to add a small correction (< 1.5%).
Default voltage is 5.0 Volts. Default voltage is 5.0 Volts, this define can be overruled from command line.
#### Irradiance
- **float irradiance(uint32_t Hz)** returns the irradiance in uW/cm2. - **float irradiance(uint32_t Hz)** returns the irradiance in uW/cm2.
Note that Hz implies the measured pulses for 1 second. Note that Hz implies the measured pulses for 1 second.
- **float irradiance(uint32_t pulses, uint32_t milliseconds)** returns the irradiance in uW/cm2. - **float irradiance(uint32_t pulses, uint32_t milliseconds)** returns the irradiance in uW/cm2.
@ -55,6 +73,9 @@ To get irradiance in W/m2 one must divide by 100.
- **float irradiance_HS(uint32_t pulses, uint32_t microseconds)** returns the irradiance in uW/cm2. - **float irradiance_HS(uint32_t pulses, uint32_t microseconds)** returns the irradiance in uW/cm2.
This formula is used when the time is measured in microseconds. This formula is used when the time is measured in microseconds.
This is the most accurate measurement. This is the most accurate measurement.
#### Configuration
- **float getFactor()** returns the inner conversion factor from Hz to Watt/cm2. - **float getFactor()** returns the inner conversion factor from Hz to Watt/cm2.
- **void setWavelength(uint16_t wavelength = 635)** sets the wavelength so the formulas can use a - **void setWavelength(uint16_t wavelength = 635)** sets the wavelength so the formulas can use a
correction factor. correction factor.
@ -63,7 +84,7 @@ At the default wavelength of 635 nm the wavelength correction factor == 1.0.
- **float getWaveLengthFactor()** returns the wavelength correction factor. - **float getWaveLengthFactor()** returns the wavelength correction factor.
As the sensor is most sensitive around 750 nm this value helps to normalize the signal. As the sensor is most sensitive around 750 nm this value helps to normalize the signal.
This works only for (almost) monochromatic light. This works only for (almost) monochromatic light.
- **void setVoltage(float voltage)** sets the voltage so the formulas can use a correction factor. - **void setVoltage(float voltage = TSL235_DEFAULT_VOLTAGE)** sets the voltage so the formulas can use a correction factor.
This voltage correction factor is rather small < 1.5%. This voltage correction factor is rather small < 1.5%.
Note: this voltage can be changed runtime. Note: this voltage can be changed runtime.
- **float getVoltage()** returns the set voltage, by constructor or by **setVoltage()**. - **float getVoltage()** returns the set voltage, by constructor or by **setVoltage()**.
@ -77,19 +98,22 @@ See examples for typical usage.
## Future ## Future
#### must #### Must
- improve documentation - improve documentation
- test test test - test test test
#### should #### Should
- default voltage should be a #define so people who use 3.3V processor
can change the library with minimal effort. #### Could
- irradiance(pulses, millis) can be given a default of 1000 millis.
makes irradiance(Hz) obsolete.
- move code from .h to .cpp
#### could
- investigate hardware solutions for e.g. divide by 100 or 1000 or so. - investigate hardware solutions for e.g. divide by 100 or 1000 or so.
- investigate correction factor for white light and mixed light sources. - investigate correction factor for white light and mixed light sources.
- investigate calibration factor for timing of processor used. - investigate calibration factor for timing of processor used.
#### Wont
- irradiance(pulses, millis) can be given a default of 1000 millis.
- makes irradiance(Hz) obsolete.
- performance is less!

View File

@ -1,8 +1,10 @@
// //
// FILE: TSL235R.cpp // FILE: TSL235R.cpp
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// VERSION: 0.1.3 // VERSION: 0.1.4
// DATE: 2020-05-29
// PURPOSE: library for the TSL235R light to frequency convertor // PURPOSE: library for the TSL235R light to frequency convertor
// URL: https://github.com/RobTillaart/TSL235R
#include "TSL235R.h" #include "TSL235R.h"
@ -10,7 +12,10 @@
TSL235R::TSL235R(float voltage) TSL235R::TSL235R(float voltage)
{ {
_voltage = voltage; _waveLength = 635;
_waveLengthFactor = 1.0;
_voltageFactor = 1.0;
_voltage = voltage;
calculateFactor(); calculateFactor();
} }
@ -26,12 +31,19 @@ float TSL235R::irradiance(uint32_t pulses, uint32_t milliseconds)
return (pulses * 1000.0 * _factor) / milliseconds; return (pulses * 1000.0 * _factor) / milliseconds;
} }
float TSL235R::irradiance_HS(uint32_t pulses, uint32_t microseconds) float TSL235R::irradiance_HS(uint32_t pulses, uint32_t microseconds)
{ {
return (pulses * 1000000.0 * _factor) / microseconds; return (pulses * 1000000.0 * _factor) / microseconds;
} }
float TSL235R::getFactor()
{
return _factor;
}
void TSL235R::setWavelength(uint16_t wavelength) void TSL235R::setWavelength(uint16_t wavelength)
{ {
_waveLength = wavelength; _waveLength = wavelength;
@ -39,6 +51,18 @@ void TSL235R::setWavelength(uint16_t wavelength)
} }
uint16_t TSL235R::getWavelength()
{
return _waveLength;
}
float TSL235R::getWaveLengthFactor()
{
return _waveLengthFactor;
}
void TSL235R::setVoltage(float voltage) void TSL235R::setVoltage(float voltage)
{ {
_voltage = voltage; _voltage = voltage;
@ -46,14 +70,26 @@ void TSL235R::setVoltage(float voltage)
} }
float TSL235R::getVoltage()
{
return _voltage;
}
float TSL235R::getVoltageFactor()
{
return _voltageFactor;
}
void TSL235R::calculateFactor() void TSL235R::calculateFactor()
{ {
// figure 1 datasheet // figure 1 datasheet
// 1 KHz crosses the line at 35/230 between 1 and 10. // 1 KHz crosses the line at 35/230 between 1 and 10.
// so the correction factor is 10^0.15217 = 1.419659 = 1.42 (as all math has 3 decimals) // so the correction factor is 10^0.15217 = 1.419659
// as the graph is in kHz we need to correct a factor 1000 // as the graph is in kHz we need to correct a factor 1000
// as the irradiance function gets Hz // as the irradiance function gets Hz
const float cf = 0.00142; const float cf = 0.001419659;
_waveLengthFactor = calculateWaveLengthFactor(_waveLength); _waveLengthFactor = calculateWaveLengthFactor(_waveLength);
_voltageFactor = 0.988 + (_voltage - 2.7) * (0.015 / 2.8); _voltageFactor = 0.988 + (_voltage - 2.7) * (0.015 / 2.8);
@ -72,6 +108,7 @@ float TSL235R::calculateWaveLengthFactor(uint16_t _waveLength)
} }
// from https://github.com/RobTillaart/MultiMap
float TSL235R::multiMap(float value, float * _in, float * _out, uint8_t size) float TSL235R::multiMap(float value, float * _in, float * _out, uint8_t size)
{ {
// take care the value is within range // take care the value is within range
@ -80,7 +117,7 @@ float TSL235R::multiMap(float value, float * _in, float * _out, uint8_t size)
if (value >= _in[size-1]) return _out[size-1]; if (value >= _in[size-1]) return _out[size-1];
// search right interval // search right interval
uint8_t pos = 1; // _in[0] already tested uint8_t pos = 1; // _in[0] already tested
while(value > _in[pos]) pos++; while(value > _in[pos]) pos++;
// this will handle all exact "points" in the _in array // this will handle all exact "points" in the _in array

View File

@ -2,41 +2,48 @@
// //
// FILE: TSL235R.h // FILE: TSL235R.h
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// VERSION: 0.1.3 // VERSION: 0.1.4
// DATE: 2020-05-29
// PURPOSE: library for the TSL235R light to frequency convertor // PURPOSE: library for the TSL235R light to frequency convertor
// URL: https://github.com/RobTillaart/TSL235R
#define TSL235R_LIB_VERSION (F("0.1.3")) #define TSL235R_LIB_VERSION (F("0.1.4"))
#include "Arduino.h" #include "Arduino.h"
#if not defined(TSL235_DEFAULT_VOLTAGE)
#define TSL235_DEFAULT_VOLTAGE 5.0
#endif
class TSL235R class TSL235R
{ {
public: public:
TSL235R(float voltage = 5.0); TSL235R(float voltage = TSL235_DEFAULT_VOLTAGE);
float irradiance(uint32_t Hz); // Hz == pulses in one second. // Hz == pulses in one second.
float irradiance(uint32_t pulses, uint32_t milliseconds); // obsolete? // could be calculated from shorter/longer measurement.
float irradiance(uint32_t Hz);
float irradiance(uint32_t pulses, uint32_t milliseconds);
float irradiance_HS(uint32_t pulses, uint32_t microseconds); float irradiance_HS(uint32_t pulses, uint32_t microseconds);
float getFactor() { return _factor; }; float getFactor();
void setWavelength(uint16_t wavelength = 635); void setWavelength(uint16_t wavelength = 635);
uint16_t getWavelength() { return _waveLength; } uint16_t getWavelength();
float getWaveLengthFactor() { return _waveLengthFactor; } float getWaveLengthFactor();
void setVoltage(float voltage = 5.0); void setVoltage(float voltage = TSL235_DEFAULT_VOLTAGE);
float getVoltage() { return _voltage; }; float getVoltage();
float getVoltageFactor() { return _voltageFactor; }; float getVoltageFactor();
private: private:
uint16_t _waveLength = 635; uint16_t _waveLength = 635;
float _waveLengthFactor = 1.0; float _waveLengthFactor = 1.0;
float _voltage = 5.0;
float _voltageFactor = 1.0; float _voltageFactor = 1.0;
float _factor = 1.2; float _voltage;
float _factor;
void calculateFactor(); void calculateFactor();
float calculateWaveLengthFactor(uint16_t _waveLength); float calculateWaveLengthFactor(uint16_t _waveLength);

View File

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

View File

@ -1,5 +1,5 @@
name=TSL235R name=TSL235R
version=0.1.3 version=0.1.4
author=Rob Tillaart <rob.tillaart@gmail.com> author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com> maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Library for the TSL235R light to frequency convertor. sentence=Library for the TSL235R light to frequency convertor.

View File

@ -48,14 +48,21 @@ unittest_teardown()
} }
unittest(test_constants)
{
assertEqualFloat(5.0, TSL235_DEFAULT_VOLTAGE, 0.001);
}
unittest(test_constructor) unittest(test_constructor)
{ {
TSL235R mysensor; TSL235R mysensor;
assertEqual(635, mysensor.getWavelength() ); assertEqual(635, mysensor.getWavelength() );
assertEqualFloat(1.0, mysensor.getWaveLengthFactor(), 0.001); assertEqualFloat(1.0, mysensor.getWaveLengthFactor(), 0.001);
assertEqualFloat(5.0, mysensor.getVoltage(), 0.001); assertEqualFloat(5.0, mysensor.getVoltage(), 0.001);
assertEqualFloat(1.0, mysensor.getVoltageFactor(), 0.001); assertEqualFloat(1.0, mysensor.getVoltageFactor(), 0.001);
assertEqualFloat(0.00142, mysensor.getFactor(), 0.001); assertEqualFloat(0.001419659, mysensor.getFactor(), 0.001);
fprintf(stderr, "%1.6f\n", mysensor.getFactor() ); fprintf(stderr, "%1.6f\n", mysensor.getFactor() );
} }
@ -64,6 +71,7 @@ unittest(test_constructor)
unittest(test_wavelength) unittest(test_wavelength)
{ {
TSL235R mysensor; TSL235R mysensor;
assertEqual(635, mysensor.getWavelength() ); assertEqual(635, mysensor.getWavelength() );
assertEqualFloat(1.0, mysensor.getWaveLengthFactor(), 0.001); assertEqualFloat(1.0, mysensor.getWaveLengthFactor(), 0.001);
fprintf(stderr,"\n"); fprintf(stderr,"\n");
@ -90,6 +98,7 @@ unittest(test_wavelength)
unittest(test_voltage) unittest(test_voltage)
{ {
TSL235R mysensor(2.7); TSL235R mysensor(2.7);
assertEqualFloat(2.7, mysensor.getVoltage(), 0.001); assertEqualFloat(2.7, mysensor.getVoltage(), 0.001);
assertEqualFloat(0.988, mysensor.getVoltageFactor(), 0.001); assertEqualFloat(0.988, mysensor.getVoltageFactor(), 0.001);
fprintf(stderr,"\n"); fprintf(stderr,"\n");
@ -116,6 +125,7 @@ unittest(test_voltage)
unittest(test_conversion1) unittest(test_conversion1)
{ {
TSL235R mysensor; TSL235R mysensor;
assertEqualFloat(1.0, mysensor.getVoltageFactor(), 0.001); assertEqualFloat(1.0, mysensor.getVoltageFactor(), 0.001);
assertEqualFloat(1.0, mysensor.getWaveLengthFactor(), 0.001); assertEqualFloat(1.0, mysensor.getWaveLengthFactor(), 0.001);
@ -125,7 +135,7 @@ unittest(test_conversion1)
for (uint32_t Hz = 10; Hz < 1000000; Hz *= 2) for (uint32_t Hz = 10; Hz < 1000000; Hz *= 2)
{ {
float rad = mysensor.irradiance(Hz); float rad = mysensor.irradiance(Hz);
assertEqualFloat(0.00142 * Hz, mysensor.irradiance(Hz), 0.001 * Hz); // we must have a relative error here! assertEqualFloat(0.001419659 * Hz, mysensor.irradiance(Hz), 0.001 * Hz); // we must have a relative error here!
} }
} }
@ -133,6 +143,7 @@ unittest(test_conversion1)
unittest(test_conversion2) unittest(test_conversion2)
{ {
TSL235R mysensor; TSL235R mysensor;
assertEqualFloat(1.0, mysensor.getVoltageFactor(), 0.001); assertEqualFloat(1.0, mysensor.getVoltageFactor(), 0.001);
assertEqualFloat(1.0, mysensor.getWaveLengthFactor(), 0.001); assertEqualFloat(1.0, mysensor.getWaveLengthFactor(), 0.001);
fprintf(stderr,"\n"); fprintf(stderr,"\n");
@ -151,4 +162,5 @@ unittest(test_conversion2)
unittest_main() unittest_main()
// -------- // -- END OF FILE --