|
| 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 | +} |
0 commit comments