add TSL235R

This commit is contained in:
rob tillaart 2021-05-30 14:16:15 +02:00
parent f27f754fbb
commit 1fd79bf4a2
15 changed files with 793 additions and 0 deletions

View File

@ -0,0 +1,7 @@
compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
- uno
- leonardo
- due
- zero

View File

@ -0,0 +1,13 @@
name: Arduino-lint
on: [push, pull_request]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: arduino/arduino-lint-action@v1
with:
library-manager: update
compliance: strict

View File

@ -0,0 +1,13 @@
---
name: Arduino CI
on: [push, pull_request]
jobs:
arduino_ci:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: Arduino-CI/action@master
# Arduino-CI/action@v0.1.1

View File

@ -0,0 +1,18 @@
name: JSON check
on:
push:
paths:
- '**.json'
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: json-syntax-check
uses: limitusus/json-syntax-check@v1
with:
pattern: "\\.json$"

21
libraries/TSL235R/LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2021-2021 Rob Tillaart
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -0,0 +1,64 @@
[![Arduino CI](https://github.com/RobTillaart/TSL235R/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/TSL235R/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/TSL235R.svg?maxAge=3600)](https://github.com/RobTillaart/TSL235R/releases)
# TSL235R
Arduino library for the TSL235R light to frequency convertor.
## Description
This library does not measure the frequency but has some functions to compensate e.g.
for wavelength and voltage used.
The library is not tested extensively yet.
The sensor operating voltage is between 2.7 and 5.5 max.
For measurements below 1uW/cm2 one bests measures for multiple seconds
Above 1 uW/cm2 1 second or shorter is OK.
Note that for longer and shorter measurements than 1 second one must convert the
value to Hz, which is the nr of pulses in 1 second.
## Connection
```
// PIN 1 - GND
// PIN 2 - VDD 2.7 V .. 5.5 V
// PIN 3 - SIGNAL 1 Hz .. 800 KHz
```
## Interface
- **TSL235R(float voltage = 5.0)** constructor, optionally one can give the operational voltage
to add a small correction (< 1.5%)
- **float irradiance(uint32_t Hz)** returns the irradiance in uW/cm2.
NOte that Hz implies the measured pulses for 1 second.
- **float irradiance(uint32_t pulses, uint32_t milliseconds)** returns the irradiance in uW/cm2
This formula is used for other duration than 1 second.
To get irradiance in W/m2 one must divide by 100.
- **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 correction factor. At the default wavelength of 635 nm the wavelength correction factor == 1.0
- **uint16_t getWavelength()** returns the set wavelength. Convenience function.
- **float getWaveLengthFactor()** returns the wavelength correction factor.
As the sensor is most sensitive around 750 nm this value helps to normalize the signal.
This works only for (almost) monochomatic light.
- **void setVoltage(float voltage)** sets the voltage so the formulas can use a correction factor.
This voltage correction factor is rather small < 1.5%
- **float getVoltage()** returns the set voltage. Convenience function.
## Operations
See examples for typical usage.
## Future
- investigate correction factor for white light and mixed light sources.

View File

@ -0,0 +1,92 @@
//
// FILE: TSL235R.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: library fot the TSL235R light to frequency convertor
//
// HISTORY:
// 0.1.0 2020-05-29 initial version
#include "TSL235R.h"
TSL235R::TSL235R(float voltage)
{
_voltage = voltage;
calculateFactor();
}
float TSL235R::irradiance(uint32_t Hz)
{
return Hz * _factor;
}
float TSL235R::irradiance(uint32_t pulses, uint32_t milliseconds)
{
return (pulses * 1000.0 * _factor) / milliseconds;
}
void TSL235R::setWavelength(uint16_t wavelength)
{
_waveLength = wavelength;
calculateFactor();
}
void TSL235R::setVoltage(float voltage)
{
_voltage = voltage;
calculateFactor();
}
void TSL235R::calculateFactor()
{
// figure 1 datasheet
// 1 Khz crosses the line at 35/230 between 1 and 10.
// so the correctiion factor is 10^0.15217 = 1.419659 = 1.42
// as the graph is in kHz we need to correct a factor 1000
// as the irradiance function gets Hz
const float cf = 0.00142;
_waveLengthFactor = calcWLF(_waveLength);
_voltageFactor = 0.988 + (_voltage - 2.7) * (0.015 / 2.8);
_factor = cf * _waveLengthFactor * _voltageFactor;
}
float TSL235R::calcWLF(uint16_t _waveLength)
{
// figure 2 datasheet
// 635 nm is reference 1.000
// remaining is linear interpolated between points in the graph
float in[] = { 300, 350, 400, 500, 600, 635, 700, 750, 800, 850, 900, 1000, 1100};
float out[] = { 0.1, 0.35, 0.5, 0.75, 0.93, 1.00, 1.15, 1.20, 1.15, 1.10, 0.95, 0.40, 0.10};
return 1.0 / multiMap(_waveLength, in, out, 13);
}
float TSL235R::multiMap(float val, float * _in, float * _out, uint8_t size)
{
// take care the value is within range
// val = constrain(val, _in[0], _in[size-1]);
if (val <= _in[0]) return _out[0];
if (val >= _in[size-1]) return _out[size-1];
// search right interval
uint8_t pos = 1; // _in[0] allready tested
while(val > _in[pos]) pos++;
// this will handle all exact "points" in the _in array
if (val == _in[pos]) return _out[pos];
// interpolate in the right segment for the rest
return (val - _in[pos-1]) * (_out[pos] - _out[pos-1]) / (_in[pos] - _in[pos-1]) + _out[pos-1];
}
// -- END OF FILE --

View File

@ -0,0 +1,45 @@
#pragma once
//
// FILE: TSL235R.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: library fot the TSL235R light to frequency convertor
#define TSL235R_LIB_VERSION (F("0.1.0"))
#include "Arduino.h"
class TSL235R
{
public:
TSL235R(float voltage = 5.0);
float irradiance(uint32_t Hz);
float irradiance(uint32_t pulses, uint32_t milliseconds);
float getFactor() { return _factor; };
void setWavelength(uint16_t wavelength = 635);
uint16_t getWavelength() { return _waveLength; }
float getWaveLengthFactor() { return _waveLengthFactor; }
void setVoltage(float voltage = 5.0);
float getVoltage() { return _voltage; };
float getVoltageFactor() { return _voltageFactor; };
private:
uint16_t _waveLength = 635;
float _waveLengthFactor = 1.0;
float _voltage = 5.0;
float _voltageFactor = 1.0;
float _factor = 1.2;
void calculateFactor();
float calcWLF(uint16_t _waveLength);
float multiMap(float val, float * _in, float * _out, uint8_t size);
};
// -- END OF FILE --

View File

@ -0,0 +1,69 @@
//
// FILE: TSL235R_demo.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: demo
// DATE: 2021-05-29
//
// Digital Pin layout ARDUINO
// =============================
// 2 IRQ 0 - to TSL235R
//
// PIN 1 - GND
// PIN 2 - VDD - 5V
// PIN 3 - SIGNAL
//
#include "TSL235R.h"
TSL235R mySensor;
volatile uint32_t cnt = 0;
uint32_t oldcnt = 0;
uint32_t t = 0;
uint32_t lastMeasurement = 0;
void count_irq()
{
cnt++;
}
///////////////////////////////////////////////////////////////////
//
// SETUP
//
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
pinMode(2, INPUT);
digitalWrite(2, HIGH);
attachInterrupt(0, count_irq, RISING);
mySensor.setWavelength(450);
}
void loop()
{
uint32_t now = millis();
if (now - lastMeasurement >= 1000)
{
lastMeasurement = now;
t = cnt;
uint32_t Hz = t - oldcnt;
oldcnt = t;
Serial.print("irradiance:\t");
Serial.print(mySensor.irradiance(Hz)); // assumption 1 second
Serial.println(" uW/cm2");
}
}
// -- END OF FILE --

View File

@ -0,0 +1,91 @@
//
// FILE: TSL235R_multi.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: demo
// DATE: 2021-05-29
// Note the max number of interrupt an Arduino UNO can handle
// is in the order of 20000.
//
// Digital Pin layout ARDUINO
// =============================
// 2 IRQ 0 - to TSL235R
// 3 IRQ 1 - to TSL235R
//
// PIN 1 - GND
// PIN 2 - VDD - 5V
// PIN 3 - SIGNAL
//
#include "TSL235R.h"
TSL235R mySensor_450;
TSL235R mySensor_650;
volatile uint32_t cnt1 = 0;
volatile uint32_t cnt2 = 0;
uint32_t oldcnt1 = 0;
uint32_t oldcnt2 = 0;
uint32_t t = 0;
uint32_t lastMeasurement = 0;
void count_irq1()
{
cnt1++;
}
void count_irq2()
{
cnt2++;
}
///////////////////////////////////////////////////////////////////
//
// SETUP
//
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
pinMode(2, INPUT);
digitalWrite(2, HIGH);
attachInterrupt(0, count_irq1, RISING);
attachInterrupt(1, count_irq2, RISING);
mySensor_450.setWavelength(450);
mySensor_650.setWavelength(650);
}
void loop()
{
uint32_t now = millis();
if (now - lastMeasurement >= 1000)
{
lastMeasurement = now;
t = cnt1;
uint32_t Hz = t - oldcnt1;
oldcnt1 = t;
Serial.print("irradiance 450 nm:\t");
Serial.print(mySensor_450.irradiance(Hz)); // assumption 1 second
Serial.println(" uW/cm2");
t = cnt2;
Hz = t - oldcnt2;
oldcnt2 = t;
Serial.print("irradiance 650 nm:\t");
Serial.print(mySensor_650.irradiance(Hz)); // assumption 1 second
Serial.println(" uW/cm2");
}
}
// -- END OF FILE --

