-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmicrobit and opt3001.txt
121 lines (90 loc) · 3.02 KB
/
microbit and opt3001.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//This example uses the following library https://github.com/closedcube/ClosedCube_OPT3001_Arduino
/*
This is example for ClosedCube OPT3001 Digital Ambient Light Sensor breakout board
Initial Date: 02-Dec-2015
Hardware connections for Arduino Uno:
VDD to 3.3V DC
SDA to A4
SCL to A5
GND to common ground
Written by AA for ClosedCube
MIT License
*/
#include <Wire.h>
#include <ClosedCube_OPT3001.h>
ClosedCube_OPT3001 opt3001;
#define OPT3001_ADDRESS 0x44
void setup()
{
Serial.begin(9600);
Serial.println("ClosedCube OPT3001 Arduino Test");
opt3001.begin(OPT3001_ADDRESS);
Serial.print("OPT3001 Manufacturer ID");
Serial.println(opt3001.readManufacturerID());
Serial.print("OPT3001 Device ID");
Serial.println(opt3001.readDeviceID());
configureSensor();
printResult("High-Limit", opt3001.readHighLimit());
printResult("Low-Limit", opt3001.readLowLimit());
Serial.println("----");
}
void loop()
{
OPT3001 result = opt3001.readResult();
printResult("OPT3001", result);
delay(500);
}
void configureSensor() {
OPT3001_Config newConfig;
newConfig.RangeNumber = B1100;
newConfig.ConvertionTime = B0;
newConfig.Latch = B1;
newConfig.ModeOfConversionOperation = B11;
OPT3001_ErrorCode errorConfig = opt3001.writeConfig(newConfig);
if (errorConfig != NO_ERROR)
printError("OPT3001 configuration", errorConfig);
else {
OPT3001_Config sensorConfig = opt3001.readConfig();
Serial.println("OPT3001 Current Config:");
Serial.println("------------------------------");
Serial.print("Conversion ready (R):");
Serial.println(sensorConfig.ConversionReady,HEX);
Serial.print("Conversion time (R/W):");
Serial.println(sensorConfig.ConvertionTime, HEX);
Serial.print("Fault count field (R/W):");
Serial.println(sensorConfig.FaultCount, HEX);
Serial.print("Flag high field (R-only):");
Serial.println(sensorConfig.FlagHigh, HEX);
Serial.print("Flag low field (R-only):");
Serial.println(sensorConfig.FlagLow, HEX);
Serial.print("Latch field (R/W):");
Serial.println(sensorConfig.Latch, HEX);
Serial.print("Mask exponent field (R/W):");
Serial.println(sensorConfig.MaskExponent, HEX);
Serial.print("Mode of conversion operation (R/W):");
Serial.println(sensorConfig.ModeOfConversionOperation, HEX);
Serial.print("Polarity field (R/W):");
Serial.println(sensorConfig.Polarity, HEX);
Serial.print("Overflow flag (R-only):");
Serial.println(sensorConfig.OverflowFlag, HEX);
Serial.print("Range number (R/W):");
Serial.println(sensorConfig.RangeNumber, HEX);
Serial.println("------------------------------");
}
}
void printResult(String text, OPT3001 result) {
if (result.error == NO_ERROR) {
Serial.print(text);
Serial.print(": ");
Serial.print(result.lux);
Serial.println(" lux");
}
else {
printError(text,result.error);
}
}
void printError(String text, OPT3001_ErrorCode error) {
Serial.print(text);
Serial.print(": [ERROR] Code #");
Serial.println(error);
}