Characteristic Locus
Inheritance System.Object FilterableObservableBase<AtmosphericConditionChangeResult, AtmosphericConditions> > AnalogTemperature
Implements System.IObservable<AtmosphericConditionChangeResult> ITemperatureSensor
Inherited Members FilterableObservableBase<AtmosphericConditionChangeResult, AtmosphericConditions>._observers FilterableObservableBase<AtmosphericConditionChangeResult, AtmosphericConditions>.NotifyObservers(AtmosphericConditionChangeResult) FilterableObservableBase<AtmosphericConditionChangeResult, AtmosphericConditions>.Subscribe(IObserver<AtmosphericConditionChangeResult>) System.Object.ToString() System.Object.Equals(System.Object) System.Object.Equals(System.Object, System.Object) System.Object.ReferenceEquals(System.Object, System.Object) System.Object.GetHashCode() System.Object.GetType() System.Object.MemberwiseClone()
Namespace Meadow.Foundation.Sensors.Temperature
Assembly Meadow.Foundation.dll

Syntax

public class AnalogTemperature : FilterableObservableBase<AtmosphericConditionChangeResult, AtmosphericConditions>, IObservable<AtmosphericConditionChangeResult>, ITemperatureSensor

Remarks

The analog temperature sensor driver can be used with any sensor that has a linear voltage response. It has been tested with the following series of temperature sensors:

  • TMP35
  • TMP36
  • TMP37
  • LM35

These sensors exhibit a linear change in the analog voltage for each degree centigrade. This is often presented in the datasheet as follows:

This driver should work with any sensor of this type.

Purchasing

TMP36 sensors can be purchased from a number of suppliers including:

Examples

The following application demonstrates how to use the TMP36 in interrupt mode. The sensor will be read every second and changes in values greater than +/- 0.1C will generate and interrupt:

using System;
using System.Threading;
using System.Threading.Tasks;
using Meadow;
using Meadow.Devices;
using Meadow.Foundation.Sensors.Temperature;
using Meadow.Peripherals.Sensors.Atmospheric;

namespace AnalogTemperature_Sample
{
    public class Program
    {
        static IApp app;
        public static void Main(string[] args)
        {
            if (args.Length > 0 && args[0] == "--exitOnDebug") return;

            // instantiate and run new meadow app
            app = new MeadowApp();

            // keep app alive
            Thread.Sleep(Timeout.Infinite);
        }
    }

    public class MeadowApp : App<F7Micro, MeadowApp>
    {
        AnalogTemperature analogTemperature;

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

            // configure our AnalogTemperature sensor
            analogTemperature = new AnalogTemperature (
                device: Device,
                analogPin: Device.Pins.A00,
                sensorType: AnalogTemperature.KnownSensorType.LM35
            );

            // Example that uses an IObersvable subscription to only be notified
            // when the temperature changes by at least a degree.
            analogTemperature.Subscribe(new FilterableObserver<AtmosphericConditionChangeResult, AtmosphericConditions>(
                h => {
                    Console.WriteLine($"Temp changed by a degree; new: {h.New.Temperature}, old: {h.Old.Temperature}");
                },
                e => {
                    return (Math.Abs(e.Delta.Temperature) > 1);
                }
                ));

            // classical .NET events can also be used:
            analogTemperature.Updated += (object sender, AtmosphericConditionChangeResult e) => {
                Console.WriteLine($"Temp Changed, temp: {e.New.Temperature}ºC");
            };

            // Get an initial reading.
            ReadTemp().Wait();

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

        async Task ReadTemp()
        {
            var conditions = await analogTemperature.Read();
            Console.WriteLine($"Initial temp: { conditions.Temperature }");
        }
    }
}
Example Circuit

Consider the TMP36, this sensor requires only three connections; power, ground and the analog output:

Constructors

View Source

AnalogTemperature(IAnalogInputPort, AnalogTemperature.KnownSensorType, AnalogTemperature.Calibration)

Declaration
public AnalogTemperature(IAnalogInputPort analogInputPort, AnalogTemperature.KnownSensorType sensorType, AnalogTemperature.Calibration calibration = null)

Parameters

Type Name Description
IAnalogInputPort analogInputPort
AnalogTemperature.KnownSensorType sensorType
AnalogTemperature.Calibration calibration
View Source

AnalogTemperature(IIODevice, IPin, AnalogTemperature.KnownSensorType, AnalogTemperature.Calibration)

New instance of the AnalogTemperatureSensor class.

Declaration
public AnalogTemperature(IIODevice device, IPin analogPin, AnalogTemperature.KnownSensorType sensorType, AnalogTemperature.Calibration calibration = null)

Parameters

Type Name Description
IIODevice device
IPin analogPin

Analog pin the temperature sensor is connected to.

AnalogTemperature.KnownSensorType sensorType

Type of sensor attached to the analog port.

AnalogTemperature.Calibration calibration

Calibration for the analog temperature sensor.

Properties

View Source

AnalogInputPort

Declaration
public IAnalogInputPort AnalogInputPort { get; protected set; }

Property Value

Type Description
IAnalogInputPort
View Source

Temperature

Temperature in degrees centigrade.

Declaration
public float Temperature { get; protected set; }

Property Value

Type Description
System.Single

Remarks

The temperature is given by the following calculation: temperature = (reading in millivolts - yIntercept) / millivolts per degree centigrade

Methods

View Source

RaiseEventsAndNotify(AtmosphericConditionChangeResult)

Declaration
protected void RaiseEventsAndNotify(AtmosphericConditionChangeResult changeResult)

Parameters

Type Name Description
AtmosphericConditionChangeResult changeResult
View Source

Read(Int32, Int32)

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

Declaration
public Task<AtmosphericConditions> Read(int sampleCount = 10, int sampleIntervalDuration = 40)

Parameters

Type Name Description
System.Int32 sampleCount

The number of sample readings to take. Must be greater than 0. These samples are automatically averaged.

System.Int32 sampleIntervalDuration

The time, in milliseconds, to wait in between samples during a reading.

Returns

Type Description
System.Threading.Tasks.Task<AtmosphericConditions>

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

View Source

StartUpdating(Int32, Int32, Int32)

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(int sampleCount = 10, int sampleIntervalDuration = 40, int standbyDuration = 100)

Parameters

Type Name Description
System.Int32 sampleCount

How many samples to take during a given reading. These are automatically averaged to reduce noise.

System.Int32 sampleIntervalDuration

The time, in milliseconds, to wait in between samples during a reading.

System.Int32 standbyDuration

The time, in milliseconds, to wait between sets of sample readings. This value determines how often Changed events are raised and IObservable consumers are notified.

View Source

StopUpdating()

Stops sampling the temperature.

Declaration
public void StopUpdating()
View Source

VoltageToTemperature(Single)

Converts a voltage value to a celsius temp, based on the current calibration values.

Declaration
protected float VoltageToTemperature(float voltage)

Parameters

Type Name Description
System.Single voltage

Returns

Type Description
System.Single

Events

View Source

Updated

Raised when the value of the reading changes.

Declaration
public event EventHandler<AtmosphericConditionChangeResult> Updated

Event Type

Type Description
System.EventHandler<AtmosphericConditionChangeResult>