4_light sensor

  1. 下载驱动库

搜索 LTR308_library库并安装。

../../../../_images/LTR308.png

  1. 示例代码

本示例会使用LTR308_library库获取数字光线传感器数据。在arduino IDE中,点击「File」→「Examples」→「LTR308_library」→「LTR308example」打开示例。

示例-LTR308.ino
  1/* 
  2	LTR308-ALS library example sketch
  3*/
  4
  5#include <LTR308.h>
  6#include <Wire.h>
  7
  8LTR308 light;
  9
 10unsigned char gain = 0;     // Gain setting, values = 0-4 
 11unsigned char integrationTime = 0;  // Integration ("shutter") time, values 0 - 4
 12unsigned char measurementRate = 3;  // Interval between DATA_REGISTERS update, values 0 - 7, except 4
 13unsigned char ID;
 14unsigned char control;
 15
 16void setup() {
 17  Serial.begin(9600);
 18  Serial.println();
 19  Serial.println("LTR-308ALS example sketch");
 20  delay(100);
 21
 22  light.begin();
 23
 24  if (light.getPartID(ID)) {
 25    Serial.print("Got Sensor Part ID: 0X");
 26    Serial.print(ID, HEX);
 27    Serial.println();
 28  }
 29  else {
 30    byte error = light.getError();
 31    printError(error);
 32  }
 33  
 34  if (light.setPowerUp()) {
 35    Serial.print("Powering up...");
 36    Serial.println();
 37  }
 38  else {
 39    byte error = light.getError();
 40    printError(error);
 41  }
 42  delay(10);
 43
 44  if (light.getPower(control)) {
 45    Serial.print("Control byte is: 0X");
 46    Serial.print(control, HEX);
 47    Serial.println();
 48  }
 49  else {
 50    byte error = light.getError();
 51    printError(error);
 52  }
 53  
 54  Serial.println("Setting Gain...");
 55  
 56  if (light.setGain(gain)) {
 57    light.getGain(gain);
 58    
 59    Serial.print("Gain Set to 0X");
 60    Serial.print(gain, HEX);
 61    Serial.println();
 62  }
 63  else {
 64    byte error = light.getError();
 65    printError(error);
 66  }
 67
 68  Serial.println("Set timing...");
 69  if (light.setMeasurementRate(integrationTime, measurementRate)) {
 70    light.getMeasurementRate(integrationTime, measurementRate);
 71    
 72    Serial.print("Timing Set to ");
 73    Serial.print(integrationTime, HEX);
 74    Serial.println();
 75
 76    Serial.print("Meas Rate Set to ");
 77    Serial.print(measurementRate, HEX);
 78    Serial.println();
 79  }
 80  else {
 81    byte error = light.getError();
 82    printError(error);
 83  }
 84 
 85}
 86
 87void loop() {
 88  int ms = 1000;
 89  
 90  delay(ms);
 91  unsigned long rawData;
 92  
 93  if (light.getData(rawData)) {
 94    
 95    Serial.print("Raw Data: ");
 96    Serial.println(rawData);
 97  
 98    double lux;    // Resulting lux value
 99    boolean good;  // True if sensor is not saturated
100
101    good = light.getLux(gain, integrationTime, rawData, lux);
102	
103    Serial.print("Lux: ");
104    Serial.print(lux);
105    if (good) Serial.println(" (valid data)"); 
106    else Serial.println(" (BAD)");
107  }
108  else {
109    byte error = light.getError();
110    printError(error);
111  }
112}
113
114void printError(byte error) {
115  Serial.print("I2C error: ");
116  Serial.print(error,DEC);
117  Serial.print(", ");
118  
119  switch(error) {
120    case 0:
121      Serial.println("success");
122      break;
123    case 1:
124      Serial.println("data too long for transmit buffer");
125      break;
126    case 2:
127      Serial.println("received NACK on address (disconnected?)");
128      break;
129    case 3:
130      Serial.println("received NACK on data");
131      break;
132    case 4:
133      Serial.println("other error");
134      break;
135    default:
136      Serial.println("unknown error");
137  }
138}

  1. 运行效果

串口打印数字光线采集值。