0.5.3 Max44009

This commit is contained in:
rob tillaart 2022-11-16 16:00:38 +01:00
parent a256f12bd3
commit 981c9f9168
8 changed files with 219 additions and 119 deletions

View File

@ -1,3 +1,18 @@
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:
@ -9,3 +24,4 @@ compile:
- esp32
# - esp8266
# - mega2560
- rpipico

View File

@ -0,0 +1,99 @@
# Change Log MAX44009
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.5.3] - 2022-11-16
- add RP2040 in build-CI
- add changelog.md
- minor edit readme.md
## [0.5.2] - 2022-01-05
- minor edits after creating Max44007 library
## [0.5.1] - 2021-12-21
- update library.json
- update license
- minor edits
## [0.5.0] - 2021-06-04
- fix exponent math.
----
## [0.4.4] - 2021-05-27
- add Arduino-lint
## [0.4.3] - 2020-12-30
- Arduino-CI, unit test
## [0.4.2] - 2020-06-19
- fix library.json
## [0.4.1] - 2020-03-21
- add #pragma
- update readme.md
- license.md
## [0.4.0] - 2020-01-30
- remove automatic mode from constructors;
- added example code
----
## [0.3.3] - 2020-01-27
- fix #140 refactor constructors / configure
## [0.3.2] - 2020-01-21
- solve #132 cannot read full range in manual mode.
- set automatic mode explicitly in constructors;
- added some error checks
## [0.3.1] - 2020-01-21
- fix #133 overflow of the exponent.
## [0.3.0] - 2020-01-20
- fix #141 Kudos to Moritz89
## [0.2.0] - 2019-08-23
- solve #127 == redo #118
----
## [0.1.10] - 2018-12-08
- issue #118 Fix constructor esp8266 (thanks to Bolukan)
## [0.1.9] - 2018-07-01
- issue #108 Fix shift math (thanks Roland vandecook)
## [0.1.8] - 2018-05-13
- issue #105 Fix read register (thanks morxGrillmeister)
## [0.1.7] - 2018-04-02
- issue #98 extend constructor for ESP8266
## [0.1.6] - 2017-07-26
- revert double to float
## [0.1.5]
- update history
## [0.1.4]
- added setAutomaticMode() to max44009.h (thanks debsahu)
## [0.1.03]
- added configuration
## [0.1.02]
- added threshold code
## [0.1.01]
- added interrupt code
## [0.1.00] - 2010-??-??
- initial version.

View File

@ -2,29 +2,29 @@
// FILE: Max44009.h
// AUTHOR: Rob dot Tillaart at gmail dot com
// VERSION: 0.5.2
// VERSION: 0.5.3
// PURPOSE: library for MAX44009 lux sensor Arduino
// HISTORY: See Max440099.cpp
// HISTORY: See changelog.md
// breakout MAX44009 / GY-49
// breakout MAX44009 / GY-49
//
// +--------+
// VCC |o |
// GND |o |
// SCL |o o| ADDRESS
// SDA |o o| -INT
// +--------+
// +--------+
// VCC |o |
// GND |o |
// SCL |o o| ADDRESS
// SDA |o o| -INT
// +--------+
//
// ADDRESS:
// 0 = 0x4A
// 1 = 0x4B
// ADDRESS:
// 0 = 0x4A
// 1 = 0x4B
//
// INT:
// Connect the INT pin to an pull up resistor
// 0 = interrupt
// 1 = no interrupt
// connect to an LED or an interrupt pin of an Arduino
// INT:
// Connect the INT pin to an pull up resistor
// 0 = interrupt
// 1 = no interrupt
// connect to an LED or an interrupt pin of an Arduino
//
@ -32,7 +32,7 @@
#include "Arduino.h"
#define MAX44009_LIB_VERSION (F("0.5.2"))
#define MAX44009_LIB_VERSION (F("0.5.3"))
#define MAX44009_DEFAULT_ADDRESS 0x4A
#define MAX44009_ALT_ADDRESS 0x4B
@ -67,31 +67,31 @@
class Max44009
{
public:
// enum class to prevent bool to be implicitly casted to int
// enum class to prevent bool to be implicitly casted to int
enum class Boolean { True, False };
#if defined(ESP8266) || defined(ESP32)
// dataPin and clockPin can be used for ESP8266
// dataPin and clockPin can be used for ESP8266
Max44009(const uint8_t address, const uint8_t dataPin, const uint8_t clockPin);
#endif
// ctor for UNO
// constructor for UNO
Max44009(const uint8_t address, const Boolean begin = Boolean::True);
// default ctor with default address
// default constructor with default address
Max44009(const Boolean begin = Boolean::True);
// Change I2C interface and address
// Change I2C interface and address
void configure(const uint8_t address, TwoWire *wire, const Boolean begin = Boolean::True);
bool isConnected();
float getLux();
int getError();
// threshold must be between 0 and 188006
bool setHighThreshold(const float value); // returns false if value out of range
// threshold must be between 0 and 188006
bool setHighThreshold(const float value); // returns false if value out of range
float getHighThreshold(void);
bool setLowThreshold(const float value); // returns false if value out of range
bool setLowThreshold(const float value); // returns false if value out of range
float getLowThreshold(void);
void setThresholdTimer(const uint8_t value); // 2 seems practical minimum
void setThresholdTimer(const uint8_t value); // 2 seems practical minimum
uint8_t getThresholdTimer();
void enableInterrupt() { write(MAX44009_INTERRUPT_ENABLE, 1); };
@ -99,27 +99,28 @@ public:
bool interruptEnabled() { return read(MAX44009_INTERRUPT_ENABLE) & 0x01; };
uint8_t getInterruptStatus() { return read(MAX44009_INTERRUPT_STATUS) & 0x01; };
// check datasheet for detailed behavior
// check datasheet for detailed behaviour
void setConfiguration(uint8_t);
uint8_t getConfiguration();
void setAutomaticMode();
void setContinuousMode(); // uses more power
void clrContinuousMode(); // uses less power
// CDR = Current Divisor Ratio
// CDR = 1 ==> only 1/8th is measured
// TIM = Time Integration Measurement (table)
// 000 800ms
// 001 400ms
// 010 200ms
// 011 100ms
// 100 50ms manual only
// 101 25ms manual only
// 110 12.5ms manual only
// 111 6.25ms manual only
void setManualMode(uint8_t CDR, uint8_t TIM);
int getIntegrationTime() { return 800 >> (getConfiguration() & 0x07); }; // ms
void setContinuousMode(); // uses more power
void clrContinuousMode(); // uses less power
// TEST the math
// CDR = Current Divisor Ratio
// CDR = 1 ==> only 1/8th is measured
// TIM = Time Integration Measurement (table)
// 000 800ms
// 001 400ms
// 010 200ms
// 011 100ms
// 100 50ms manual only
// 101 25ms manual only
// 110 12.5ms manual only
// 111 6.25ms manual only
void setManualMode(uint8_t CDR, uint8_t TIM);
int getIntegrationTime() { return 800 >> (getConfiguration() & 0x07); }; // ms
// TEST the math
float convertToLux(uint8_t datahigh, uint8_t datalow);
@ -138,5 +139,5 @@ protected:
};
// -- END OF FILE --
// -- END OF FILE --

