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.
main.cpp
00001 /** A demo application to show how to mangage and control a heater element 00002 * through a PID loop using a Thermistor input and PWM output 00003 * for the @NXP (@freescale) FRDM-K64F demo board. 00004 * 00005 * This particular example drives the heater element for a 3d Printer Extruder. 00006 * 00007 * For more information on PID control check out Brett Beauregard's Arduino PID library: 00008 * 00009 * https://github.com/br3ttb/Arduino-PID-Library 00010 * 00011 * The wikipedia article on PID controllers is a good place to start on 00012 * understanding how they work: 00013 * 00014 * http://en.wikipedia.org/wiki/PID_controller 00015 * 00016 * The Thermistor value to Temerature routine uses the Steinhart-Hart equation. 00017 This is a Thermistor to Temerature conversion demo 00018 00019 Much thanks to @Adafruit for this tutorial: 00020 https://learn.adafruit.com/thermistor/using-a-thermistor 00021 00022 The 100K Thermistor is configured with a 4.7k series resistor 00023 tied to vcc (3.3v) like this: 00024 00025 +3.3v 00026 | 00027 \ 00028 / 4.7k series resistor 00029 \ 00030 / 00031 | 00032 .-----------O To Anlog pin on FRDM board 00033 | 00034 \ 00035 / 00036 Thermistor 100k Nominal 00037 \ 00038 / 00039 | 00040 --- 00041 GND 00042 * 00043 * Author(s): Michael Ball unix_guru@hotmail.com 00044 * 00045 */ 00046 00047 #include "mbed.h" 00048 #include "millis.h" 00049 #include "PID.h" 00050 00051 float getTemperature(); 00052 00053 Serial pc(USBTX, USBRX); 00054 Ticker PrintTicker; // Send process results to Console once per second 00055 Ticker ticker; // Set up the millis() ticker. 00056 00057 #define DEFAULT_Kp 1 00058 #define DEFAULT_Ki 0.002 00059 #define DEFAULT_Kd 20 00060 00061 #define AUTOMATIC 1 00062 #define MANUAL 0 00063 #define DIRECT 0 00064 #define REVERSE 1 00065 #define thermistor A3 // FRDM-K64F Analog input pin A3 - Adjust to your particular board 00066 #define driver PTC3 // FRDM-K64F PWM output pin PTC3 - Adjust to your particular board 00067 00068 AnalogIn Thermistor(thermistor); // Read temperature value from thermistor on A3 00069 PwmOut Driver(driver); // PWM drive FET heater on PTC3 values are 0-1.0 00070 // For 0-100% 00071 00072 float Input, Output, Setpoint; 00073 PID controller(&Input, &Output, &Setpoint, DEFAULT_Kp , DEFAULT_Ki , DEFAULT_Kd , DIRECT); 00074 00075 #define RATE 1.0 // Print rate Once per second 00076 00077 void PrintValues() { // Routine to print out results to console 00078 pc.printf("Input Output Setpoint Kp Ki Kd time\r\n"); 00079 pc.printf("%f, %f, %f, %f, %f, %f, %d \r\n", 00080 Input, Output, Setpoint, controller.GetKp() , controller.GetKi() , controller.GetKd() , millis() ); 00081 00082 } 00083 00084 00085 int main(){ 00086 00087 startMillis(); // Initialize timer. 00088 00089 pc.baud(115200); 00090 pc.printf("\r\nThermistor PID Test - Build " __DATE__ " " __TIME__ "\r\n"); 00091 00092 PrintTicker.attach(&PrintValues,RATE); // Start PID process running at 100ms rate. 00093 00094 Setpoint = 80; // Set target temperature in degrees Celcius. 00095 controller.SetMode(AUTOMATIC); // Turn PID controller on. 00096 00097 00098 while(1){ 00099 00100 Input = getTemperature(); // Actual temperature in Degrees Celcius 00101 00102 controller.Compute(); // Process PID loop. 00103 00104 Driver = Output/1000; // Sent PWM value scaled 0 - 1.0 as mbed requires 00105 00106 } 00107 00108 } 00109 00110 00111 // This is the workhorse routine that calculates the temperature 00112 // using the Steinhart-Hart equation for thermistors 00113 // https://en.wikipedia.org/wiki/Steinhart%E2%80%93Hart_equation 00114 00115 float getTemperature() { 00116 #define THERMISTORNOMINAL 100000 // 100k 00117 // temp. for nominal resistance (almost always 25 C) 00118 #define TEMPERATURENOMINAL 25 00119 // The beta coefficient of the thermistor (usually 3000-4000) 00120 #define BCOEFFICIENT 3950 00121 // the value of the 'other' resistor 00122 #define SERIESRESISTOR 4700 00123 00124 // This is the workhorse routine that calculates the temperature 00125 // using the Steinhart-Hart equation for thermistors 00126 // https://en.wikipedia.org/wiki/Steinhart%E2%80%93Hart_equation 00127 00128 float temperature, resistance; 00129 float steinhart; 00130 int a; 00131 00132 a = Thermistor.read_u16(); // Read 16bit Analog value 00133 // pc.printf("Raw Analog Value for Thermistor = %d\r\n",a); 00134 00135 /* Calculate the resistance of the thermistor from analog votage read. */ 00136 resistance = (float) SERIESRESISTOR / ((65536.0 / a) - 1); 00137 // pc.printf("Resistance for Thermistor = %f\r\n",resistance); 00138 00139 steinhart = resistance / THERMISTORNOMINAL; // (R/Ro) 00140 steinhart = log(steinhart); // ln(R/Ro) 00141 steinhart /= BCOEFFICIENT; // 1/B * ln(R/Ro) 00142 steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To) 00143 steinhart = 1.0 / steinhart; // Invert 00144 temperature = steinhart - 273.15; // convert to C 00145 00146 // pc.printf("Extruder Temperature is %f\r\n", temperature); 00147 00148 return temperature; 00149 00150 } 00151
Generated on Sun Jul 17 2022 09:06:02 by
1.7.2