NTC Thermistor Driver
API reference
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | List of all members
NtcThermistor< AdcType > Class Template Reference

Hardware-agnostic NTC thermistor driver. More...

#include <ntc_thermistor.hpp>

Public Member Functions

 NtcThermistor (NtcType ntc_type, AdcType *adc_interface) noexcept
 Constructor with NTC type and ADC interface.
 
 NtcThermistor (const ntc_config_t &config, AdcType *adc_interface) noexcept
 Constructor with custom configuration.
 
 NtcThermistor (const NtcThermistor &)=delete
 Copy constructor is deleted.
 
NtcThermistor & operator= (const NtcThermistor &)=delete
 Assignment operator is deleted.
 
 NtcThermistor (NtcThermistor &&) noexcept=default
 Move constructor.
 
NtcThermistor & operator= (NtcThermistor &&) noexcept=default
 Move assignment operator.
 
virtual ~NtcThermistor () noexcept=default
 Virtual destructor.
 
bool Initialize () noexcept
 Initialize the NTC thermistor driver.
 
bool Deinitialize () noexcept
 Deinitialize the NTC thermistor driver.
 
bool IsInitialized () const noexcept
 Check if initialized.
 
NtcError GetConfiguration (ntc_config_t *config) const noexcept
 Get current configuration.
 
NtcError SetConfiguration (const ntc_config_t &config) noexcept
 Set configuration.
 
NtcError ReadTemperatureCelsius (float *temperature_celsius) noexcept
 Read temperature in Celsius.
 
NtcError ReadTemperatureFahrenheit (float *temperature_fahrenheit) noexcept
 Read temperature in Fahrenheit.
 
NtcError ReadTemperatureKelvin (float *temperature_kelvin) noexcept
 Read temperature in Kelvin.
 
NtcError ReadTemperature (ntc_reading_t *reading) noexcept
 Read complete temperature information.
 
NtcError GetResistance (float *resistance_ohms) noexcept
 Get thermistor resistance.
 
NtcError GetVoltage (float *voltage_volts) noexcept
 Get voltage across thermistor.
 
NtcError GetRawAdcValue (uint32_t *adc_value) noexcept
 Get raw ADC value.
 
NtcError Calibrate (float reference_temperature_celsius) noexcept
 Calibrate the thermistor.
 
NtcError SetCalibrationOffset (float offset_celsius) noexcept
 Set calibration offset.
 
NtcError GetCalibrationOffset (float *offset_celsius) const noexcept
 Get calibration offset.
 
NtcError ResetCalibration () noexcept
 Reset calibration.
 
NtcError SetConversionMethod (NtcConversionMethod method) noexcept
 Set conversion method.
 
NtcError SetVoltageDivider (float series_resistance) noexcept
 Set voltage divider parameters.
 
NtcError SetReferenceVoltage (float reference_voltage) noexcept
 Set reference voltage.
 
NtcError SetBetaValue (float beta_value) noexcept
 Set beta value.
 
NtcError SetAdcChannel (uint8_t adc_channel) noexcept
 Set ADC channel.
 
NtcError SetSamplingParameters (uint32_t sample_count, uint32_t sample_delay_ms) noexcept
 Set sampling parameters.
 
NtcError SetFiltering (bool enable, float alpha=0.1F) noexcept
 Enable/disable filtering.
 

Static Public Member Functions

static float CelsiusToFahrenheit (float celsius) noexcept
 Convert Celsius to Fahrenheit.
 
static float FahrenheitToCelsius (float fahrenheit) noexcept
 Convert Fahrenheit to Celsius.
 
static float CelsiusToKelvin (float celsius) noexcept
 Convert Celsius to Kelvin.
 
static float KelvinToCelsius (float kelvin) noexcept
 Convert Kelvin to Celsius.
 
static const char * GetErrorString (NtcError error) noexcept
 Get error string.
 
static const char * GetTypeString (NtcType type) noexcept
 Get NTC type string.
 
static constexpr const char * GetDriverVersion () noexcept
 Get the compiled driver version string.
 
static constexpr uint8_t GetDriverVersionMajor () noexcept
 Get the compiled driver major version number.
 
static constexpr uint8_t GetDriverVersionMinor () noexcept
 Get the compiled driver minor version number.
 
static constexpr uint8_t GetDriverVersionPatch () noexcept
 Get the compiled driver patch version number.
 

Detailed Description

template<typename AdcType>
class NtcThermistor< AdcType >

Hardware-agnostic NTC thermistor driver.

This class provides a comprehensive interface for temperature measurement using NTC thermistors. It supports various NTC types and provides both lookup table and mathematical conversion methods.

Key features:

Template Parameters
AdcTypeThe ADC implementation type that inherits from ntc::AdcInterface<AdcType>
Note
The driver requires an ADC type that implements ntc::AdcInterface
Basic usage
// Define your ADC implementation
class MyAdc : public ntc::AdcInterface<MyAdc> {
public:
bool IsInitialized() const { return initialized_; }
bool EnsureInitialized() { return true; }
// ... implement other required methods
};
// Create ADC instance
MyAdc my_adc;
my_adc.EnsureInitialized();
// Create NTC thermistor with default configuration
// Initialize and read temperature
*if (thermistor.Initialize()) {
float temp_c;
if (thermistor.ReadTemperatureCelsius(&temp_c) == NtcError::Success) {
printf("Temperature: %.2F°C\n", temp_c);
}
}
Hardware-agnostic NTC thermistor driver.
Definition ntc_thermistor.hpp:75
CRTP-based template interface for ADC operations.
Definition ntc_adc_interface.hpp:55
@ Success
Operation successful.
@ NtcG163Jf103Ft1S
NTCG163JF103FT1S (10kΩ @ 25°C, β=3435K)
Custom configuration
// Create custom configuration
config.adc_channel = 1;
config.series_resistance = 10000.0F;
config.enable_filtering = true;
config.filter_alpha = 0.1F;
NtcThermistor<MyAdc> thermistor(config, &my_adc);
thermistor.Initialize();
@ Mathematical
Use mathematical conversion (slower, more accurate)
constexpr ntc_config_t GetDefaultNtcG163Jft103Ft1SConfig() noexcept
Default NTC configuration for NTCG163JFT103FT1S.
Definition ntc_types.hpp:195
NTC thermistor configuration structure.
Definition ntc_types.hpp:107
float filter_alpha
Filter alpha value (0.0-1.0)
Definition ntc_types.hpp:122
NtcConversionMethod conversion_method
Conversion method.
Definition ntc_types.hpp:114
bool enable_filtering
Enable temperature filtering.
Definition ntc_types.hpp:121
float series_resistance
Series resistance in voltage divider (ohms)
Definition ntc_types.hpp:112
uint8_t adc_channel
ADC channel number.
Definition ntc_types.hpp:115

Constructor & Destructor Documentation

◆ NtcThermistor() [1/4]

template<typename AdcType >
NtcThermistor< AdcType >::NtcThermistor ( NtcType  ntc_type,
AdcType *  adc_interface 
)
noexcept

Constructor with NTC type and ADC interface.

Creates an NTC thermistor instance with default configuration for the specified NTC type. The configuration is automatically initialized with appropriate values for the thermistor type.

Parameters
ntc_typeNTC thermistor type (e.g., NtcType::NtcG163Jf103Ft1S)
adc_interfacePointer to ADC interface (must inherit from ntc::AdcInterface<AdcType>)
Note
The ADC interface must remain valid for the lifetime of the NtcThermistor instance. The driver does not take ownership.
Warning
The ADC interface pointer must not be nullptr.

◆ NtcThermistor() [2/4]

template<typename AdcType >
NtcThermistor< AdcType >::NtcThermistor ( const ntc_config_t &  config,
AdcType *  adc_interface 
)
noexcept

Constructor with custom configuration.

Parameters
configNTC configuration structure
adc_interfacePointer to ADC interface (must inherit from ntc::AdcInterface<AdcType>)

◆ NtcThermistor() [3/4]

template<typename AdcType >
NtcThermistor< AdcType >::NtcThermistor ( const NtcThermistor< AdcType > &  )
delete

Copy constructor is deleted.

◆ NtcThermistor() [4/4]

template<typename AdcType >
NtcThermistor< AdcType >::NtcThermistor ( NtcThermistor< AdcType > &&  )
defaultnoexcept

Move constructor.

◆ ~NtcThermistor()

template<typename AdcType >
virtual NtcThermistor< AdcType >::~NtcThermistor ( )
virtualdefaultnoexcept

Virtual destructor.

Member Function Documentation

◆ Calibrate()

template<typename AdcType >
NtcError NtcThermistor< AdcType >::Calibrate ( float  reference_temperature_celsius)
noexcept

Calibrate the thermistor.

Takes a reading (current offset applied) and adds (reference - reading) to the offset, so afterwards the sensor reads the reference temperature whatever offset was set before.

Parameters
reference_temperature_celsiusKnown reference temperature
Returns
Error code

◆ CelsiusToFahrenheit()

