Skip to content

Commit 362d6f1

Browse files
author
Elias Santistevan
committed
Adds Example three which demonstrates power save mode.
1 parent 71adad1 commit 362d6f1

File tree

4 files changed

+133
-89
lines changed

4 files changed

+133
-89
lines changed

Examples/Example3_All_Ambient_Features/Example3_All_Ambient_Features.ino

Lines changed: 0 additions & 75 deletions
This file was deleted.
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
This example code will walk you through how to enable power save mode on your
3+
Ambient Light Sensor. When would you use power save mode? The datasheet
4+
specifies that power save mode is perfect when you want to continuously
5+
sample ambient light readings from your sensor, as opposed to getting a
6+
reading once an hour for example. The benefit of power save mode is lower
7+
power consumption, with current draw in the single digit micro-amp range.
8+
Enabling the power save mode decreases the refresh rate of the sensor and so
9+
sampling should happen at a slower rate. Last thing, the refresh rate is also
10+
dependent on the integration time setting of the sensor. See the hookup guide
11+
for a table showing the correlation of refresh times, current consumption, power
12+
mode, and integration mode settings.
13+
14+
SparkFun Electronics
15+
Author: Elias Santistevan
16+
Date: July 2019
17+
18+
License: This code is public domain but if you use this and we meet someday, get me a beer!
19+
20+
Feel like supporting our work? Buy a board from Sparkfun!
21+
https://www.sparkfun.com/products/15436
22+
23+
*/
24+
25+
#include <Wire.h>
26+
#include "SparkFun_VEML6030_Ambient_Light_Sensor.h"
27+
28+
// Close the jumper on the product to use address 0x10.
29+
#define AL_ADDR 0x48
30+
31+
SparkFun_Ambient_Light light(AL_ADDR);
32+
33+
// Possible values: .125, .25, 1, 2
34+
// Both .125 and .25 should be used in most cases except darker rooms.
35+
// A gain of 2 should only be used if the sensor will be covered by a dark
36+
// glass.
37+
float gain = .125;
38+
39+
// Possible integration times in milliseconds: 800, 400, 200, 100, 50, 25
40+
// Higher times give higher resolutions and should be used in darker light.
41+
int time = 100;
42+
long luxVal = 0;
43+
44+
// Power save mode, options range from 1-4.
45+
// Default is 1.
46+
int powMode = 2;
47+
48+
void setup(){
49+
50+
Wire.begin();
51+
Serial.begin(115200);
52+
53+
if(light.begin())
54+
Serial.println("Ready to sense some light!");
55+
else
56+
Serial.println("Could not communicate with the sensor!");
57+
58+
// Again the gain and integration times determine the resolution of the lux
59+
// value, and give different ranges of possible light readings. Check out
60+
// hoookup guide for more info.
61+
light.setGain(gain);
62+
light.setIntegTime(time);
63+
64+
Serial.println("Reading settings...");
65+
Serial.print("Gain: ");
66+
float gainVal = light.readGain();
67+
Serial.print(gainVal, 3);
68+
Serial.print(" Integration Time: ");
69+
int timeVal = light.readIntegTime();
70+
Serial.println(timeVal);
71+
72+
// Power save mode enables low power consumption from your Ambient Light
73+
// Sensor. You can set modes from 1-4, four being the highest power saving
74+
// option but slowest refresh rate. The sensor's refresh rate is also
75+
// dependent on the integration time. For example a setting of 4 with an
76+
// integration time of 100ms == 4100ms refresh rate but 2 micro-amps current
77+
// draw. Check hookup guide for more info.
78+
light.setPowSavMode(powMode);
79+
// Let's see that it was set correctly.
80+
Serial.print("Power Save Mode: ");
81+
int savVal = light.readPowSavMode();
82+
Serial.println(savVal);
83+
light.enablePowSave();
84+
Serial.println("Is power save enabled: ");
85+
int enaVal = light.readPowSavEnabled();
86+
87+
if(enaVal)
88+
Serial.println("Yes!");
89+
else
90+
Serial.println("No!");
91+
92+
// This will power down the sensor and the sensor will draw 0.5 micro-amps of
93+
// power while shutdown.
94+
// light.shutDown();
95+
// light.powerOn();
96+
97+
// Give some time to read your settings.
98+
delay(1000);
99+
100+
}
101+
102+
void loop(){
103+
104+
luxVal = light.readLight();
105+
Serial.print("Ambient Light Reading: ");
106+
Serial.print(luxVal);
107+
Serial.println(" Lux");
108+
// Sampling at the rate specified in the hookup guide power save mode table.
109+
delay(1100);
110+
111+
}

