Skip to content

Instantly share code, notes, and snippets.

@elktros
Created July 24, 2018 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elktros/5136f7bfeccd0b82568068463c29409d to your computer and use it in GitHub Desktop.
Save elktros/5136f7bfeccd0b82568068463c29409d to your computer and use it in GitHub Desktop.
Code for interfacing BH1750 Ambient Light Sensor with Arduino.
/*
Connection:
BH1750 - Arduino
------------------
VCC - 5v
GND - GND
SCL - SCL(Analog Pin 5)
SDA - SDA(Analog Pin 4)
ADD - NC (or GND)
*/
#include <Wire.h>
#include<LiquidCrystal.h>
int BH1750address = 0x23;
byte buff[2];
LiquidCrystal lcd (7,6,5,4,3,2); //RS, E, D4, D5, D6, D7
void setup()
{
Wire.begin();
//Serial.begin(9600);
lcd.begin(16,2);
lcd.print(" BH1750 Light ");
lcd.setCursor(0,1);
lcd.print("Intensity Sensor");
delay(2000);
}
void loop()
{
int i;
uint16_t value=0;
BH1750_Init(BH1750address);
delay(200);
if(2==BH1750_Read(BH1750address))
{
value=((buff[0]<<8)|buff[1])/1.2;
lcd.clear();
lcd.print("Intensity in LUX");
lcd.setCursor(6,1);
lcd.print(value);
//Serial.print(val);
//Serial.println("[lux]");
}
delay(150);
}
int BH1750_Read(int address)
{
int i=0;
Wire.beginTransmission(address);
Wire.requestFrom(address, 2);
while(Wire.available())
{
buff[i] = Wire.read();
i++;
}
Wire.endTransmission();
return i;
}
void BH1750_Init(int address)
{
Wire.beginTransmission(address);
Wire.write(0x10);
Wire.endTransmission();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment