Remarks

Bh1745
Status Status badge: working
Source code GitHub
Datasheet(s) GitHub
NuGet package NuGet Gallery for Meadow.Foundation.Sensors.Light.Bh1745

The BH1745 is a RGB color and luminance sensor that communicates over I2C.

Code Example

Bh1745 sensor;
RgbPwmLed rgbLed;

public override Task Initialize()
{
    Console.WriteLine("Initialize...");

    sensor = new Bh1745(Device.CreateI2cBus());

    // instantiate our onboard LED that we'll show the color with
    rgbLed = new RgbPwmLed(
        Device,
        Device.Pins.OnboardLedRed,
        Device.Pins.OnboardLedGreen,
        Device.Pins.OnboardLedBlue,
        commonType: CommonType.CommonAnode);

    // Example that uses an IObservable subscription to only be notified
    var consumer = Bh1745.CreateObserver(
        handler: result => Console.WriteLine($"Observer: filter satisifed: {result.New.AmbientLight?.Lux:N2}Lux, old: {result.Old?.AmbientLight?.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.AmbientLight.Value - old.AmbientLight.Value).Abs().Lux > 100);
            }
            return false;
        });

    sensor.Subscribe(consumer);

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

        if(result.New.Color is { } color) 
        {
            rgbLed.SetColor(color); 
        }
    };

    return Task.CompletedTask;
}

public override async Task Run()
{
    var result = await sensor.Read();

    Console.WriteLine("Initial Readings:");
    Console.WriteLine($" Visible Light: {result.AmbientLight?.Lux:N2}Lux");
    Console.WriteLine($" Color: {result.Color}");

    if (result.Color is { } color) 
    {
        rgbLed.SetColor(color); 
    }

    sensor.StartUpdating(TimeSpan.FromSeconds(1));
}

Sample project(s) available on GitHub

Wiring Example

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

Bh1745 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<System.ValueTuple<System.Nullable<Illuminance>, System.Nullable<Color>, System.Boolean>> SensorBase<System.ValueTuple<System.Nullable<Illuminance>, System.Nullable<Color>, System.Boolean>> SamplingSensorBase<System.ValueTuple<System.Nullable<Illuminance>, System.Nullable<Color>, System.Boolean>> ByteCommsSensorBase<System.ValueTuple<System.Nullable<Illuminance>, System.Nullable<Color>, System.Boolean>> > Bh1745
Implements IDisposable ILightSensor
Inherited Members ByteCommsSensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.Peripheral ByteCommsSensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.ReadBuffer ByteCommsSensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.WriteBuffer ByteCommsSensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.Init(Int32, Int32) ByteCommsSensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.Dispose(Boolean) ByteCommsSensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.Dispose() SamplingSensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.StartUpdating(Nullable<TimeSpan>) SamplingSensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.StopUpdating() SensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.Updated SensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.samplingLock SensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.SamplingTokenSource SensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.Conditions SensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.IsSampling SensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.UpdateInterval SensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.ReadSensor() SensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.RaiseEventsAndNotify(IChangeResult<>) SensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.Read() ObservableBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.observers ObservableBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.NotifyObservers(IChangeResult<>) Meadow.Foundation.ObservableBase<System.ValueTuple<System.Nullable<Illuminance>, System.Nullable<Meadow.Foundation.Color>, System.Boolean>>.Subscribe(IObserver<>) Meadow.Foundation.ObservableBase<System.ValueTuple<System.Nullable<Illuminance>, System.Nullable<Meadow.Foundation.Color>, System.Boolean>>.CreateObserver(Action<>, System.Nullable<Predicate<IChangeResult<UNIT>>>)
Namespace Meadow.Foundation.Sensors.Light
Assembly Bh1745.dll

Syntax

public class Bh1745 : ByteCommsSensorBase<(Illuminance? AmbientLight, Color? Color, bool Valid)>, IDisposable, ILightSensor

Constructors

Bh1745(II2cBus, Byte)

Create a new BH17545 color sensor object

Declaration
public Bh1745(II2cBus i2cBus, byte address = null)

Parameters

Type Name Description
II2cBus i2cBus
System.Byte address

Fields

adcGain

Declaration
protected Bh1745.AdcGainTypes adcGain

Field Value

Type Description
Bh1745.AdcGainTypes

interruptPersistence

Declaration
protected Bh1745.InterruptTypes interruptPersistence

Field Value

Type Description
Bh1745.InterruptTypes

interruptSource

Declaration
protected Bh1745.InterruptChannels interruptSource

Field Value

Type Description
Bh1745.InterruptChannels

isInterruptEnabled

Declaration
protected bool isInterruptEnabled

Field Value

Type Description
System.Boolean

isMeasurementActive

Declaration
protected bool isMeasurementActive

Field Value

Type Description
System.Boolean

latchBehavior

Declaration
protected Bh1745.LatchBehaviorTypes latchBehavior

Field Value

