Skip to content

Commit

Permalink
Calculate NTC temperature using Steinhart-Hart equation
Browse files Browse the repository at this point in the history
  • Loading branch information
luis-godinez committed Aug 16, 2024
1 parent 9d75105 commit a912aa0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,25 @@
"name": "PCB_NTC",
"channel": 0,
"system": "controller",
"control": false
"control": false,
"resistanceAt25C": 10000,
"bValue": 3977
},
{
"name": "SAUNA_NTC",
"channel": 1,
"system": "sauna",
"control": true
"control": true,
"resistanceAt25C": 10000,
"bValue": 3950
},
{
"name": "Outside",
"channel": 2,
"system": null,
"control": false
"control": false,
"resistanceAt25C": 10000,
"bValue": 3950
}
],
"saunaOnWhileDoorOpen": false,
Expand Down
20 changes: 19 additions & 1 deletion src/platformAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ export class OpenSaunaAccessory {
}

// Convert the ADC reading to a temperature value
const temperatureCelsius = (reading.value * 3.3 - 0.5) * 100;
const temperatureCelsius = this.calculateTemperature(reading.value, sensor.resistanceAt25C, sensor.bValue);
const displayTemperature = this.config.temperatureUnitFahrenheit
? this.convertToFahrenheit(temperatureCelsius)
: temperatureCelsius;
Expand Down Expand Up @@ -990,6 +990,24 @@ export class OpenSaunaAccessory {
);
}

private calculateTemperature(adcValue: number, resistanceAt25C: number, bValue: number): number {
const pullUpResistor = 10000; // 10k ohm pull-up resistor

// Calculate the resistance of the thermistor based on the ADC value
let resistance = (1023 / adcValue) - 1;
resistance = pullUpResistor / resistance;

// Apply the Steinhart-Hart equation
let steinhart = resistance / resistanceAt25C; // (R/Ro)
steinhart = Math.log(steinhart); // ln(R/Ro)
steinhart /= bValue; // 1/B * ln(R/Ro)
steinhart += 1.0 / (25 + 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
steinhart -= 273.15; // convert to Celsius

return steinhart;
}

// Utility function to convert Celsius to Fahrenheit
private convertToFahrenheit(celsius: number): number {
return celsius * 1.8 + 32;
Expand Down
2 changes: 2 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ export interface AuxSensorConfig {
channel: number; // ADC channel number associated with the sensor
system: 'sauna' | 'steam' | 'controller' | null; // The sensor to system association, or null if not associated
control: boolean; // Whether the sensor affects control logic (e.g., turns off power if overheating)
resistanceAt25C: number; // NTC resistance in Ohms @ 25°C
bValue: number; // NTC beta value
}

0 comments on commit a912aa0

Please sign in to comment.