0.1.1 LTR390_RT

This commit is contained in:
Rob Tillaart 2024-05-09 20:44:14 +02:00
parent cf8200a3e8
commit f08c33cf1a
16 changed files with 982 additions and 0 deletions

View File

@ -0,0 +1,30 @@
platforms:
rpipico:
board: rp2040:rp2040:rpipico
package: rp2040:rp2040
gcc:
features:
defines:
- ARDUINO_ARCH_RP2040
warnings:
flags:
packages:
rp2040:rp2040:
url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
- uno
# - due
# - zero
# - leonardo
- m4
- esp32
- esp8266
# - mega2560
- rpipico
libraries:
- "printHelpers"

View File

@ -0,0 +1,4 @@
# These are supported funding model platforms
github: RobTillaart
custom: "https://www.paypal.me/robtillaart"

View File

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

View File

@ -0,0 +1,17 @@
name: Arduino CI
on: [push, pull_request]
jobs:
runTest:
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: 2.6
- run: |
gem install arduino_ci
arduino_ci.rb

View File

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

View File

@ -0,0 +1,16 @@
# Change Log LTR390
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.1.1] - 2024-05-09
- fix URL's
## [0.1.0] - 2024-04-29
- initial version

View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2024-2024 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,343 @@
#pragma once
//
// FILE: LTR390.h
// AUTHOR: Rob Tillaart
// DATE: 2024-04-29
// VERSION: 0.1.1
// PURPOSE: Arduino library for the I2C LTR390 UV sensor.
// URL: https://github.com/RobTillaart/LTR390_RT
#include "Arduino.h"
#include "Wire.h"
#define LTR390_LIB_VERSION (F("0.1.1"))
// LTR390 ERROR CODES
// TODO
// LTR390 REGISTERS (page 13 datasheet)
#define LTR390_MAIN_CTRL 0x00
#define LTR390_ALS_UVS_MEAS_RATE 0x04
#define LTR390_ALS_UVS_GAIN 0x05
#define LTR390_PART_ID 0x06
#define LTR390_MAIN_STATUS 0x07
#define LTR390_ALS_DATA_0 0x0D
#define LTR390_ALS_DATA_1 0x0E
#define LTR390_ALS_DATA_2 0x0F
#define LTR390_UVS_DATA_0 0x10
#define LTR390_UVS_DATA_1 0x11
#define LTR390_UVS_DATA_2 0x12
#define LTR390_INT_CFG 0x19
#define LTR390_INT_PST 0x1A
#define LTR390_ALS_UVS_THRES_UP_0 0x21
#define LTR390_ALS_UVS_THRES_UP_1 0x22
#define LTR390_ALS_UVS_THRES_UP_2 0x23
#define LTR390_ALS_UVS_THRES_LOW_0 0x24
#define LTR390_ALS_UVS_THRES_LOW_1 0x25
#define LTR390_ALS_UVS_THRES_LOW_2 0x26
class LTR390
{
public:
LTR390(TwoWire *wire = &Wire)
{
_address = 0x53; // LTR390 device itself
_wire = wire;
}
bool begin()
{
return isConnected();
}
bool isConnected()
{
_wire->beginTransmission(_address);
return (_wire->endTransmission() == 0);
}
uint8_t getAddress()
{
return _address;
}
//////////////////////////////////////////////
//
// MAIN CONTROL
//
void setALSMode()
{
writeRegister(LTR390_MAIN_CTRL, 0x02);
}
void setUVSMode()
{
writeRegister(LTR390_MAIN_CTRL, 0x0A);
}
uint8_t reset()
{
writeRegister(LTR390_MAIN_CTRL, 0x10);
delay(100);
return readRegister(LTR390_MAIN_CTRL);
}
void enable()
{
uint8_t reg = readRegister(LTR390_MAIN_CTRL);
reg |= 0x02;
writeRegister(LTR390_MAIN_CTRL, reg);
}
void disable()
{
uint8_t reg = readRegister(LTR390_MAIN_CTRL);
reg &= ~0x02;
writeRegister(LTR390_MAIN_CTRL, reg);
}
bool isEnabled()
{
uint8_t reg = readRegister(LTR390_MAIN_CTRL);
return (reg & 0x02) > 0;
}
//////////////////////////////////////////////
//
// MEASUREMENT CONFIGURATION
//
void setResolution(uint8_t resolution) // res = 0..5
{
if (resolution > 5) resolution = 5;
uint8_t reg = readRegister(LTR390_ALS_UVS_MEAS_RATE);
reg &= 0x07;
reg |= (resolution << 4);
writeRegister(LTR390_ALS_UVS_MEAS_RATE, reg);
}
uint8_t getResolution()
{
uint8_t reg = readRegister(LTR390_ALS_UVS_MEAS_RATE);
return (reg >> 4) & 0x07;
}
void setRate(uint8_t rate) // rate = 0..7
{
uint8_t reg = readRegister(LTR390_ALS_UVS_MEAS_RATE);
reg &= 0xF8;
reg |= rate;
writeRegister(LTR390_ALS_UVS_MEAS_RATE, reg);
}
uint8_t getRate()
{
uint8_t reg = readRegister(LTR390_ALS_UVS_MEAS_RATE);
return reg & 0x07;
}
void setGain(uint8_t gain) // gain = 0..4
{
if (gain > 4) gain = 4;
uint8_t reg = readRegister(LTR390_ALS_UVS_GAIN);
reg &= 0xF8;
reg |= gain;
writeRegister(LTR390_ALS_UVS_GAIN, reg);
}
uint8_t getGain()
{
uint8_t reg = readRegister(LTR390_ALS_UVS_GAIN);
return reg & 0x07;
}
//////////////////////////////////////////////
//
// PART_ID
//
uint8_t getPartID()
{
uint8_t reg = readRegister(LTR390_PART_ID);
return reg >> 4;
}
uint8_t getRevisionID()
{
uint8_t reg = readRegister(LTR390_PART_ID);
return reg & 0x0F;
}
//////////////////////////////////////////////
//
// MAIN STATUS
// TODO elaborate - need split? or masks?
//
uint8_t getStatus()
{
uint8_t reg = readRegister(LTR390_MAIN_STATUS);
return reg & 0x38;
}
//////////////////////////////////////////////
//
// GET DATA
//
uint32_t getALSData()
{
uint32_t value = readRegister(LTR390_ALS_DATA_2) & 0x0F;
value <<= 8;
value += readRegister(LTR390_ALS_DATA_1);
value <<= 8;
value += readRegister(LTR390_ALS_DATA_0);
return value;
}
uint32_t getUVSData()
{
uint32_t value = readRegister(LTR390_UVS_DATA_2) & 0x0F;
value <<= 8;
value += readRegister(LTR390_UVS_DATA_1);
value <<= 8;
value += readRegister(LTR390_UVS_DATA_0);
return value;
}
//////////////////////////////////////////////
//
// INTERRUPT
// TODO elaborate
//
int setInterruptConfig(uint8_t value)
{
return writeRegister(LTR390_INT_CFG, value);
}
uint8_t getInterruptConfig()
{
return readRegister(LTR390_INT_CFG);
}
int setInterruptPersist(uint8_t value)
{
return writeRegister(LTR390_INT_PST, value);
}
uint8_t getInterruptPersist()
{
return readRegister(LTR390_INT_PST);
}
//////////////////////////////////////////////
//
// THRESHOLD
// TODO elaborate
//
void setHighThreshold(uint32_t value)
{
writeRegister(LTR390_ALS_UVS_THRES_UP_0, value & 0xFF);
value >>= 8;
writeRegister(LTR390_ALS_UVS_THRES_UP_1, value & 0xFF);
value >>= 8;
writeRegister(LTR390_ALS_UVS_THRES_UP_2, value & 0x0F);
}
uint32_t getHighThreshold()
{
uint32_t value = readRegister(LTR390_ALS_UVS_THRES_UP_2) & 0x0F;
value <<= 8;
value += readRegister(LTR390_ALS_UVS_THRES_UP_1);
value <<= 8;
value += readRegister(LTR390_ALS_UVS_THRES_UP_0);
return value;
}
void setLowThreshold(uint32_t value)
{
writeRegister(LTR390_ALS_UVS_THRES_LOW_0, value & 0xFF);
value >>= 8;
writeRegister(LTR390_ALS_UVS_THRES_LOW_1, value & 0xFF);
value >>= 8;
writeRegister(LTR390_ALS_UVS_THRES_LOW_2, value & 0x0F);
}
uint32_t getLowThreshold()
{
uint32_t value = readRegister(LTR390_ALS_UVS_THRES_LOW_2) & 0x0F;
value <<= 8;
value += readRegister(LTR390_ALS_UVS_THRES_LOW_1);
value <<= 8;
value += readRegister(LTR390_ALS_UVS_THRES_LOW_0);
return value;
}
//////////////////////////////////////////////
//
// PRIVATE
//
int writeRegister(uint8_t reg, uint8_t value)
{
_wire->beginTransmission(_address);
_wire->write(reg);
_wire->write(value);
int n = _wire->endTransmission();
//if (n != 0)
//{
// Serial.print("write:\t");
// Serial.println(n);
//}
return n;
}
uint8_t readRegister(uint8_t reg)
{
_wire->beginTransmission(_address);
_wire->write(reg);
int n = _wire->endTransmission();
//if (n != 0)
//{
// Serial.print("read:\t");
// Serial.println(n);
// return n;
//}
n = _wire->requestFrom(_address, (uint8_t)1);
if (n != 1)
{
// Serial.print("requestFrom: \t");
// Serial.print(n);
// return n;
}
return _wire->read();
}
private:
TwoWire * _wire;
uint8_t _address;
};
// -- END OF FILE --

