-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1090 from WildernessLabs/feature/thermistor
added b-parameter calculation for a thermistor
- Loading branch information
Showing
2 changed files
with
151 additions
and
88 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
...adow.Foundation.Peripherals/Sensors.Temperature.Thermistor/Driver/BParameterThermistor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using Meadow.Hardware; | ||
using Meadow.Units; | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Meadow.Foundation.Sensors.Temperature; | ||
|
||
/// <summary> | ||
/// Thermistor temperature sensor object using the B-Parameter or Beta equation to determine temperature | ||
/// </summary> | ||
public class BParameterThermistor : Thermistor | ||
{ | ||
// Constants for the thermistor | ||
private const double B = 3950; // B-parameter (in Kelvin) | ||
private const double T0 = 298.15; // Reference temperature T0 (in Kelvin, which is 25°C) | ||
|
||
/// <inheritdoc/> | ||
public override Resistance NominalResistance { get; } | ||
|
||
/// <summary> | ||
/// Gets the resistance of the fixed-value series resistor in your voltage divider circuit | ||
/// </summary> | ||
public Resistance SeriesResistance { get; } | ||
|
||
/// <summary> | ||
/// Creates an instance of a BParameterThermistor using a 10k resistor and a 10k thermistor | ||
/// </summary> | ||
/// <param name="analogInput">The analog input reading the thermistor voltage divider output</param> | ||
public BParameterThermistor(IAnalogInputPort analogInput) | ||
: this(analogInput, 10_000.Ohms(), 10_000.Ohms()) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Creates an instance of a BParameterThermistor | ||
/// </summary> | ||
/// <param name="analogInput">The analog input reading the thermistor voltage divider output</param> | ||
/// <param name="nominalResistance">The nominal resistance of the thermistor (e.g. 10kOhm for a 10k thermistor)</param> | ||
/// <param name="seriesResistance">The resistance of the fixed-value series resistor in your voltage divider circuit</param> | ||
public BParameterThermistor( | ||
IAnalogInputPort analogInput, | ||
Resistance nominalResistance, | ||
Resistance seriesResistance | ||
) | ||
: base(analogInput) | ||
{ | ||
SeriesResistance = seriesResistance; | ||
NominalResistance = nominalResistance; | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override async Task<Units.Temperature> ReadSensor() | ||
{ | ||
var voltageReading = await AnalogInputPort.Read(); | ||
|
||
// ohms | ||
var measuredResistance = (SeriesResistance.Ohms * voltageReading.Volts) / (AnalogInputPort.ReferenceVoltage.Volts - voltageReading.Volts); | ||
|
||
// Calculate temperature in Kelvin using the B-parameter equation | ||
double temperatureK = 1 / ((1 / T0) + (1 / B) * Math.Log(measuredResistance / NominalResistance.Ohms)); | ||
|
||
return new Units.Temperature(temperatureK, Units.Temperature.UnitType.Kelvin); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters