{"id":1908,"date":"2019-12-19T20:58:17","date_gmt":"2019-12-19T12:58:17","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=1908"},"modified":"2020-05-12T14:05:34","modified_gmt":"2020-05-12T06:05:34","slug":"how-to-use-an-esp32-development-board-to-read-from-a-vcnl4010-proximity-light-sensor","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-use-an-esp32-development-board-to-read-from-a-vcnl4010-proximity-light-sensor\/","title":{"rendered":"How to use an ESP32 development board to read from a VCNL4010 Proximity\/Light sensor"},"content":{"rendered":"<p>If you have a Proximity\/Light sensor, you will be able to detect how close an object is. When your range of detection is within 200mm, you can use the <a href=\"https:\/\/www.amazon.com\/466-VCNL4010-Proximity-Light-Sensor\/dp\/B07QF2VVBD\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=77d2095a2cd036db656ad0f6766b306e&language=en_US\" rel=\"noopener\" target=\"_blank\">VCNL4010 Proximity\/Light sensor<\/a>. <\/p>\n<p>Given that characteristic, you can use the VCNL4010 Proximity\/Light sensor as a contactless switch to trigger some action.<\/p>\n<p>For example, you can use it in a soap dispenser that will dispense some soap when a palm is near the nozzle.<\/p>\n<p>In case you need it, this is how to use an <a href=\"https:\/\/www.amazon.com\/s\/ref=as_li_ss_tl?k=ESP32+development+board&ref=nb_sb_noss&linkCode=ll2&tag=clivsperswebs-20&linkId=63f90c5f6570e90d0e7fce17d15d867c&language=en_US\" rel=\"noopener\" target=\"_blank\">ESP32 development board<\/a> to read from a VCNL4010 Proximity\/Light sensor.<\/p>\n<p><img decoding=\"async\" width=\"800\" height=\"600\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/VCNL4010-Proximity-Light-sensor.jpg\" alt=\"VCNL4010 Proximity\/Light sensor\" class=\"aligncenter size-full wp-image-1909\" \/><\/p>\n<h2>Soldering the GPIO header pins onto the VCNL4010 Proximity\/Light sensor<\/h2>\n<p>First, solder the GPIO header pins onto the VCNL4010 Proximity\/Light sensor:<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/VCNL4010-Proximity-Light-sensor-with-GPIO-pins-soldered-on-it.jpg\" alt=\"VCNL4010 Proximity\/Light sensor with GPIO pins soldered on it\" class=\"aligncenter size-full wp-image-1910\" \/><\/p>\n<h2>Connecting your VCNL4010 Proximity\/Light sensor to your ESP32 development board<\/h2>\n<p>Once you had soldered the GPIO pins onto your VCNL4010 Proximity\/Light sensor, connect it to your ESP32 development board:<br \/>\n<img decoding=\"async\" width=\"800\" height=\"600\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/VCNL4010-Proximity-Light-sensor-with-Vin-GND-SCL-and-SDA-pins-connected-to-GPIO-cables.jpg\" alt=\"VCNL4010 Proximity\/Light sensor with Vin, GND, SCL and SDA pins connected to GPIO cables\" class=\"aligncenter size-full wp-image-1912\" \/><br \/>\n<img decoding=\"async\" width=\"800\" height=\"600\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Underside-of-Espressif-ESP32-devkit-v4-with-GPIO-wires-connected-to-GND-5v-GPIO21-and-GPIO22-pins.jpg\" alt=\"Underside of Espressif ESP32 devkit v4 with GPIO wires connected to GND, 5v, GPIO21 and GPIO22 pins\"  class=\"aligncenter size-full wp-image-1913\" \/><\/p>\n<p>As shown above, I had connected:<\/p>\n<ul>\n<li><strong>GND<\/strong> pin of VCNL4010 Proximity\/Light sensor to one of the <strong>GND<\/strong> pin on the ESP32 development board<\/li>\n<li><strong>Vin<\/strong> pin of VCNL4010 Proximity\/Light sensor to one of the <strong>5v<\/strong> pin on the ESP32 development board <\/li>\n<li><strong>SDA<\/strong> pin of VCNL4010 Proximity\/Light sensor to <strong>GPIO21<\/strong> on the ESP32 development board<\/li>\n<li><strong>SCL<\/strong> pin of VCNL4010 Proximity\/Light sensor to <strong>GPIO22<\/strong> pin on the ESP32 development board<\/li>\n<\/ul>\n<h2>Writing an Arduino Sketch to read from a VCNL4010 Proximity\/Light sensor connected to ESP32 board<\/h2>\n<p>At this point in time, you can write the software to read from the VCNL4010 Proximity\/Light sensor.<\/p>\n<p>If you <a href=\"https:\/\/www.techcoil.com\/blog\/enabling-esp32-development-on-arduino-ide\/\" rel=\"noopener\" target=\"_blank\">enable ESP32 Development on Arduino IDE<\/a>, then you can upload an Arduino Sketch to your ESP32 board to read from the VCNL4010 Proximity\/Light sensor.<\/p>\n<h3>Installing the Arduino Library to read from a VCNL4010 Proximity\/Light sensor<\/h3>\n<p>After you had started your Arduino IDE, go to <strong>Tools -> Manage Libraries...<\/strong>. When you do that, the Library Manager window will appear.<\/p>\n<p>Given that, search for <strong>Adafruit VCNL4010<\/strong> and install the <a href=\"https:\/\/github.com\/adafruit\/Adafruit_VCNL4010\" rel=\"noopener\" target=\"_blank\">Adafruit_VCNL4010<\/a> library:<br \/>\n<img decoding=\"async\" width=\"800\" height=\"450\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Searching-for-Adafruit-VCNL4010-in-Arduino-IDE-1.8.9-libary-manager.gif\" alt=\"Searching for Adafruit VCNL4010 in Arduino IDE 1.8.9 libary manager\" class=\"aligncenter size-full wp-image-1914\" \/><\/p>\n<h3>Arduino sketch to read from a VCNL4010 Proximity\/Light sensor connected to an ESP32 development board<\/h3>\n<p>Once you had installed the Adafruit VCNL4010 library, you will be able to read from a VCNL4010 Proximity\/Light sensor. In order to give us a reference on how to do so, the library included an example sketch:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &lt;Wire.h&gt;\r\n#include &lt;Adafruit_VCNL4010.h&gt;\r\n\r\nAdafruit_VCNL4010 vcnl;\r\n\r\nvoid setup() {\r\n  Serial.begin(9600);\r\n  Serial.println(&quot;VCNL4010 test&quot;);\r\n\r\n  if (! vcnl.begin()){\r\n    Serial.println(&quot;Sensor not found :(&quot;);\r\n    while (1);\r\n  }\r\n  Serial.println(&quot;Found VCNL4010&quot;);\r\n}\r\n\r\n\r\nvoid loop() {\r\n   Serial.print(&quot;Ambient: &quot;); Serial.println(vcnl.readAmbient());\r\n   Serial.print(&quot;Proximity: &quot;); Serial.println(vcnl.readProximity());\r\n   delay(100);\r\n}\r\n<\/pre>\n<p>So what is the above code doing?<\/p>\n<p>When the setup function is called, we configure the board's serial to transmit data at a baud rate of 9600. After that, we call <code>vcnl.begin()<\/code> to start the proximity\/light sensor. If we can successfully start the proximity\/light sensor, we then send an indication to the board's serial.<\/p>\n<p>Once we have prepared the instance of <code>Adafruit_VCNL4010<\/code>, we use it in the <code>loop<\/code> function to read the ambient and proximity values detected by the sensor.<\/p>\n<p>While ambient is proportional to the brightness level, proximity value is inversely proportional to the distance an object is away from the sensor.<\/p>\n<p>For example, if you cover the sensor with your hand, <code>vcnl.readAmbient()<\/code> return 0 and <code>vcnl.readProximity()<\/code> return 65535.<\/p>\n<h3>Sample output of Arduino Sketch to read from a VCNL4010 Proximity\/Light sensor connected to an ESP32 development board<\/h3>\n<p>When I ran the sample sketch for a while, the following output was captured in my serial monitor: <img decoding=\"async\" width=\"800\" height=\"446\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Adafruit_VNCL4010-example-program-sample-output-on-Serial-Monitor-of-Arduino-IDE.gif\" alt=\"Adafruit_VNCL4010 example program sample output on Serial Monitor of Arduino IDE\" class=\"aligncenter size-full wp-image-1915\" \/><\/p>\n<p><img decoding=\"async\" width=\"600\" height=\"900\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/How-to-use-an-ESP32-development-board-to-read-from-a-VCNL4010-Proximity-Light-sensor.jpg\" alt=\"How to use an ESP32 development board to read from a VCNL4010 Proximity Light sensor\" class=\"aligncenter size-full wp-image-1945\" \/><\/p>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-uM\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-uM&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-uM&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F1908&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>If you have a Proximity\/Light sensor, you will be able to detect how close an object is. When your range of detection is within 200mm, you can use the <a href=\"https:\/\/www.amazon.com\/466-VCNL4010-Proximity-Light-Sensor\/dp\/B07QF2VVBD\/ref=as_li_ss_tl?ie=UTF8&#038;linkCode=ll1&#038;tag=clivsperswebs-20&#038;linkId=77d2095a2cd036db656ad0f6766b306e&#038;language=en_US\" rel=\"noopener\" target=\"_blank\">VCNL4010 Proximity\/Light sensor<\/a>. <\/p>\n<p>Given that characteristic, you can use the VCNL4010 Proximity\/Light sensor as a contactless switch to trigger some action.<\/p>\n<p>For example, you can use it in a soap dispenser that will dispense some soap when a palm is near the nozzle.<\/p>\n<p>In case you need it, this is how to use an <a href=\"https:\/\/www.amazon.com\/s\/ref=as_li_ss_tl?k=ESP32+development+board&#038;ref=nb_sb_noss&#038;linkCode=ll2&#038;tag=clivsperswebs-20&#038;linkId=63f90c5f6570e90d0e7fce17d15d867c&#038;language=en_US\" rel=\"noopener\" target=\"_blank\">ESP32 development board<\/a> to read from a VCNL4010 Proximity\/Light sensor.<\/p>\n<p><img decoding=\"async\" width=\"800\" height=\"600\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/VCNL4010-Proximity-Light-sensor.jpg\" alt=\"VCNL4010 Proximity\/Light sensor\" class=\"aligncenter size-full wp-image-1909\" \/><\/p>\n","protected":false},"author":1,"featured_media":1909,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375,4],"tags":[710,588,630,484,709],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/VCNL4010-Proximity-Light-sensor.jpg","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-uM","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1908"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=1908"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1908\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1909"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=1908"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=1908"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=1908"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}