Posts with «gy-302» label

BH1750 Sensor: Measuring Lux with Arduino and the GY-302

The BH1750 sensor converts light intensity into a digital value and makes it available on the I2C bus. Its measurement range goes from 1 to 65,535 lux. On the GY-302 breakout the sensor comes ready to use with Arduino, ESP32 and other boards. AdriRobot tried it with an Arduino UNO and writes about it on his blog.

What the BH1750 sensor measures

The lux is the unit of measurement for illuminance: it corresponds to one lumen per square metre. It therefore describes the light falling on a surface, evaluated as the human eye perceives it. For reference, light reflected by a full Moon is about 1 lx, a bright office is about 400 lx, and sunlight ranges between 32,000 and 100,000 lx.

Measurement modes and resolution

The BH1750 works in six modes, divided into two groups: continuous measurement and single measurement. In continuous mode the sensor detects light without stopping. In single mode it takes one measurement and then shuts down, so it must be reconfigured before the next reading. The library used in the article does this automatically.

Each mode offers three levels of precision:

  • low resolution: 4 lx precision and 16 ms measurement time;
  • high resolution: 1 lx and 120 ms;
  • high resolution 2: 0.5 lx and 120 ms.

By default the library selects continuous high resolution. In low and high resolution modes the maximum value is 54,612.5 lx, while in high resolution 2 it stops at 27,306.25 lx. In practice the sensor returns a 16-bit integer, which the library converts into lux by dividing it by 1.2. A result of -1 means no valid data arrived, while -2 signals an unconfigured device.

You can also change the time interval in which the sensor counts light, through the MTreg register. The value ranges from 32 to 254 and the default is 69.

The GY-302 module: power, pinout and address

The GY-302 breakout with the BH1750 sensor and the pins to be soldered (photo: AdriRobot)

On the GY-302 the sensor is joined by a 3.3 V LDO regulator, marked 662K, and its capacitors. The module therefore accepts Arduino’s 5 V on the VCC pin. The regulator supplies up to 250 mA and withstands a maximum of 6.5 V at its input. On the SCL and SDA lines there are also two 4.7 kΩ pull-up resistors. The breakout is sold as a light intensity sensor.

The connector has five pins: VCC, GND, SCL, SDA and ADDR. The last one is used to choose the I2C address and comes into play when several BH1750 sensors share the same bus. With ADDR at a low level the address is 0x23, with ADDR high it becomes 0x5C. On the module tested the pin is tied to ground through a 4.7 kΩ resistor, so the sensor responds at 0x23.

  • measurement range: from 1 to 65,535 lux;
  • supply voltage: from 3 to 5 V;
  • operating temperature: from -40 to +85 °C;
  • dimensions: 18.5 × 13.9 × 2.5 mm, connectors excluded.

Connecting it to an Arduino UNO and an OLED display

Basic connection between Arduino UNO and GY-302 (diagram: AdriRobot)

For the first test you only need an Arduino UNO board, the GY-302 module and a few male-to-female jumper wires. VCC and GND go to the power supply, while SDA and SCL connect to Arduino’s I2C pins. Before uploading the sketch it is worth running the IDE’s i2c_scanner example: if the wiring is correct, the serial monitor reports a device at address 0x23.

The reading uses Christopher Laws’ BH1750 library, which is installed from the Library Manager together with the Wire library, already included in the IDE. The sketch reads the value every second and also writes it to the serial monitor.

The project is then completed with a 0.96-inch OLED display, 128×64 pixels, SSD1306 driver and I2C interface. The display responds at address 0x3C, so it shares the same bus as the sensor. The Adafruit SSD1306 and Adafruit GFX libraries are needed, and the lux meter shows the lux directly on the screen.

The lux meter with GY-302 and OLED display during the measurement (photo: AdriRobot)

Anyone who wants to see the BH1750 at work in a larger project can read the ESP32 weather station with a live web dashboard, which pairs it with a DHT22 and a BMP280.

Source: the original AdriRobot article also contains the complete schematic, the sketches and the test video.

Related products

The post BH1750 Sensor: Measuring Lux with Arduino and the GY-302 appeared first on Open Electronics.