Skip to content

Commit 75e967c

Browse files
authored
Merge pull request #18 from bobwolff68/master
Update for more accurate ESP32-C3 samples
2 parents 5bc56e2 + c38e880 commit 75e967c

File tree

5 files changed

+148
-8
lines changed

5 files changed

+148
-8
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
# NTC Thermistor Library
22

3-
For Arduino ant STM32 boards.
3+
For Arduino, ESP32 and STM32 boards.
4+
5+
Enhanced in 2024 by Bob Wolff to support more accurate NTC results
6+
on ESP32 devices. Some of the ESP32 devices like the ESP32-C3 have
7+
the ADC raw count values as "uncalibrated" results while the
8+
millivolt reading from analogReadMillivolts() are calibrated and
9+
are quite a bit more accurate. Instantiating NTC_Thermistor_ESP32
10+
and passing in the voltage reference in millivoltes (likely 3300),
11+
this derived class will give more accurate results for temperatures.
412

513
The Library implements a set of methods for working with a NTC thermistor.
614
Provides a temperature reading in Celsius, Fahrenheit and Kelvin.
715

816
## Installation
917

10-
1. [Download](https://github.com/YuriiSalimov/NTC_Thermistor/releases) the Latest release from gitHub.
18+
1. [Download](https://github.com/bobwolff68/NTC_Thermistor/releases) the Latest release from gitHub.
1119
2. Unzip and modify the Folder name to "NTC_Thermistor" (Remove the '-version')
1220
3. Paste the modified folder on your Library folder (On your `libraries` folder inside Sketchbooks or Arduino software).
1321
4. Restart the Arduino IDE.
1422

1523
## Circuit Diagram
1624

17-
Connect to the analog side of an Arduino Uno. Run GND through the thermistor, then a pull-down resistor (R0), and into 5V. To measure the temperature pull a line off the junction of the thermistor and the resistor, and into an analog pin (A1 here).
25+
Connect to the analog side of an Arduino Uno. Run GND through the thermistor, then a pull-down resistor (R0), and into reference voltage. To measure the temperature pull a line off the junction of the thermistor and the resistor, and into an analog pin (A1 here).
1826

1927
![Diagram](Diagram.png)
2028

examples/ESP32/ESP32.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
NTC Thermistor for ESP32 higher accuracy results
3+
4+
Reads a temperature from the NTC 3950 thermistor and displays
5+
it in the default Serial.
6+
7+
ESP32 devices are 12-bit ADC devices so the defuault is 4095 here.
8+
9+
https://github.com/bobwolff68/NTC_Thermistor
10+
11+
Created by Bob Wolff from Yuri's original - 2024
12+
Released into the public domain.
13+
*/
14+
#include <Thermistor.h>
15+
#include <NTC_Thermistor.h>
16+
17+
#define SENSOR_PIN 3
18+
#define REFERENCE_RESISTANCE 10000
19+
#define NOMINAL_RESISTANCE 10000
20+
#define NOMINAL_TEMPERATURE 25
21+
#define B_VALUE 3950
22+
#define ESP32_ANALOG_RESOLUTION 4095
23+
#define ESP32_ADC_VREF_MV 3300
24+
25+
Thermistor* thermistor;
26+
27+
// the setup function runs once when you press reset or power the board
28+
void setup() {
29+
Serial.begin(9600);
30+
31+
thermistor = new NTC_Thermistor_ESP32(
32+
SENSOR_PIN,
33+
REFERENCE_RESISTANCE,
34+
NOMINAL_RESISTANCE,
35+
NOMINAL_TEMPERATURE,
36+
B_VALUE,
37+
ESP32_ADC_VREF_MV,
38+
ESP32_ANALOG_RESOLUTION
39+
);
40+
}
41+
42+
// the loop function runs over and over again forever
43+
void loop() {
44+
// Reads temperature
45+
const double celsius = thermistor->readCelsius();
46+
const double kelvin = thermistor->readKelvin();
47+
const double fahrenheit = thermistor->readFahrenheit();
48+
49+
// Output of information
50+
Serial.print("Temperature: ");
51+
Serial.print(celsius);
52+
Serial.print(" C, ");
53+
Serial.print(kelvin);
54+
Serial.print(" K, ");
55+
Serial.print(fahrenheit);
56+
Serial.println(" F");
57+
58+
delay(500); // optionally, only to delay the output of information in the example.
59+
}

library.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=NTC_Thermistor
2-
version=2.0.3
3-
author=Yurii Salimov <yuriy.alex.salimov@gmail.com>
2+
version=2.1.0
3+
author=Yurii Salimov <yuriy.alex.salimov@gmail.com> and Bob Wolff
44
maintainer=Yurii Salimov <yuriy.alex.salimov@gmail.com>
55
sentence=The Library implements a set of methods for working with a NTC thermistor.
66
paragraph=Provides a temperature reading in Celsius, Fahrenheit and Kelvin.
77
category=Sensors
8-
url=https://github.com/YuriiSalimov/NTC_Thermistor
8+
url=https://github.com/bobwolff68/NTC_Thermistor
99
architectures=*

src/NTC_Thermistor.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
/**
22
Created by Yurii Salimov, February, 2018.
33
Released into the public domain.
4+
5+
Modified by Bob Wolff - 2024 - adding in a derived class for
6+
ESP32 which utilizes the ADC reading of millivolts instead of
7+
raw ADC count values. This is because in some of the ESP32
8+
chips, only the millivolt reading is a calibrated result.
9+
To do this, the user's Vref much be input in the constructor.
410
*/
511
#include "NTC_Thermistor.h"
612

@@ -89,3 +95,32 @@ inline double NTC_Thermistor::celsiusToFahrenheit(const double celsius) {
8995
inline double NTC_Thermistor::kelvinsToFahrenheit(const double kelvins) {
9096
return celsiusToFahrenheit(kelvinsToCelsius(kelvins));
9197
}
98+
99+
/***
100+
* @brief Slight derivation which reads the 'voltage' (which is
101+
* really in raw ADC count values) indirectly through reading
102+
* the ADC's millivolt reading. This is due to a calibration
103+
* which happens for the millivolt reading that gives a much
104+
* more accurate reading than raw 'analogRead()'
105+
*/
106+
NTC_Thermistor_ESP32::NTC_Thermistor_ESP32(
107+
const int pin,
108+
const double referenceResistance,
109+
const double nominalResistance,
110+
const double nominalTemperatureCelsius,
111+
const double bValue,
112+
const uint16_t adcVref,
113+
const int adcResolution
114+
) : NTC_Thermistor(pin, referenceResistance, nominalResistance, nominalTemperatureCelsius, bValue, adcResolution)
115+
{
116+
this->vref_mv = adcVref;
117+
}
118+
119+
/***
120+
* @brief read the calibrated version of the ADC count value indirectly
121+
* by reading the millivolts value (which is calibrated by Espressif)
122+
* and back-calculating the raw ADC count value.
123+
*/
124+
double NTC_Thermistor_ESP32::readVoltage() {
125+
return (double)analogReadMilliVolts(this->pin) / (double)this->vref_mv * this->adcResolution;
126+
}

src/NTC_Thermistor.h

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class NTC_Thermistor : public Thermistor {
4848
// Default analog resolution for Arduino board
4949
static const int DEFAULT_ADC_RESOLUTION = 1023;
5050

51+
protected:
5152
int pin; // an analog port.
5253
double referenceResistance;
5354
double nominalResistance;
@@ -96,7 +97,7 @@ class NTC_Thermistor : public Thermistor {
9697
*/
9798
double readFahrenheit() override;
9899

99-
private:
100+
protected:
100101
/**
101102
Resistance to Kelvin conversion:
102103
1/K = 1/K0 + ln(R/R0)/B;
@@ -130,7 +131,7 @@ class NTC_Thermistor : public Thermistor {
130131
131132
@return thermistor voltage in analog range (0...1023, for Arduino).
132133
*/
133-
inline double readVoltage();
134+
virtual double readVoltage();
134135

135136
/**
136137
Celsius to Kelvin conversion:
@@ -169,4 +170,41 @@ class NTC_Thermistor : public Thermistor {
169170
inline double kelvinsToFahrenheit(double kelvins);
170171
};
171172

173+
class NTC_Thermistor_ESP32 : public NTC_Thermistor {
174+
public:
175+
/**
176+
Constructor
177+
178+
@param pin - an analog port number to be attached to the thermistor
179+
@param referenceResistance - reference resistance
180+
@param nominalResistance - nominal resistance at a nominal temperature
181+
@param nominalTemperature - nominal temperature in Celsius
182+
@param bValue - b-value of a thermistor
183+
@param adcVref - The ADC's reference voltage (likely 3300 for 3.3volts)
184+
@param adcResolution - ADC resolution (default 4095, for ESP32)
185+
*/
186+
NTC_Thermistor_ESP32(
187+
int pin,
188+
double referenceResistance,
189+
double nominalResistance,
190+
double nominalTemperatureCelsius,
191+
double bValue,
192+
uint16_t adcVref,
193+
int adcResolution = DEFAULT_ESP32_ADC_RESOLUTION
194+
);
195+
protected:
196+
/**
197+
@brief read the calibrated version of the ADC count value indirectly
198+
by reading the millivolts value (which is calibrated by Espressif)
199+
and back-calculating the raw ADC count value.
200+
201+
@return thermistor voltage in analog range (0...4095, for ESP32).
202+
*/
203+
virtual double readVoltage();
204+
private:
205+
// Default analog resolution for Arduino board
206+
static const int DEFAULT_ESP32_ADC_RESOLUTION = 4095;
207+
uint16_t vref_mv;
208+
};
209+
172210
#endif

0 commit comments

Comments
 (0)