src/SparkFun_VEML6030_Ambient_Light_Sensor.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ float SparkFun_Ambient_Light::readGain(){
8080
// REG0x00, bits[9:6]
8181
// This function sets the integration time (the saturation time of light on the
8282
// sensor) of the ambient light sensor. Higher integration time leads to better
83-
// resolution but slower refresh times.
83+
// resolution but slower sensor refresh times.
8484
void SparkFun_Ambient_Light::setIntegTime(uint16_t time){
8585

8686
uint16_t bits;
@@ -108,7 +108,7 @@ void SparkFun_Ambient_Light::setIntegTime(uint16_t time){
108108
// REG0x00, bits[9:6]
109109
// This function reads the integration time (the saturation time of light on the
110110
// sensor) of the ambient light sensor. Higher integration time leads to better
111-
// resolution but slower refresh times.
111+
// resolution but slower sensor refresh times.
112112
uint16_t SparkFun_Ambient_Light::readIntegTime(){
113113

114114
uint16_t regVal = _readRegister(SETTING_REG);
@@ -469,9 +469,11 @@ uint32_t SparkFun_Ambient_Light::_calculateLux(uint16_t _lightBits){
469469
}
470470

471471

472-
// This function does the opposite above. I belive the interrupt values are
473-
// also set with the given gain and intergration times settings, though I don't
474-
// know because the datasheet doesn't mention anything on the issue.
472+
// This function does the opposite calculation then the function above. The interrupt
473+
// threshold values given by the user are dependent on the gain and
474+
// intergration time settings. As a result the lux value needs to be
475+
// calculated with the current settings and this function accomplishes
476+
// that.
475477
uint16_t SparkFun_Ambient_Light::_calculateBits(uint32_t _luxVal){
476478

477479
float _luxConv;

src/SparkFun_VEML6030_Ambient_Light_Sensor.h

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#define INT_LOW 0x02
1414
#define UNKNOWN_ERROR 0xFF
1515

16+
// 7-Bit address options
1617
const uint8_t defAddr = 0x48;
1718
const uint8_t altAddr = 0x10;
1819

@@ -29,7 +30,7 @@ enum VEML6030_16BIT_REGISTERS {
2930
};
3031

3132
enum VEML6030_16BIT_REG_MASKS {
32-
33+
3334
THRESH_MASK = 0x0,
3435
GAIN_MASK = 0xE7FF,
3536
INTEG_MASK = 0xFC3F,
@@ -56,7 +57,8 @@ enum REGISTER_BIT_POSITIONS {
5657

5758
// Table of lux conversion values depending on the integration time and gain.
5859
// The arrays represent the all possible integration times and the index of the
59-
// arrays represent the register's gain settings.
60+
// arrays represent the register's gain settings, which is directly analgous to
61+
// their bit representations.
6062
const float eightHIt[] = {.0036, .0072, .0288, .0576};
6163
const float fourHIt[] = {.0072, .0144, .0576, .1152};
6264
const float twoHIt[] = {.0144, .0288, .1152, .2304};
@@ -89,21 +91,23 @@ class SparkFun_Ambient_Light
8991
// REG0x00, bits[9:6]
9092
// This function sets the integration time (the saturation time of light on the
9193
// sensor) of the ambient light sensor. Higher integration time leads to better
92-
// resolution but slower refresh times.
94+
// resolution but slower sensor refresh times.
9395
void setIntegTime(uint16_t time);
9496

9597
// REG0x00, bits[9:6]
9698
// This function reads the integration time (the saturation time of light on the
9799
// sensor) of the ambient light sensor. Higher integration time leads to better
98-
// resolution but slower refresh times.
100+
// resolution but slower sensor refresh times.
99101
uint16_t readIntegTime();
100102

101103
// REG0x00, bits[5:4]
102-
// This function sets the persistence protect number.
104+
// This function sets the persistence protect number i.e. the number of
105+
// values needing to crosh the interrupt thresholds.
103106
void setProtect(uint8_t protVal);
104107

105108
// REG0x00, bits[5:4]
106-
// This function reads the persistence protect number.
109+
// This function reads the persistence protect number i.e. the number of
110+
// values needing to crosh the interrupt thresholds.
107111
uint8_t readProtect();
108112

109113
// REG0x00, bit[1]
@@ -212,9 +216,11 @@ class SparkFun_Ambient_Light
212216
// the value and returns it.
213217
uint32_t _calculateLux(uint16_t _lightBits);
214218

215-
// This function does the opposite above. I belive the interrupt values are
216-
// also set with the given gain and intergration times settings, though I don't
217-
// know because the datasheet doesn't mention anything on the issue.
219+
// This function does the opposite calculation then the function above. The interrupt
220+
// threshold values given by the user are dependent on the gain and
221+
// intergration time settings. As a result the lux value needs to be
222+
// calculated with the current settings and this function accomplishes
223+
// that.
218224
uint16_t _calculateBits(uint32_t _luxVal);
219225

220226
// This function writes to a 16 bit register. Paramaters include the register's address, a mask

0 commit comments

Comments
 (0)