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)
|
||||
#######################################
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
Vera Arduino BH1750FVI Light sensor
|
||||
communicate using I2C Protocol
|
||||
this library enable 2 slave device addresses
|
||||
Main address 0x23
|
||||
secondary address 0x5C
|
||||
connect the sensor as follows :
|
||||
|
||||
VCC >>> 5V
|
||||
Gnd >>> Gnd
|
||||
ADDR >>> NC or GND
|
||||
SCL >>> A5
|
||||
SDA >>> A4
|
||||
|
||||
Contribution: idefix
|
||||
|
||||
*/
|
||||
|
||||
|
||||
#include <Sleep_n0m1.h>
|
||||
#include <SPI.h>
|
||||
#include <RF24.h>
|
||||
#include <EEPROM.h>
|
||||
#include <Sensor.h>
|
||||
#include <BH1750.h>
|
||||
#include <Wire.h>
|
||||
|
||||
// Set RADIO_ID to something unique in your sensor network (1-254)
|
||||
// or set to AUTO if you want gw to assign a RADIO_ID for you.
|
||||
#define RADIO_ID AUTO
|
||||
#define CHILD_ID_LIGHT 0
|
||||
#define LIGHT_SENSOR_ANALOG_PIN 0
|
||||
|
||||
BH1750 lightSensor;
|
||||
|
||||
unsigned long SLEEP_TIME = 30; // Sleep time between reads (in seconds)
|
||||
|
||||
Sensor gw(9,10);
|
||||
|
||||
int lastlux;
|
||||
Sleep sleep;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(BAUD_RATE); // Used to type in characters
|
||||
gw.begin(RADIO_ID);
|
||||
|
||||
// Register all sensors to gateway (they will be created as child devices)
|
||||
gw.sendSensorPresentation(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
|
||||
|
||||
lightSensor.begin();
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
gw.powerUp(); // Power up radio
|
||||
|
||||
uint16_t lux = lightSensor.readLightLevel();// Get Lux value
|
||||
Serial.println(lux);
|
||||
if (lux != lastlux) {
|
||||
gw.sendVariable(CHILD_ID_LIGHT, V_LIGHT_LEVEL, lux);
|
||||
lastlux = lux;
|
||||
}
|
||||
|
||||
// Power down the radio. Note that the radio will get powered back up
|
||||
// on the next write() call.
|
||||
delay(1000); //delay to allow serial to fully print before sleep
|
||||
gw.powerDown();
|
||||
sleep.pwrDownMode(); //set sleep mode
|
||||
sleep.sleepDelay(SLEEP_TIME * 1000); //sleep for: sleepTime
|
||||
}
|
||||
Reference in New Issue
Block a user