Type Description
Bh1745.LatchBehaviorTypes

lowerInterruptThreshold

Declaration
protected ushort lowerInterruptThreshold

Field Value

Type Description
System.UInt16

measurementTime

Declaration
protected Bh1745.MeasurementTimeType measurementTime

Field Value

Type Description
Bh1745.MeasurementTimeType

upperInterruptThreshold

Declaration
protected ushort upperInterruptThreshold

Field Value

Type Description
System.UInt16

Properties

AdcGain

Gets or sets the ADC gain of the sensor

Declaration
public Bh1745.AdcGainTypes AdcGain { get; set; }

Property Value

Type Description
Bh1745.AdcGainTypes

CompensationMultipliers

Gets or sets the channel compensation multipliers which are used to scale the channel measurements

Declaration
public Bh1745.ChannelMultipliers CompensationMultipliers { get; set; }

Property Value

Type Description
Bh1745.ChannelMultipliers

Illuminance

Declaration
public Illuminance? Illuminance { get; }

Property Value

Type Description
System.Nullable<Illuminance>

InterruptIsEnabled

Gets or sets whether the interrupt pin is enabled

Declaration
public bool InterruptIsEnabled { get; set; }

Property Value

Type Description
System.Boolean

InterruptPersistence

Gets or sets the persistence function of the interrupt

Declaration
public Bh1745.InterruptTypes InterruptPersistence { get; set; }

Property Value

Type Description
Bh1745.InterruptTypes

InterruptReset

Declaration
public Bh1745.InterruptStatus InterruptReset { get; set; }

Property Value

Type Description
Bh1745.InterruptStatus

InterruptSignalIsActive

Is the interrupt active

Declaration
public bool InterruptSignalIsActive { get; }

Property Value

Type Description
System.Boolean

InterruptSource

Gets or sets the source channel that triggers the interrupt

Declaration
public Bh1745.InterruptChannels InterruptSource { get; set; }

Property Value

Type Description
Bh1745.InterruptChannels

IsMeasurementActive

Is the sensor actively measuring

Declaration
public bool IsMeasurementActive { get; set; }

Property Value

Type Description
System.Boolean

LatchBehavior

Gets or sets how the interrupt pin latches

Declaration
public Bh1745.LatchBehaviorTypes LatchBehavior { get; set; }

Property Value

Type Description
Bh1745.LatchBehaviorTypes

LowerInterruptThreshold

Gets or sets the lower interrupt threshold

Declaration
public ushort LowerInterruptThreshold { get; set; }

Property Value

Type Description
System.UInt16

MeasurementTime

Gets or sets the currently set measurement time.

Declaration
public Bh1745.MeasurementTimeType MeasurementTime { get; set; }

Property Value

Type Description
Bh1745.MeasurementTimeType

UpperInterruptThreshold

Gets or sets the upper interrupt threshold

Declaration
public ushort UpperInterruptThreshold { get; set; }

Property Value

Type Description
System.UInt16

Methods

RaiseEventsAndNotify(IChangeResult<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>)

Declaration
protected override void RaiseEventsAndNotify(IChangeResult<(Illuminance? AmbientLight, Color? Color, bool Valid)> changeResult)

Parameters

Type Name Description
IChangeResult<System.ValueTuple<System.Nullable<Illuminance>, System.Nullable<Color>, System.Boolean>> changeResult

ReadBlueDataRegister()

Reads the blue data register of the sensor

Declaration
protected ushort ReadBlueDataRegister()

Returns

Type Description
System.UInt16

ReadClearDataRegister()

Reads the clear data register of the sensor

Declaration
protected ushort ReadClearDataRegister()

Returns

Type Description
System.UInt16

ReadGreenDataRegister()

Reads the green data register of the sensor

Declaration
protected ushort ReadGreenDataRegister()

Returns

Type Description
System.UInt16

ReadMeasurementIsValid()

Reads whether the last measurement is valid

Declaration
protected bool ReadMeasurementIsValid()

Returns

Type Description
System.Boolean

ReadRedDataRegister()

Reads the red data register of the sensor

Declaration
protected ushort ReadRedDataRegister()

Returns

Type Description
System.UInt16

ReadSensor()

Declaration
protected override Task<(Illuminance? AmbientLight, Color? Color, bool Valid)> ReadSensor()

Returns

Type Description
Task<System.ValueTuple<System.Nullable<Illuminance>, System.Nullable<Color>, System.Boolean>>

Overrides

Meadow.Foundation.SensorBase<System.ValueTuple<System.Nullable<Illuminance>, System.Nullable<Meadow.Foundation.Color>, System.Boolean>>.ReadSensor()

Reset()

Resets the device to the default configuration On reset the sensor goes to power down mode

Declaration
protected void Reset()

Events

LuminosityUpdated

Declaration
public event EventHandler<IChangeResult<Illuminance>> LuminosityUpdated

Event Type

Type Description
EventHandler<IChangeResult<Illuminance>>