Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 179 lines (141 sloc) 4.302 kb
445f1f4 @ladyada added delay
ladyada authored
1 // Example sketch for talking to the VCNL4000 i2c proximity/light sensor
2 // Written by Adafruit! Public domain.
3 // To use: Connect VCC to 3.3-5V (5V is best if it is available), GND to
4 // ground, SCL to i2c clock (on classic arduinos, Analog 5), SDA
5 // to i2c data (on classic arduinos Analog 4). The 3.3v pin is
6 // an ouptut if you need 3.3V
7 // This sensor is 5V compliant so you can use it with 3.3 or 5V micros
8
9 // You can pick one up at the Adafruit shop: www.adafruit.com/products/466
10
11
12
1e054b2 @ladyada Initial commit - reads proximity and ambient light
ladyada authored
13 #include <Wire.h>
14
445f1f4 @ladyada added delay
ladyada authored
15 // the i2c address
1e054b2 @ladyada Initial commit - reads proximity and ambient light
ladyada authored
16 #define VCNL4000_ADDRESS 0x13
17
445f1f4 @ladyada added delay
ladyada authored
18 // commands and constants
1e054b2 @ladyada Initial commit - reads proximity and ambient light
ladyada authored
19 #define VCNL4000_COMMAND 0x80
20 #define VCNL4000_PRODUCTID 0x81
21 #define VCNL4000_IRLED 0x83
22 #define VCNL4000_AMBIENTPARAMETER 0x84
23 #define VCNL4000_AMBIENTDATA 0x85
24 #define VCNL4000_PROXIMITYDATA 0x87
25 #define VCNL4000_SIGNALFREQ 0x89
26 #define VCNL4000_PROXINITYADJUST 0x8A
27
28 #define VCNL4000_3M125 0
29 #define VCNL4000_1M5625 1
30 #define VCNL4000_781K25 2
31 #define VCNL4000_390K625 3
32
33 #define VCNL4000_MEASUREAMBIENT 0x10
34 #define VCNL4000_MEASUREPROXIMITY 0x08
35 #define VCNL4000_AMBIENTREADY 0x40
36 #define VCNL4000_PROXIMITYREADY 0x20
37
38 void setup() {
39 Serial.begin(9600);
40
41 Serial.println("VCNL");
42 Wire.begin();
43
44 uint8_t rev = read8(VCNL4000_PRODUCTID);
45
46 if ((rev & 0xF0) != 0x10) {
47 Serial.println("Sensor not found :(");
48 while (1);
49 }
50
51
52 write8(VCNL4000_IRLED, 20); // set to 20 * 10mA = 200mA
53 Serial.print("IR LED current = ");
54 Serial.print(read8(VCNL4000_IRLED) * 10, DEC);
55 Serial.println(" mA");
56
57 //write8(VCNL4000_SIGNALFREQ, 3);
58 Serial.print("Proximity measurement frequency = ");
59 uint8_t freq = read8(VCNL4000_SIGNALFREQ);
60 if (freq == VCNL4000_3M125) Serial.println("3.125 MHz");
61 if (freq == VCNL4000_1M5625) Serial.println("1.5625 MHz");
62 if (freq == VCNL4000_781K25) Serial.println("781.25 KHz");
63 if (freq == VCNL4000_390K625) Serial.println("390.625 KHz");
64
65 write8(VCNL4000_PROXINITYADJUST, 0x81);
66 Serial.print("Proximity adjustment register = ");
67 Serial.println(read8(VCNL4000_PROXINITYADJUST), HEX);
68
69 // arrange for continuous conversion
70 //write8(VCNL4000_AMBIENTPARAMETER, 0x89);
71
72 }
73
74 uint16_t readProximity() {
75 write8(VCNL4000_COMMAND, VCNL4000_MEASUREPROXIMITY);
76 while (1) {
77 uint8_t result = read8(VCNL4000_COMMAND);
78 //Serial.print("Ready = 0x"); Serial.println(result, HEX);
79 if (result & VCNL4000_PROXIMITYREADY) {
80 return read16(VCNL4000_PROXIMITYDATA);
81 }
82 delay(1);
83 }
84 }
85
86
87
88 void loop() {
89
90 // read ambient light!
91 write8(VCNL4000_COMMAND, VCNL4000_MEASUREAMBIENT | VCNL4000_MEASUREPROXIMITY);
92
93 while (1) {
94 uint8_t result = read8(VCNL4000_COMMAND);
95 //Serial.print("Ready = 0x"); Serial.println(result, HEX);
96 if ((result & VCNL4000_AMBIENTREADY)&&(result & VCNL4000_PROXIMITYREADY)) {
97
98 Serial.print("Ambient = ");
99 Serial.print(read16(VCNL4000_AMBIENTDATA));
100 Serial.print("\t\tProximity = ");
101 Serial.println(read16(VCNL4000_PROXIMITYDATA));
102 break;
103 }
104 delay(10);
105 }
106
107 delay(100);
108 }
109
f695050 @ladyada added header
ladyada authored
110 // Read 1 byte from the VCNL4000 at 'address'
1e054b2 @ladyada Initial commit - reads proximity and ambient light
ladyada authored
111 uint8_t read8(uint8_t address)
112 {
113 uint8_t data;
114
115 Wire.beginTransmission(VCNL4000_ADDRESS);
7eca389 @PaintYourDragon Arduino 1.0 compliant
PaintYourDragon authored
116 #if ARDUINO >= 100
117 Wire.write(address);
118 #else
1e054b2 @ladyada Initial commit - reads proximity and ambient light
ladyada authored
119 Wire.send(address);
7eca389 @PaintYourDragon Arduino 1.0 compliant
PaintYourDragon authored
120 #endif
1e054b2 @ladyada Initial commit - reads proximity and ambient light
ladyada authored
121 Wire.endTransmission();
122
445f1f4 @ladyada added delay
ladyada authored
123 delayMicroseconds(170); // delay required
124
1e054b2 @ladyada Initial commit - reads proximity and ambient light
ladyada authored
125 Wire.requestFrom(VCNL4000_ADDRESS, 1);
126 while(!Wire.available());
127
7eca389 @PaintYourDragon Arduino 1.0 compliant
PaintYourDragon authored
128 #if ARDUINO >= 100
129 return Wire.read();
130 #else
1e054b2 @ladyada Initial commit - reads proximity and ambient light
ladyada authored
131 return Wire.receive();
7eca389 @PaintYourDragon Arduino 1.0 compliant
PaintYourDragon authored
132 #endif
1e054b2 @ladyada Initial commit - reads proximity and ambient light
ladyada authored
133 }
134
135
f695050 @ladyada added header
ladyada authored
136 // Read 2 byte from the VCNL4000 at 'address'
1e054b2 @ladyada Initial commit - reads proximity and ambient light
ladyada authored
137 uint16_t read16(uint8_t address)
138 {
139 uint16_t data;
140
141 Wire.beginTransmission(VCNL4000_ADDRESS);
7eca389 @PaintYourDragon Arduino 1.0 compliant
PaintYourDragon authored
142 #if ARDUINO >= 100
143 Wire.write(address);
144 #else
1e054b2 @ladyada Initial commit - reads proximity and ambient light
ladyada authored
145 Wire.send(address);
7eca389 @PaintYourDragon Arduino 1.0 compliant
PaintYourDragon authored
146 #endif
1e054b2 @ladyada Initial commit - reads proximity and ambient light
ladyada authored
147 Wire.endTransmission();
148
149 Wire.requestFrom(VCNL4000_ADDRESS, 2);
150 while(!Wire.available());
7eca389 @PaintYourDragon Arduino 1.0 compliant
PaintYourDragon authored
151 #if ARDUINO >= 100
152 data = Wire.read();
153 data <<= 8;
154 while(!Wire.available());
155 data |= Wire.read();
156 #else
1e054b2 @ladyada Initial commit - reads proximity and ambient light
ladyada authored
157 data = Wire.receive();
158 data <<= 8;
159 while(!Wire.available());
160 data |= Wire.receive();
7eca389 @PaintYourDragon Arduino 1.0 compliant
PaintYourDragon authored
161 #endif
1e054b2 @ladyada Initial commit - reads proximity and ambient light
ladyada authored
162
163 return data;
164 }
165
166 // write 1 byte
167 void write8(uint8_t address, uint8_t data)
168 {
169 Wire.beginTransmission(VCNL4000_ADDRESS);
7eca389 @PaintYourDragon Arduino 1.0 compliant
PaintYourDragon authored
170 #if ARDUINO >= 100
171 Wire.write(address);
172 Wire.write(data);
173 #else
1e054b2 @ladyada Initial commit - reads proximity and ambient light
ladyada authored
174 Wire.send(address);
175 Wire.send(data);
7eca389 @PaintYourDragon Arduino 1.0 compliant
PaintYourDragon authored
176 #endif
1e054b2 @ladyada Initial commit - reads proximity and ambient light
ladyada authored
177 Wire.endTransmission();
178 }
Something went wrong with that request. Please try again.