View File

@ -0,0 +1,96 @@
//
// FILE: TSL235R_multi_alternate.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: demo
// DATE: 2021-05-29
// Note the max number of interrupt an Arduino UNO can handle
// is in the order of 20000.
// in the demo we alternate the two interrupt pins to be able
// to have a larger range per sensor.
//
// Digital Pin layout ARDUINO
// =============================
// 2 IRQ 0 - to TSL235R
// 3 IRQ 1 - to TSL235R
//
// PIN 1 - GND
// PIN 2 - VDD - 5V
// PIN 3 - SIGNAL
//
#include "TSL235R.h"
TSL235R mySensor_450;
TSL235R mySensor_650;
volatile uint32_t pulses = 0;
uint32_t t = 0;
uint32_t lastMeasurement = 0;
uint8_t irq_select = 0;
void count_irq()
{
pulses++;
}
///////////////////////////////////////////////////////////////////
//
// SETUP
//
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
pinMode(2, INPUT);
digitalWrite(2, HIGH);
mySensor_450.setWavelength(450);
mySensor_650.setWavelength(650);
irq_select = 0;
attachInterrupt(0, count_irq, RISING);
lastMeasurement = millis();
}
void loop()
{
if (millis() - lastMeasurement >= 1000)
{
if (irq_select == 0)
{
detachInterrupt(irq_select); // 0
Serial.print("irradiance (0):\t");
Serial.print(mySensor_450.irradiance(pulses)); // assumption 1 second
Serial.println(" uW/cm2");
pulses = 0;
irq_select = 1;
attachInterrupt(irq_select, count_irq, RISING);
}
else
{
detachInterrupt(irq_select); // 1
Serial.print("irradiance (1):\t");
Serial.print(mySensor_650.irradiance(pulses)); // assumption 1 second
Serial.println(" uW/cm2");
pulses = 0;
irq_select = 0;
attachInterrupt(irq_select, count_irq, RISING);
}
lastMeasurement = millis();
}
// ...
}
// -- END OF FILE --

View File

@ -0,0 +1,74 @@
//
// FILE: TSL235R_pulses.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: demo
// DATE: 2021-05-29
//
// Digital Pin layout ARDUINO
// =============================
// 2 IRQ 0 - to TSL235R
// 3 IRQ 1 - to TSL235R
//
// PIN 1 - GND
// PIN 2 - VDD - 5V
// PIN 3 - SIGNAL
//
#include "TSL235R.h"
TSL235R mySensor;
volatile uint32_t cnt1 = 0;
uint32_t oldcnt1 = 0;
uint32_t t = 0;
uint32_t lastMeasurement = 0;
void count_irq1()
{
cnt1++;
}
///////////////////////////////////////////////////////////////////
//
// SETUP
//
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
pinMode(2, INPUT);
digitalWrite(2, HIGH);
attachInterrupt(0, count_irq1, RISING);
mySensor.setWavelength(450);
}
void loop()
{
uint32_t now = millis();
if (now - lastMeasurement >= 1000)
{
t = cnt1;
uint32_t Hz = t - oldcnt1;
oldcnt1 = t;
Serial.print("irradiance(Hz):\t\t");
Serial.print(mySensor.irradiance(Hz)); // assumption 1 second
Serial.println(" uW/cm2");
Serial.print("irradiance(puls, time):\t");
Serial.print(mySensor.irradiance(Hz, now - lastMeasurement)); // accurate time
Serial.println(" uW/cm2");
lastMeasurement = now;
}
}
// -- END OF FILE --

View File

@ -0,0 +1,22 @@
{
"name": "TSL235R",
"keywords": "TSL235R, light, frequency, wavelength",
"description": "Library for the TSL235R light to frequency convertor.",
"authors":
[
{
"name": "Rob Tillaart",
"email": "Rob.Tillaart@gmail.com",
"maintainer": true
}
],
"repository":
{
"type": "git",
"url": "https://github.com/RobTillaart/TSL235R.git"
},
"version": "0.1.0",
"license": "MIT",
"frameworks": "*",
"platforms": "*"
}

View File

@ -0,0 +1,11 @@
name=TSL235R
version=0.1.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Library for the TSL235R light to frequency convertor.
paragraph=include wavelength compensation
category=Sensors
url=https://github.com/RobTillaart/TSL235R
architectures=*
includes=TSL235R.h
depends=

View File

@ -0,0 +1,157 @@
//
// FILE: unit_test_001.cpp
// AUTHOR: Rob Tillaart
// DATE: 2021-05-29
// PURPOSE: unit tests for the temperature library
// https://github.com/RobTillaart/TSL235R
// https://github.com/Arduino-CI/arduino_ci/blob/master/REFERENCE.md
//
// supported assertions
// ----------------------------
// assertEqual(expected, actual); // a == b
// assertNotEqual(unwanted, actual); // a != b
// assertComparativeEquivalent(expected, actual); // abs(a - b) == 0 or (!(a > b) && !(a < b))
// assertComparativeNotEquivalent(unwanted, actual); // abs(a - b) > 0 or ((a > b) || (a < b))
// assertLess(upperBound, actual); // a < b
// assertMore(lowerBound, actual); // a > b
// assertLessOrEqual(upperBound, actual); // a <= b
// assertMoreOrEqual(lowerBound, actual); // a >= b
// assertTrue(actual);
// assertFalse(actual);
// assertNull(actual);
// // special cases for floats
// assertEqualFloat(expected, actual, epsilon); // fabs(a - b) <= epsilon
// assertNotEqualFloat(unwanted, actual, epsilon); // fabs(a - b) >= epsilon
// assertInfinity(actual); // isinf(a)
// assertNotInfinity(actual); // !isinf(a)
// assertNAN(arg); // isnan(a)
// assertNotNAN(arg); // !isnan(a)
#include <ArduinoUnitTests.h>
#include "Arduino.h"
#include "TSL235R.h"
unittest_setup()
{
}
unittest_teardown()
{
}
unittest(test_constructor)
{
fprintf(stderr, "VERSION: %s\n", TSL235R_LIB_VERSION);
TSL235R mysensor;
assertEqual(635, mysensor.getWavelength() );
assertEqualFloat(1.0, mysensor.getWaveLengthFactor(), 0.001);
assertEqualFloat(5.0, mysensor.getVoltage(), 0.001);
assertEqualFloat(1.0, mysensor.getVoltageFactor(), 0.001);
assertEqualFloat(0.00142, mysensor.getFactor(), 0.001);
fprintf(stderr, "%1.6f\n", mysensor.getFactor() );
}
unittest(test_wavelength)
{
TSL235R mysensor;
assertEqual(635, mysensor.getWavelength() );
assertEqualFloat(1.0, mysensor.getWaveLengthFactor(), 0.001);
fprintf(stderr,"\n");
for (int wl = 300; wl < 1150; wl += 50)
{
mysensor.setWavelength(wl);
assertEqual(wl, mysensor.getWavelength() );
}
fprintf(stderr, "\n\tWavelen\tfactor\n");
for (int wl = 300; wl < 1150; wl += 50)
{
mysensor.setWavelength(wl);
fprintf(stderr, "\t%d\t %1.3f\n", wl, mysensor.getWaveLengthFactor() );
}
fprintf(stderr, "\n");
mysensor.setWavelength();
assertEqual(635, mysensor.getWavelength() );
}
unittest(test_voltage)
{
TSL235R mysensor(2.7);
assertEqualFloat(2.7, mysensor.getVoltage(), 0.001);
assertEqualFloat(0.988, mysensor.getVoltageFactor(), 0.001);
fprintf(stderr,"\n");
for (float volts = 2.7; volts < 5.5; volts += 0.1)
{
mysensor.setVoltage(volts);
assertEqualFloat(volts, mysensor.getVoltage(), 0.001);
}
fprintf(stderr, "\n\tVolts\tfactor\n");
for (float volts = 2.7; volts < 5.5; volts += 0.1)
{
mysensor.setVoltage(volts);
fprintf(stderr, "\t%1.1f\t %1.3f\n", volts, mysensor.getVoltageFactor() );
}
fprintf(stderr, "\n");
mysensor.setVoltage();
assertEqualFloat(5.0, mysensor.getVoltage(), 0.001);
}
unittest(test_conversion1)
{
TSL235R mysensor;
assertEqualFloat(1.0, mysensor.getVoltageFactor(), 0.001);
assertEqualFloat(1.0, mysensor.getWaveLengthFactor(), 0.001);
fprintf(stderr, "%1.6f\n", mysensor.getFactor() );
fprintf(stderr,"\n");
for (uint32_t Hz = 10; Hz < 1000000; Hz *= 2)
{
float rad = mysensor.irradiance(Hz);
assertEqualFloat(0.00142 * Hz, mysensor.irradiance(Hz), 0.001 * Hz); // we must have a relative error here!
}
}
unittest(test_conversion2)
{
TSL235R mysensor;
assertEqualFloat(1.0, mysensor.getVoltageFactor(), 0.001);
assertEqualFloat(1.0, mysensor.getWaveLengthFactor(), 0.001);
fprintf(stderr,"\n");
for (uint32_t Hz = 10; Hz < 1000000; Hz *= 2)
{
float rad1 = mysensor.irradiance(Hz);
float rad2 = mysensor.irradiance(Hz * 10, 10000); // 10 seconds 10 times as many pulses
assertEqualFloat(rad1, rad2, 0.001);
}
fprintf(stderr, "\ndone...");
}
unittest_main()
// --------