Updaate 2019-09-29 Error in VEMLConfig.h Register numbers should have been binary.
File updated in the post.
HI,
I'm using the Vishay VEML7700 lux sensor for a project. Upon starting I looked at the VEML7700 code/library on Github. I found a few but they seemed to be large and cumbersome for such a trivial sensor.
So I wrote my own very very simple code. As I do not have a GitHub account I posted it here hoping someone will find it useful.
JohnRob
/*
VEML7700_basic.ino
2019-09-18, johnrob
License:
No license is required, this code is released with no restrictions (and no warranties) whatsoever.
If you wish to copy this small code and claim it as your own I pity you for arrogating such a trivial
application.
veml7700 basic application
Usage and testing was performed on a 8Mhz ProMini
This program performs the following:
1) configures the sensor using choices selected by commenting / uncommenting lines in the VEMLConfig.h file.
2) Prints the date (both ALS and White channels) in Lux to the serial port.
We do not fully understand the false in "Wire.endTransmission(false)"
Published "libraries" for the VEML7700 require more than 2X the resources this program uses. Any added
capability (like auto range) is of little use for the average user. This configuration of the VEML7700
provides a range of 0 to 60397 Lux with a resolution of ~ 0.9 lux (see VEML7700Config.h). Having "played" with
this sensor I feel it is unlikely tenths of Lux will be of any use to most.
One area I have not investigated is the difference between the ALS reading and the White reading. Perhaps the difference
carries some useful information about the type of light hitting the sensor.
On Pro Mini this sketch uses 3784 bytes (12%) of program storage space. Maximum is 30720 bytes.
Global variables use 434 bytes (21%) of dynamic memory, leaving 1614 bytes for local variables. Maximum is 2048 bytes.
*/
#include <Wire.h>
#include "VEMLConfig.h" //defines cmd00, aka configuration command
// -------------------------------------------------
#define Lux_Address 0x10
// --- function prototypes -------------------------
// -------------------------------------------------
void LuxSendData(uint8_t command, uint32_t data);
void LuxReceiveData(uint8_t command, uint32_t& data);
// --- Global Variables-----------------------------
// -------------------------------------------------
uint32_t data;
uint16_t command;
// --- Initialization ------------------------------
// -------------------------------------------------
void setup()
{
Serial.begin(115200);
Wire.begin();
Wire.setClock(100000);
LuxSendData(0x00,cmd00);
}
// --- Main ----------------------------------------
// -------------------------------------------------
void loop()
{
LuxReceiveData(0x04,data);
Serial.print(" ALS (Lux) ");
Serial.print(data*Lux_Factor, 0);
LuxReceiveData(0x05,data);
Serial.print(" White (Lux) ");
Serial.println(data*Lux_Factor, 0);
delay(2000); // should be a non-blocking timer, but except this
// code is likely to be incorporated into a larger program
}
// -------------------------------------------------
// --- functions -----------------------------------
// -------------------------------------------------
//--- send data: ----------------------------------
void LuxSendData(uint8_t command, uint32_t data)
{
Wire.beginTransmission(Lux_Address);
Wire.write(command);
Wire.write(uint8_t(data & 0xff));
Wire.write(uint8_t(data >> 8));
Wire.endTransmission();
}
//--- receive data --------------------------------
void LuxReceiveData(uint8_t command, uint32_t& data)
{
Wire.beginTransmission(Lux_Address);
Wire.write(command);
Wire.endTransmission(false); // need to investigate the (false) req'ment
Wire.requestFrom(uint8_t(Lux_Address), uint8_t(2));
data = Wire.read();
data |= uint32_t(Wire.read()) << 8;
}
// --- end of program ------------------------------
and
/*
/*
2019-09-06 VEML7700 Configuration creation.
2019-09-29 fixed error in register numbers; was hex, now binary.
Instructions:
- Uncomment one #define from each of the 1st groups
- For the Gain and Integration Time (ALS_IT) find
the factor in the table below. Assign
Lux_Factor to the listed lux/count.
*/
// Unless edited, this configuration is for:
// 0.9216 lux / count and a range of 16 bits.
// 0 to 65536 counts * 0.9216 lux/count = 0 to 60397 Lux
// (for both ALS and White channels)
// --- Configuration Section -----------------------
// --- all operations are performed by the pre-processor
// --- No memory or processor resources are req'd
// -------------------------------------------------
//--- Uncomment one from this group:
//#define ALS_GAIN 0b00 // Gain = 1
//#define ALS_GAIN 0b01 // Gain = 2
#define ALS_GAIN 0b10 // Gain = 1/8
//#define ALS_GAIN 0b11 // Gain = 1/4
// --- Uncomment one from this group:
//#define ALS_IT 0b1100 // 25 ms
#define ALS_IT 0b1000 // 50 ms
//#define ALS_IT 0b0000 // 100 ms
//#define ALS_IT 0b0001 // 200 ms
//#define ALS_IT 0b0010 // 400 ms
//#define ALS_IT 0b0011 // 800 ms
// See datasheet for options for this group:
#define ALS_PERS 0b00
#define ALS_INT_EN 0b00
#define ALS_SD 0b00
#define cmd00 ( (ALS_GAIN << 11) | (ALS_IT << 6) |(ALS_PERS << 4) |(ALS_INT_EN << 1) | (ALS_SD << 0) )
#define Lux_Factor 0.9216
/*
Gain = 2 Gain = 1 Gain = 1/4 Gain = 1/8
IT(ms) Resolution lux/count
800 0.0036 0.0072 0.0288 0.0576
400 0.0072 0.0144 0.0576 0.1152
200 0.0144 0.0288 0.1152 0.2304
100 0.0288 0.0576 0.2304 0.4608
50 0.0576 0.1152 0.4608 >0.9216<
25 0.1152 0.2304 0.9216 1.8432
*/
*/