{"id":108851,"date":"2022-03-08T16:16:40","date_gmt":"2022-03-08T16:16:40","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=108851"},"modified":"2022-03-08T16:16:43","modified_gmt":"2022-03-08T16:16:43","slug":"esp32-bh1750-ambient-light-sensor","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp32-bh1750-ambient-light-sensor\/","title":{"rendered":"ESP32 with BH1750 Ambient Light Sensor"},"content":{"rendered":"\n<p>The BH1750 is a 16-bit ambient light sensor. In this guide, you\u2019ll learn how to use the BH1750 ambient light sensor with the ESP32 board using Arduino IDE. The sensor communicates with a microcontroller using I2C communication protocol.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/BH1750-Ambient-Light-Sensor-ESP32.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 with BH1750 Ambient Light Sensor Arduino IDE Core Programming\" class=\"wp-image-108852\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/BH1750-Ambient-Light-Sensor-ESP32.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/BH1750-Ambient-Light-Sensor-ESP32.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/BH1750-Ambient-Light-Sensor-ESP32.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/BH1750-Ambient-Light-Sensor-ESP32.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n\n<p>You\u2019ll learn how to wire the sensor to the ESP32 board, install the required libraries and use a simple sketch to display the sensor readings in the Serial Monitor.<\/p>\n\n\n\n<p>This tutorial covers the following topics:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"#introducing-bh1750\">Introducing the BH1750 Ambient Light Sensor<\/a><\/li><li><a href=\"#bh1750-pinout\">BH1750 Pinout<\/a><\/li><li><a href=\"#bh1750-interface\">BH1750 I2C Interface<\/a><\/li><li><a href=\"#bh1750-esp32-example\">Example: BH1750: Read Ambient Light with ESP32<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"introducing-bh1750\">Introducing BH1750 Ambient Light Sensor<\/h2>\n\n\n\n<p>The BH1750 is a 16-bit ambient light sensor that communicates via I2C protocol. It outputs luminosity measurements in lux (SI-derived unit of illuminance). It can measure a minimum of 1 lux and a maximum of 65535 lux. <\/p>\n\n\n\n<p>The sensor may come in different breakout board formats. See pictures below. Both images represent a BH1750 sensor.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"288\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/BH1750-ambient-light-sensors.jpg?resize=750%2C288&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"BH1750 Ambient Light Sensor Breakout Boards\" class=\"wp-image-108841\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/BH1750-ambient-light-sensors.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/BH1750-ambient-light-sensors.jpg?resize=300%2C115&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">BH1750 Features<\/h3>\n\n\n\n<p>Here&#8217;s a list of the BH1750 sensor features. For more information consult the <a href=\"https:\/\/datasheet.octopart.com\/BH1750FVI-TR-Rohm-datasheet-25365051.pdf\" target=\"_blank\" rel=\"noreferrer noopener\">BH1750 sensor datasheet<\/a>. <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>I2C bus Interface<\/li><li>Spectral responsibility is approximately human eye response<\/li><li>Illuminance to digital converter<\/li><li>Range: 1 &#8211; 65535 lux<\/li><li>Low current by power down function<\/li><li>50Hz \/ 60Hz Light noise reject-function<\/li><li>It is possible to select 2 different I2 C slave addresses<\/li><li>Small measurement variation (+\/- 20%)<\/li><li>The influence of infrared is very small<\/li><li>Supports continuous measurement mode<\/li><li>Supports one-time measurement mode<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"measurement-modes\">Measurement Modes<\/h3>\n\n\n\n<p>The sensor supports two different measurement modes: <em>continuous measurement mode<\/em>, and <em>one-time <\/em>measurement mode. Each mode supports three different resolution modes.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Low Resolution Mode<\/strong><\/td><td>4 lux precision<\/td><td>16 ms measurement time<\/td><\/tr><tr><td><strong>High Resolution Mode<\/strong><\/td><td>1 lux precision<\/td><td>120 ms measurement time<\/td><\/tr><tr><td><strong>High Resolution Mode 2<\/strong><\/td><td>0.5 lux precision<\/td><td> 120 ms measurement time <\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>In continuous measurement mode, the sensor continuously measures ambient light values. In one-time measurement mode, the sensor measures the ambient light value once, and then it goes to power down mode.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Applications<\/strong><\/h3>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/02\/BH1750-Ambient-light-sensor-f.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"BH1750 Ambient Light Sensor\" class=\"wp-image-109235\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/02\/BH1750-Ambient-light-sensor-f.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/02\/BH1750-Ambient-light-sensor-f.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>The BH1750 is an ambient light sensor so it can be used in a wide variety of projects. For example:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>to detect if it is day or night;<\/li><li>to adjust or turn on\/off LED&#8217;s brightness accordingly to ambient light;<\/li><li>to adjust LCDs and screen&#8217;s brightness;<\/li><li>to detect if an LED is lit;<\/li><li>&#8230;<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"bh1750-pinout\">BH1750 Pinout<\/h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/02\/BH1750-pinout.JPG.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"BH1750 Ambient Light Sensor Pinout\" class=\"wp-image-109234\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/02\/BH1750-pinout.JPG.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/02\/BH1750-pinout.JPG.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>Here&#8217;s the BH1750 Pinout:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>VCC<\/strong><\/td><td>Powers the sensor (3.3V or 5V)<\/td><\/tr><tr><td><strong>GND<\/strong><\/td><td>Common GND<\/td><\/tr><tr><td><strong>SCL<\/strong><\/td><td>SCL pin for I2C communication<\/td><\/tr><tr><td><strong>SDA<\/strong> (Data)<\/td><td>SDA pin for I2C communication<\/td><\/tr><tr><td><strong>ADD<\/strong>*<\/td><td>Selects address<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The  ADD pin is used to set the I2C sensor address. If the voltage on that pin is less than 0.7VCC (pin is left floating or connected to GND), the I2C address is <span class=\"rnthl rntliteral\">0x23<\/span>. But, if the voltage is higher than 0.7xVCC (pin is connected to VCC), the address is <span class=\"rnthl rntliteral\">0x5C<\/span>. In summary:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>ADD pin floating or connected to <span class=\"rnthl rntcblack\">GND<\/span> \u2192 address: <span class=\"rnthl rntliteral\">0x23<\/span><\/li><li>ADD pin connected to <span class=\"rnthl rntcred\">VCC<\/span> \u2192 address: <span class=\"rnthl rntliteral\">0x5C<\/span><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"bh1750-interface\">BH1750 I2C Interface<\/h2>\n\n\n\n<p>The BH1750 ambient light sensor supports I2C interface.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/02\/BH1750-with-ESP32-f.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 with BH1750 Ambient Light Sensor\" class=\"wp-image-109239\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/02\/BH1750-with-ESP32-f.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/02\/BH1750-with-ESP32-f.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>You can connect the BH1750 sensor to the ESP32 using the default&#8217;s I2C pins:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>BH1750<\/strong><\/td><td><strong>ESP32<\/strong><\/td><\/tr><tr><td>SCL<\/td><td><span class=\"rnthl rntcyellow\">GPIO 22<\/span><\/td><\/tr><tr><td>SDA<\/td><td><span class=\"rnthl rntclgray\">GPIO 21<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><span class=\"rnthl rntcyellow\">GPIO 22<\/span> and <span class=\"rnthl rntclgray\">GPIO 21<\/span> are the ESP32 default I2C pins. You can use other pins as long as you set them properly on code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"bh1750-esp32-example\">BH1750: Read Ambient Light with ESP32<\/h2>\n\n\n\n<p>Now that you are more familiar with the BH1750 sensor, let&#8217;s test it. In this section, we&#8217;ll build a simple project that reads the ambient light and displays it in the Arduino IDE Serial Monitor.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Parts Required<\/h3>\n\n\n\n<p>To complete this tutorial you need the following parts:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/makeradvisor.com\/tools\/bh1750-ambient-light-sensor\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"https:\/\/makeradvisor.com\/tools\/bh1750-ambient-light-sensor\/\">BH1750 ambient light sensor<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\" rel=\"noreferrer noopener\">ESP32 <\/a>(read <a href=\"https:\/\/makeradvisor.com\/esp32-development-boards-review-comparison\/\" target=\"_blank\" rel=\"noreferrer noopener\">Best ESP32 development boards<\/a>)<\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noreferrer noopener\">Breadboard <\/a>(optional)<\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noreferrer noopener\">Jumper wires<\/a> (optional)<\/li><\/ul>\n\n\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Schematic &#8211; ESP32 with BH1750<\/h3>\n\n\n\n<p>Wire the BH1750 sensor to the ESP32 I2C pins. You can follow the next schematic diagram.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"672\" height=\"600\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/ESP32-BH1750-Wiring-Diagram.png?resize=672%2C600&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 BH1750 Wiring Diagram Circuit\" class=\"wp-image-108853\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/ESP32-BH1750-Wiring-Diagram.png?w=672&amp;quality=100&amp;strip=all&amp;ssl=1 672w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/ESP32-BH1750-Wiring-Diagram.png?resize=300%2C268&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 672px) 100vw, 672px\" \/><\/figure><\/div>\n\n\n\n<p>You can also follow the next table:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>BH1750<\/strong><\/td><td><strong>ESP32<\/strong><\/td><\/tr><tr><td><strong>VCC<\/strong><\/td><td><span class=\"rnthl rntcred\">3.3V<\/span><\/td><\/tr><tr><td><strong>GND<\/strong><\/td><td><span class=\"rnthl rntcblack\">GND<\/span><\/td><\/tr><tr><td><strong>SCL<\/strong><\/td><td><span class=\"rnthl rntcyellow\">GPIO 22<\/span><\/td><\/tr><tr><td><strong>SDA<\/strong> (Data)<\/td><td><span class=\"rnthl rntclgray\">GPIO 21<\/span><\/td><\/tr><tr><td><strong>ADD<\/strong>*<\/td><td>Don&#8217;t connect<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>By not connecting the <strong>ADD <\/strong>pin, we&#8217;re selecting <span class=\"rnthl rntliteral\">0x23<\/span> I2C address. Connect it to 3.3V to select <span class=\"rnthl rntliteral\">0x5C<\/span> address instead.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Preparing Arduino IDE<\/h2>\n\n\n\n<p>We&#8217;ll program the ESP32 board using Arduino IDE. So, make sure you have the ESP32 add-on installed. Follow the next tutorial:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/installing-the-esp32-board-in-arduino-ide-windows-instructions\/\">Installing the ESP32 Board in Arduino IDE (Windows, Mac OS X, Linux)<\/a><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Installing the BH1750 Library<\/h3>\n\n\n\n<p>There are several libraries to read from the BH1750 sensor. We&#8217;ll use the <a href=\"https:\/\/github.com\/claws\/BH1750\" target=\"_blank\" rel=\"noreferrer noopener\">BH1750 library by Christopher Laws<\/a>. It is compatible with the ESP32, ESP8266, and Arduino.<\/p>\n\n\n\n<p>Open your Arduino IDE and go to&nbsp;<strong>Sketch&nbsp;<\/strong>&gt;&nbsp;<strong>Include Library<\/strong>&nbsp;&gt;&nbsp;<strong>Manage Libraries<\/strong>. The Library Manager should open. <\/p>\n\n\n\n<p> Search for \u201c<strong>BH1750<\/strong>\u201d on the search box and install the <em>BH1750 library by Christopher Laws<\/em>.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"786\" height=\"443\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/BHT1750-Library-Arduino-IDE.png?resize=786%2C443&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"BHT1750 Library Arduino IDE\" class=\"wp-image-108821\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/BHT1750-Library-Arduino-IDE.png?w=786&amp;quality=100&amp;strip=all&amp;ssl=1 786w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/BHT1750-Library-Arduino-IDE.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/BHT1750-Library-Arduino-IDE.png?resize=768%2C433&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Code &#8211; Reading BH1750 Ambient Light Sensor<\/h2>\n\n\n\n<p>Copy the following code to your Arduino IDE. This code simply reads ambient light in lux and displays the values on the Serial Monitor. It is the example code from the library called <strong>BH1750test <\/strong>(you can access it in <strong>File <\/strong>&gt; <strong>Examples <\/strong>&gt; <strong>BH1750 <\/strong>&gt; <strong>BH1750test<\/strong><\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\n  Example of BH1750 library usage. This example initialises the BH1750 object using the default high resolution continuous mode and then makes a light level reading every second.\n*\/\n\n#include &lt;Wire.h&gt;\n#include &lt;BH1750.h&gt;\n\nBH1750 lightMeter;\n\nvoid setup(){\n  Serial.begin(9600);\n\n  \/\/ Initialize the I2C bus (BH1750 library doesn't do this automatically)\n  Wire.begin();\n  \/\/ On esp8266 you can select SCL and SDA pins using Wire.begin(D4, D3);\n  \/\/ For Wemos \/ Lolin D1 Mini Pro and the Ambient Light shield use Wire.begin(D2, D1);\n\n  lightMeter.begin();\n\n  Serial.println(F(&quot;BH1750 Test begin&quot;));\n}\n\nvoid loop() {\n  float lux = lightMeter.readLightLevel();\n  Serial.print(&quot;Light: &quot;);\n  Serial.print(lux);\n  Serial.println(&quot; lx&quot;);\n  delay(1000);\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Arduino\/BH1750test.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>The library also provides other examples worth exploring.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How the Code Works<\/h2>\n\n\n\n<p>We start by including the required libraries. The <span class=\"rnthl rntliteral\">Wire.h<\/span> library to use I2C communication protocol and the <span class=\"rnthl rntliteral\">BH1750.h<\/span> library to read from the sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;Wire.h&gt;\n#include &lt;BH1750.h&gt;<\/code><\/pre>\n\n\n\n<p>Then, we create a <span class=\"rnthl rntliteral\">BH1750<\/span> object called <span class=\"rnthl rntliteral\">lightMeter<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>BH1750 lightMeter;<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, initialize the Serial Monitor at a baud rate of 9600.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.begin(9600);<\/code><\/pre>\n\n\n\n<p>Initialize I2C communication protocol. It will start an I2C communication on the microcontroller&#8217;s default I2C pins. If you want to use different I2C pins, pass them to the <span class=\"rnthl rntliteral\">begin()<\/span> method like this <span class=\"rnthl rntliteral\">Wire.begin(SDA, SCL)<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Wire.begin();<\/code><\/pre>\n\n\n\n<p>Initialize the sensor using the <span class=\"rnthl rntliteral\">begin()<\/span> method on the <span class=\"rnthl rntliteral\">BH1750<\/span> object (<span class=\"rnthl rntliteral\">lightMeter<\/span>).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>lightMeter.begin();<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, we create a variable called <span class=\"rnthl rntliteral\">lux<\/span>, that saves the luminance values. To get the value, you simply call the <span class=\"rnthl rntliteral\">readLightLevel()<\/span> function on the <span class=\"rnthl rntliteral\">BH1750<\/span> object (<span class=\"rnthl rntliteral\">lightMeter<\/span>).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>float lux = lightMeter.readLightLevel();<\/code><\/pre>\n\n\n\n<p>Finally, display the measurement on the Serial Monitor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.print(\"Light: \");\nSerial.print(lux);\nSerial.println(\" lx\");<\/code><\/pre>\n\n\n\n<p>You get and print a new reading every second.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>delay(1000);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Demonstration<\/h2>\n\n\n\n<p>Now, you can upload the code to your board. First, connect your board to your computer. Then, go to <strong>Tools <\/strong>&gt; <strong>Board <\/strong>and select the ESP32 board you&#8217;re using. Go to <strong>Tools <\/strong>&gt; <strong>Port <\/strong>and select the COM port your board is connected to. Finally, click on the upload button.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"30\" height=\"30\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/05\/arduino-2-0-upload-button.png?resize=30%2C30&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Arduino 2.0 Upload Button\" class=\"wp-image-103678\"\/><\/figure><\/div>\n\n\n\n<p>After successfully uploading the code, open the Serial Monitor at a <u>baud rate of 9600<\/u> and press the ESP32 on-board RST button.<\/p>\n\n\n\n<p>New luminance readings should be printed in the Serial Monitor.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"601\" height=\"461\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/BH1750-readings-serial-monitor.png?resize=601%2C461&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 with BH1750 Ambient Light Sensor Arduino IDE Demonstration\" class=\"wp-image-108824\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/BH1750-readings-serial-monitor.png?w=601&amp;quality=100&amp;strip=all&amp;ssl=1 601w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/BH1750-readings-serial-monitor.png?resize=300%2C230&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 601px) 100vw, 601px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Other Useful Functions<\/h2>\n\n\n\n<p>The library we&#8217;re using with the BH1750 sensor provides other examples that illustrate other useful functions and features. You can <a href=\"https:\/\/github.com\/claws\/BH1750\/tree\/master\/examples\" target=\"_blank\" rel=\"noreferrer noopener\">check all BH1750 library examples here<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Setting Measurement Mode<\/h3>\n\n\n\n<p>By default, the library uses the continuous high resolution measurement mode, but you can change it by passing the desired measurement mode to the <span class=\"rnthl rntliteral\">begin()<\/span> method when initializing the sensor. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)<\/code><\/pre>\n\n\n\n<p>Here&#8217;s a list of all available modes:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><span class=\"rnthl rntliteral\">BH1750_CONTINUOUS_LOW_RES_MODE<\/span><\/li><li><span class=\"rnthl rntliteral\">BH1750_CONTINUOUS_HIGH_RES_MODE<\/span> (default)<\/li><li><span class=\"rnthl rntliteral\">BH1750_CONTINUOUS_HIGH_RES_MODE_2<\/span><\/li><li><span class=\"rnthl rntliteral\">BH1750_ONE_TIME_LOW_RES_MODE<\/span><\/li><li><span class=\"rnthl rntliteral\">BH1750_ONE_TIME_HIGH_RES_MODE<\/span><\/li><li><span class=\"rnthl rntliteral\">BH1750_ONE_TIME_HIGH_RES_MODE_2<\/span><\/li><\/ul>\n\n\n\n<p>See the properties of each mode in <a href=\"#measurement-modes\">this previous section<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this tutorial, you&#8217;ve learned how to use the BH1750 ambient light sensor with the ESP32. The sensor is very easy to use. It uses I2C communication protocol, which makes wiring simple, and the library provides methods to easily get the readings.<\/p>\n\n\n\n<p>We hope you found this tutorial useful. Tell us in the comments below in which project would you use the BH1750 sensor.<\/p>\n\n\n\n<p>We have tutorials for other sensors with the ESP32 board that you may like:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-bmp388-arduino\/\">ESP32 with BMP388: <strong>Altimeter Sensor<\/strong><\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-mqtt-publish-ds18b20-temperature-arduino\/\">ESP32 with DS18B20: <strong>Temperature Sensor<\/strong><\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-mqtt-publish-bme680-arduino\/\">ESP32 with BME680: <strong>Gas, Pressure, Humidity, and Temperature<\/strong> <strong>Sensor<\/strong><\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-mqtt-publish-bme280-arduino\/\">ESP32 with BME280: <strong>Temperature, Humidity, and Pressure Sensor<\/strong><\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide\/\">ESP32 DHT11\/DHT22: <strong>Temperature , and Humidity Sensor<\/strong><\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-hc-sr04-ultrasonic-arduino\/\">ESP32 HC-SR04: <strong>Ultrasonic Distance Sensor<\/strong><\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-pir-motion-sensor-interrupts-timers\/\">ESP32 PIR: <strong>Motion Sensor<\/strong><\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-with-bmp180-barometric-sensor\/\">ESP32 BMP180: <strong>Pressure Sensor<\/strong><\/a><\/li><\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The BH1750 is a 16-bit ambient light sensor. In this guide, you\u2019ll learn how to use the BH1750 ambient light sensor with the ESP32 board using Arduino IDE. The sensor &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP32 with BH1750 Ambient Light Sensor\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp32-bh1750-ambient-light-sensor\/#more-108851\" aria-label=\"Read more about ESP32 with BH1750 Ambient Light Sensor\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":108852,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[276,281,277,299,264],"tags":[],"class_list":["post-108851","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32","category-esp32-project","category-esp32-arduino-ide","category-0-esp32","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/BH1750-Ambient-Light-Sensor-ESP32.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/108851","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=108851"}],"version-history":[{"count":11,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/108851\/revisions"}],"predecessor-version":[{"id":109762,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/108851\/revisions\/109762"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/108852"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=108851"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=108851"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=108851"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}