Qwiic Ambient Light Sensor (VEML6030) Hookup Guide
Example 1: Ambient Light Basics
In this first example, we'll get you comfortable with gathering ambient light and setting two vital properties of the sensor's ability to read light: the gain and the integration time. These two properties determine the resolution (accuracy) of the reading and the available ranges of light that you can read! For example, a gain of 1/8 and 800ms integration time cannot read anything above 3775 Lux. This means you'll max out your sensor outdoors but would be a proper setting for dim rooms due to it's higher resolution.
At the top of the example we have three variables gain, time, and luxval. The first two hold the value for the gain and integration time settings mentioned above. Gain settings can be: 2
, 1
, 1/4
, and 1/8
; typically 1/4 gain will capture everything you need with good resolution. Possible integration times can be 800
, 400
, 200
, 100
, 50
, 25
and by default the sensor is set to 100
; times are in milliseconds. Check the Gain and Integration Time table above under Hardware Overview to see maximum illumination capabilities and resolution for every setting.
If you have any doubt with which settings to pick, just keep the example code's default settings: a gain of 1/4 (0.125) and an integration time of 100ms. This will give you a range of up to 15,000 Lux with a decent resolution!
language:c
#include <Wire.h>
#include "SparkFun_VEML6030_Ambient_Light_Sensor.h"
#define AL_ADDR 0x48
SparkFun_Ambient_Light light(AL_ADDR);
// Possible values: .125(1/8), .25(1/4), 1, 2
// Both .125 and .25 should be used in most cases except darker rooms.
// A gain of 2 should only be used if the sensor will be covered by a dark
// glass.
float gain = .125;
// Possible integration times in milliseconds: 800, 400, 200, 100, 50, 25
// Higher times give higher resolutions and should be used in darker light.
int time = 100
long luxVal = 0;
In the setup, we call light.begin()
to check if we can communicate with the SparkFun Ambient Light Sensor. Next, we call the light.setGain()
and light.setIntegTime()
functions giving them the variables holding the gain and time values above. Next we'll read back those values to make sure that they were set correctly. That's it! We're now set to read some light!
language:c
void setup(){
Wire.begin();
Serial.begin(115200);
if(light.begin())
Serial.println("Ready to sense some light!");
else
Serial.println("Could not communicate with the sensor!");
// Again the gain and integration times determine the resolution of the lux
// value, and give different ranges of possible light readings. Check out
// hoookup guide for more info.
light.setGain(gain);
light.setIntegTime(time);
Serial.println("Reading settings...");
Serial.print("Gain: ");
float gainVal = light.readGain();
Serial.print(gainVal, 3);
Serial.print(" Integration Time: ");
int timeVal = light.readIntegTime();
Serial.println(timeVal);
}
One thing to keep in mind is that you need to set a delay()
in between readings. The Integration Time is the amount of time that the sensor uses to fill its' sensitive components with light. If you set an integration time of 100ms then make sure you're delay is at least that long. A longer integration time will need a longer delay.
language:c
void loop(){
luxVal = light.readLight();
Serial.print("Ambient Light Reading: ");
Serial.print(luxVal);
Serial.println(" Lux");
delay(1000);
}