Docstoc

Arduino Playground

Document Sample
Arduino Playground Powered By Docstoc
					Arduino playground - Thermistor2                                                                                                            Page 1 of 2




    Arduino: Playground                                                                                                                             search



                                      view history edit print login register
    The playground is a
    publicly-editable wiki about
    Arduino.                          Reading a Thermistor
     • Deutsch                        This is a function I wrote to convert the value from an analogRead call of a pin with a thermistor connected to it
     • English                        to a temperature. Unlike most other programs that use a look-up table, this function utilizes the Steinhart-Hart
     • Español                        Thermistor Equation to convert "Thermistor Resistance" to "Temperature in Degrees Kelvin." I found the equation
     • Français                       here, but it can also be found at Wikipedia.
     • Italiano
     • Português                      The more simple program shows how little code is necessary to do the conversion, and how much memory and
     • Български                      program space can be saved when compared to a large look-up table.
     • Русский
                                      The more elaborate program shows the power of the function. It displays lots of data including the ADC (from 0 to
    Manuals and Curriculum            1023), the voltage on the analog pin (note: ideally, the arduino would be running at 5.0 volts, but my power
                                      supply gives it 4.86 volts, so I divide by 4.86. Look at the code to change that to whatever your power supply is
    Board Setup and                   giving), the resistance of the Thermistor, and the temperature in Celsius and Fahrenheit. It uses the printDouble
    Configuration                     routine which I found in a forum post here.

    Development Tools                 I hope this comes in handy for everyone. Like most of my code, it's freely usable and modifiable, and no credit is
                                      needed. Free, like arduino should be.
    Interfacing With Hardware
                                      ============================================================
     • Output
     • Input
     • Storage
                                      Thermistor Test Schematic
     • Communication                  ============================================================
     • General
                                          (Ground) ---- (10k-Resister) -------|------- (Thermistor) ---- (+5v)
    Interfacing with Software                                                        |
                                                                               Analog Pin 0
    Code Library and Tutorials
                                      ============================================================
    Suggestions & Bugs

    Electronics Technique             The Simple Code
    Sources for Electronic Parts      ============================================================

    Related Hardware and              #include <math.h>
    Initiatives
                                      double Thermister(int RawADC) {
    Arduino People/Groups &
                                          double Temp;
    Sites
                                          Temp = log(((10240000/RawADC) - 10000));
                                          Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
    Exhibition
                                          Temp = Temp - 273.15;                  // Convert Kelvin to Celcius
    Project Ideas                         Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
                                          return Temp;
    Languages
                                      }


    PARTICIPATE                       void setup() {
                                          Serial.begin(115200);
     • create an account              }
     • suggestions
     • formatting suggestions         void loop() {
     • all recent changes                 Serial.println(int(Thermister(analogRead(0))));           // display Fahrenheit
     • PmWiki                             delay(100);
     • WikiSandBox training           }
     • Basic Editing
     • Cookbook (addons)
     • Documentation Index            ============================================================


                       edit SideBar
                                      The Elaborate Code
                                      ============================================================

                                      #include <math.h>
                                      //Schematic:
                                      // [Ground] ---- [10k-Resister] -------|------- [Thermistor] ---- [+5v]
                                      //                                                 |
                                      //                                        Analog Pin 0


                                      double Thermistor(int RawADC) {
                                          // Inputs ADC Value from Thermistor and outputs Temperature in Celsius
                                          //   requires: include <math.h>
                                          // Utilizes the Steinhart-Hart Thermistor Equation:
                                          //    Temperature in Kelvin = 1 / {A + B[ln(R)] + C[ln(R)]^3}
                                          //    where A = 0.001129148, B = 0.000234125 and C = 8.76741E-08
                                          long Resistance;   double Temp;       // Dual-Purpose variable to save space.
                                          Resistance=((10240000/RawADC) - 10000);            // Assuming a 10k Thermistor.    Calculation is actually: Resis
                                          Temp = log(Resistance); // Saving the Log(resistance) so not to calculate it 4 times later. // "Temp" m




http://www.arduino.cc/playground/ComponentLib/Thermistor2                                                                                     19-5-2010
Arduino playground - Thermistor2                                                                                  Page 2 of 2



                             Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));      // Now it m
                             Temp = Temp - 273.15;   // Convert Kelvin to Celsius                                           // Now it o


                             // BEGIN- Remove these lines for the function not to display anything
                             Serial.print("ADC: "); Serial.print(RawADC); Serial.print("/1024");     // Print out RAW ADC Number
                             Serial.print(", Volts: "); printDouble(((RawADC*4.860)/1024.0),3);      // 4.860 volts is what my USB Po
                             Serial.print(", Resistance: "); Serial.print(Resistance); Serial.print("ohms");
                             // END- Remove these lines for the function not to display anything


                             // Uncomment this line for the function to return Fahrenheit instead.
                             //Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert to Fahrenheit
                             return Temp;   // Return the Temperature
                         }


                         void printDouble(double val, byte precision) {
                             // prints val with number of decimal places determine by precision
                             // precision is a number from 0 to 6 indicating the desired decimal places
                             // example: printDouble(3.1415, 2); // prints 3.14 (two decimal places)
                             Serial.print (int(val));       //prints the int part
                             if( precision > 0) {
                                 Serial.print("."); // print the decimal point
                                 unsigned long frac, mult = 1;
                                 byte padding = precision -1;
                                 while(precision--) mult *=10;
                                 if(val >= 0) frac = (val - int(val)) * mult; else frac = (int(val) - val) * mult;
                                 unsigned long frac1 = frac;
                                 while(frac1 /= 10) padding--;
                                 while(padding--) Serial.print("0");
                                 Serial.print(frac,DEC) ;
                             }
                         }


                         void setup() {
                             Serial.begin(115200);
                         }


                         #define ThermistorPIN 0       // Analog Pin 0
                         double temp;
                         void loop() {
                             temp=Thermistor(analogRead(ThermistorPIN));            // read ADC and convert it to Celsius
                             Serial.print(", Celsius: "); printDouble(temp,3);      // display Celsius
                             temp = (temp * 9.0)/ 5.0 + 32.0;                       // converts to Fahrenheit
                             Serial.print(", Fahrenheit: "); printDouble(temp,3);   // display Fahrenheit
                             Serial.println("");                                    // End of Line
                             delay(100);                                            // Delay a bit... for fun, and to not Serial.pri
                         }



                                 Share |




http://www.arduino.cc/playground/ComponentLib/Thermistor2                                                            19-5-2010

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:8
posted:10/1/2011
language:English
pages:2