Remarks

Bh1750
Status Status badge: working
Source code GitHub
Datasheet(s) GitHub
NuGet package NuGet Gallery for Bh1750

The BH1750 is a light intensity sensor that communicates over I2C.

Code Example

Bh1750 sensor;

public MeadowApp()
{
    Console.WriteLine("Initializing...");

    var i2c = Device.CreateI2cBus();
    sensor = new Bh1750(
        i2c,
        measuringMode: Mode.ContinuouslyHighResolutionMode, // the various modes take differing amounts of time.
        lightTransmittance: 1 // lower this to increase sensitivity, for instance, if it's behind a semi opaque window
        ); 

    // Example that uses an IObservable subscription to only be notified when the filter is satisfied
    var consumer = Bh1750.CreateObserver(
        handler: result => Console.WriteLine($"Observer: filter satisifed: {result.New.Lux:N2}Lux, old: {result.Old?.Lux:N2}Lux"),

        // only notify if the visible light changes by 100 lux (put your hand over the sensor to trigger)
        filter: result => {
            if (result.Old is { } old) { //c# 8 pattern match syntax. checks for !null and assigns var.
                // returns true if > 100lux change
                return ((result.New - old).Abs().Lux > 100);
            }
            return false;
        });
    sensor.Subscribe(consumer);

    //==== Events
    // classical .NET events can also be used:
    sensor.Updated += (sender, result) => Console.WriteLine($"Light: {result.New.Lux:N2}Lux");

    //==== one-off read
    ReadConditions().Wait();

    // start updating continuously
    sensor.StartUpdating(TimeSpan.FromSeconds(1));
}

protected async Task ReadConditions()
{
    var result = await sensor.Read();
    Console.WriteLine("Initial Readings:");
    Console.WriteLine($"   Light: {result.Lux:N2}Lux");
}

Sample project(s) available on GitHub

Wiring Example

To wire a Bh1750 to your Meadow board, connect the following:

Bh1750 Meadow Pin
GND GND
SCL D08 (SCL)
SDA D07 (SDA)
VCC 3V3

It should look like the following diagram:

Characteristic Locus
Inheritance System.Object ObservableBase<Illuminance> SensorBase<Illuminance> SamplingSensorBase<Illuminance> ByteCommsSensorBase<Illuminance> > Bh1750
Implements IDisposable ILightSensor
Inherited Members ByteCommsSensorBase<Illuminance>.Peripheral ByteCommsSensorBase<Illuminance>.ReadBuffer ByteCommsSensorBase<Illuminance>.WriteBuffer ByteCommsSensorBase<Illuminance>.Init(Int32, Int32) ByteCommsSensorBase<Illuminance>.Dispose(Boolean) ByteCommsSensorBase<Illuminance>.Dispose() SamplingSensorBase<Illuminance>.StartUpdating(Nullable<TimeSpan>) SamplingSensorBase<Illuminance>.StopUpdating() SensorBase<Illuminance>.Updated SensorBase<Illuminance>.samplingLock SensorBase<Illuminance>.SamplingTokenSource SensorBase<Illuminance>.Conditions SensorBase<Illuminance>.IsSampling SensorBase<Illuminance>.UpdateInterval SensorBase<Illuminance>.RaiseEventsAndNotify(IChangeResult<Illuminance>) SensorBase<Illuminance>.Read() ObservableBase<Illuminance>.observers ObservableBase<Illuminance>.NotifyObservers(IChangeResult<Illuminance>) Meadow.Foundation.ObservableBase<Illuminance>.Subscribe(IObserver<>) Meadow.Foundation.ObservableBase<Illuminance>.CreateObserver(Action<>, System.Nullable<Predicate<Meadow.IChangeResult<UNIT>>>)
Namespace Meadow.Foundation.Sensors.Light
Assembly Bh1750.dll

Syntax

public class Bh1750 : ByteCommsSensorBase<Illuminance>, IDisposable, ILightSensor

Constructors

Bh1750(II2cBus, Byte, Bh1750.MeasuringModes, Double)

Create a new BH1750 light sensor object using a static reference voltage.

Declaration
public Bh1750(II2cBus i2cBus, byte address = null, Bh1750.MeasuringModes measuringMode = default(Bh1750.MeasuringModes), double lightTransmittance = null)

Parameters

Type Name Description
II2cBus i2cBus
System.Byte address
Bh1750.MeasuringModes measuringMode
System.Double lightTransmittance

Properties

Illuminance

Declaration
public Illuminance? Illuminance { get; }

Property Value

Type Description
System.Nullable<Illuminance>

LightTransmittance

BH1750 Light Transmittance (27.20-222.50%)

Declaration
public double LightTransmittance { get; set; }

Property Value

Type Description
System.Double

MeasuringMode

BH1750 Measuring Mode

Declaration
public Bh1750.MeasuringModes MeasuringMode { get; set; }

Property Value

Type Description
Bh1750.MeasuringModes

Methods

RaiseEventsAndNotify(IChangeResult<Illuminance>)

Declaration
protected override void RaiseEventsAndNotify(IChangeResult<Illuminance> changeResult)

Parameters

Type Name Description
IChangeResult<Illuminance> changeResult

ReadSensor()

Declaration
protected override Task<Illuminance> ReadSensor()

Returns

Type Description
Task<Illuminance>

Overrides

Meadow.Foundation.SensorBase<Illuminance>.ReadSensor()

Events

LuminosityUpdated

Declaration
public event EventHandler<IChangeResult<Illuminance>> LuminosityUpdated

Event Type

Type Description
EventHandler<IChangeResult<Illuminance>>