|
| 1 | +/* |
| 2 | + This example code will walk you through how to set the interrupts on the |
| 3 | + SparkFun VEML6030 Ambient Light Sensor. You can set both high and low |
| 4 | + thresholds as well as how many values must be below or above the threshold |
| 5 | + before an interrupt occurs. This example does not require the interrupt pin on |
| 6 | + the product to be connected a pin on your micro-controller. |
| 7 | + |
| 8 | + SparkFun Electronics |
| 9 | + Author: Elias Santistevan |
| 10 | + Date: July 2019 |
| 11 | +
|
| 12 | + License: This code is public domain but if you use this and we meet someday, get me a beer! |
| 13 | +
|
| 14 | + Feel like supporting our work? Buy a board from Sparkfun! |
| 15 | + https://www.sparkfun.com/products/15436 |
| 16 | +
|
| 17 | +*/ |
| 18 | + |
| 19 | +#include <Wire.h> |
| 20 | +#include "SparkFun_VEML6030_Ambient_Light_Sensor.h" |
| 21 | + |
| 22 | +// Close the address jumper on the product for addres 0x10. |
| 23 | +#define AL_ADDR 0x48 |
| 24 | + |
| 25 | +SparkFun_Ambient_Light light(AL_ADDR); |
| 26 | + |
| 27 | +// Possible values: .125, .25, 1, 2 |
| 28 | +// Both .125 and .25 should be used in most cases except darker rooms. |
| 29 | +// A gain of 2 should only be used if the sensor will be covered by a dark |
| 30 | +// glass. |
| 31 | +float gain = .125; |
| 32 | + |
| 33 | +// Possible integration times in milliseconds: 800, 400, 200, 100, 50, 25 |
| 34 | +// Higher times give higher resolutions and should be used in darker light. |
| 35 | +int time = 100; |
| 36 | +long luxVal = 0; |
| 37 | + |
| 38 | + |
| 39 | +// Interrupt settings. |
| 40 | +long lowThresh = 20; |
| 41 | +long highThresh = 400; |
| 42 | +int numbValues = 1; |
| 43 | + |
| 44 | +// Interrupt variable |
| 45 | +int interrupt; |
| 46 | + |
| 47 | +void setup(){ |
| 48 | + |
| 49 | + Wire.begin(); |
| 50 | + Serial.begin(115200); |
| 51 | + |
| 52 | + if(light.begin()) |
| 53 | + Serial.println("Ready to sense some light!"); |
| 54 | + else |
| 55 | + Serial.println("Could not communicate with the sensor!"); |
| 56 | + |
| 57 | + // Again the gain and integration times determine the resolution of the lux |
| 58 | + // value, and give different ranges of possible light readings. Check out |
| 59 | + // hoookup guide for more info. The gain/integration time also affects |
| 60 | + // interrupt threshold settings so ALWAYS set gain and time first. |
| 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 | + // Set both low and high thresholds, they take values in lux. |
| 73 | + light.setIntLowThresh(lowThresh); |
| 74 | + light.setIntHighThresh(highThresh); |
| 75 | + // Let's check that they were set correctly. |
| 76 | + // There are some rounding issues inherently in the IC's design so your lux |
| 77 | + // may be one off. |
| 78 | + Serial.print("Low Threshold: "); |
| 79 | + long lowVal = light.readLowThresh(); |
| 80 | + Serial.print(lowVal); |
| 81 | + Serial.print(" High Threshold: "); |
| 82 | + long highVal = light.readHighThresh(); |
| 83 | + Serial.println(highVal); |
| 84 | + |
| 85 | + // This setting modifies the number of times a value has to fall below or |
| 86 | + // above the threshold before the interrupt fires! Values include 1, 2, 4 and |
| 87 | + // 8. |
| 88 | + light.setProtect(numbValues); |
| 89 | + Serial.print("Number of values that must fall below/above threshold before interupt occurrs: "); |
| 90 | + int protectVal = light.readProtect(); |
| 91 | + Serial.println(protectVal); |
| 92 | + |
| 93 | + // Now we enable the interrupt, now that he thresholds are set. |
| 94 | + light.enableInt(); |
| 95 | + Serial.print("Is interrupt enabled: "); |
| 96 | + int intVal = light.readIntSetting(); |
| 97 | + if (intVal == 1) |
| 98 | + Serial.println("Yes"); |
| 99 | + else |
| 100 | + Serial.println("No"); |
| 101 | + |
| 102 | + |
| 103 | + Serial.println("-------------------------------------------------"); |
| 104 | + |
| 105 | + // Give some time to read our settings on startup. |
| 106 | + delay(3000); |
| 107 | +} |
| 108 | + |
| 109 | +void loop(){ |
| 110 | + |
| 111 | + luxVal = light.readLight(); |
| 112 | + Serial.print("Ambient Light Reading: "); |
| 113 | + Serial.print(luxVal); |
| 114 | + Serial.println(" Lux"); |
| 115 | + |
| 116 | + |
| 117 | + interrupt = light.readInterrupt(); |
| 118 | + if (interrupt == 1) |
| 119 | + Serial.println("High threshold crossed!"); |
| 120 | + else if (interrupt == 2) |
| 121 | + Serial.println("Low threshold crossed!"); |
| 122 | + |
| 123 | + delay(200); |
| 124 | +} |
0 commit comments