Skip to content

Arduino Example

Now that we have the SparkFun VEML7700 Arduino library installed, let's take a closer look at the examples included in the library.

Example 1 - Get Lux

The first example shows how to initialize the VEML7700 with default settings and print out light values in lux. Open the example in Arduino by navigating to File > Libraries > SparkFun VEML7700 Arduino Library > Example1_getLux. Select your board and port and click the "Upload" button. After the code finishes compiling and uploading, open the serial monitor with the baud set to 115200 and you should see values print out every 250ms:

Screenshot of serial monitor output for example 1

Example 2 - Change Settings

The second example demonstrates how to adjust the device settings for the VEML7700 from default, prints the values for the settings and then prints out light values in lux, ambient light level and the white channel level. The exampleode adjusts the integration time, sensitivity and persistence settings.

The code sets the integration to 50ms (default is 100ms) using mySensor.setIntegrationTime(VEML7700_INTEGRATION_50ms);. Acceptable values for integration time are:

  • 25ms
  • 50ms
  • 100ms
  • 200ms
  • 400ms
  • 800ms

Next, it sets the sensitivity to 2x (default is 1) using mySensor.setSensitivityMode(VEML7700_SENSITIVITY_x2);. Acceptable values for sensitivty are:

  • x1
  • x2
  • x1_8 (1/8)
  • x1_4 (1/4)

Lastly, the example sets the persistence value to 4 (default is 1) using mySensor.setPersistenceProtect(VEML7700_PERSISTENCE_4);. Possible values for persistence are:

  • 1
  • 2
  • 4
  • 8

Example 3 - Threshold

Example 3 shows how to change the VEML7700's measurement threshold settings to trigger interrupts from the sensor when reading exceed the values set. This allows you to set thresholds for HIGH and LOW readings from the sensor for applications where you need the lux values to remain within a specific range. The example defaults to set the HIGH threshold to 30000 counts and the low threshold to 1000 counts. You can adjust these vaules in the setup functions:

 mySensor.setHighThreshold(1000);
 mySensor.setLowThreshold(1000);
 ```

The main loop takes readings from the sensor every 250ms and then checks the interrupt status and prints out whether either threshold was exceeded or if the interrupt return is invalid. Reading the interrupt status register clears the interrupts, so the code checks both the high and low interrupt flags in a single read:

```c++
VEML7700_interrupt_status_t intStatus = mySensor.getInterruptStatus();

if (intStatus == VEML7700_INT_STATUS_INVALID)
  {
    Serial.print(F("\tInterrupt Status Read Error!"));
  }
  else
  {
    if (intStatus & VEML7700_INT_STATUS_HIGH) // Use a logical AND to check if the high flag is set
      Serial.print(F("\tHigh Threshold Exceeded"));

    if (intStatus & VEML7700_INT_STATUS_LOW) // Use a logical AND to check if the low flag is set
      Serial.print(F("\tLow Threshold Exceeded"));
  }

Example 4 - Shutdown

The fourth example cycles the VEML7700 on and off every five seconds to help conserve power. The code sets up a timer to keep track of time in between power cycles and then checks the power state to see if the sensor needs to power on or off. While on, the sensor prints out lux data every 125ms.