Hello all! I'm kinda new to Arduino and decided to dive deeper into learning abit more about Arduino; So i decided to grab a Sparkfun VEML6075 UV sensor and try to:
- Initialise it
- Take readings from it
Without using the Sparkfun libraries at all. Based on what I read in the datasheets, this seems possible by using the I2C communication protocol provided by communicating directly with the registers of the device itself.
However, I can't seem to be able to even initialise the device properly as the readings I have gotten are weird and do not change (I presume this means the device only took one reading and stops at that)
My code is as below:
#include <Wire.h>
byte address = 0x10; // Slave address of the VEML6075
byte UVA_LSB, UVA_MSB;
word UVA_data;
uint16_t UVA_combined;
void setup() {
Serial.begin(9600);
Wire.begin();
VEML6075_init(address);
// uv.begin();
Serial.println("Module is detected");
}
void loop() {
VEML6075_Fetch_UV(address); // Fetch UVA data *TEST*
Serial.print("UVA: ");
Serial.println(UVA_combined);
delay(1000);
}
// shutdown module and init it; restarting the module
void VEML6075_init(byte address){
Wire.beginTransmission(address); // Shutdown command
Wire.write(highByte(0x00));
Wire.write(lowByte(0x01));
Wire.endTransmission();
Wire.beginTransmission(address);
Wire.write(highByte(0x00));
Wire.write(lowByte(0x12)); // Reset command using conf register
Wire.endTransmission();
delay(200); // Wait for module to reset
// Done //
}
void VEML6075_Fetch_UV(byte address){
Wire.beginTransmission(address); // Begin transmission into I2C line with Slave address
Wire.write(highByte(0x07)); // Issue command to read UVA data
Wire.write(lowByte(0x07));
Wire.requestFrom(address, 2);
while(!Wire.available()); // Needs refining; read twice for 16 bits
UVA_LSB = Wire.read();
UVA_MSB = Wire.read();
Wire.endTransmission();
UVA_combined = ((UVA_MSB << 8) | UVA_LSB); // Combine to 16bits
}
As stated in the datasheet:
The VEML6075 contains a CONF register (00h) used for operation control and parameter setup. Measurement results are stored
in four separate registers, one each for UVA, UVB, UVcomp1, and UVcomp2 (07h to 0Bh respectively). All registers are accessible
via I2C communication. Fig. 7 shows the basic I2C communication with the VEML6075. Each of the registers in the
VEML6075 are 16 bit wide, so 16 bit should be written when a write command is sent, and 16 bit should be read when a read
command is sent.
I have allocated 16 bits for receiving the data, and have also used the address 0x12 to set the timing to 100ms and activate active-force-mode which i presume is the device taking readings automatically. The readings given are way off the accurate readings given when i upload a example code that uses the Sparkfun library, so I'm quite lost at this point. How am I using the I2C protocol wrongly? Thanks!