Veml7700

Discussion about Z-Uno product. Visit http://z-uno.z-wave.me for more details.
Post Reply
leiff
Posts: 1
Joined: 06 Oct 2020 21:34

Veml7700

Post by leiff »

Hello

Try to use a VEML7700 to get lux information but I can't get it to work.
It use I2C protocol and i have connected this to port 9/10.
The only response i got is 0 (zero)
What have I done wrong ?
/Leif

Code: Select all

#include "Wire.h"

#define VEML7700_I2CADDR 0x10 ///< I2C address
#define VEML7700_ALS_CONFIG 0x00        ///< Light configuration register
#define VEML7700_ALS_THREHOLD_HIGH 0x01 ///< Light high threshold for irq
#define VEML7700_ALS_THREHOLD_LOW 0x02  ///< Light low threshold for irq
#define VEML7700_ALS_POWER_SAVE 0x03    ///< Power save regiester
#define VEML7700_ALS_DATA 0x04          ///< The light data output
#define VEML7700_WHITE_DATA 0x05        ///< The white light data output
#define VEML7700_INTERRUPTSTATUS 0x06   ///< What IRQ (if any) 
#define ALS_GAIN 0b10 // Gain = 1/8
#define ALS_IT 0b1000   //   50 ms
#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) )


void setup() {
  Serial.begin(9600);
  Wire.begin(); // wake up I2C bus

  SendData(VEML7700_ALS_CONFIG,cmd00);
  SendData(VEML7700_ALS_THREHOLD_HIGH,0x00);
  SendData(VEML7700_ALS_THREHOLD_LOW,0x00);
  SendData(VEML7700_ALS_POWER_SAVE,0x00);
 
}

void loop() {
  readlux();
  delay(1000);
}

void readlux() {
   uint32_t data;
   data = ReceiveData(VEML7700_ALS_DATA);
   Serial.println(data,DEC);
}

void SendData(uint8_t command, uint32_t data)
{
    Wire.beginTransmission(VEML7700_I2CADDR);
    Wire.write(command);
    Wire.write(uint8_t(data & 0xff));
    Wire.write(uint8_t(data >> 8));
    Wire.endTransmission();
}


uint32_t ReceiveData(uint8_t command)
{
    uint32_t data;
    Wire.beginTransmission(VEML7700_I2CADDR);
    Wire.write(command);
    Wire.endTransmission();      
    Wire.requestFrom(uint8_t(VEML7700_I2CADDR), uint8_t(2));
    data = Wire.read();
    data |= uint32_t(Wire.read()) << 8;
    Wire.endTransmission();      
    return data;
}

p0lyg0n1
Posts: 183
Joined: 04 Aug 2016 07:14

Re: Veml7700

Post by p0lyg0n1 »

Hi, I didn't try this sensor myself yet(I dont have Veml7700), but lets try to get it work together:
1) Lets test that sensor says "Ack" when you call its address. Use this code to check your i2c bus:

Code: Select all

/*
 *  I2CScanner
 *  Scans all devices connected to I2C bus.
 *  SCL = PIN #9 of Z-Uno
 *  SDA = PIN #10 of Z-Uno  
 *  Writes table of found devices in style 
 */
 
#include <Wire.h>
#define MY_SERIAL Serial //Serial0

void setup() {
  Wire.begin();
  // Wire.enableTS(true);
  MY_SERIAL.begin(9600);
  MY_SERIAL.println("\nI2C Scanner");

}

void loop() {
  int nDevices;
  byte error, address;  
 
  MY_SERIAL.println("Scanning I2C bus...\n");
  nDevices = 0;

  MY_SERIAL.print("   00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F");
  
  
  for(address = 0; address < 128; address++ )
  {
    if((address % 0x10) == 0)
    {
      MY_SERIAL.println();
      if(address < 16)
        MY_SERIAL.print('0');
      MY_SERIAL.print(address, 16);
      MY_SERIAL.print(" ");
    }
    // The i2c_scanner uses the return value of
    // the Write.endTransmission to see if
    // a device sends acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();
 
    
    if (error == 0)
    {
      MY_SERIAL.print(address, HEX);   
      nDevices++;
    }
    else 
    {
      MY_SERIAL.print("--");
    } 

    MY_SERIAL.print(" ");
    //delay(1);
  }
  MY_SERIAL.println();
  
  if (nDevices == 0)
     MY_SERIAL.println("No I2C devices found\n");
   else{
     MY_SERIAL.print("Found ");
     MY_SERIAL.print(nDevices);
     MY_SERIAL.println(" device(s) "); 
   }
 
  delay(2500); 
}
2) If you see that 0x10 filled in output of previous sketch (Use SerialMonitor of IDE to see the output) modify a little your code. This place looks strange

Code: Select all

uint32_t ReceiveData(uint8_t command)
{
    uint32_t data;
    Wire.beginTransmission(VEML7700_I2CADDR);
    Wire.write(command);
    Wire.endTransmission();      
    Wire.requestFrom(uint8_t(VEML7700_I2CADDR), uint8_t(2));
    data = Wire.read();
    data |= uint32_t(Wire.read()) << 8;
    Wire.endTransmission();      
    return data;
}
lets rewrite it this way:

Code: Select all

uint16_t ReceiveData(uint8_t command)
{
    uint16_t data;
    Wire.beginTransmission(VEML7700_I2CADDR);
    Wire.write(command);
    Wire.endTransmission();      
    Wire.requestFrom(uint8_t(VEML7700_I2CADDR), uint8_t(2));
    data = Wire.read();
    data |= uint16_t(Wire.read()) << 8;    
    return data;
}
Good luck. Keep us informed.

Alex.

Post Reply