This repository has been archived by the owner on Jul 8, 2024. It is now read-only.
forked from adafruit/Adafruit-VCNL40X0-PCB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcnl4000.pde
178 lines (141 loc) · 4.2 KB
/
vcnl4000.pde
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Example sketch for talking to the VCNL4000 i2c proximity/light sensor
// Written by Adafruit! Public domain.
// To use: Connect VCC to 3.3-5V (5V is best if it is available), GND to
// ground, SCL to i2c clock (on classic arduinos, Analog 5), SDA
// to i2c data (on classic arduinos Analog 4). The 3.3v pin is
// an ouptut if you need 3.3V
// This sensor is 5V compliant so you can use it with 3.3 or 5V micros
// You can pick one up at the Adafruit shop: www.adafruit.com/products/466
#include <Wire.h>
// the i2c address
#define VCNL4000_ADDRESS 0x13
// commands and constants
#define VCNL4000_COMMAND 0x80
#define VCNL4000_PRODUCTID 0x81
#define VCNL4000_IRLED 0x83
#define VCNL4000_AMBIENTPARAMETER 0x84
#define VCNL4000_AMBIENTDATA 0x85
#define VCNL4000_PROXIMITYDATA 0x87
#define VCNL4000_SIGNALFREQ 0x89
#define VCNL4000_PROXINITYADJUST 0x8A
#define VCNL4000_3M125 0
#define VCNL4000_1M5625 1
#define VCNL4000_781K25 2
#define VCNL4000_390K625 3
#define VCNL4000_MEASUREAMBIENT 0x10
#define VCNL4000_MEASUREPROXIMITY 0x08
#define VCNL4000_AMBIENTREADY 0x40
#define VCNL4000_PROXIMITYREADY 0x20
void setup() {
Serial.begin(9600);
Serial.println("VCNL");
Wire.begin();
uint8_t rev = read8(VCNL4000_PRODUCTID);
if ((rev & 0xF0) != 0x10) {
Serial.println("Sensor not found :(");
while (1);
}
write8(VCNL4000_IRLED, 20); // set to 20 * 10mA = 200mA
Serial.print("IR LED current = ");
Serial.print(read8(VCNL4000_IRLED) * 10, DEC);
Serial.println(" mA");
//write8(VCNL4000_SIGNALFREQ, 3);
Serial.print("Proximity measurement frequency = ");
uint8_t freq = read8(VCNL4000_SIGNALFREQ);
if (freq == VCNL4000_3M125) Serial.println("3.125 MHz");
if (freq == VCNL4000_1M5625) Serial.println("1.5625 MHz");
if (freq == VCNL4000_781K25) Serial.println("781.25 KHz");
if (freq == VCNL4000_390K625) Serial.println("390.625 KHz");
write8(VCNL4000_PROXINITYADJUST, 0x81);
Serial.print("Proximity adjustment register = ");
Serial.println(read8(VCNL4000_PROXINITYADJUST), HEX);
// arrange for continuous conversion
//write8(VCNL4000_AMBIENTPARAMETER, 0x89);
}
uint16_t readProximity() {
write8(VCNL4000_COMMAND, VCNL4000_MEASUREPROXIMITY);
while (1) {
uint8_t result = read8(VCNL4000_COMMAND);
//Serial.print("Ready = 0x"); Serial.println(result, HEX);
if (result & VCNL4000_PROXIMITYREADY) {
return read16(VCNL4000_PROXIMITYDATA);
}
delay(1);
}
}
void loop() {
// read ambient light!
write8(VCNL4000_COMMAND, VCNL4000_MEASUREAMBIENT | VCNL4000_MEASUREPROXIMITY);
while (1) {
uint8_t result = read8(VCNL4000_COMMAND);
//Serial.print("Ready = 0x"); Serial.println(result, HEX);
if ((result & VCNL4000_AMBIENTREADY)&&(result & VCNL4000_PROXIMITYREADY)) {
Serial.print("Ambient = ");
Serial.print(read16(VCNL4000_AMBIENTDATA));
Serial.print("\t\tProximity = ");
Serial.println(read16(VCNL4000_PROXIMITYDATA));
break;
}
delay(10);
}
delay(100);
}
// Read 1 byte from the VCNL4000 at 'address'
uint8_t read8(uint8_t address)
{
uint8_t data;
Wire.beginTransmission(VCNL4000_ADDRESS);
#if ARDUINO >= 100
Wire.write(address);
#else
Wire.send(address);
#endif
Wire.endTransmission();
delayMicroseconds(170); // delay required
Wire.requestFrom(VCNL4000_ADDRESS, 1);
while(!Wire.available());
#if ARDUINO >= 100
return Wire.read();
#else
return Wire.receive();
#endif
}
// Read 2 byte from the VCNL4000 at 'address'
uint16_t read16(uint8_t address)
{
uint16_t data;
Wire.beginTransmission(VCNL4000_ADDRESS);
#if ARDUINO >= 100
Wire.write(address);
#else
Wire.send(address);
#endif
Wire.endTransmission();
Wire.requestFrom(VCNL4000_ADDRESS, 2);
while(!Wire.available());
#if ARDUINO >= 100
data = Wire.read();
data <<= 8;
while(!Wire.available());
data |= Wire.read();
#else
data = Wire.receive();
data <<= 8;
while(!Wire.available());
data |= Wire.receive();
#endif
return data;
}
// write 1 byte
void write8(uint8_t address, uint8_t data)
{
Wire.beginTransmission(VCNL4000_ADDRESS);
#if ARDUINO >= 100
Wire.write(address);
Wire.write(data);
#else
Wire.send(address);
Wire.send(data);
#endif
Wire.endTransmission();
}