Advertisement
microrobotics

BH1750 I2C Light Sensor Module

Apr 4th, 2023
2,394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. To interface the BH1750 I2C Light Sensor Module with an Arduino, you'll need to install the BH1750 library. You can install it through the Arduino IDE by going to Sketch > Include Library > Manage Libraries, then search for "BH1750" and install the library by ZHomeS (or other compatible libraries).
  3.  
  4. Here's an example code for interfacing the BH1750 I2C Light Sensor Module with an Arduino:
  5.  
  6. This code initializes the BH1750 I2C Light Sensor Module in continuous high-resolution mode and reads the light level in lux every second. It then prints the light level to the Serial Monitor.
  7.  
  8. Make sure to connect the BH1750 module to the Arduino as follows:
  9.  
  10. VCC to 5V (or 3.3V, depending on your sensor's voltage level)
  11. GND to GND
  12. SDA to the SDA pin on your Arduino (A4 on the Uno, Leonardo, or Nano; pin 20 on the Mega)
  13. SCL to the SCL pin on your Arduino (A5 on the Uno, Leonardo, or Nano; pin 21 on the Mega)
  14. Please note that different BH1750 modules might have slightly different pin arrangements, so verify the pin labels on your specific module before making connections.
  15. */
  16.  
  17. #include <Arduino.h>
  18. #include <Wire.h>
  19. #include <BH1750.h>
  20.  
  21. BH1750 lightSensor;
  22.  
  23. void setup() {
  24.   Serial.begin(9600);
  25.   Wire.begin();
  26.  
  27.   if (lightSensor.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
  28.     Serial.println("BH1750 initialized successfully");
  29.   } else {
  30.     Serial.println("Error initializing BH1750");
  31.     while (1);
  32.   }
  33. }
  34.  
  35. void loop() {
  36.   uint16_t lux = lightSensor.readLightLevel();
  37.   Serial.print("Light: ");
  38.   Serial.print(lux);
  39.   Serial.println(" lx");
  40.   delay(1000);
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement