Remarks

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

The ALS-PT19-315C is a low cost analog ambient light sensor, consisting of phototransistor in a miniature SMD.

Code Example

Alspt19315C sensor;

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

    // configure our sensor
    sensor = new Alspt19315C(Device, Device.Pins.A03);

    //==== IObservable Pattern with an optional notification filter.
    // Example that uses an IObservable subscription to only be notified
    // when the voltage changes by at least 0.5V
    var consumer = Alspt19315C.CreateObserver(
        handler: result => Console.WriteLine($"Observer filter satisfied: {result.New.Volts:N2}V, old: {result.Old?.Volts:N2}V"),

        // only notify if the change is greater than 0.5V
        filter: result => {
            if (result.Old is { } old) { //c# 8 pattern match syntax. checks for !null and assigns var.
                return (result.New - old).Abs().Volts > 0.5; // returns true if > 0.5V change.
            }
            return false;
        }
    );
    sensor.Subscribe(consumer);

    //==== Classic Events Pattern
    sensor.Updated += (sender, result) => {
        Console.WriteLine($"Voltage Changed, new: {result.New.Volts:N2}V, old: {result.Old?.Volts:N2}V");
    };

    //==== One-off reading use case/pattern
    ReadTemp().Wait();

    // Spin up the sampling thread so that events are raised and IObservable notifications are sent.
    sensor.StartUpdating(TimeSpan.FromMilliseconds(1000));
}

protected async Task ReadTemp()
{
    var result = await sensor.Read();
    Console.WriteLine($"Initial temp: {result.Volts:N2}V");
}

Sample project(s) available on GitHub

Purchasing

The ALS-PT19 senors are available on breakout boards and as individual sensors:

Wiring Example

The ALS-PT19C is a simple analog device requiring only three connections:

Characteristic Locus
Inheritance System.Object ObservableBase<Voltage> SensorBase<Voltage> > Alspt19315C
Inherited Members SensorBase<Voltage>.Updated SensorBase<Voltage>.samplingLock SensorBase<Voltage>.SamplingTokenSource SensorBase<Voltage>.Conditions SensorBase<Voltage>.IsSampling SensorBase<Voltage>.UpdateInterval SensorBase<Voltage>.RaiseEventsAndNotify(IChangeResult<Voltage>) SensorBase<Voltage>.Read() ObservableBase<Voltage>.observers ObservableBase<Voltage>.NotifyObservers(IChangeResult<Voltage>) Meadow.Foundation.ObservableBase<Voltage>.Subscribe(IObserver<>) Meadow.Foundation.ObservableBase<Voltage>.CreateObserver(Action<>, System.Nullable<Predicate<Meadow.IChangeResult<UNIT>>>)
Namespace Meadow.Foundation.Sensors.Light
Assembly Alspt19315C.dll

Syntax

public class Alspt19315C : SensorBase<Voltage>

Constructors

Alspt19315C(IAnalogInputController, IPin, Int32, Nullable<TimeSpan>, Nullable<Voltage>)

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

Declaration
public Alspt19315C(IAnalogInputController device, IPin pin, int sampleCount = 5, TimeSpan? sampleInterval = null, Voltage? voltage = null)

Parameters

Type Name Description
IAnalogInputController device
IPin pin

AnalogChannel connected to the sensor.

System.Int32 sampleCount
System.Nullable<TimeSpan> sampleInterval
System.Nullable<Voltage> voltage

Alspt19315C(IAnalogInputPort)

Declaration
public Alspt19315C(IAnalogInputPort port)

Parameters

Type Name Description
IAnalogInputPort port

Properties

Voltage

Declaration
public Voltage Voltage { get; protected set; }

Property Value

Type Description
Voltage

Methods

ReadSensor()

Convenience method to get the current temperature. For frequent reads, use StartSampling() and StopSampling() in conjunction with the SampleBuffer.

Declaration
protected override Task<Voltage> ReadSensor()

Returns

Type Description
Task<Voltage>

A float value that's ann average value of all the samples taken.

Overrides

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

StartUpdating(Nullable<TimeSpan>)

Starts continuously sampling the sensor.

This method also starts raising Changed events and IObservable subscribers getting notified. Use the readIntervalDuration parameter to specify how often events and notifications are raised/sent.

Declaration
public void StartUpdating(TimeSpan? updateInterval)

Parameters

Type Name Description
System.Nullable<TimeSpan> updateInterval

A TimeSpan that specifies how long to wait between readings. This value influences how often *Updated events are raised and IObservable consumers are notified. The default is 5 seconds.

StopUpdating()

Stops sampling the temperature.

Declaration
public void StopUpdating()