Added example for BH1750 lux sensor
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
|
||||
This is a library for the BH1750FVI Digital Light Sensor
|
||||
breakout board.
|
||||
|
||||
The board uses I2C for communication. 2 pins are required to
|
||||
interface to the device.
|
||||
|
||||
|
||||
Written by Christopher Laws, March, 2013.
|
||||
|
||||
*/
|
||||
|
||||
#include "BH1750.h"
|
||||
#include <util/delay.h>
|
||||
|
||||
|
||||
BH1750::BH1750() {
|
||||
}
|
||||
|
||||
void BH1750::begin(uint8_t mode) {
|
||||
|
||||
Wire.begin();
|
||||
//write8(mode);
|
||||
configure(mode);
|
||||
}
|
||||
|
||||
|
||||
void BH1750::configure(uint8_t mode) {
|
||||
|
||||
switch (mode) {
|
||||
case BH1750_CONTINUOUS_HIGH_RES_MODE:
|
||||
case BH1750_CONTINUOUS_HIGH_RES_MODE_2:
|
||||
case BH1750_CONTINUOUS_LOW_RES_MODE:
|
||||
case BH1750_ONE_TIME_HIGH_RES_MODE:
|
||||
case BH1750_ONE_TIME_HIGH_RES_MODE_2:
|
||||
case BH1750_ONE_TIME_LOW_RES_MODE:
|
||||
// apply a valid mode change
|
||||
write8(mode);
|
||||
_delay_ms(10);
|
||||
break;
|
||||
default:
|
||||
// Invalid measurement mode
|
||||
#if BH1750_DEBUG == 1
|
||||
Serial.println("Invalid measurement mode");
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
uint16_t BH1750::readLightLevel(void) {
|
||||
|
||||
uint16_t level;
|
||||
|
||||
Wire.beginTransmission(BH1750_I2CADDR);
|
||||
Wire.requestFrom(BH1750_I2CADDR, 2);
|
||||
#if (ARDUINO >= 100)
|
||||
level = Wire.read();
|
||||
level <<= 8;
|
||||
level |= Wire.read();
|
||||
#else
|
||||
level = Wire.receive();
|
||||
level <<= 8;
|
||||
level |= Wire.receive();
|
||||
#endif
|
||||
Wire.endTransmission();
|
||||
|
||||
#if BH1750_DEBUG == 1
|
||||
Serial.print("Raw light level: ");
|
||||
Serial.println(level);
|
||||
#endif
|
||||
|
||||
level = level/1.2; // convert to lux
|
||||
|
||||
#if BH1750_DEBUG == 1
|
||||
Serial.print("Light level: ");
|
||||
Serial.println(level);
|
||||
#endif
|
||||
return level;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*********************************************************************/
|
||||
|
||||
|
||||
void BH1750::write8(uint8_t d) {
|
||||
Wire.beginTransmission(BH1750_I2CADDR);
|
||||
#if (ARDUINO >= 100)
|
||||
Wire.write(d);
|
||||
#else
|
||||
Wire.send(d);
|
||||
#endif
|
||||
Wire.endTransmission();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
|
||||
This is a library for the BH1750FVI Digital Light Sensor
|
||||
breakout board.
|
||||
|
||||
The board uses I2C for communication. 2 pins are required to
|
||||
interface to the device.
|
||||
|
||||
Datasheet:
|
||||
http://rohmfs.rohm.com/en/products/databook/datasheet/ic/sensor/light/bh1750fvi-e.pdf
|
||||
|
||||
Written by Christopher Laws, March, 2013.
|
||||
|
||||
*/
|
||||
|
||||
#ifndef BH1750_h
|
||||
#define BH1750_h
|
||||
|
||||
#if (ARDUINO >= 100)
|
||||
#include <Arduino.h>
|
||||
#else
|
||||
#include <WProgram.h>
|
||||
#endif
|
||||
#include "Wire.h"
|
||||
|
||||
#define BH1750_DEBUG 0
|
||||
|
||||
#define BH1750_I2CADDR 0x23
|
||||
|
||||
// No active state
|
||||
#define BH1750_POWER_DOWN 0x00
|
||||
|
||||
// Wating for measurment command
|
||||
#define BH1750_POWER_ON 0x01
|
||||
|
||||
// Reset data register value - not accepted in POWER_DOWN mode
|
||||
#define BH1750_RESET 0x07
|
||||
|
||||
// Start measurement at 1lx resolution. Measurement time is approx 120ms.
|
||||
#define BH1750_CONTINUOUS_HIGH_RES_MODE 0x10
|
||||
|
||||
// Start measurement at 0.5lx resolution. Measurement time is approx 120ms.
|
||||
#define BH1750_CONTINUOUS_HIGH_RES_MODE_2 0x11
|
||||
|
||||
// Start measurement at 4lx resolution. Measurement time is approx 16ms.
|
||||
#define BH1750_CONTINUOUS_LOW_RES_MODE 0x13
|
||||
|
||||
// Start measurement at 1lx resolution. Measurement time is approx 120ms.
|
||||
// Device is automatically set to Power Down after measurement.
|
||||
#define BH1750_ONE_TIME_HIGH_RES_MODE 0x20
|
||||
|
||||
// Start measurement at 0.5lx resolution. Measurement time is approx 120ms.
|
||||
// Device is automatically set to Power Down after measurement.
|
||||
#define BH1750_ONE_TIME_HIGH_RES_MODE_2 0x21
|
||||
|
||||
// Start measurement at 1lx resolution. Measurement time is approx 120ms.
|
||||
// Device is automatically set to Power Down after measurement.
|
||||
#define BH1750_ONE_TIME_LOW_RES_MODE 0x23
|
||||
|
||||
class BH1750 {
|
||||
public:
|
||||
BH1750();
|
||||
void begin(uint8_t mode = BH1750_CONTINUOUS_HIGH_RES_MODE);
|
||||
void configure(uint8_t mode);
|
||||
uint16_t readLightLevel(void);
|
||||
|
||||
private:
|
||||
void write8(uint8_t data);
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,58 @@
|
||||
BH1750
|
||||
======
|
||||
|
||||
An Arduino library for digital light sensor breakout boards containing the BH1750FVI IC.
|
||||
|
||||
The board uses I2C for communication.
|
||||
|
||||
2 pins are required to interface to the device.
|
||||
|
||||
Datasheet for the light sensor:
|
||||
http://rohmfs.rohm.com/en/products/databook/datasheet/ic/sensor/light/bh1750fvi-e.pdf
|
||||
|
||||
|
||||
To download, click the ZIP button above. Extract the zip file and rename the uncompressed folder to BH1750. Check that it contains BH1750.cpp and BH1750.h, keywords.txt.
|
||||
|
||||
Place the BH1750 library folder your <arduinosketchfolder>/libraries/ folder. Restart the IDE.
|
||||
|
||||
|
||||
Example usage:
|
||||
```c
|
||||
/*
|
||||
|
||||
Example of BH1750 library usage.
|
||||
|
||||
This example initalises the BH1750 object using the default
|
||||
high resolution mode and then makes a light level reading every second.
|
||||
|
||||
Connection:
|
||||
VCC-5v
|
||||
GND-GND
|
||||
SCL-SCL(analog pin 5)
|
||||
SDA-SDA(analog pin 4)
|
||||
ADD-NC or GND
|
||||
|
||||
*/
|
||||
|
||||
#include <Wire.h>
|
||||
#include <BH1750.h>
|
||||
|
||||
|
||||
BH1750 lightMeter;
|
||||
|
||||
|
||||
void setup(){
|
||||
Serial.begin(9600);
|
||||
lightMeter.begin();
|
||||
Serial.println("Running...");
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
uint16_t lux = lightMeter.readLightLevel();
|
||||
Serial.print("Light: ");
|
||||
Serial.print(lux);
|
||||
Serial.println(" lx");
|
||||
delay(1000);
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
|
||||
Example of BH1750 library usage.
|
||||
|
||||
This example initalises the BH1750 object using the default
|
||||
high resolution mode and then makes a light level reading every second.
|
||||
|
||||
Connection:
|
||||
VCC-5v
|
||||
GND-GND
|
||||
SCL-SCL(analog pin 5)
|
||||
SDA-SDA(analog pin 4)
|
||||
ADD-NC or GND
|
||||
|
||||
*/
|
||||
|
||||
#include <Wire.h>
|
||||
#include <BH1750.h>
|
||||
|
||||
|
||||
BH1750 lightMeter;
|
||||
|
||||
|
||||
void setup(){
|
||||
Serial.begin(9600);
|
||||
lightMeter.begin();
|
||||
Serial.println("Running...");
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
uint16_t lux = lightMeter.readLightLevel();
|
||||
Serial.print("Light: ");
|
||||
Serial.print(lux);
|
||||
Serial.println(" lx");
|
||||
delay(1000);
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
#######################################
|
||||
# Syntax Coloring Map For BH1750
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
BH1750 KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
begin KEYWORD2
|
||||
configure KEYWORD2
|
||||
readLightLevel KEYWORD2
|
||||
|
||||
|
||||
#######################################
|
||||
# Instances (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
#######################################
|
||||
Reference in New Issue
Block a user