Added a bh1750 light sensor module to test the 12c Functions and to expriment with the overall structure
parent
016f1ad4e6
commit
805597a71b
@ -0,0 +1,61 @@
|
||||
#include "bh1750.h"
|
||||
|
||||
|
||||
I2C_Driver i2c(1,1);
|
||||
|
||||
Bh1750::Bh1750()
|
||||
{
|
||||
currentMode = 0; // no mode selected
|
||||
}
|
||||
|
||||
uint8_t Bh1750::sleep()
|
||||
{
|
||||
i2c.writeByte(BH1750_ADDR,BH1750_POWER_DOWN);
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t Bh1750::wake()
|
||||
{
|
||||
i2c.writeByte(BH1750_ADDR,BH1750_POWER_ON);
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t Bh1750::reset()
|
||||
{
|
||||
i2c.writeByte(BH1750_ADDR,BH1750_RESET);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
float Bh1750::oneShot(uint8_t mode)
|
||||
{
|
||||
if(mode > 0)
|
||||
{
|
||||
if( mode == BH1750_ONE_TIME_HIGH_RES_MODE_1 ||
|
||||
mode == BH1750_ONE_TIME_HIGH_RES_MODE_2 ||
|
||||
mode == BH1750_ONE_TIME_LOW_RES_MODE)
|
||||
{
|
||||
return i2c.readWord(BH1750_ADDR,mode) / 1.2 ;
|
||||
|
||||
}
|
||||
}
|
||||
std::cout<< "please seelct a one shot mode "<< std::endl;
|
||||
exit(1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
float Bh1750::continious(uint8_t mode, uint8_t delayMs)
|
||||
{
|
||||
if(mode > 0)
|
||||
{
|
||||
if( mode == BH1750_CONTINUOUS_HIGH_RES_MODE_1 ||
|
||||
mode == BH1750_CONTINUOUS_HIGH_RES_MODE_2 ||
|
||||
mode == BH1750_CONTINUOUS_LOW_RES_MODE)
|
||||
{
|
||||
return i2c.readWord(BH1750_ADDR,mode) / 1.2;
|
||||
}
|
||||
}
|
||||
std::cout<< "please seelct a continious mode "<< std::endl;
|
||||
exit(1);
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
|
||||
#ifndef _BH1750_H_
|
||||
#define _BH1750_H_
|
||||
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
#include <string>
|
||||
#include "i2c_driver.h"
|
||||
|
||||
|
||||
//Start measurement at 4lx resolution. Time typically 16ms.
|
||||
#define BH1750_CONTINUOUS_LOW_RES_MODE 0x13
|
||||
//Start measurement at 1lx resolution. Time typically 120ms
|
||||
#define BH1750_CONTINUOUS_HIGH_RES_MODE_1 0x10
|
||||
//Start measurement at 0.5lx resolution. Time typically 120ms
|
||||
#define BH1750_CONTINUOUS_HIGH_RES_MODE_2 0x11
|
||||
//Start measurement at 1lx resolution. Time typically 120ms
|
||||
//Device is automatically set to Power Down after measurement.
|
||||
#define BH1750_ONE_TIME_HIGH_RES_MODE_1 0x20
|
||||
//Start measurement at 0.5lx resolution. Time typically 120ms
|
||||
//Device is automatically set to Power Down after measurement.
|
||||
#define BH1750_ONE_TIME_HIGH_RES_MODE_2 0x21
|
||||
//Start measurement at 1lx resolution. Time typically 120ms
|
||||
//Device is automatically set to Power Down after measurement.
|
||||
#define BH1750_ONE_TIME_LOW_RES_MODE 0x23
|
||||
|
||||
#define BH1750_POWER_DOWN 0x00 // No active state
|
||||
#define BH1750_POWER_ON 0x01 // Power on
|
||||
#define BH1750_RESET 0x07 // Reset data register value
|
||||
|
||||
#define BH1750_ADDR 0x23 // Device Adress
|
||||
|
||||
class Bh1750
|
||||
{
|
||||
public:
|
||||
Bh1750();
|
||||
uint8_t sleep(); // To be testes
|
||||
uint8_t wake(); // To be tested
|
||||
uint8_t reset(); // To be tested
|
||||
float oneShot(uint8_t mode); // ok
|
||||
float continious(uint8_t mode, uint8_t delayMs); // IMplment delay or make a delay class ???
|
||||
private:
|
||||
uint8_t high;
|
||||
uint8_t low;
|
||||
uint8_t currentMode;
|
||||
uint8_t currentState;
|
||||
};
|
||||
|
||||
#endif // _BH1750_H_
|
@ -0,0 +1,69 @@
|
||||
#!/usr/bin/python
|
||||
#---------------------------------------------------------------------
|
||||
# ___ ___ _ ____
|
||||
# / _ \/ _ \(_) __/__ __ __
|
||||
# / , _/ ___/ /\ \/ _ \/ // /
|
||||
# /_/|_/_/ /_/___/ .__/\_, /
|
||||
# /_/ /___/
|
||||
#
|
||||
# bh1750.py
|
||||
# Read data from a BH1750 digital light sensor.
|
||||
#
|
||||
# Author : Matt Hawkins
|
||||
# Date : 26/06/2018
|
||||
#
|
||||
# For more information please visit :
|
||||
# https://www.raspberrypi-spy.co.uk/?s=bh1750
|
||||
#
|
||||
#---------------------------------------------------------------------
|
||||
import smbus
|
||||
import time
|
||||
|
||||
# Define some constants from the datasheet
|
||||
|
||||
DEVICE = 0x23 # Default device I2C address
|
||||
|
||||
POWER_DOWN = 0x00 # No active state
|
||||
POWER_ON = 0x01 # Power on
|
||||
RESET = 0x07 # Reset data register value
|
||||
|
||||
# Start measurement at 4lx resolution. Time typically 16ms.
|
||||
CONTINUOUS_LOW_RES_MODE = 0x13
|
||||
# Start measurement at 1lx resolution. Time typically 120ms
|
||||
CONTINUOUS_HIGH_RES_MODE_1 = 0x10
|
||||
# Start measurement at 0.5lx resolution. Time typically 120ms
|
||||
CONTINUOUS_HIGH_RES_MODE_2 = 0x11
|
||||
# Start measurement at 1lx resolution. Time typically 120ms
|
||||
# Device is automatically set to Power Down after measurement.
|
||||
ONE_TIME_HIGH_RES_MODE_1 = 0x20
|
||||
# Start measurement at 0.5lx resolution. Time typically 120ms
|
||||
# Device is automatically set to Power Down after measurement.
|
||||
ONE_TIME_HIGH_RES_MODE_2 = 0x21
|
||||
# Start measurement at 1lx resolution. Time typically 120ms
|
||||
# Device is automatically set to Power Down after measurement.
|
||||
ONE_TIME_LOW_RES_MODE = 0x23
|
||||
|
||||
#bus = smbus.SMBus(0) # Rev 1 Pi uses 0
|
||||
bus = smbus.SMBus(1) # Rev 2 Pi uses 1
|
||||
|
||||
def convertToNumber(data):
|
||||
# Simple function to convert 2 bytes of data
|
||||
# into a decimal number. Optional parameter 'decimals'
|
||||
# will round to specified number of decimal places.
|
||||
result=(data[1] + (256 * data[0])) / 1.2
|
||||
return (result)
|
||||
|
||||
def readLight(addr=DEVICE):
|
||||
# Read data from I2C interface
|
||||
data = bus.read_i2c_block_data(addr,ONE_TIME_HIGH_RES_MODE_1)
|
||||
return convertToNumber(data)
|
||||
|
||||
def main():
|
||||
|
||||
while True:
|
||||
lightLevel=readLight()
|
||||
print("Light Level : " + format(lightLevel,'.2f') + " lx")
|
||||
time.sleep(0.5)
|
||||
|
||||
if __name__=="__main__":
|
||||
main()
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue