{"id":150642,"date":"2024-07-30T14:24:36","date_gmt":"2024-07-30T14:24:36","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=150642"},"modified":"2024-07-30T14:42:17","modified_gmt":"2024-07-30T14:42:17","slug":"raspberry-pi-pico-bh1750-micropython","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-bh1750-micropython\/","title":{"rendered":"Raspberry Pi Pico: BH1750 Ambient Light Sensor (MicroPython)"},"content":{"rendered":"\n<p>Learn to use the BH1750 ambient light sensor with the Raspberry Pi Pico programmed with MicroPython to get ambient light measurements. We&#8217;ll show you how to wire the sensor, install the required libraries, and get sensor measurements in LUX unit, the standardized 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\/Raspberry-Pi-Pico-BH1750-Micropython.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico BH1750 Ambient Light Sensor MicroPython\" class=\"wp-image-150655\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Raspberry-Pi-Pico-BH1750-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\/Raspberry-Pi-Pico-BH1750-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\/Raspberry-Pi-Pico-BH1750-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\/Raspberry-Pi-Pico-BH1750-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\/Raspberry-Pi-Pico-BH1750-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 class=\"rntbox rntclgreen\"><strong>New to the Raspberry Pi Pico?<\/strong>&nbsp;Read the following guide:&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/getting-started-raspberry-pi-pico-w\/\">Getting Started with Raspberry Pi Pico (and Pico W)<\/a>.<\/p>\n\n\n\n<p><strong>Table of Contents:<\/strong><\/p>\n\n\n\n<p>Throughout this tutorial, we\u2019ll cover the following subjects:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"#bh1750-intro\" title=\"\">Introducing the BH1750 Ambient Light Sensor<\/a><\/li>\n\n\n\n<li><a href=\"#rpi-pico-bh1750-wiring\" title=\"\">Wiring the BH1750 to the Raspberry Pi Pico<\/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=\"\">Raspberry Pi Pico &#8211; BH1750 Luminance (Lux) &#8211; MicroPython Code<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>Before proceeding with this tutorial, make sure you follow the next prerequisites.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"prerequisites\">MicroPython Firmware<\/h3>\n\n\n\n<p>To follow this tutorial you need MicroPython firmware installed in your Raspberry Pi Pico board. You also need an IDE to write and upload the code to your board.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"250\" height=\"250\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/micropython-logo.png?resize=150%2C150&amp;quality=100&amp;strip=all&amp;ssl=1\" alt=\"micorpython logo\" class=\"wp-image-148797\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/micropython-logo.png?w=250&amp;quality=100&amp;strip=all&amp;ssl=1 250w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/micropython-logo.png?resize=150%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 150w\" sizes=\"(max-width: 250px) 100vw, 250px\" \/><\/figure><\/div>\n\n\n<p>The recommended MicroPython IDE for the Raspberry Pi Pico is Thonny IDE. Follow the next tutorial to learn how to install Thonny IDE, flash MicroPython firmware, and upload code to the board.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-raspberry-pi-pico-w\/#install-thonny-ide\">Programming Raspberry Pi Pico using MicroPython<\/a><\/li>\n<\/ul>\n\n\n\n<p>Alternatively, if you like programming using VS Code, you can start with the following tutorial:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-vs-code-micropython\/\">Programming Raspberry Pi Pico with VS Code and MicroPython<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Parts Required<\/h3>\n\n\n\n<p>To follow this tutorial, you need the following parts:<\/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\/raspberry-pi-pico-w\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Raspberry Pi Pico or Pico W<\/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\n<h2 class=\"wp-block-heading\" id=\"bh1750-intro\">Introducing the 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 picture below. Both are BH1750 sensors.<\/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=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/BH1750-two-breakout-boards.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"BH1750 two different breakout boards\" class=\"wp-image-150645\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/BH1750-two-breakout-boards.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/BH1750-two-breakout-boards.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<h3 class=\"wp-block-heading\">BH1750 Features<\/h3>\n\n\n\n<p>Here\u2019s a list of the BH1750 sensor features. For more information consult the&nbsp;<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>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\">Measurement Modes<\/h3>\n\n\n\n<p>The sensor supports two different measurement modes:&nbsp;continuous measurement mode, and&nbsp;one-time&nbsp;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\">Applications<\/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=\"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\u2019s brightness accordingly to ambient light;<\/li>\n\n\n\n<li>to adjust LCDs and screen\u2019s brightness;<\/li>\n\n\n\n<li>to detect if an LED is lit;<\/li>\n\n\n\n<li>\u2026<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"rpi-pico-bh1750-wiring\">Wiring the BH1750 to the Raspberry Pi Pico<\/h2>\n\n\n\n<p>The BH1750 sensor uses I2C communication protocol to interface with microcontrollers. Take a look at its pinout in the table below.<\/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-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\u2019s the BH1750 Pinout, it\u2019s the same for both breakout boards:<\/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>GND<\/td><\/tr><tr><td><strong>SCL<\/strong><\/td><td>SCL pin for I2C communication<\/td><\/tr><tr><td><strong>SDA\/DAT<\/strong><\/td><td>SDA pin for I2C communication<\/td><\/tr><tr><td><strong>ADDR<\/strong><\/td><td>Selects address<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The ADDR 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\u00a0<strong>0x23<\/strong>. But, if the voltage is higher than 0.7xVCC (pin is connected to VCC), the address is\u00a0<strong>0x5C<\/strong>. In summary:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>ADDR pin floating or connected to\u00a0GND\u00a0\u2192 address:\u00a0<strong>0x23<\/strong><\/li>\n\n\n\n<li>ADDR pin connected to\u00a0VCC\u00a0\u2192 address:\u00a0<strong>0x5C<\/strong><\/li>\n<\/ul>\n\n\n\n<p>We\u2019ll wire the sensor to the Raspberry Pi Pico GPIO 4 (SDA) and GPIO 5 (SCL).<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>BH1750<\/strong><\/td><td><strong>Raspberry Pi Pico<\/strong><\/td><\/tr><tr><td>VCC<\/td><td>3V3(OUT)<\/td><\/tr><tr><td>GND<\/td><td>GND<\/td><\/tr><tr><td>SCL<\/td><td>GPIO 5<\/td><\/tr><tr><td>SDA<\/td><td>GPIO 4<\/td><\/tr><tr><td>ADDR*<\/td><td>Don\u2019t connect<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>By not connecting the&nbsp;ADDR&nbsp;pin, we\u2019re selecting&nbsp;<strong>0x23<\/strong>&nbsp;I2C address. However, it\u2019s always better to run an <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-i2c-scanner-micropython\/\" title=\"\">I2C scanner<\/a> to be sure:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-i2c-scanner-micropython\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Raspberry Pi Pico: I2C Scanner (MicroPython) \u2013 Finding the Address of I2C Devices<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Circuit Diagram<\/h3>\n\n\n\n<p>You can use the following diagram as a reference.<\/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=\"678\" height=\"629\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Raspberry-Pi-Pico-BH1750_bb.png?resize=678%2C629&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico and Pico W BH1750 Schematic Diagram\" class=\"wp-image-150644\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Raspberry-Pi-Pico-BH1750_bb.png?w=678&amp;quality=100&amp;strip=all&amp;ssl=1 678w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Raspberry-Pi-Pico-BH1750_bb.png?resize=300%2C278&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 678px) 100vw, 678px\" \/><\/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:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/blob\/master\/Projects\/Raspberry-Pi-Pico\/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 Pico board:<\/p>\n\n\n\n<p><strong>1)<\/strong> Copy the library code to a new file. The <a href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/blob\/master\/Projects\/Raspberry-Pi-Pico\/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\">\r\n# Micropython BH1750 ambient light sensor driver: https:\/\/github.com\/PinkInk\/upylib\/tree\/master\/bh1750\r\n\r\nfrom utime import sleep_ms\r\n\r\nclass BH1750():\r\n    &quot;&quot;&quot;Micropython BH1750 ambient light sensor driver.&quot;&quot;&quot;\r\n\r\n    PWR_OFF = 0x00\r\n    PWR_ON = 0x01\r\n    RESET = 0x07\r\n\r\n    # modes\r\n    CONT_LOWRES = 0x13\r\n    CONT_HIRES_1 = 0x10\r\n    CONT_HIRES_2 = 0x11\r\n    ONCE_HIRES_1 = 0x20\r\n    ONCE_HIRES_2 = 0x21\r\n    ONCE_LOWRES = 0x23\r\n\r\n    # default addr=0x23 if addr pin floating or pulled to ground\r\n    # addr=0x5c if addr pin pulled high\r\n    def __init__(self, bus, addr=0x23):\r\n        self.bus = bus\r\n        self.addr = addr\r\n        self.off()\r\n        self.reset()\r\n\r\n    def off(self):\r\n        &quot;&quot;&quot;Turn sensor off.&quot;&quot;&quot;\r\n        self.set_mode(self.PWR_OFF)\r\n\r\n    def on(self):\r\n        &quot;&quot;&quot;Turn sensor on.&quot;&quot;&quot;\r\n        self.set_mode(self.PWR_ON)\r\n\r\n    def reset(self):\r\n        &quot;&quot;&quot;Reset sensor, turn on first if required.&quot;&quot;&quot;\r\n        self.on()\r\n        self.set_mode(self.RESET)\r\n\r\n    def set_mode(self, mode):\r\n        &quot;&quot;&quot;Set sensor mode.&quot;&quot;&quot;\r\n        self.mode = mode\r\n        self.bus.writeto(self.addr, bytes([self.mode]))\r\n\r\n    def luminance(self, mode):\r\n        &quot;&quot;&quot;Sample luminance (in lux), using specified sensor mode.&quot;&quot;&quot;\r\n        # continuous modes\r\n        if mode &amp; 0x10 and mode != self.mode:\r\n            self.set_mode(mode)\r\n        # one shot modes\r\n        if mode &amp; 0x20:\r\n            self.set_mode(mode)\r\n        # earlier measurements return previous reading\r\n        sleep_ms(24 if mode in (0x13, 0x23) else 180)\r\n        data = self.bus.readfrom(self.addr, 2)\r\n        factor = 2.0 if mode in (0x11, 0x21) else 1.0\r\n        return (data[0]&lt;&lt;8 | data[1]) \/ (1.2 * factor)<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Raspberry-Pi-Pico\/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=\"569\" height=\"600\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/BH1750-micropython-library.png?resize=569%2C600&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"BH1750 micropython library thonny ide\" class=\"wp-image-150646\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/BH1750-micropython-library.png?w=569&amp;quality=100&amp;strip=all&amp;ssl=1 569w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/BH1750-micropython-library.png?resize=285%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 285w\" sizes=\"(max-width: 569px) 100vw, 569px\" \/><\/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>Raspberry Pi Pico<\/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=\"214\" height=\"203\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/05\/Save-file-to-Raspberry-Pi-Pico.png?resize=214%2C203&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Save Files to Raspberry Pi Pico Thonny IDE\" class=\"wp-image-130899\"\/><\/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\/bh1750-py-save.png?resize=545%2C327&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"saving bh1750.py to raspberry pi\" class=\"wp-image-150650\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/bh1750-py-save.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-py-save.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 Raspberry Pi Pico 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-saved-pico.png?resize=545%2C327&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"BH1750 file saved on the Raspberry Pi Pico board\" class=\"wp-image-150651\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/bh1750-saved-pico.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-saved-pico.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\">Raspberry Pi Pico &#8211; BH1750 Luminance (Lux) &#8211; Code<\/h2>\n\n\n\n<p>After uploading the library to the Raspberry Pi Pico, 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\/raspberry-pi-pico-bh1750-micropython\/\n\nfrom machine import Pin, SoftI2C\nfrom bh1750 import BH1750\nimport time\n\n# Initialize I2C communication\ni2c = 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\/Raspberry-Pi-Pico\/MicroPython\/BH1750_Basic.py\" target=\"_blank\">View raw code<\/a><\/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 5 and 4, the I2C pins that the sensor is connected to.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Initialize I2C communication\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.<\/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 Raspberry Pi Pico board. 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=\"651\" height=\"219\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/BH1750-light-sensor-demonstration.png?resize=651%2C219&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Micropython getting luminance values from BH1750 sensor with the Raspberry Pi Pico\" class=\"wp-image-150653\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/BH1750-light-sensor-demonstration.png?w=651&amp;quality=100&amp;strip=all&amp;ssl=1 651w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/BH1750-light-sensor-demonstration.png?resize=300%2C101&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 651px) 100vw, 651px\" \/><\/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\/2024\/03\/Raspberry-Pi-Pico-with-BH1750.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico with the BH1750 luminance sensor\" class=\"wp-image-150654\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Raspberry-Pi-Pico-with-BH1750.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Raspberry-Pi-Pico-with-BH1750.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 guide, you learned how to use the BH1750 luminance sensor with the Raspberry Pi Pico to get measurements of ambient light in LUX units.<\/p>\n\n\n\n<p>We have a similar guide for other boards:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-bh1750-ambient-light-sensor\/\" title=\"\"><strong>ESP32<\/strong> with BH1750 Ambient Light Sensor (Arduino IDE)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-bh1750-ambient-light-sensor\/\"><strong>ESP8266<\/strong> NodeMCU with BH1750 Ambient Light Sensor (Arduino IDE)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/arduino-bh1750-ambient-light-sensor\/\"><strong>Arduino<\/strong> with BH1750 Ambient Light Sensor<\/a><\/li>\n<\/ul>\n\n\n\n<p>We also have tutorials for other sensors with the Raspberry Pi Pico that you may like:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-bme280-micropython\/\">Raspberry Pi Pico: <strong>BME280<\/strong> Get Temperature, Humidity, and Pressure (MicroPython)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-rcwl-0516-micropython\/\">Raspberry Pi Pico: <strong>RCWL-0516 <\/strong>Microwave Radar <strong>Proximity <\/strong>Sensor<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-ds18b20-micropython\/\">Raspberry Pi Pico: <strong>DS18B20<\/strong> Temperature Sensor (MicroPython) \u2013 Single and Multiple<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-dht11-dht22-micropython\/\">Raspberry Pi Pico: <strong>DHT11\/DHT22<\/strong> Temperature and Humidity Sensor (MicroPython)<\/a><\/li>\n<\/ul>\n\n\n\n<p>We hope you found this tutorial useful. Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn to use the BH1750 ambient light sensor with the Raspberry Pi Pico programmed with MicroPython to get ambient light measurements. We&#8217;ll show you how to wire the sensor, install &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Raspberry Pi Pico: BH1750 Ambient Light Sensor (MicroPython)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-bh1750-micropython\/#more-150642\" aria-label=\"Read more about Raspberry Pi Pico: BH1750 Ambient Light Sensor (MicroPython)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":150655,"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":[324,326],"tags":[],"class_list":["post-150642","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-raspberry-pi-pico","category-raspberry-pi-pico-micropython"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/03\/Raspberry-Pi-Pico-BH1750-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\/150642","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=150642"}],"version-history":[{"count":7,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/150642\/revisions"}],"predecessor-version":[{"id":161224,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/150642\/revisions\/161224"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/150655"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=150642"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=150642"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=150642"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}