Using the BH1750 light sensor

This sensor can give a fairly accurate indication of light level and is available on the usual breakout board.

/home/wpcom/public_html/wp-content/blogs.dir/10d/72092500/files/2014/12/img_1554.jpg

Connection is as follows

Gnd to Arduino Gnd
ADD to Gnd for 0x23 or 5volts for 0x5C address
SDA to Arduino A4
SCL to Arduino A5
Vcc to Arduino 5volts

The sketch I used reports light levels in lux units, the Serial Monitor needs to be set to 9600.

// Measuring Light Intensity with the BH1750 board
// Connect Gnd --> Gnd
// Connect ADD --> Gnd for address 0x23 or leave unconnected
// Connect ADD --> Vcc 5v for address 0x5C
// Connect SDA --> A4
// Connect SCL --> A5
// Connect Vcc --> 5 volts

int ledPin =13; // onboard LED

#include //BH1750 IIC Mode
int BH1750address = 0x23; //setting i2c address with ADD LOW
//int BH1750address = 0x5C; //setting i2c address with ADD HIGH

byte buff[2];
void setup()
{
Wire.begin();
Serial.begin(9600);//init Serial band rate
pinMode(ledPin, OUTPUT);
}

void loop()
{
int i;
uint16_t val=0;
BH1750_Init(BH1750address);
delay(200);

if(2==BH1750_Read(BH1750address))
{
val=((buff[0]< 1000) { // set the level here
digitalWrite(ledPin,HIGH); // turn on the onboard LED if light level above a certain amount
}
else {
digitalWrite(ledPin, LOW);
}
}
delay(150);
}

int BH1750_Read(int address) //
{
int i=0;
Wire.beginTransmission(address);
Wire.requestFrom(address, 2);
while(Wire.available()) //
{
buff[i] = Wire.read(); // receive one byte
i++;
}
Wire.endTransmission();
return i;
}

void BH1750_Init(int address)
{
Wire.beginTransmission(address);
Wire.write(0x10);//1lx reolution 120ms
Wire.endTransmission();
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s