Skip to content

Commit 1fd79bf

Browse files
committed
add TSL235R
1 parent f27f754 commit 1fd79bf

File tree

15 files changed

+793
-0
lines changed

15 files changed

+793
-0
lines changed

libraries/TSL235R/.arduino-ci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
compile:
2+
# Choosing to run compilation tests on 2 different Arduino platforms
3+
platforms:
4+
- uno
5+
- leonardo
6+
- due
7+
- zero
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
name: Arduino-lint
3+
4+
on: [push, pull_request]
5+
jobs:
6+
lint:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v2
10+
- uses: arduino/arduino-lint-action@v1
11+
with:
12+
library-manager: update
13+
compliance: strict
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
name: Arduino CI
3+
4+
on: [push, pull_request]
5+
6+
jobs:
7+
arduino_ci:
8+
runs-on: ubuntu-latest
9+
10+
steps:
11+
- uses: actions/checkout@v2
12+
- uses: Arduino-CI/action@master
13+
# Arduino-CI/[email protected]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: JSON check
2+
3+
on:
4+
push:
5+
paths:
6+
- '**.json'
7+
pull_request:
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v2
14+
- name: json-syntax-check
15+
uses: limitusus/json-syntax-check@v1
16+
with:
17+
pattern: "\\.json$"
18+

libraries/TSL235R/LICENSE

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

libraries/TSL235R/README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
[![Arduino CI](https://github.com/RobTillaart/TSL235R/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
3+
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/TSL235R/blob/master/LICENSE)
4+
[![GitHub release](https://img.shields.io/github/release/RobTillaart/TSL235R.svg?maxAge=3600)](https://github.com/RobTillaart/TSL235R/releases)
5+
6+
7+
# TSL235R
8+
9+
Arduino library for the TSL235R light to frequency convertor.
10+
11+
12+
## Description
13+
14+
This library does not measure the frequency but has some functions to compensate e.g.
15+
for wavelength and voltage used.
16+
17+
The library is not tested extensively yet.
18+
19+
The sensor operating voltage is between 2.7 and 5.5 max.
20+
21+
For measurements below 1uW/cm2 one bests measures for multiple seconds
22+
Above 1 uW/cm2 1 second or shorter is OK.
23+
24+
Note that for longer and shorter measurements than 1 second one must convert the
25+
value to Hz, which is the nr of pulses in 1 second.
26+
27+
28+
## Connection
29+
30+
```
31+
// PIN 1 - GND
32+
// PIN 2 - VDD 2.7 V .. 5.5 V
33+
// PIN 3 - SIGNAL 1 Hz .. 800 KHz
34+
```
35+
36+
37+
## Interface
38+
39+
- **TSL235R(float voltage = 5.0)** constructor, optionally one can give the operational voltage
40+
to add a small correction (< 1.5%)
41+
- **float irradiance(uint32_t Hz)** returns the irradiance in uW/cm2.
42+
NOte that Hz implies the measured pulses for 1 second.
43+
- **float irradiance(uint32_t pulses, uint32_t milliseconds)** returns the irradiance in uW/cm2
44+
This formula is used for other duration than 1 second.
45+
To get irradiance in W/m2 one must divide by 100.
46+
- **float getFactor()** returns the inner conversion factor from Hz to Watt/cm2.
47+
- **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
48+
- **uint16_t getWavelength()** returns the set wavelength. Convenience function.
49+
- **float getWaveLengthFactor()** returns the wavelength correction factor.
50+
As the sensor is most sensitive around 750 nm this value helps to normalize the signal.
51+
This works only for (almost) monochomatic light.
52+
- **void setVoltage(float voltage)** sets the voltage so the formulas can use a correction factor.
53+
This voltage correction factor is rather small < 1.5%
54+
- **float getVoltage()** returns the set voltage. Convenience function.
55+
56+
57+
## Operations
58+
59+
See examples for typical usage.
60+
61+
62+
## Future
63+
64+
- investigate correction factor for white light and mixed light sources.

libraries/TSL235R/TSL235R .cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
//
2+
// FILE: TSL235R.cpp
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.0
5+
// PURPOSE: library fot the TSL235R light to frequency convertor
6+
//
7+
// HISTORY:
8+
// 0.1.0 2020-05-29 initial version
9+
10+
11+
#include "TSL235R.h"
12+
13+
14+
TSL235R::TSL235R(float voltage)
15+
{
16+
_voltage = voltage;
17+
calculateFactor();
18+
}
19+
20+
21+
float TSL235R::irradiance(uint32_t Hz)
22+
{
23+
return Hz * _factor;
24+
}
25+
26+
27+
float TSL235R::irradiance(uint32_t pulses, uint32_t milliseconds)
28+
{
29+
return (pulses * 1000.0 * _factor) / milliseconds;
30+
}
31+
32+
33+
void TSL235R::setWavelength(uint16_t wavelength)
34+
{
35+
_waveLength = wavelength;
36+
calculateFactor();
37+
}
38+
39+
40+
void TSL235R::setVoltage(float voltage)
41+
{
42+
_voltage = voltage;
43+
calculateFactor();
44+
}
45+
46+
void TSL235R::calculateFactor()
47+
{
48+
// figure 1 datasheet
49+
// 1 Khz crosses the line at 35/230 between 1 and 10.
50+
// so the correctiion factor is 10^0.15217 = 1.419659 = 1.42
51+
// as the graph is in kHz we need to correct a factor 1000
52+
// as the irradiance function gets Hz
53+
const float cf = 0.00142;
54+
_waveLengthFactor = calcWLF(_waveLength);
55+
56+
_voltageFactor = 0.988 + (_voltage - 2.7) * (0.015 / 2.8);
57+
_factor = cf * _waveLengthFactor * _voltageFactor;
58+
}
59+
60+
61+
float TSL235R::calcWLF(uint16_t _waveLength)
62+
{
63+
// figure 2 datasheet
64+
// 635 nm is reference 1.000
65+
// remaining is linear interpolated between points in the graph
66+
float in[] = { 300, 350, 400, 500, 600, 635, 700, 750, 800, 850, 900, 1000, 1100};
67+
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};
68+
return 1.0 / multiMap(_waveLength, in, out, 13);
69+
}
70+
71+
72+
float TSL235R::multiMap(float val, float * _in, float * _out, uint8_t size)
73+
{
74+
// take care the value is within range
75+
// val = constrain(val, _in[0], _in[size-1]);
76+
if (val <= _in[0]) return _out[0];
77+
if (val >= _in[size-1]) return _out[size-1];
78+
79+
// search right interval
80+
uint8_t pos = 1; // _in[0] allready tested
81+
while(val > _in[pos]) pos++;
82+
83+
// this will handle all exact "points" in the _in array
84+
if (val == _in[pos]) return _out[pos];
85+
86+
// interpolate in the right segment for the rest
87+
return (val - _in[pos-1]) * (_out[pos] - _out[pos-1]) / (_in[pos] - _in[pos-1]) + _out[pos-1];
88+
}
89+
90+
91+
92+
// -- END OF FILE --

libraries/TSL235R/TSL235R.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#pragma once
2+
//
3+
// FILE: TSL235R.h
4+
// AUTHOR: Rob Tillaart
5+
// VERSION: 0.1.0
6+
// PURPOSE: library fot the TSL235R light to frequency convertor
7+
8+
9+
#define TSL235R_LIB_VERSION (F("0.1.0"))
10+
11+
#include "Arduino.h"
12+
13+
14+
class TSL235R
15+
{
16+
public:
17+
18+
TSL235R(float voltage = 5.0);
19+
20+
float irradiance(uint32_t Hz);
21+
float irradiance(uint32_t pulses, uint32_t milliseconds);
22+
float getFactor() { return _factor; };
23+
24+
void setWavelength(uint16_t wavelength = 635);
25+
uint16_t getWavelength() { return _waveLength; }
26+
float getWaveLengthFactor() { return _waveLengthFactor; }
27+
28+
void setVoltage(float voltage = 5.0);
29+
float getVoltage() { return _voltage; };
30+
float getVoltageFactor() { return _voltageFactor; };
31+
32+
private:
33+
uint16_t _waveLength = 635;
34+
float _waveLengthFactor = 1.0;
35+
float _voltage = 5.0;
36+
float _voltageFactor = 1.0;
37+
float _factor = 1.2;
38+
39+
void calculateFactor();
40+
float calcWLF(uint16_t _waveLength);
41+
float multiMap(float val, float * _in, float * _out, uint8_t size);
42+
};
43+
44+
45+
// -- END OF FILE --
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//
2+
// FILE: TSL235R_demo.ino
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.0
5+
// PURPOSE: demo
6+
// DATE: 2021-05-29
7+
8+
//
9+
// Digital Pin layout ARDUINO
10+
// =============================
11+
// 2 IRQ 0 - to TSL235R
12+
//
13+
// PIN 1 - GND
14+
// PIN 2 - VDD - 5V
15+
// PIN 3 - SIGNAL
16+
//
17+
18+
19+
#include "TSL235R.h"
20+
21+
22+
TSL235R mySensor;
23+
24+
volatile uint32_t cnt = 0;
25+
uint32_t oldcnt = 0;
26+
uint32_t t = 0;
27+
uint32_t lastMeasurement = 0;
28+
29+
30+
void count_irq()
31+
{
32+
cnt++;
33+
}
34+
35+
36+
///////////////////////////////////////////////////////////////////
37+
//
38+
// SETUP
39+
//
40+
void setup()
41+
{
42+
Serial.begin(115200);
43+
Serial.println(__FILE__);
44+
45+
pinMode(2, INPUT);
46+
digitalWrite(2, HIGH);
47+
attachInterrupt(0, count_irq, RISING);
48+
49+
mySensor.setWavelength(450);
50+
}
51+
52+
53+
void loop()
54+
{
55+
uint32_t now = millis();
56+
if (now - lastMeasurement >= 1000)
57+
{
58+
lastMeasurement = now;
59+
t = cnt;
60+
uint32_t Hz = t - oldcnt;
61+
oldcnt = t;
62+
63+
Serial.print("irradiance:\t");
64+
Serial.print(mySensor.irradiance(Hz)); // assumption 1 second
65+
Serial.println(" uW/cm2");
66+
}
67+
}
68+
69+
// -- END OF FILE --

0 commit comments

Comments
 (0)