template<typename AdcType >
float NtcThermistor< AdcType >::CelsiusToFahrenheit ( float  celsius)
staticnoexcept

Convert Celsius to Fahrenheit.

Parameters
celsiusTemperature in Celsius
Returns
Temperature in Fahrenheit

◆ CelsiusToKelvin()

template<typename AdcType >
float NtcThermistor< AdcType >::CelsiusToKelvin ( float  celsius)
staticnoexcept

Convert Celsius to Kelvin.

Parameters
celsiusTemperature in Celsius
Returns
Temperature in Kelvin

◆ Deinitialize()

template<typename AdcType >
bool NtcThermistor< AdcType >::Deinitialize ( )
noexcept

Deinitialize the NTC thermistor driver.

Returns
true if successful, false otherwise

◆ FahrenheitToCelsius()

template<typename AdcType >
float NtcThermistor< AdcType >::FahrenheitToCelsius ( float  fahrenheit)
staticnoexcept

Convert Fahrenheit to Celsius.

Parameters
fahrenheitTemperature in Fahrenheit
Returns
Temperature in Celsius

◆ GetCalibrationOffset()

template<typename AdcType >
NtcError NtcThermistor< AdcType >::GetCalibrationOffset ( float *  offset_celsius) const
noexcept

Get calibration offset.

Parameters
offset_celsiusPointer to store offset
Returns
Error code

◆ GetConfiguration()

template<typename AdcType >
NtcError NtcThermistor< AdcType >::GetConfiguration ( ntc_config_t *  config) const
noexcept

Get current configuration.

Parameters
configPointer to store configuration
Returns
Error code

◆ GetDriverVersion()

template<typename AdcType >
static constexpr const char * NtcThermistor< AdcType >::GetDriverVersion ( )
inlinestaticconstexprnoexcept

Get the compiled driver version string.

◆ GetDriverVersionMajor()

template<typename AdcType >
static constexpr uint8_t NtcThermistor< AdcType >::GetDriverVersionMajor ( )
inlinestaticconstexprnoexcept

Get the compiled driver major version number.

◆ GetDriverVersionMinor()

template<typename AdcType >
static constexpr uint8_t NtcThermistor< AdcType >::GetDriverVersionMinor ( )
inlinestaticconstexprnoexcept

Get the compiled driver minor version number.

◆ GetDriverVersionPatch()

template<typename AdcType >
static constexpr uint8_t NtcThermistor< AdcType >::GetDriverVersionPatch ( )
inlinestaticconstexprnoexcept

Get the compiled driver patch version number.

◆ GetErrorString()

template<typename AdcType >
const char * NtcThermistor< AdcType >::GetErrorString ( NtcError  error)
staticnoexcept

Get error string.

Parameters
errorError code
Returns
Error string

◆ GetRawAdcValue()

template<typename AdcType >
NtcError NtcThermistor< AdcType >::GetRawAdcValue ( uint32_t *  adc_value)
noexcept

Get raw ADC value.

Parameters
adc_valuePointer to store ADC value
Returns
Error code

◆ GetResistance()

template<typename AdcType >
NtcError NtcThermistor< AdcType >::GetResistance ( float *  resistance_ohms)
noexcept

Get thermistor resistance.

Parameters
resistance_ohmsPointer to store resistance
Returns
Error code

◆ GetTypeString()

template<typename AdcType >
const char * NtcThermistor< AdcType >::GetTypeString ( NtcType  type)
staticnoexcept

Get NTC type string.

Parameters
typeNTC type
Returns
Type string

◆ GetVoltage()

template<typename AdcType >
NtcError NtcThermistor< AdcType >::GetVoltage ( float *  voltage_volts)
noexcept

Get voltage across thermistor.

Parameters
voltage_voltsPointer to store voltage
Returns
Error code

◆ Initialize()

template<typename AdcType >
bool NtcThermistor< AdcType >::Initialize ( )
noexcept

Initialize the NTC thermistor driver.

Returns
true if successful, false otherwise

◆ IsInitialized()

template<typename AdcType >
bool NtcThermistor< AdcType >::IsInitialized ( ) const
noexcept

Check if initialized.

Returns
true if initialized, false otherwise

◆ KelvinToCelsius()

template<typename AdcType >
float NtcThermistor< AdcType >::KelvinToCelsius ( float  kelvin)
staticnoexcept

Convert Kelvin to Celsius.

Parameters
kelvinTemperature in Kelvin
Returns
Temperature in Celsius

◆ operator=() [1/2]

template<typename AdcType >
NtcThermistor & NtcThermistor< AdcType >::operator= ( const NtcThermistor< AdcType > &  )
delete

