{"id":148,"date":"2013-08-19T15:30:10","date_gmt":"2013-08-19T15:30:10","guid":{"rendered":"http:\/\/blog.soton.ac.uk\/pi\/?page_id=148"},"modified":"2013-08-23T16:42:49","modified_gmt":"2013-08-23T16:42:49","slug":"adafruit-proximity-and-ambient-light-sensor","status":"publish","type":"page","link":"https:\/\/blog.soton.ac.uk\/pi\/adafruit-proximity-and-ambient-light-sensor\/","title":{"rendered":"Adafruit Proximity and Ambient Light Sensor"},"content":{"rendered":"<p><!--\nH3 { margin-bottom: 0.21cm; }H3.ctl { font-family: \"Lohit Hindi\"; }PRE.ctl { font-family: \"Lohit Hindi\",monospace; }P { margin-bottom: 0.21cm; }A:link {  }\n--><\/p>\n<p align=\"LEFT\"><span style=\"font-family: Times New Roman,serif\"><b>Adafruit VCNL4000 Proximity and Light Ambient sensor<\/b><\/span><\/p>\n<p>The VCNL4000 looks like it is a single piece of silicon, but it really just is an infrared transmitter and receiver in a package that looks like a chip. The way it works is that it shines a beam of IR light from an LED, and measures the intensity of light that is bounced pack in order to determine the distance to the object in front of it. It also incorporates an ambient light sensor. Also, the VCNL4000 does not have a simple analog output, but instead outputs a 16bit digital reading.<\/p>\n<p>The VCNL4000 data sheet claims a proximity sensing range of 20cm. In reality, a reasonable change is only seen from few centimeters apart. Also note that the sensing is not linear so the closer you get the more rapidly the reading increases. The reading varies between 0 and 65535 (corresponding to a 16bit digital reading, 2^(16) = 65535).<\/p>\n<p>&nbsp;<\/p>\n<h3 class=\"western\">Wiring it up<\/h3>\n<p>The VCNL4000 is an I2C device and will be connected somehow similar to the TMP102 Temperature Sensor (see <a href=\"http:\/\/blog.soton.ac.uk\/pi\/tmp102-temperature-sensor-breakout-board\/\">http:\/\/blog.soton.ac.uk\/pi\/tmp102-temperature-sensor-breakout-board\/<\/a>), using the Gertboard instead of the Arduino. Again connections need to be done for Atmega programming and for Serial Interfacing (see link above). The 5 connections from the Gertboard to the VCNL4000 Breakout Board are: <b>PC4 <\/b><span style=\"font-weight: normal\">on <\/span><b>J25 <\/b><span style=\"font-weight: normal\">to <\/span><b>SDA, PC5 <\/b><span style=\"font-weight: normal\">on <\/span><b>J25 <\/b><span style=\"font-weight: normal\">to <\/span><b>SCL, GND <\/b><span style=\"font-weight: normal\">on <\/span><b>J25 <\/b><span style=\"font-weight: normal\">to <\/span><b>GND <\/b><span style=\"font-weight: normal\">and <\/span><b>3v3 <\/b><span style=\"font-weight: normal\">on the Gertboard connected to both <\/span><b>3v3 <\/b><span style=\"font-weight: normal\">and <\/span><b>Vin <\/b><span style=\"font-weight: normal\">on the Breakout Board.<\/span><\/p>\n<p><span style=\"font-weight: normal\">Once all these connections have been done, copy the following code into the Arduino IDE and upload it onto the Atmega. The code is written by Adafruit and makes use of both the proximity and the light ambient sensors. <\/span><span style=\"font-weight: normal\">Once the code has been uploaded successfully, open up a terminal and type <\/span><b>sudo minicom ama0 <\/b><span style=\"font-weight: normal\">to visualise the readings. You can see they update once every second. You can change the speed at which they update by changing the value inside the <\/span><b>delay() <\/b><span style=\"font-weight: normal\">from inside the <\/span><b>loop() <\/b><span style=\"font-weight: normal\">function. <\/span><\/p>\n<p><span style=\"font-weight: normal\">Have a bit of play with the sensor in order to see it`s working correctly. Put an object or your hand in front of the sensor and see how the <\/span><b>Proximity <\/b><span style=\"font-weight: normal\">variable increases as you get closer. Also test the ambient light sensor by putting a source of light in front of the sensor, exposing it to direct sun or by placing in front of it an object which restricts light and see how the <\/span><b>Ambient <\/b><span style=\"font-weight: normal\">variable modifies<\/span><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"western\">#include &lt;Wire.h&gt;<\/pre>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC15\">\n<pre class=\"western\">\/\/ the i2c address<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC16\">\n<pre class=\"western\">#define VCNL4000_ADDRESS 0x13<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC17\"><\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC18\">\n<pre class=\"western\">\/\/ commands and constants<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"Section2\">\n<pre class=\"western\"><a name=\"LC19\"><\/a>#define VCNL4000_COMMAND 0x80<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"Section3\">\n<pre class=\"western\"><a name=\"LC20\"><\/a>#define VCNL4000_PRODUCTID 0x81<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"Section4\">\n<pre class=\"western\"><a name=\"LC21\"><\/a>#define VCNL4000_IRLED 0x83<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"Section5\">\n<pre class=\"western\"><a name=\"LC22\"><\/a>#define VCNL4000_AMBIENTPARAMETER 0x84<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"Section6\">\n<pre class=\"western\"><a name=\"LC23\"><\/a>#define VCNL4000_AMBIENTDATA 0x85<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"Section7\">\n<pre class=\"western\"><a name=\"LC24\"><\/a>#define VCNL4000_PROXIMITYDATA 0x87<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"Section8\">\n<pre class=\"western\"><a name=\"LC25\"><\/a>#define VCNL4000_SIGNALFREQ 0x89<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC26\">\n<pre class=\"western\">#define VCNL4000_PROXINITYADJUST 0x8A<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC27\"><\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC28\">\n<pre class=\"western\">#define VCNL4000_3M125 0<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC29\">\n<pre class=\"western\">#define VCNL4000_1M5625 1<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC30\">\n<pre class=\"western\">#define VCNL4000_781K25 2<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC31\">\n<pre class=\"western\">#define VCNL4000_390K625 3<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC32\"><\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC33\">\n<pre class=\"western\">#define VCNL4000_MEASUREAMBIENT 0x10<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"Section9\">\n<pre class=\"western\"><a name=\"LC34\"><\/a>#define VCNL4000_MEASUREPROXIMITY 0x08<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"Section10\">\n<pre class=\"western\"><a name=\"LC35\"><\/a>#define VCNL4000_AMBIENTREADY 0x40<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"Section11\">\n<pre class=\"western\"><a name=\"LC36\"><\/a>#define VCNL4000_PROXIMITYREADY 0x20<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"Section12\">\n<pre class=\"western\"><a name=\"LC37\"><\/a><\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"Section13\">\n<pre class=\"western\"><a name=\"LC38\"><\/a>void setup() {<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"Section14\">\n<pre class=\"western\"><a name=\"LC39\"><\/a>\u00a0\u00a0Serial.begin(9600);<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"Section15\">\n<pre class=\"western\"><a name=\"LC40\"><\/a><\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"Section16\">\n<pre class=\"western\"><a name=\"LC41\"><\/a>\u00a0\u00a0Serial.println(\"VCNL\");<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"Section17\">\n<pre class=\"western\"><a name=\"LC42\"><\/a>\u00a0\u00a0Wire.begin();<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"Section18\">\n<pre class=\"western\"><a name=\"LC43\"><\/a><\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"Section19\">\n<pre class=\"western\"><a name=\"LC44\"><\/a>\u00a0\u00a0uint8_t rev = read8(VCNL4000_PRODUCTID);<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"Section20\">\n<pre class=\"western\"><a name=\"LC45\"><\/a><\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"Section21\">\n<pre class=\"western\"><a name=\"LC46\"><\/a>\u00a0\u00a0if ((rev &amp; 0xF0) != 0x10) {<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"Section22\">\n<pre class=\"western\"><a name=\"LC47\"><\/a>\u00a0\u00a0\u00a0\u00a0Serial.println(\"Sensor not found :(\");<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"Section23\">\n<pre class=\"western\"><a name=\"LC48\"><\/a>\u00a0\u00a0\u00a0\u00a0while (1);<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"Section24\">\n<pre class=\"western\"><a name=\"LC49\"><\/a>\u00a0\u00a0}<\/pre>\n<p><a name=\"LC50\"><\/a><\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"Section26\">\n<pre class=\"western\"><a name=\"LC51\"><\/a><\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"Section27\">\n<pre class=\"western\"><a name=\"LC52\"><\/a>\u00a0\u00a0write8(VCNL4000_IRLED, 20);        \/\/ set to 20 * 10mA = 200mA<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"Section28\">\n<pre class=\"western\"><a name=\"LC53\"><\/a>\u00a0\u00a0Serial.print(\"IR LED current = \");<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"Section29\">\n<pre class=\"western\"><a name=\"LC54\"><\/a>\u00a0\u00a0Serial.print(read8(VCNL4000_IRLED) * 10, DEC);<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC55\">\n<pre class=\"western\">\u00a0\u00a0Serial.println(\" mA\");<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC56\"><\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC57\">\n<pre class=\"western\">\u00a0\u00a0\/\/write8(VCNL4000_SIGNALFREQ, 3);<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC58\">\n<pre class=\"western\">\u00a0\u00a0Serial.print(\"Proximity measurement frequency = \");<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC59\">\n<pre class=\"western\">\u00a0\u00a0uint8_t freq = read8(VCNL4000_SIGNALFREQ);<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC60\">\n<pre class=\"western\">\u00a0\u00a0if (freq == VCNL4000_3M125) Serial.println(\"3.125 MHz\");<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC61\">\n<pre class=\"western\">\u00a0\u00a0if (freq == VCNL4000_1M5625) Serial.pr<\/pre>\n<div dir=\"LTR\" id=\"LC16\">\n<pre class=\"western\">#define VCNL4000_ADDRESS 0x13<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC17\"><\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC18\">\n<pre class=\"western\">\/\/ commands and c<\/pre>\n<\/div>\n<pre class=\"western\">intln(\"1.5625 MHz\");<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC62\">\n<pre class=\"western\">\u00a0\u00a0if (freq == VCNL4000_781K25) Serial.println(\"781.25 KHz\");<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC63\">\n<pre class=\"western\">\u00a0\u00a0if (freq == VCNL4000_390K625) Serial.println(\"390.625 KHz\");<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC64\"><\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC65\">\n<pre class=\"western\">\u00a0\u00a0write8(VCNL4000_PROXINITYADJUST, 0x81);<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC66\">\n<pre class=\"western\">\u00a0\u00a0Serial.print(\"Proximity adjustment register = \");<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC67\">\n<pre class=\"western\">\u00a0\u00a0Serial.println(read8(VCNL4000_PROXINITYADJUST), HEX);<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC68\"><\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC69\">\n<pre class=\"western\">\u00a0\u00a0\/\/ arrange for continuous conversion<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC70\">\n<pre class=\"western\">\u00a0\u00a0\/\/write8(VCNL4000_AMBIENTPARAMETER, 0x89);<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC71\"><\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC72\">\n<pre class=\"western\">}<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC73\"><\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC74\">\n<pre class=\"western\">uint16_t readProximity() {<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC75\">\n<pre class=\"western\">\u00a0\u00a0write8(VCNL4000_COMMAND, VCNL4000_MEASUREPROXIMITY);<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC76\">\n<pre class=\"western\">\u00a0\u00a0while (1) {<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC77\">\n<pre class=\"western\">\u00a0\u00a0\u00a0\u00a0uint8_t result = read8(VCNL4000_COMMAND);<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC78\">\n<pre class=\"western\">\u00a0\u00a0\u00a0\u00a0\/\/Serial.print(\"Ready = 0x\"); Serial.println(result, HEX);<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC79\">\n<pre class=\"western\">\u00a0\u00a0\u00a0\u00a0if (result &amp; VCNL4000_PROXIMITYREADY) {<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC80\">\n<pre class=\"western\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return read16(VCNL4000_PROXIMITYDATA);<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC81\">\n<pre class=\"western\">\u00a0\u00a0\u00a0\u00a0}<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC82\">\n<pre class=\"western\">\u00a0\u00a0\u00a0\u00a0delay(1);<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC83\">\n<pre class=\"western\">\u00a0\u00a0}<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC84\">\n<pre class=\"western\">}<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC85\"><\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC86\"><\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC87\"><\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC88\">\n<pre class=\"western\">void loop() {<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC89\"><\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC90\">\n<pre class=\"western\">\u00a0\u00a0\/\/ read ambient light!<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC91\">\n<pre class=\"western\">\u00a0\u00a0write8(VCNL4000_COMMAND, VCNL4000_MEASUREAMBIENT | VCNL4000_MEASUREPROXIMITY);<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC92\"><\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC93\">\n<pre class=\"western\">\u00a0\u00a0while (1) {<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC94\">\n<pre class=\"western\">\u00a0\u00a0\u00a0\u00a0uint8_t result = read8(VCNL4000_COMMAND);<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC95\">\n<pre class=\"western\">\u00a0\u00a0\u00a0\u00a0\/\/Serial.print(\"Ready = 0x\"); Serial.println(result, HEX);<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC96\">\n<pre class=\"western\">\u00a0\u00a0\u00a0\u00a0if ((result &amp; VCNL4000_AMBIENTREADY)&amp;&amp;(result &amp; VCNL4000_PROXIMITYREADY)) {<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC97\"><\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC98\">\n<pre class=\"western\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Serial.print(\"Ambient = \");<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC99\">\n<pre class=\"western\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Serial.print(read16(VCNL4000_AMBIENTDATA));<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC100\">\n<pre class=\"western\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Serial.print(\"\\t\\tProximity = \");<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC101\">\n<pre class=\"western\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Serial.println(read16(VCNL4000_PROXIMITYDATA));<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC102\">\n<pre class=\"western\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0break;<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC103\">\n<pre class=\"western\">\u00a0\u00a0\u00a0\u00a0}<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC104\">\n<pre class=\"western\">\u00a0\u00a0\u00a0\u00a0delay(10);<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC105\">\n<pre class=\"western\">\u00a0\u00a0}<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC106\"><\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC107\">\n<pre class=\"western\">\u00a0\u00a0\u00a0delay(1000);<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC108\">\n<pre class=\"western\">\u00a0}<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC109\"><\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC110\">\n<pre class=\"western\">\/\/ Read 1 byte from the VCNL4000 at 'address'<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC111\">\n<pre class=\"western\">uint8_t read8(uint8_t address)<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC112\">\n<pre class=\"western\">{<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC113\">\n<pre class=\"western\">\u00a0\u00a0uint8_t data;<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC114\"><\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC115\">\n<pre class=\"western\">\u00a0\u00a0Wire.beginTransmission(VCNL4000_ADDRESS);<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC116\">\n<pre class=\"western\">#if ARDUINO &gt;= 100<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC117\">\n<pre class=\"western\">\u00a0\u00a0Wire.write(address);<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC118\">\n<pre class=\"western\">#else<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC119\">\n<pre class=\"western\">\u00a0\u00a0Wire.send(address);<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC120\">\n<pre class=\"western\">#endif<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC121\">\n<pre class=\"western\">\u00a0\u00a0Wire.endTransmission();<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC122\"><\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC123\">\n<pre class=\"western\">\u00a0\u00a0delayMicroseconds(170);  \/\/ delay required<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC124\"><\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC125\">\n<pre class=\"western\">\u00a0\u00a0Wire.requestFrom(VCNL4000_ADDRESS, 1);<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC126\">\n<pre class=\"western\">\u00a0\u00a0while(!Wire.available());<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC127\"><\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC128\">\n<pre class=\"western\">#if ARDUINO &gt;= 100<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC129\">\n<pre class=\"western\">\u00a0\u00a0return Wire.read();<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC130\">\n<pre class=\"western\">#else<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC131\">\n<pre class=\"western\">\u00a0\u00a0return Wire.receive();<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC132\">\n<pre class=\"western\">#endif<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC133\">\n<pre class=\"western\">}<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC134\"><\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC135\"><\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC136\">\n<pre class=\"western\">\/\/ Read 2 byte from the VCNL4000 at 'address'<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC137\">\n<pre class=\"western\">uint16_t read16(uint8_t address)<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC138\">\n<pre class=\"western\">{<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC139\">\n<pre class=\"western\">\u00a0\u00a0uint16_t data;<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC140\"><\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC141\">\n<pre class=\"western\">\u00a0\u00a0Wire.beginTransmission(VCNL4000_ADDRESS);<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC142\">\n<pre class=\"western\">#if ARDUINO &gt;= 100<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC143\">\n<pre class=\"western\">\u00a0\u00a0Wire.write(address);<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC144\">\n<pre class=\"western\">#else<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC145\">\n<pre class=\"western\">\u00a0\u00a0Wire.send(address);<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC146\">\n<pre class=\"western\">#endif<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC147\">\n<pre class=\"western\">\u00a0\u00a0Wire.endTransmission();<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC148\"><\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC149\">\n<pre class=\"western\">\u00a0\u00a0Wire.requestFrom(VCNL4000_ADDRESS, 2);<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC150\">\n<pre class=\"western\">\u00a0\u00a0while(!Wire.available());<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC151\">\n<pre class=\"western\">#if ARDUINO &gt;= 100<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC152\">\n<pre class=\"western\">\u00a0\u00a0data = Wire.read();<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC153\">\n<pre class=\"western\">\u00a0\u00a0data &lt;&lt;= 8;<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC154\">\n<pre class=\"western\">\u00a0\u00a0while(!Wire.available());<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC155\">\n<pre class=\"western\">\u00a0\u00a0data |= Wire.read();<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC156\">\n<pre class=\"western\">#else<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC157\">\n<pre class=\"western\">\u00a0\u00a0data = Wire.receive();<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC158\">\n<pre class=\"western\">\u00a0\u00a0data &lt;&lt;= 8;<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC159\">\n<pre class=\"western\">\u00a0\u00a0while(!Wire.available());<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC160\">\n<pre class=\"western\">\u00a0\u00a0data |= Wire.receive();<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC161\">\n<pre class=\"western\">#endif<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC162\"><\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC163\">\n<pre class=\"western\">\u00a0\u00a0return data;<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC164\">\n<pre class=\"western\">}<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC165\"><\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC166\">\n<pre class=\"western\">\/\/ write 1 byte<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC167\">\n<pre class=\"western\">void write8(uint8_t address, uint8_t data)<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC168\">\n<pre class=\"western\">{<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC169\">\n<pre class=\"western\">\u00a0\u00a0Wire.beginTransmission(VCNL4000_ADDRESS);<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC170\">\n<pre class=\"western\">#if ARDUINO &gt;= 100<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC171\">\n<pre class=\"western\">\u00a0\u00a0Wire.write(address);<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC172\">\n<pre class=\"western\">\u00a0\u00a0Wire.write(data);<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC173\">\n<pre class=\"western\">#else<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC174\">\n<pre class=\"western\">\u00a0\u00a0Wire.send(address);<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC175\">\n<pre class=\"western\">\u00a0\u00a0Wire.send(data);<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC176\">\n<pre class=\"western\">#endif<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC177\">\n<pre class=\"western\">\u00a0\u00a0Wire.endTransmission();<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div dir=\"LTR\" id=\"LC178\">\n<pre class=\"western\">}<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: Times New Roman,serif\"><b><a href=\"http:\/\/bildr.org\/2012\/11\/vcnl4000-arduino\/\">http:\/\/bildr.org\/2012\/11\/vcnl4000-arduino\/<\/a><\/b><\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"font-weight: normal\"><span style=\"font-family: Times New Roman,serif\">code: https:\/\/github.com\/adafruit\/VCNL4000\/blob\/master\/vcnl4000.pde <\/span><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Adafruit VCNL4000 Proximity and Light Ambient sensor The VCNL4000 looks like it is a single piece of silicon, but it really just is an infrared transmitter and receiver in a package that looks like a chip. The way it works is that it shines a beam of IR light from an LED, and measures the &hellip; <\/p>\n<p><a class=\"more-link block-button\" href=\"https:\/\/blog.soton.ac.uk\/pi\/adafruit-proximity-and-ambient-light-sensor\/\">Continue reading &raquo;<\/a><\/p>\n","protected":false},"author":84718,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-148","page","type-page","status-publish","hentry","nodate"],"_links":{"self":[{"href":"https:\/\/blog.soton.ac.uk\/pi\/wp-json\/wp\/v2\/pages\/148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.soton.ac.uk\/pi\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blog.soton.ac.uk\/pi\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blog.soton.ac.uk\/pi\/wp-json\/wp\/v2\/users\/84718"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.soton.ac.uk\/pi\/wp-json\/wp\/v2\/comments?post=148"}],"version-history":[{"count":4,"href":"https:\/\/blog.soton.ac.uk\/pi\/wp-json\/wp\/v2\/pages\/148\/revisions"}],"predecessor-version":[{"id":149,"href":"https:\/\/blog.soton.ac.uk\/pi\/wp-json\/wp\/v2\/pages\/148\/revisions\/149"}],"wp:attachment":[{"href":"https:\/\/blog.soton.ac.uk\/pi\/wp-json\/wp\/v2\/media?parent=148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}