{"id":150659,"date":"2024-07-25T13:10:52","date_gmt":"2024-07-25T13:10:52","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=150659"},"modified":"2024-07-25T13:54:12","modified_gmt":"2024-07-25T13:54:12","slug":"micropython-bh1750-esp32-esp8266","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/micropython-bh1750-esp32-esp8266\/","title":{"rendered":"MicroPython: BH1750 Ambient Light Sensor with ESP32\/ESP8266"},"content":{"rendered":"\n<p>The BH1750 is a 16-bit ambient light sensor. In this tutorial, you\u2019ll learn how to use the BH1750 ambient light sensor with the ESP32 and ESP8266 boards programmed with MicroPython firmware. You\u2019ll learn how to wire the sensor to the ESP boards, install the required libraries, and get ambient light measurements in Lux (the standard unit for illuminance).<\/p>\n\n\n<div class=\"wp-block-image\">\n<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\/2024\/03\/ESP32-ESP8266-BH1750-light-sensor-MicroPython.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"MicroPython BH1750 Ambient Light Sensor with ESP32 ESP8266 NodeMCU\" class=\"wp-image-150660\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/ESP32-ESP8266-BH1750-light-sensor-MicroPython.jpg?w=1920&amp;quality=100&amp;strip=all&amp;ssl=1 1920w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/ESP32-ESP8266-BH1750-light-sensor-MicroPython.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/ESP32-ESP8266-BH1750-light-sensor-MicroPython.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/ESP32-ESP8266-BH1750-light-sensor-MicroPython.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/ESP32-ESP8266-BH1750-light-sensor-MicroPython.jpg?resize=1536%2C864&amp;quality=100&amp;strip=all&amp;ssl=1 1536w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p><strong>Using Arduino IDE?<\/strong> Follow the next tutorials instead:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-bh1750-ambient-light-sensor\/\">ESP32 with BH1750 Ambient Light Sensor<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-bh1750-ambient-light-sensor\/\">ESP8266 NodeMCU with BH1750 Ambient Light Sensor<\/a><\/li>\n<\/ul>\n\n\n\n<p><strong>Table of Contents:<\/strong><\/p>\n\n\n\n<p>This tutorial covers the following topics:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"#introducing-bh1750\" title=\"\">Introducing the BH1750 Ambient Light Sensor<\/a><\/li>\n\n\n\n<li><a href=\"#bh1750-pinout\" title=\"\">BH1750 Pinout<\/a><\/li>\n\n\n\n<li><a href=\"#wiring\" title=\"\">Wiring the BH1750 to the ESP32 or ESP8266<\/a><\/li>\n\n\n\n<li><a href=\"#bh1750-micropython-library\" title=\"\">BH1750 MicroPython Library<\/a><\/li>\n\n\n\n<li><a href=\"#rpi-pico-bh1750-micropython-code\" title=\"\">BH1750 Read Ambient Light with ESP32\/ESP8266 (MicroPython)<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>To follow this tutorial you need MicroPython firmware installed in your ESP32 or ESP8266 boards. You also need an IDE to write and upload the code to your board. We suggest using Thonny IDE or uPyCraft IDE:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Thonny IDE:\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-thonny-micropython-python-ide-esp32-esp8266\/\">Installing and getting started with Thonny IDE<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/flashing-micropython-firmware-esptool-py-esp32-esp8266\/\">Flashing MicroPython Firmware with esptool.py<\/a><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>uPyCraft IDE:\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-micropython-esp32-esp8266\/\">Getting Started with uPyCraft IDE<\/a><\/li>\n\n\n\n<li>Install uPyCraft IDE (<a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-windows-pc-instructions\/\">Windows<\/a>,&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-mac-os-x-instructions\/\">Mac OS X<\/a>,&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-linux-ubuntu-instructions\/\">Linux<\/a>)<\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/flash-upload-micropython-firmware-esp32-esp8266\/\">Flash\/Upload MicroPython Firmware to ESP32 and ESP8266<\/a><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"rntbox rntclgreen\">Learn more about MicroPython:&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/micropython-programming-with-esp32-and-esp8266\/\">MicroPython Programming with ESP32 and ESP8266<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Parts Required<\/h2>\n\n\n\n<p>To follow this tutorial you need the following part:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/bh1750-ambient-light-sensor\/\" target=\"_blank\" rel=\"noreferrer noopener\">BH1750 ambient light sensor<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\" rel=\"noopener\" title=\"\">ESP32 <\/a>or <a href=\"https:\/\/makeradvisor.com\/tools\/esp8266-esp-12e-nodemcu-wi-fi-development-board\/\" target=\"_blank\" rel=\"noopener\" title=\"\">ESP8266<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noreferrer noopener\">Breadboard&nbsp;<\/a>(optional)<\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noreferrer noopener\">Jumper wires<\/a>&nbsp;(optional)<\/li>\n<\/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<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 the pictures below. Both images represent a BH1750 sensor.<\/p>\n\n\n<div class=\"wp-block-image\">\n<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<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\">\n<li>I2C bus Interface<\/li>\n\n\n\n<li>Spectral responsibility is approximately human eye response<\/li>\n\n\n\n<li>Illuminance to digital converter<\/li>\n\n\n\n<li>Range: 1 &#8211; 65535 lux<\/li>\n\n\n\n<li>Low current by power down function<\/li>\n\n\n\n<li>50Hz \/ 60Hz Light noise reject-function<\/li>\n\n\n\n<li>It is possible to select 2 different I2 C slave addresses<\/li>\n\n\n\n<li>Small measurement variation (+\/- 20%)<\/li>\n\n\n\n<li>The influence of infrared is very small<\/li>\n\n\n\n<li>Supports continuous measurement mode<\/li>\n\n\n\n<li>Supports one-time measurement mode<\/li>\n<\/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<div class=\"wp-block-image\">\n<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<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\">\n<li>to detect if it is day or night;<\/li>\n\n\n\n<li>to adjust or turn on\/off LED&#8217;s brightness accordingly to ambient light;<\/li>\n\n\n\n<li>to adjust LCDs and screen&#8217;s brightness;<\/li>\n\n\n\n<li>to detect if an LED is lit;<\/li>\n\n\n\n<li>&#8230;<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"bh1750-pinout\">BH1750 Pinout<\/h2>\n\n\n<div class=\"wp-block-image\">\n<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<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\">\n<li>ADD pin floating or connected to <span class=\"rnthl rntcblack\">GND<\/span> \u2192 address: <span class=\"rnthl rntliteral\">0x23<\/span><\/li>\n\n\n\n<li>ADD pin connected to <span class=\"rnthl rntcred\">VCC<\/span> \u2192 address: <span class=\"rnthl rntliteral\">0x5C<\/span><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"wiring\">Wiring the BH1750 to the ESP32 or ESP8266<\/h2>\n\n\n\n<p>The BH1750 ambient light sensor supports I2C interface. You can connect the BH1750 sensor to the ESP32\/ESP8266 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><td><strong>ESP8266<\/strong><\/td><\/tr><tr><td><strong>VCC<\/strong><\/td><td><span class=\"rnthl rntcred\">3.3V<\/span><\/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><td><span class=\"rnthl rntcblack\">GND<\/span><\/td><\/tr><tr><td><strong>SCL<\/strong><\/td><td><span class=\"rnthl rntcyellow\">GPIO 22<\/span><\/td><td><span class=\"rnthl rntcyellow\">GPIO 5<\/span> (D1)<\/td><\/tr><tr><td><strong>SDA<\/strong> (Data)<\/td><td><span class=\"rnthl rntclgray\">GPIO 21<\/span><\/td><td><span class=\"rnthl rntclgray\">GPIO 4<\/span> (D2)<\/td><\/tr><tr><td><strong>ADD<\/strong>*<\/td><td>Don&#8217;t connect<\/td><td>Don&#8217;t connect<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">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.<\/figcaption><\/figure>\n\n\n\n<p>You can also use the following schematic diagrams as a reference.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Schematic &#8211; ESP32 with BH1750<\/h3>\n\n\n<div class=\"wp-block-image\">\n<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<h3 class=\"wp-block-heading\">Schematic &#8211; ESP8266 NodeMCU with BH1750<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"528\" height=\"627\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/ESP8266-BH1750-Wiring-Diagram.png?resize=528%2C627&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 NodeMCU BH1750 Wiring Diagram\" class=\"wp-image-108840\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/ESP8266-BH1750-Wiring-Diagram.png?w=528&amp;quality=100&amp;strip=all&amp;ssl=1 528w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/ESP8266-BH1750-Wiring-Diagram.png?resize=253%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 253w\" sizes=\"(max-width: 528px) 100vw, 528px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"bh1750-micropython-library\">BH1750 MicroPython Library<\/h2>\n\n\n\n<p>To read from the BH1750 sensor, we\u2019ll use an external library that isn\u2019t part of the standard MicroPython library by default.<\/p>\n\n\n\n<p>Open the following link and copy the library code to Thonny IDE:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/raw.githubusercontent.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/master\/Projects\/ESP-MicroPython\/bh1750.py\" target=\"_blank\" rel=\"noopener\" title=\"\"><em>bh1750.py<\/em> library code<\/a><\/li>\n<\/ul>\n\n\n\n<p>Follow the next steps to upload the library to your ESP32 or ESP8266 board:<\/p>\n\n\n\n<p><strong>1)<\/strong> Copy the library code to a new file. The <a href=\"https:\/\/raw.githubusercontent.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/master\/Projects\/ESP-MicroPython\/bh1750.py\" target=\"_blank\" rel=\"noopener\" title=\"\"><em>bh1750.py<\/em> library code can be found here<\/a>.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># forked from: https:\/\/github.com\/PinkInk\/upylib\/tree\/master\/bh1750\n&quot;&quot;&quot;\nMicropython BH1750 ambient light sensor driver.\n&quot;&quot;&quot;\n\nfrom utime import sleep_ms\n\n\nclass BH1750():\n    &quot;&quot;&quot;Micropython BH1750 ambient light sensor driver.&quot;&quot;&quot;\n\n    PWR_OFF = 0x00\n    PWR_ON = 0x01\n    RESET = 0x07\n\n    # modes\n    CONT_LOWRES = 0x13\n    CONT_HIRES_1 = 0x10\n    CONT_HIRES_2 = 0x11\n    ONCE_HIRES_1 = 0x20\n    ONCE_HIRES_2 = 0x21\n    ONCE_LOWRES = 0x23\n\n    # default addr=0x23 if addr pin floating or pulled to ground\n    # addr=0x5c if addr pin pulled high\n    def __init__(self, bus, addr=0x23):\n        self.bus = bus\n        self.addr = addr\n        self.off()\n        self.reset()\n\n    def off(self):\n        &quot;&quot;&quot;Turn sensor off.&quot;&quot;&quot;\n        self.set_mode(self.PWR_OFF)\n\n    def on(self):\n        &quot;&quot;&quot;Turn sensor on.&quot;&quot;&quot;\n        self.set_mode(self.PWR_ON)\n\n    def reset(self):\n        &quot;&quot;&quot;Reset sensor, turn on first if required.&quot;&quot;&quot;\n        self.on()\n        self.set_mode(self.RESET)\n\n    def set_mode(self, mode):\n        &quot;&quot;&quot;Set sensor mode.&quot;&quot;&quot;\n        self.mode = mode\n        self.bus.writeto(self.addr, bytes([self.mode]))\n\n    def luminance(self, mode):\n        &quot;&quot;&quot;Sample luminance (in lux), using specified sensor mode.&quot;&quot;&quot;\n        # continuous modes\n        if mode &amp; 0x10 and mode != self.mode:\n            self.set_mode(mode)\n        # one shot modes\n        if mode &amp; 0x20:\n            self.set_mode(mode)\n        # earlier measurements return previous reading\n        sleep_ms(24 if mode in (0x13, 0x23) else 180)\n        data = self.bus.readfrom(self.addr, 2)\n        factor = 2.0 if mode in (0x11, 0x21) else 1.0\n        return (data[0]&lt;&lt;8 | data[1]) \/ (1.2 * factor)\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP-MicroPython\/bh1750.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"785\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/bh1750-micropython-library-1.png?resize=708%2C785&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"BH1750 micropython library on Thonny IDE\" class=\"wp-image-150661\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/bh1750-micropython-library-1.png?w=708&amp;quality=100&amp;strip=all&amp;ssl=1 708w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/bh1750-micropython-library-1.png?resize=271%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 271w\" sizes=\"(max-width: 708px) 100vw, 708px\" \/><\/figure><\/div>\n\n\n<p><strong>2)<\/strong> Go to <strong>File <\/strong>&gt; <strong>Save as&#8230;<\/strong><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"206\" height=\"294\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/Thonny-IDE-ESP32-ESP8266-MicroPython-Save-file-library-to-device-save-as.png?resize=206%2C294&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Thonny IDE ESP32 ESP8266 MicroPython Save file library to device save as\" class=\"wp-image-99603\"\/><\/figure><\/div>\n\n\n<p><strong>3)<\/strong> Select save to &#8220;<strong>MicroPython device<\/strong>&#8220;:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"220\" height=\"202\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/01\/save-micropython-device-thonny-ide.png?resize=220%2C202&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Thonny IDE Save to MicroPython Device\" class=\"wp-image-109022\"\/><\/figure><\/div>\n\n\n<p><strong>4)<\/strong> <strong><u>Name your file as <em>bh1750.py<\/em><\/u><\/strong> and press the <strong>OK<\/strong> button:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"545\" height=\"327\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/saving-bh1750-library-micropython.png?resize=545%2C327&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"saving bh1750.py to esp32 esp8266\" class=\"wp-image-150662\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/saving-bh1750-library-micropython.png?w=545&amp;quality=100&amp;strip=all&amp;ssl=1 545w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/saving-bh1750-library-micropython.png?resize=300%2C180&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 545px) 100vw, 545px\" \/><\/figure><\/div>\n\n\n<p>And that&#8217;s it. The library was uploaded to your board. To make sure that it was uploaded successfully, go to <strong>File<\/strong> &gt; <strong>Save as&#8230;<\/strong> and select the MicroPython device. Your file should be listed there:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"545\" height=\"327\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/bh1750-library-saved-sucessfully.png?resize=545%2C327&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"BH1750 file saved on the ESP32 and ESP8266 boards\" class=\"wp-image-150663\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/bh1750-library-saved-sucessfully.png?w=545&amp;quality=100&amp;strip=all&amp;ssl=1 545w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/bh1750-library-saved-sucessfully.png?resize=300%2C180&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 545px) 100vw, 545px\" \/><\/figure><\/div>\n\n\n<p>After uploading the library to your board, you can use the library functionalities in your code by importing the library.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"rpi-pico-bh1750-micropython-code\">ESP32\/ESP8266 &#8211; BH1750 Luminance (Lux) &#8211; Code<\/h2>\n\n\n\n<p>After uploading the library, create a new file and paste the following code. It reads the luminance in lux units and prints the readings into the Shell every two seconds.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n# Complete project details at https:\/\/RandomNerdTutorials.com\/micropython-bh1750-esp32-esp8266\/\n\nfrom machine import Pin, SoftI2C\nfrom bh1750 import BH1750\nimport time\n\n# Initialize I2C communication (ESP32)\ni2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=400000)\n\n\n# Initialize I2C communication (ESP8266)\n#i2c = SoftI2C(scl=Pin(5), sda=Pin(4), freq=400000)\n\n# Create BH1750 object\nlight_sensor = BH1750(bus=i2c, addr=0x23)\n\ntry:\n    # Read lux every 2 seconds\n    while True:\n        lux = light_sensor.luminance(BH1750.CONT_HIRES_1)\n        print(&quot;Luminance: {:.2f} lux&quot;.format(lux))\n        time.sleep(2)\n\nexcept Exception as e:\n    # Handle any exceptions during sensor reading\n    print(&quot;An error occurred:&quot;, e)\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP-MicroPython\/esp_bh1750_test.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">How the Code Works<\/h4>\n\n\n\n<p>We need to import the <span class=\"rnthl rntliteral\">Pin<\/span> and <span class=\"rnthl rntliteral\">SoftI2C<\/span> classes from the <span class=\"rnthl rntliteral\">machine<\/span> module.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>from machine import Pin, SoftI2C\nfrom bh1750 import BH1750\nimport time<\/code><\/pre>\n\n\n\n<p>In this case, this library needs <span class=\"rnthl rntliteral\">SoftI2C<\/span> instead of <span class=\"rnthl rntliteral\">I2C<\/span>. In MicroPython, <span class=\"rnthl rntliteral\">SoftI2C<\/span> refers to software I2C and <span class=\"rnthl rntliteral\">I2C<\/span> refers to hardware I2C.<\/p>\n\n\n\n<p>Hardware I2C relies on dedicated hardware modules that are optimized for I2C communication. Software-based I2C, on the other hand, uses software-defined GPIO pins to emulate I2C communication.<\/p>\n\n\n\n<p>We initialize an I2C communication on GPIOs 21 and 22 on the ESP32, or GPIOs 4 and 5 on the ESP8266. Make sure to uncomment the pins for the board you&#8217;re using.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Initialize I2C communication (ESP32)\n#i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=400000)\n\n# Initialize I2C communication (ESP8266)\ni2c = SoftI2C(scl=Pin(5), sda=Pin(4), freq=400000)<\/code><\/pre>\n\n\n\n<p>Then, create a <span class=\"rnthl rntliteral\">BH1750<\/span> instance, called <span class=\"rnthl rntliteral\">light_sensor<\/span> on the I2C bus defined previously. We&#8217;re using 0x23 I2C address. If you have a different address, change that on the following line.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>light_sensor = BH1750(bus=i2c, addr=0x23)<\/code><\/pre>\n\n\n\n<p>To get luminance values, we just need to use the <span class=\"rnthl rntliteral\">luminance()<\/span> method on the <span class=\"rnthl rntliteral\">BH1750<\/span> object and pass a measurement mode as an argument.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>lux = light_sensor.luminance(BH1750.CONT_HIRES_1)<\/code><\/pre>\n\n\n\n<p>The library provides a list of measurement modes you can use. See the table below.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Constant<\/strong><\/td><td><strong>Description<\/strong><\/td><\/tr><tr><td><span class=\"rnthl rntliteral\">CONT_LOWRES<\/span><\/td><td>Continuous, low-resolution (4lx), sampling takes ~24ms, sensor remains on after reading.<\/td><\/tr><tr><td><span class=\"rnthl rntliteral\">CONT_HIRES_1<\/span><\/td><td>Continuous, high-resolution (1lx), sampling takes ~180ms, sensor remains on after reading.<\/td><\/tr><tr><td><span class=\"rnthl rntliteral\">CONT_HIRES_2<\/span><\/td><td>Continuous, very high resolution (.5lx), sampling takes ~180ms, sensor remains on after reading.<\/td><\/tr><tr><td><span class=\"rnthl rntliteral\">ONCE_HIRES_1<\/span><\/td><td>One-shot, low resolution (4lx), sampling takes ~24ms, sensor powered down after reading<\/td><\/tr><tr><td><span class=\"rnthl rntliteral\">ONCE_HIRES_2<\/span><\/td><td>One-shot, high resolution (1lx), sampling takes ~180ms, sensor powered down after reading<\/td><\/tr><tr><td><span class=\"rnthl rntliteral\">ONCE_LOWRES<\/span><\/td><td>One-shot, very high resolution (.5lx), sampling takes ~180ms, sensor powered down after reading<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Finally, we print the readings in the shell:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>print(\"Luminance: {:.2f} lux\".format(lux))<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Testing the Code<\/h2>\n\n\n\n<p>Run the previous code on your ESP32 or ESP8266 board.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"470\" height=\"114\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/thonny-ide-run-button.png?resize=470%2C114&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Testing the Code Script Thonny IDE\" class=\"wp-image-144594\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/thonny-ide-run-button.png?w=470&amp;quality=100&amp;strip=all&amp;ssl=1 470w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/thonny-ide-run-button.png?resize=300%2C73&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 470px) 100vw, 470px\" \/><\/figure><\/div>\n\n\n<p>You should get new luminance values in lux units every two seconds.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"674\" height=\"495\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/BH1750-lihht-sensor-micropython-ESP32-ESP8266-testing.png?resize=674%2C495&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"BH1750 getting luminance values on Thonny IDE ESP32 and ESP8266\" class=\"wp-image-150665\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/BH1750-lihht-sensor-micropython-ESP32-ESP8266-testing.png?w=674&amp;quality=100&amp;strip=all&amp;ssl=1 674w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/BH1750-lihht-sensor-micropython-ESP32-ESP8266-testing.png?resize=300%2C220&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 674px) 100vw, 674px\" \/><\/figure><\/div>\n\n\n<p>Cover the sensor or point some light to see the values changing.<\/p>\n\n\n<div class=\"wp-block-image\">\n<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<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this tutorial, we&#8217;ve covered how to use the BH1750 ambient light sensor with the ESP32 and ESP8266 boards programmed with MicroPython. <\/p>\n\n\n\n<p>The BH1750 sensor can be useful in a wide range of projects to detect if it is day or night, adjust an LED brightness according to ambient light, detect if an LED is lit, and much more.<\/p>\n\n\n\n<p>We hope you&#8217;ve found this tutorial useful.<\/p>\n\n\n\n<p>We have tutorials for other sensors with the ESP32\/ESP8266 using MicroPython that you may like:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-dht11-dht22-micropython-temperature-humidity-sensor\/\">MicroPython: ESP32\/ESP8266 with <strong>DHT11\/DHT22 <\/strong>Temperature and Humidity Sensor<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-bme280-esp32-esp8266\/\">MicroPython:<strong> BME280<\/strong> with ESP32 and ESP8266 (Pressure, Temperature, Humidity)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-bme680-esp32-esp8266\/\">MicroPython: <strong>BME680<\/strong> with ESP32 and ESP8266 (Temperature, Humidity, Pressure, Gas)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-ds18b20-esp32-esp8266\/\">MicroPython: <strong>DS18B20 <\/strong>Temperature Sensor with ESP32 and ESP8266<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-hc-sr04-ultrasonic-esp32-esp8266\/\">MicroPython: HC-SR04 <strong>Ultrasonic Sensor<\/strong> with ESP32 and ESP8266 (Measure distance)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-rcwl-0516-esp32-esp8266\/\">MicroPython: RCWL-0516 <strong>Microwave Radar Sensor<\/strong> \u2013 Detect Motion with ESP32\/ESP8266<\/a><\/li>\n<\/ul>\n\n\n\n<p>Learn more about MicroPython with our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-programming-with-esp32-and-esp8266\/\" title=\"\">MicroPython Programming with ESP32 and ESP8266 eBook<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32-esp8266-micropython\/\" title=\"\">Free MicroPython Tutorials and Guides<\/a><\/li>\n<\/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 tutorial, you\u2019ll learn how to use the BH1750 ambient light sensor with the ESP32 and ESP8266 boards programmed with MicroPython &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"MicroPython: BH1750 Ambient Light Sensor with ESP32\/ESP8266\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/micropython-bh1750-esp32-esp8266\/#more-150659\" aria-label=\"Read more about MicroPython: BH1750 Ambient Light Sensor with ESP32\/ESP8266\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":150660,"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":[310,309,264],"tags":[],"class_list":["post-150659","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-micropython","category-0-esp32-micropython","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/ESP32-ESP8266-BH1750-light-sensor-MicroPython.jpg?fit=1920%2C1080&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/150659","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=150659"}],"version-history":[{"count":7,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/150659\/revisions"}],"predecessor-version":[{"id":161107,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/150659\/revisions\/161107"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/150660"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=150659"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=150659"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=150659"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}