Assignment operator is deleted.

◆ operator=() [2/2]

template<typename AdcType >
NtcThermistor & NtcThermistor< AdcType >::operator= ( NtcThermistor< AdcType > &&  )
defaultnoexcept

Move assignment operator.

◆ ReadTemperature()

template<typename AdcType >
NtcError NtcThermistor< AdcType >::ReadTemperature ( ntc_reading_t *  reading)
noexcept

Read complete temperature information.

Parameters
readingPointer to store reading information
Returns
Error code

◆ ReadTemperatureCelsius()

template<typename AdcType >
NtcError NtcThermistor< AdcType >::ReadTemperatureCelsius ( float *  temperature_celsius)
noexcept

Read temperature in Celsius.

Performs a complete temperature reading cycle: reads ADC, calculates resistance, converts to temperature, applies filtering (if enabled), and validates the result.

Parameters
temperature_celsiusPointer to store temperature (°C)
Returns
Error code (NtcError::Success on success)
Note
This function may take several milliseconds depending on sample_count and sample_delay_ms configuration.
Warning
The driver must be initialized before calling this function. Check return value before using the temperature value.

◆ ReadTemperatureFahrenheit()

template<typename AdcType >
NtcError NtcThermistor< AdcType >::ReadTemperatureFahrenheit ( float *  temperature_fahrenheit)
noexcept

Read temperature in Fahrenheit.

Parameters
temperature_fahrenheitPointer to store temperature
Returns
Error code

◆ ReadTemperatureKelvin()

template<typename AdcType >
NtcError NtcThermistor< AdcType >::ReadTemperatureKelvin ( float *  temperature_kelvin)
noexcept

Read temperature in Kelvin.

Parameters
temperature_kelvinPointer to store temperature
Returns
Error code

◆ ResetCalibration()

template<typename AdcType >
NtcError NtcThermistor< AdcType >::ResetCalibration ( )
noexcept

Reset calibration.

Returns
Error code

◆ SetAdcChannel()

template<typename AdcType >
NtcError NtcThermistor< AdcType >::SetAdcChannel ( uint8_t  adc_channel)
noexcept

Set ADC channel.

Parameters
adc_channelADC channel number
Returns
Error code

◆ SetBetaValue()

template<typename AdcType >
NtcError NtcThermistor< AdcType >::SetBetaValue ( float  beta_value)
noexcept

Set beta value.

Parameters
beta_valueBeta value (K)
Returns
Error code

◆ SetCalibrationOffset()

template<typename AdcType >
NtcError NtcThermistor< AdcType >::SetCalibrationOffset ( float  offset_celsius)
noexcept

Set calibration offset.

Parameters
offset_celsiusCalibration offset in Celsius
Returns
Error code

◆ SetConfiguration()

template<typename AdcType >
NtcError NtcThermistor< AdcType >::SetConfiguration ( const ntc_config_t &  config)
noexcept

Set configuration.

Parameters
configNew configuration
Returns
Error code

◆ SetConversionMethod()

template<typename AdcType >
NtcError NtcThermistor< AdcType >::SetConversionMethod ( NtcConversionMethod  method)
noexcept

Set conversion method.

Parameters
methodConversion method
Returns
Error code

◆ SetFiltering()

template<typename AdcType >
NtcError NtcThermistor< AdcType >::SetFiltering ( bool  enable,
float  alpha = 0.1F 
)
noexcept

Enable/disable filtering.

Parameters
enableEnable filtering
alphaFilter alpha value (0.0-1.0)
Returns
Error code

◆ SetReferenceVoltage()

template<typename AdcType >
NtcError NtcThermistor< AdcType >::SetReferenceVoltage ( float  reference_voltage)
noexcept

Set reference voltage.

Parameters
reference_voltageReference voltage (V)
Returns
Error code

◆ SetSamplingParameters()

template<typename AdcType >
NtcError NtcThermistor< AdcType >::SetSamplingParameters ( uint32_t  sample_count,
uint32_t  sample_delay_ms 
)
noexcept

Set sampling parameters.

Parameters
sample_countNumber of samples
sample_delay_msDelay between samples (ms), waited through the port's optional DelayMs(); ignored by a port without one
Returns
Error code

◆ SetVoltageDivider()

template<typename AdcType >
NtcError NtcThermistor< AdcType >::SetVoltageDivider ( float  series_resistance)
noexcept

Set voltage divider parameters.

Parameters
series_resistanceSeries resistance (ohms)
Returns
Error code

The documentation for this class was generated from the following files: