Class: IoT::BH1750

Inherits:
DigitalReceptor show all
Defined in:
lib/iot/bh1750.rb

Overview

BH1750 - ambient light sensor

Constant Summary collapse

DEVICE =

I2C sensor address

0x23
POWER_DOWN =

off state

0x00
POWER_ON =

on state

0x01
RESET =

reset state: POWER_DOWN = 0x00

0x07
CONTINUOUS_LOW_RES_MODE =

Measure with resolution 4.0 lx and timing ~16 ms

0x13
CONTINUOUS_HIGH_RES_MODE_1 =

Measure with resolution 1.0 lx and timing ~120 ms

0x10
CONTINUOUS_HIGH_RES_MODE_2 =

Measure with resolution 0.5 lx and timing ~120 ms

0x11
ONE_TIME_LOW_RES_MODE =

Measure with resolution 4.0 lx, POWER DOWN after measuring

0x23
ONE_TIME_HIGH_RES_MODE_1 =

Measure with resolution 1.0 lx, POWER DOWN after measuring

0x20
ONE_TIME_HIGH_RES_MODE_2 =

Measure with resolution 0.5 lx, POWER DOWN after measuring

0x21

Instance Method Summary collapse

Methods inherited from DigitalReceptor

one_wire_file

Methods inherited from Receptor

#model_name

Constructor Details

#initialize(device_address = DEVICE, bus_number = 1) ⇒ BH1750

Returns a new instance of BH1750.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/iot/bh1750.rb', line 19

def initialize(device_address=DEVICE, bus_number=1)
  @sensor_name = 'BH1750'
  Bus::I2C.bus_number = bus_number
  @device_address = device_address

  @i2c_bus = Bus::I2C.bus
  @device = I2CDevice.new(address: DEVICE, driver: I2CDevice::Driver::I2CDev.new(@i2c_bus))
  @resolution = ONE_TIME_HIGH_RES_MODE_1
  @length = 2
  @value = read
end

Instance Method Details

#luxObject

Return value in Lux



38
39
40
41
# File 'lib/iot/bh1750.rb', line 38

def lux
  read
  @value
end

#nameObject



44
45
46
# File 'lib/iot/bh1750.rb', line 44

def name
  @sensor_name
end

#readObject

Read raw data from sensor and convert it to numeric



32
33
34
35
# File 'lib/iot/bh1750.rb', line 32

def read
  data = @device.i2cget(@resolution, @length)
  @value = to_f(data)
end