bh1750

package module
v0.0.0-...-1195122 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 22, 2018 License: MIT Imports: 8 Imported by: 0

README

BH1750 ambient light sensor

Build Status Go Report Card GoDoc MIT License

BH1750 (general specification) is a power effective ambient light sensor with spectral response close to human eye. Sensor returns measured ambient light value in lux units. Easily integrated with Arduino and Raspberry PI via i2c communication interface: image

Here is a library written in Go programming language for Raspberry PI and counterparts, which gives you in the output ambient light value (making all necessary i2c-bus interacting and values computing).

Golang usage

func main() {
	// Create new connection to i2c-bus on 0 line with address 0x23.
	// Use i2cdetect utility to find device address over the i2c-bus
	i2c, err := i2c.NewI2C(0x23, 0)
	if err != nil {
		log.Fatal(err)
	}
	defer i2c.Close()

	sensor := bh1750.NewBH1750()

	resolution := bh1750.HighResolution
	amb, err := sensor.MeasureAmbientLight(i2c, resolution)
	if err != nil {
		lg.Fatal(err)
	}
	log.Printf("Ambient light (%s) = %v lx", resolution, amb)

Getting help

GoDoc documentation

Installation

$ go get -u github.com/d2r2/go-bh1750

Troubleshooting

  • How to obtain fresh Golang installation to RPi device (either any RPi clone): If your RaspberryPI golang installation taken by default from repository is outdated, you may consider to install actual golang manually from official Golang site. Download tar.gz file containing armv6l in the name. Follow installation instructions.

  • How to enable I2C bus on RPi device: If you employ RaspberryPI, use raspi-config utility to activate i2c-bus on the OS level. Go to "Interfacing Options" menu, to active I2C bus. Probably you will need to reboot to load i2c kernel module. Finally you should have device like /dev/i2c-1 present in the system.

  • How to find I2C bus allocation and device address: Use i2cdetect utility in format "i2cdetect -y X", where X may vary from 0 to 5 or more, to discover address occupied by peripheral device. To install utility you should run apt install i2c-tools on debian-kind system. i2cdetect -y 1 sample output:

         0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
    00:          -- -- -- -- -- -- -- -- -- -- -- -- --
    10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    70: -- -- -- -- -- -- 76 --    
    

Contact

Please use Github issue tracker for filing bugs or feature requests.

License

Go-bh1750 is licensed under MIT License.

Documentation

Index

Constants

View Source
const (
	// No active state.
	CMD_POWER_DOWN = 0x00

	// Waiting for measurement command.
	CMD_POWER_ON = 0x01

	// Reset Data register value.
	// Reset command is not acceptable in Power Down mode.
	CMD_RESET = 0x07

	// Start measurement at 1lx resolution.
	// Measurement Time is typically 120ms.
	CMD_CONTINUOUSLY_H_RES_MODE = 0x10

	// Start measurement at 0.5lx resolution.
	// Measurement Time is typically 120ms.
	CMD_CONTINUOUSLY_H_RES_MODE2 = 0x11

	// Start measurement at 4lx resolution.
	// Measurement Time is typically 16ms.
	CMD_CONTINUOUSLY_L_RES_MODE = 0x13

	// Start measurement at 1lx resolution.
	// Measurement Time is typically 120ms.
	// It is automatically set to Power Down mode after measurement
	CMD_ONE_TIME_H_RES_MODE = 0x20

	// Start measurement at 0.5lx resolution.
	// Measurement Time is typically 120ms.
	// It is automatically set to Power Down mode after measurement.
	CMD_ONE_TIME_H_RES_MODE2 = 0x21

	// Start measurement at 4lx resolution.
	// Measurement Time is typically 16ms.
	// It is automatically set to Power Down mode after measurement.
	CMD_ONE_TIME_L_RES_MODE = 0x23

	// Change measurement time. 01000_MT[7,6,5]
	CMD_CHANGE_MEAS_TIME_HIGH = 0x40

	// Change measurement time. 011_MT[4,3,2,1,0]
	CMD_CHANGE_MEAS_TIME_LOW = 0x60
)

Command bytes

Variables

This section is empty.

Functions

This section is empty.

Types

type BH1750

type BH1750 struct {
	// contains filtered or unexported fields
}

BH1750 it's a sensor itself.

func NewBH1750

func NewBH1750() *BH1750

NewBH1750 return new sensor instance.

func (*BH1750) ChangeSensivityFactor

func (v *BH1750) ChangeSensivityFactor(i2c *i2c.I2C, factor byte) error

ChangeSensivityFactor used when you close sensor with protection cover, which change (ordinary decrease) expected amount of light falling on the sensor. In this case you should calibrate you sensor and find appropriate factor to get in output correct ambient light value. Be aware, that improving sensitivity will increase measurement time.

func (*BH1750) FetchMeasuredAmbientLight

func (v *BH1750) FetchMeasuredAmbientLight(i2c *i2c.I2C) (uint16, error)

FetchMeasuredAmbientLight return current average ambient light in lux. Previous command should be any continuous measurement initiation, otherwise error will be reported.

func (*BH1750) GetDefaultSensivityFactor

func (v *BH1750) GetDefaultSensivityFactor() byte

GetDefaultSensivityFactor return factor value used when your sensor have no any protection cover. This is default setting according to specification.

func (*BH1750) MeasureAmbientLight

func (v *BH1750) MeasureAmbientLight(i2c *i2c.I2C,
	resolution ResolutionMode) (uint16, error)

MeasureAmbientLight measure and return ambient light once in lux.

func (*BH1750) PowerDown

func (v *BH1750) PowerDown(i2c *i2c.I2C) error

PowerDown return register to idle state.

func (*BH1750) PowerOn

func (v *BH1750) PowerOn(i2c *i2c.I2C) error

PowerOn activate sensor.

func (*BH1750) Reset

func (v *BH1750) Reset(i2c *i2c.I2C) error

Reset clear ambient light register value.

func (*BH1750) StartMeasureAmbientLightContinuously

func (v *BH1750) StartMeasureAmbientLightContinuously(i2c *i2c.I2C,
	resolution ResolutionMode) (wait time.Duration, err error)

StartMeasureAmbientLightContinuously start continuous measurement process. Use FetchMeasuredAmbientLight to get average ambient light amount collected and calculated over a time. Use PowerDown to stop measurements and return sensor to idle state.

type ResolutionMode

type ResolutionMode int

ResolutionMode define sensor sensitivity and measure time. Be aware, that improving sensitivity lead to increasing of measurement time.

const (
	// LowResolution precision 4 lx, 16 ms measurement time
	LowResolution ResolutionMode = iota + 1
	// HighResolution precision 1 lx, 120 ms measurement time
	HighResolution
	// HighestResolution precision 0.5 lx, 120 ms measurement time
	HighestResolution
)

func (ResolutionMode) String

func (v ResolutionMode) String() string

String define stringer interface.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL