Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 0:000a8be2414d, committed 2016-03-09
- Comitter:
- unix_guru
- Date:
- Wed Mar 09 13:52:45 2016 +0000
- Child:
- 1:601e1435dfb8
- Commit message:
- Created Thermistor Library for re-use
Changed in this revision
| Thermistor.cpp | Show annotated file Show diff for this revision Revisions of this file |
| Thermistor.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Thermistor.cpp Wed Mar 09 13:52:45 2016 +0000
@@ -0,0 +1,75 @@
+ /*
+ * Thermistor Temperature library
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "mbed.h"
+#include "Thermistor.h"
+
+
+ Thermistor::Thermistor(PinName pin) : _pin(pin) { // _pin(pin) means pass pin to the AnalogIn constructor
+ init();
+ }
+
+ void Thermistor::init() {
+ ThermistorNominal = THERMISTORNOMINAL;
+ TemperatureNominal = TEMPERATURENOMINAL;
+ BCoefficient = BCOEFFICIENT;
+ SeriesResistor = SERIESRESISTOR;
+ }
+
+ // This is the workhorse routine that calculates the temperature
+ // using the Steinhart-Hart equation for thermistors
+ // https://en.wikipedia.org/wiki/Steinhart%E2%80%93Hart_equation
+ float Thermistor::get_temperature()
+ {
+ float temperature =0, resistance =0;
+ float steinhart =0;
+ double a=0;
+ int smooth = 5; // Number of samples to smooth
+
+ for(int i=0;i<smooth;i++) {
+ a += _pin.read_u16(); // Read 16bit Analog value
+ }
+ a = a/smooth; // Get average of samples
+ // pc.printf("Raw Analog Value for Thermistor = %d\r\n",a);
+
+ /* Calculate the resistance of the thermistor from analog votage read. */
+ resistance = (float) SeriesResistor / ((65536.0 / a) - 1);
+ // pc.printf("Resistance for Thermistor = %f\r\n",resistance);
+
+ steinhart = resistance / ThermistorNominal; // (R/Ro)
+ steinhart = log(steinhart); // ln(R/Ro)
+ steinhart /= BCoefficient; // 1/B * ln(R/Ro)
+ steinhart += 1.0 / (TemperatureNominal + 273.15); // + (1/To)
+ steinhart = 1.0 / steinhart; // Invert
+ temperature = steinhart - 273.15; // convert to C
+
+ return temperature;
+ }
+
+ void Thermistor::set_ThermistorNominal(float thermnom) {
+ ThermistorNominal = thermnom;
+ }
+ void Thermistor::set_TemperatureNominal(float tempnom){
+ TemperatureNominal = tempnom;
+ }
+ void Thermistor::set_BCoefficient(float bcoefficient){
+ BCoefficient = bcoefficient;
+ }
+ void Thermistor::set_SeriesResistor(float resistor){
+ SeriesResistor = resistor;
+ }
+
+
+
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Thermistor.h Wed Mar 09 13:52:45 2016 +0000
@@ -0,0 +1,99 @@
+#ifndef THERMISTOR_H
+#define THERMISTOR_H
+///
+/// @mainpage Thermistor Temperature library
+///
+/// This is a Thermistor to Temerature conversion library
+///
+/// Much thanks to @Adafruit for this tutorial:
+/// https://learn.adafruit.com/thermistor/using-a-thermistor
+///
+/// The 100K Thermistor is configured with a 4.7k series resistor
+/// tied to vcc (3.3v) like this:
+///
+/// +3.3v
+/// |
+/// \
+/// / 4.7k series resistor
+/// \
+/// /
+/// |
+/// .-----------O To Anlog pin on FRDM board
+/// |
+/// \
+/// /
+/// Thermistor 100k Nominal
+/// \
+/// /
+/// |
+/// ---
+/// GND
+///
+///
+/// @author Michael J. Ball
+/// unix_guru at hotmail.com
+/// @unix_guru on Twitter
+/// March 2016
+///
+/// @code
+/// Thermistor bed(PTB10);
+/// printf("Temperature is %f Celcius", bed.get_temperature());
+///
+/// ...
+/// @endcode
+///
+/// @license
+/// Licensed under the Apache License, Version 2.0 (the "License");
+/// you may not use this file except in compliance with the License.
+/// You may obtain a copy of the License at
+///
+/// http://www.apache.org/licenses/LICENSE-2.0
+///
+/// Unless required by applicable law or agreed to in writing, software
+/// distributed under the License is distributed on an "AS IS" BASIS,
+/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+/// See the License for the specific language governing permissions and
+/// limitations under the License.
+///
+
+
+#include "mbed.h"
+
+
+#define THERMISTORNOMINAL 100000 // 100k default
+// temp. for nominal resistance (almost always 25 C)
+#define TEMPERATURENOMINAL 25
+// The beta coefficient of the thermistor (usually 3000-4000)
+#define BCOEFFICIENT 3950
+// the value of the 'other' resistor
+#define SERIESRESISTOR 4700
+
+
+class Thermistor {
+public:
+ Thermistor(PinName pin);
+ /** Receives a PinName variable.
+ * @param pin mbed pin to which the thermistor is connected
+ */
+
+
+ float get_temperature();
+ /** This is the workhorse routine that calculates the temperature
+ * using the Steinhart-Hart equation for thermistors
+ * https://en.wikipedia.org/wiki/Steinhart%E2%80%93Hart_equation
+ */
+ void init(); // Set default Steinhart-Hart values
+ void set_ThermistorNominal(float thermnom); // Change the thermistors nominal resistance
+ void set_TemperatureNominal(float tempnom); // Change the thermistors nominal temperature
+ void set_BCoefficient(float bcoefficient); // Change the thermistors BCoefficient
+ void set_SeriesResistor(float resistor); // Change the value of the series resistor
+
+private:
+ AnalogIn _pin;
+ float ThermistorNominal;
+ float TemperatureNominal;
+ float BCoefficient;
+ float SeriesResistor;
+};
+
+#endif