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()
{
    Resolver.Log.Info("Initialize...");

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

    // instantiate our onboard LED that we'll show the color with
    rgbLed = new RgbPwmLed(
        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 => Resolver.Log.Info($"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) =>
    {
        Resolver.Log.Info($"  Ambient Light: {result.New.AmbientLight?.Lux:N2}Lux");
        Resolver.Log.Info($"  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();

    Resolver.Log.Info("Initial Readings:");
    Resolver.Log.Info($" Visible Light: {result.AmbientLight?.Lux:N2}Lux");
    Resolver.Log.Info($" 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>> SamplingSensorBase<System.ValueTuple<System.Nullable<Illuminance>, System.Nullable<Color>, System.Boolean>> PollingSensorBase<System.ValueTuple<System.Nullable<Illuminance>, System.Nullable<Color>, System.Boolean>> ByteCommsSensorBase<System.ValueTuple<System.Nullable<Illuminance>, System.Nullable<Color>, System.Boolean>> > Bh1745
Implements ISamplingSensor<System.ValueTuple<System.Nullable<Illuminance>, System.Nullable<Color>, System.Boolean>> ISamplingSensor<System.ValueTuple<System.Nullable<Illuminance>, System.Nullable<Color>, System.Boolean>> 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() PollingSensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.StartUpdating(Nullable<TimeSpan>) PollingSensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.StopUpdating() SamplingSensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.samplingLock SamplingSensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.Updated SamplingSensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.SamplingTokenSource SamplingSensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.Conditions SamplingSensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.IsSampling SamplingSensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.UpdateInterval SamplingSensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.ReadSensor() SamplingSensorBase<(Nullable<Illuminance> AmbientLight, Nullable<Color> Color, Boolean Valid)>.RaiseEventsAndNotify(IChangeResult<>) SamplingSensorBase<(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)>, ISamplingSensor<(Illuminance? AmbientLight, Color? Color, bool Valid)>, ISamplingSensor<(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

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

The current Illuminance value

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

Interrupt reset status

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)>)

Raise events for subcribers and notify of value changes

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

The updated sensor data

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()

Reads data from the sensor

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>>

The latest sensor reading

Overrides

Meadow.Foundation.SamplingSensorBase<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

Raised when the luminosity changes

Declaration
public event EventHandler<IChangeResult<Illuminance>> LuminosityUpdated

Event Type

Type Description
EventHandler<IChangeResult<Illuminance>>