View File

@ -3,6 +3,19 @@
// AUTHOR: Rob Tillaart
// PURPOSE: demo of max44009 library
// DATE: 2015-08-06
//
// breakout MAX44009 / GY-49
//
// +--------+
// VCC |o |
// GND |o |
// SCL |o o| ADDRESS
// SDA |o o| -INT
// +--------+
//
// ADDRESS:
// 0 = 0x4A
// 1 = 0x4B
#include "Wire.h"
@ -41,7 +54,7 @@ void loop()
else
{
Serial.print("lux:\t");
Serial.println(lux);
Serial.println(lux, 3);
}
}
}

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/Max44009.git"
},
"version": "0.5.2",
"version": "0.5.3",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",

View File

@ -1,5 +1,5 @@
name=Max44009
version=0.5.2
version=0.5.3
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Library for MAX44009 I2C LUX sensor Arduino.

View File

@ -1,42 +1,12 @@
//
// FILE: Max44009.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.5.2
// VERSION: 0.5.3
// PURPOSE: library for MAX44009 lux sensor Arduino
// URL: https://github.com/RobTillaart/MAX44009
//
// HISTORY
//
// 0.5.2 2022-01-05 minor edits after creating Max44007 library
// 0.5.1 2021-12-21 update library.json, license, minor edits
// 0.5.0 2021-06-04 fix exponent math.
// 0.4.4 2021-05-27 Arduino-lint
// 0.4.3 2020-12-30 Arduino-CI, unit test
// 0.4.2 2020-06-19 fix library.json
// 0.4.1 2020-03-21 #pragma, readme.md, license.md
// 0.4.0 2020-01-30 remove automatic mode from constructors;
// added example code
// 0.3.3 2020-01-27 issue #140 refactor constructors / configure
// 0.3.2 2020-01-21 solve #132 cannot read full range in manual mode.
// set automatic mode explicitly in constructors;
// added some error checks
// 0.3.1 2020-01-21 issue #133 overflow of the exponent
// 0.3.0 2020-01-20 issue #141 Kudos to Moritz89
// 0.2.0 2019-08-23 solve #127 == redo #118
// 0.1.10 2018-12-08 issue #118 Fix constructor esp8266
// (thanks to Bolukan)
// 0.1.9 2018-07-01 issue #108 Fix shift math
// (thanks Roland vandecook)
// 0.1.8 2018-05-13 issue #105 Fix read register
// (thanks morxGrillmeister)
// 0.1.7 2018-04-02 issue #98 extend constructor for ESP8266
// 0.1.6 2017-07-26 revert double to float
// 0.1.5 updated history
// 0.1.4 added setAutomaticMode() to max44009.h (thanks debsahu)
// 0.1.03 added configuration
// 0.1.02 added threshold code
// 0.1.01 added interrupt code
// 0.1.00 initial version
// HISTORY: see changelog.md
#include "Max44009.h"
@ -207,12 +177,12 @@ void Max44009::clrContinuousMode()
void Max44009::setManualMode(uint8_t CDR, uint8_t TIM)
{
if (CDR !=0) CDR = 1; // only 0 or 1
if (CDR !=0) CDR = 1; // only 0 or 1
if (TIM > 7) TIM = 7;
uint8_t config = read(MAX44009_CONFIGURATION);
config |= MAX44009_CFG_MANUAL;
config &= 0xF0; // clear old CDR & TIM bits
config |= CDR << 3 | TIM; // set new CDR & TIM bits
config &= 0xF0; // clear old CDR & TIM bits
config |= CDR << 3 | TIM; // set new CDR & TIM bits
write(MAX44009_CONFIGURATION, config);
}
@ -228,18 +198,18 @@ float Max44009::convertToLux(uint8_t datahigh, uint8_t datalow)
///////////////////////////////////////////////////////////
//
// PRIVATE
// PRIVATE
//
bool Max44009::setThreshold(const uint8_t reg, const float value)
{
// CHECK RANGE OF VALUE
// CHECK RANGE OF VALUE
if ((value < 0.0) || (value > MAX44009_MAX_LUX)) return false;
uint32_t mantissa = round(value * (1.0 / MAX44009_MIN_LUX)); // compile time optimized.
uint8_t exponent = 0;
while (mantissa > 255)
{
mantissa >>= 1; // bits get lost
mantissa >>= 1; // bits get lost
exponent++;
};
mantissa = (mantissa >> 4) & 0x0F;
@ -252,7 +222,7 @@ bool Max44009::setThreshold(const uint8_t reg, const float value)
float Max44009::getThreshold(uint8_t reg)
{
uint8_t datahigh = read(reg);
float lux = convertToLux(datahigh, 0x08); // 0x08 = correction for lost bits
float lux = convertToLux(datahigh, 0x08); // 0x08 = correction for lost bits
return lux;
}
@ -264,12 +234,12 @@ uint8_t Max44009::read(uint8_t reg)
_error = _wire->endTransmission();
if (_error != MAX44009_OK)
{
return _data; // last value
return _data; // last value
}
if (_wire->requestFrom(_address, (uint8_t) 1) != 1)
{
_error = MAX44009_ERROR_WIRE_REQUEST;
return _data; // last value
return _data; // last value
}
_data = _wire->read();
return _data;
@ -285,4 +255,4 @@ void Max44009::write(uint8_t reg, uint8_t value)
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -18,31 +18,31 @@ a.k.a. GY-49
The MAX44009 ambient light sensor is an I2C sensor, that has a 22 bit
dynamic range from 0.045 lux to 188,000 lux.
relates to https://github.com/RobTillaart/Max44007
Relates to https://github.com/RobTillaart/Max44007
## Schema breakout max44009 / GY-49
```cpp
// breakout MAX44009 / GY-49
// breakout MAX44009 / GY-49
//
// +--------+
// VCC |o |
// GND |o |
// SCL |o o| ADDRESS
// SDA |o o| -INT
// +--------+
// +--------+
// VCC |o |
// GND |o |
// SCL |o o| ADDRESS
// SDA |o o| -INT
// +--------+
//
// ADDRESS:
// 0 = 0x4A
// 1 = 0x4B
// ADDRESS:
// 0 = 0x4A
// 1 = 0x4B
//
// INT:
// Connect the INT pin to an pull up resistor
// 0 = interrupt
// 1 = no interrupt
// connect to an LED or an interrupt pin of an Arduino
// INT:
// Connect the INT pin to an pull up resistor
// 0 = interrupt
// 1 = no interrupt
// connect to an LED or an interrupt pin of an Arduino
//
```
@ -114,7 +114,7 @@ check datasheet for details
### Configure sample mode
check datasheet for details
Check datasheet for details.
CCR = Current Divisor Ratio.
@ -134,14 +134,14 @@ integration time manually. Effectively disable automatic mode.
CDR = 1 ==> only 1/8th is measured
TIM = Time Integration Measurement (table)
000 800ms
001 400ms
010 200ms
011 100ms
100 50ms manual only
101 25ms manual only
110 12.5ms manual only
111 6.25ms manual only
000 800ms
001 400ms
010 200ms
011 100ms
100 50ms manual only
101 25ms manual only
110 12.5ms manual only
111 6.25ms manual only
```
@ -167,8 +167,8 @@ format to a LUX value.
## Notes
Please be aware this is a **3.3 Volt device** so it should not be connected
to an Arduino or other 5 Volt device directly. Use a level convertor to
solve this.
to an Arduino UNO or other 5 Volt device directly.
Use a level convertor to solve this.
Do not forget to connect the address pin as you cannot read the sensor
in a reliable way. As the line will float it will sometimes have the
@ -184,4 +184,5 @@ right address and sometimes not. (been there ;)
#### MAX44007
The MAX44007 is an almost identical sensor that uses a step size of 0.025.
That implies the library is not useable 1 to 1 for the MAX4007, however some
This implies that this library is not useable 1 to 1 for the MAX44007, however some parts will work/