View File

@ -0,0 +1,169 @@
[![Arduino CI](https://github.com/RobTillaart/LTR390_RT/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![Arduino-lint](https://github.com/RobTillaart/LTR390_RT/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/LTR390_RT/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/LTR390_RT/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/LTR390_RT/actions/workflows/jsoncheck.yml)
[![GitHub issues](https://img.shields.io/github/issues/RobTillaart/LTR390_RT.svg)](https://github.com/RobTillaart/LTR390_RT/issues)
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/LTR390_RT/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/LTR390_RT.svg?maxAge=3600)](https://github.com/RobTillaart/LTR390_RT/releases)
[![PlatformIO Registry](https://badges.registry.platformio.org/packages/robtillaart/library/LTR390.svg)](https://registry.platformio.org/libraries/robtillaart/LTR390)
# LTR390
Arduino library for the I2C LTR390 UV sensor.
## Description
**Experimental**
This library is to read the LTR390 UV sensor directly.
It was written after I ordered a LTR390 sensor (breakout) from DFRobotics.
However that one had its own processor and its own interface.
So the result was two libraries for the same product.
**Warning:** Operating voltage range: **1.7V .. 3.6V**
So it will not work on 5V devices
## I2C
The device has a fixed address of 0x53 == 83 decimal according to datasheet.
#### I2C Speed
The device should work on 100 kHz and 400 kHz I2C bus.
To be tested.
#### I2C Multiplexing
Sometimes you need to control more devices than possible with the default
address range the device provides.
This is possible with an I2C multiplexer e.g. TCA9548 which creates up
to eight channels (think of it as I2C subnets) which can use the complete
address range of the device.
Drawback of using a multiplexer is that it takes more administration in
your code e.g. which device is on which channel.
This will slow down the access, which must be taken into account when
deciding which devices are on which channel.
Also note that switching between channels will slow down other devices
too if they are behind the multiplexer.
- https://github.com/RobTillaart/TCA9548
#### Related
- https://github.com/RobTillaart/LTR390_RT (native LTR390)
- https://github.com/RobTillaart/LTR390_DFR (DF Robotics variant)
## Interface
```cpp
#include "LTR390.h"
```
#### Constructor
- **LTR390(TwoWire \* wire = &Wire)** Constructor
with optional Wire interface.
- **bool begin()** returns true if device 0x53 can be seen on the I2C bus.
- **bool isConnected()** returns true if device 0x53 can be seen on I2C bus.
- **uint8_t getAddress()** returns 0x53, fixed address, for convenience.
#### Main control
- **void setALSMode()**
- **void setUVSMode()**
- **void reset()** blocks for 100 ms.
- **void enable()** idem.
- **void disable()** idem.
- **bool isEnabled()** returns current status.
#### Measurement configuration
- **void setResolution(uint8_t resolution)** resolution = 0..5
- **uint8_t getResolution()**
- **void setRate(uint8_t rate)** rate = 0..7
- **uint8_t getRate()**
- **void setGain(uint8_t gain)** gain = 0..4
- **uint8_t getGain()**
#### Part and revision ID
- **uint8_t getPartID()** returns 11.
- **uint8_t getRevisionID()** returns 2.
#### Main status
- **uint8_t getStatus()** need split? or masks?
#### Get data
- **uint32_t getALSData()**
- **uint32_t getUVSData()**
#### Interrupt
- **int setInterruptConfig(uint8_t value)**
- **uint8_t getInterruptConfig()**
- **int setInterruptPersist(uint8_t value)**
- **uint8_t getInterruptPersist()**
#### Threshold
- **void setHighThreshold(uint32_t value)**
- **uint32_t getHighThreshold()**
- **void setLowThreshold(uint32_t value)**
- **uint32_t getLowThreshold()**
## Future
#### Must
- Elaborate and improve documentation a lot.
- add tables, ranges etc.
- test with right hardware.
#### Should
- add examples.
- test example
- performance example
- fix / elaborate TODO's in code.
- status and error codes
- interrupts and thresholds
- add **float getLUX(float wfac = 1)**
- add **float getUVI(float wfac = 1)**
#### Could
- add error handling
- unit test or test example?
#### Wont
## Support
If you appreciate my libraries, you can support the development and maintenance.
Improve the quality of the libraries by providing issues and Pull Requests, or
donate through PayPal or GitHub sponsors.
Thank you,

View File

@ -0,0 +1,61 @@
//
// FILE: LTR390_ALS_demo.ino
// AUTHOR: Rob Tillaart
// PURPOSE: test basic behaviour and performance
// URL: https://github.com/RobTillaart/LTR390
#include "LTR390.h"
LTR390 uv;
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println(__FILE__);
Serial.print("LTR390_LIB_VERSION: ");
Serial.println(LTR390_LIB_VERSION);
Serial.println();
Wire.begin();
uv.enable();
delay(1000);
if (uv.begin() == false)
{
Serial.println("Could not connect, fix and reboot");
}
else
{
Serial.println("Device found.");
}
Serial.print("RESET:\t");
Serial.println(uv.reset());
Serial.print("PARTID:\t");
Serial.println(uv.getPartID());
Serial.print("REVID:\t");
Serial.println(uv.getRevisionID());
// while (uv.isEnabled() == false)
// {
// Serial.println("enable()");
// uv.setALSMode(); //uv.enable();
// delay(1000);
// }
uv.setALSMode();
Serial.println("\ndone...");
}
void loop()
{
delay(1000);
Serial.println(uv.getALSData());
}
// -- END OF FILE --

View File

@ -0,0 +1,52 @@
//
// FILE: LTR390_UV_demo.ino
// AUTHOR: Rob Tillaart
// PURPOSE: test basic behaviour and performance
// URL: https://github.com/RobTillaart/LTR390
#include "LTR390.h"
LTR390 uv;
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println(__FILE__);
Serial.print("LTR390_LIB_VERSION: ");
Serial.println(LTR390_LIB_VERSION);
Serial.println();
Wire.begin();
if (uv.begin() == false)
{
Serial.println("Could not connect, fix and reboot");
}
else
{
Serial.println("Device found.");
}
Serial.print("PARTID:\t");
Serial.println(uv.getPartID());
Serial.print("REVID:\t");
Serial.println(uv.getRevisionID());
uv.setUVSMode();
uv.enable();
Serial.println("\ndone...");
}
void loop()
{
delay(1000);
Serial.println(uv.getUVSData());
}
// -- END OF FILE --

View File

@ -0,0 +1,106 @@
//
// FILE: LTR390_dump_registers.ino
// AUTHOR: Rob Tillaart
// PURPOSE: dump registers
// URL: https://github.com/RobTillaart/LTR390
#include "LTR390.h"
LTR390 uv;
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println(__FILE__);
Serial.print("LTR390_LIB_VERSION: ");
Serial.println(LTR390_LIB_VERSION);
Serial.println();
Wire.begin();
Wire.setClock(100000);
if (uv.begin() == false)
{
Serial.println("Could not connect, fix and reboot");
}
else
{
Serial.println("Device found.");
}
uv.reset();
// registers according to datasheet
for (uint8_t reg = 0x00; reg < 0x01; reg++)
{
Serial.print(reg, HEX);
Serial.print("\t");
Serial.print(uv.readRegister(reg), HEX);
Serial.print("\t(");
Serial.print(uv.readRegister(reg));
Serial.print(")\n");
}
Serial.println();
for (uint8_t reg = 0x04; reg < 0x08; reg++)
{
Serial.print(reg, HEX);
Serial.print("\t");
Serial.print(uv.readRegister(reg), HEX);
Serial.print("\t(");
Serial.print(uv.readRegister(reg));
Serial.print(")\n");
}
Serial.println();
for (uint8_t reg = 0x0D; reg < 0x13; reg++)
{
Serial.print(reg, HEX);
Serial.print("\t");
Serial.print(uv.readRegister(reg), HEX);
Serial.print("\t(");
Serial.print(uv.readRegister(reg));
Serial.print(")\n");
}
Serial.println();
for (uint8_t reg = 0x19; reg < 0x1B; reg++)
{
Serial.print(reg, HEX);
Serial.print("\t");
Serial.print(uv.readRegister(reg), HEX);
Serial.print("\t(");
Serial.print(uv.readRegister(reg));
Serial.print(")\n");
}
Serial.println();
for (uint8_t reg = 0x21; reg < 0x27; reg++)
{
Serial.print(reg, HEX);
Serial.print("\t");
Serial.print(uv.readRegister(reg), HEX);
Serial.print("\t(");
Serial.print(uv.readRegister(reg));
Serial.print(")\n");
}
Serial.println();
Serial.println("\ndone...");
}
void loop()
{
// lower bits of DATA
Serial.println(uv.readRegister(0x0E) * 256 + uv.readRegister(0x0D));
delay(100);
Serial.println(uv.readRegister(0x11) * 256 + uv.readRegister(0x10));
delay(100);
}
// -- END OF FILE --

View File

@ -0,0 +1,47 @@
# Syntax Colouring Map For LTR390
# Data types (KEYWORD1)
LTR390 KEYWORD1
# Methods and Functions (KEYWORD2)
begin KEYWORD2
isConnected KEYWORD2
getAddress KEYWORD2
setALSMode KEYWORD2
setUVSMode KEYWORD2
reset KEYWORD2
enable KEYWORD2
disable KEYWORD2
isEnabled KEYWORD2
setResolution KEYWORD2
getResolution KEYWORD2
setRate KEYWORD2
getRate KEYWORD2
setGain KEYWORD2
getGain KEYWORD2
getPartID KEYWORD2
getRevisionID KEYWORD2
getStatus KEYWORD2
getALSData KEYWORD2
getUVSData KEYWORD2
setInterruptConfig KEYWORD2
getInterruptConfig KEYWORD2
setInterruptPersist KEYWORD2
getInterruptPersist KEYWORD2
setHighThreshold KEYWORD2
getHighThreshold KEYWORD2
setLowThreshold KEYWORD2
getLowThreshold KEYWORD2
# Constants (LITERAL1)
LTR390_LIB_VERSION LITERAL1

View File

@ -0,0 +1,23 @@
{
"name": "LTR390",
"keywords": "UV",
"description": "Arduino library for the I2C LTR390 UV sensor.",
"authors":
[
{
"name": "Rob Tillaart",
"email": "Rob.Tillaart@gmail.com",
"maintainer": true
}
],
"repository":
{
"type": "git",
"url": "https://github.com/RobTillaart/LTR390.git"
},
"version": "0.1.1",
"license": "MIT",
"frameworks": "*",
"platforms": "*",
"headers": "LTR390.h"
}

View File

@ -0,0 +1,11 @@
name=LTR390_RT
version=0.1.1
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for the I2C LTR390 UV sensor.
paragraph=
category=Sensors
url=https://github.com/RobTillaart/LTR390_RT
architectures=*
includes=LTR390.h
depends=

View File

@ -0,0 +1,51 @@
//
// FILE: unit_test_001.cpp
// AUTHOR: Rob Tillaart
// DATE: 2024-04-29
// PURPOSE: unit tests for the LTR390 library
// URL: https://github.com/RobTillaart/LTR390
//
// supported assertions
// https://github.com/Arduino-CI/arduino_ci/blob/master/cpp/unittest/Assertion.h#L33-L42
// ----------------------------
// assertEqual(expected, actual)
// assertNotEqual(expected, actual)
// assertLess(expected, actual)
// assertMore(expected, actual)
// assertLessOrEqual(expected, actual)
// assertMoreOrEqual(expected, actual)
// assertTrue(actual)
// assertFalse(actual)
// assertNull(actual)
// assertNotNull(actual)
#include <ArduinoUnitTests.h>
#include "LTR390.h"
unittest_setup()
{
fprintf(stderr, "LTR390_LIB_VERSION: %s\n", (char *) LTR390_LIB_VERSION);
}
unittest_teardown()
{
}
unittest(test_constructor)
{
LTR390 dev;
assertEqual(0x53, dev.getAddress());
}
unittest_main()
// -- END OF FILE --