Advertisement
MELAMOURI

Untitled

May 29th, 2023
686
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.15 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2. #include <Wire.h>
  3.  
  4. LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
  5. int BH1750address = 0x23;
  6. byte buff[2];
  7.  
  8. void setup() {
  9.   Wire.begin();
  10.   lcd.begin(16, 2);
  11.   lcd.print("  BH1750 Light  ");
  12.   lcd.setCursor(0, 1);
  13.   lcd.print("Intensity Sensor");
  14.   delay(2000);
  15.   lcd.clear();
  16. }
  17.  
  18. void loop() {
  19.   int i;
  20.   uint16_t value = 0;
  21.   BH1750_Init(BH1750address);
  22.   delay(200);
  23.  
  24.   if (2 == BH1750_Read(BH1750address)) {
  25.     value = ((buff[0] << 8) | buff[1]) / 1.2;
  26.     lcd.setCursor(0, 0);
  27.     lcd.print("Intensity in LUX");
  28.     lcd.setCursor(6, 1);
  29.     lcd.print(value);
  30.     lcd.print("   ");
  31.   }
  32.   delay(150);
  33. }
  34.  
  35. int BH1750_Read(int address) {
  36.   int i = 0;
  37.   Wire.beginTransmission(address);
  38.   Wire.requestFrom(address, 2);
  39.   while (Wire.available()) {
  40.     buff[i] = Wire.read();
  41.     i++;
  42.   }
  43.   Wire.endTransmission();
  44.   return i;
  45. }
  46.  
  47. void BH1750_Init(int address) {
  48.   Wire.beginTransmission(address);
  49.   Wire.write(0x10);  // Power on the sensor
  50.   Wire.endTransmission();
  51.  
  52.   Wire.beginTransmission(address);
  53.   Wire.write(0x11);  // Set measurement mode to continuous high-resolution mode
  54.   Wire.endTransmission();
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement