|
| 1 | +/* |
| 2 | +DCU-Controlled Birthday Candle (v 2016.05) |
| 3 | +Reads the temperature from a Vernier Surface Temperature Probe (STS-BTA) |
| 4 | +connected to the BTA 1 connector. As written, the readings will be displayed every half second. |
| 5 | +Change the variable TimeBetweenReadings to change the rate. |
| 6 | +
|
| 7 | +We use the Steinhart-Hart equation (in the function Thermistor) to determine temperature |
| 8 | +from the raw A/D converter reading. Because of the use of log functions, in the Steinhart-Hart |
| 9 | +equation, this sketch requires the math.h library. |
| 10 | +
|
| 11 | +This sketch controls a "fake birthday candle", which is really an LED connected to line 1 of the |
| 12 | +Digital Control Unit (DCU). |
| 13 | +
|
| 14 | +Run the sketch and allow the temperature to stablize. When it has, press the D12 button on the Vernier |
| 15 | +Interface Shield. This will establish a threshold temperature. The candle should go out any time the |
| 16 | +temperature reading drops 2 degrees from this threshold temperture. |
| 17 | +
|
| 18 | +See www.vernier.com/engineering/stem/sensors/temperature-sensor/ |
| 19 | +for more information on how thermistors are read. |
| 20 | +
|
| 21 | +See www.vernier.com/arduino for more information. |
| 22 | + */ |
| 23 | +#include <math.h> |
| 24 | +int ThermistorPIN =0;// Analog Pin 0 |
| 25 | +int TimeBetweenReadings = 500; // in ms |
| 26 | +int ReadingNumber=0; |
| 27 | +int buttonPin= 12; // analog input pin to use as a digital input |
| 28 | +int LED= 13; // digital output pin for LED 1 indicator |
| 29 | +int Candle =6;// this is the line to control the candle if the using line 1 of the DCU in Digital 2 |
| 30 | +float Threshold= 0; //Threshold temperature, initialize to 0 so candle is on |
| 31 | +int buttonState = 0;//variable for reading the pushbutton status |
| 32 | +void setup() |
| 33 | +{ |
| 34 | + Serial.begin(9600); |
| 35 | + pinMode(LED, OUTPUT); //LED on SparkFun Vernier Shield |
| 36 | + pinMode(6, OUTPUT); // set Arduino line 6 for output, which is DCU line 1 (if DCU is connected to Digital 2) |
| 37 | + // Set up button input pin; |
| 38 | + pinMode(buttonPin, INPUT_PULLUP); |
| 39 | + Serial.println("Vernier Format 2"); |
| 40 | + Serial.println("Temperature Readings taken using Ardunio"); |
| 41 | + Serial.println("Data Set"); |
| 42 | + Serial.print("Time");//long name |
| 43 | + Serial.print("\t"); //tab character |
| 44 | + Serial.print ("Temperature"); |
| 45 | + Serial.print("\t"); //tab character |
| 46 | + Serial.println ("Threshold"); |
| 47 | + Serial.print("t"); |
| 48 | + Serial.print("\t"); //tab character |
| 49 | + Serial.println ("Temp"); //short name |
| 50 | + Serial.print("seconds"); |
| 51 | + Serial.print("\t"); // tab character |
| 52 | + Serial.print ("degrees C"); |
| 53 | + Serial.print("\t"); // tab character |
| 54 | + Serial.println ("degrees C"); |
| 55 | +} |
| 56 | +void loop() |
| 57 | +{ |
| 58 | + float Time; |
| 59 | + int Count; //reading from the A/D converter (10-bit) |
| 60 | + float Temp; //the print below does the division first to avoid overflows |
| 61 | + Serial.print(ReadingNumber/1000.0*TimeBetweenReadings); |
| 62 | + Count=analogRead(ThermistorPIN); // read count from the A/D converter |
| 63 | + Temp=Thermistor(Count); // and convert it to CelsiusSerial.print(Time/1000); //display in seconds, not milliseconds |
| 64 | + Serial.print("\t"); //tab character |
| 65 | + Serial.print(Temp,1); // display temperature to one digit |
| 66 | + Serial.print("\t"); //tab character |
| 67 | + Serial.println (Threshold,1); |
| 68 | + if (Temp<(Threshold)) { |
| 69 | + digitalWrite(Candle, LOW); //turn off candle |
| 70 | + digitalWrite(LED, LOW); //turn off LED, also |
| 71 | + } |
| 72 | + else { |
| 73 | + digitalWrite (Candle, HIGH);//turn on candle |
| 74 | + digitalWrite(LED, HIGH); //turn on LED, also |
| 75 | + } |
| 76 | + //Special section to set threshold, if button is pressed: |
| 77 | + buttonState = digitalRead(buttonPin); |
| 78 | + // if it is, the buttonState is LOW: |
| 79 | + if (buttonState == LOW) |
| 80 | + { |
| 81 | + Threshold = Temp-2;// set this as the threshold temperature, 2 degrees C below current temp |
| 82 | + Serial.print("Threshold set as "); |
| 83 | + Serial.print (Threshold,1); |
| 84 | + Serial.println(" degrees C"); |
| 85 | + }// end of special operatures done if button is down |
| 86 | + |
| 87 | + ReadingNumber++; |
| 88 | + delay(TimeBetweenReadings); // Delay a bit... |
| 89 | +} |
| 90 | + |
| 91 | +float Thermistor(int Raw) //This function calculates temperature from ADC count |
| 92 | +{ |
| 93 | + /* Inputs ADC count from Thermistor and outputs Temperature in Celsius |
| 94 | + * requires: include <math.h> |
| 95 | + * There is a huge amount of information on the web about using thermistors with the Arduino. |
| 96 | + * Here we are concerned about using the Vernier Stainless Steel Temperature Probe TMP-BTA and the |
| 97 | + * Vernier Surface Temperature Probe STS-BTA, but the general principles are easy to extend to other |
| 98 | + * thermistors. |
| 99 | + * This version utilizes the Steinhart-Hart Thermistor Equation: |
| 100 | + * Temperature in Kelvin = 1 / {A + B[ln(R)] + C[ln(R)]^3} |
| 101 | + * for the themistor in the Vernier TMP-BTA probe: |
| 102 | + * A =0.00102119 , B = 0.000222468 and C = 1.33342E-7 |
| 103 | + * Using these values should get agreement within 1 degree C to the same probe used with one |
| 104 | + * of the Vernier interfaces |
| 105 | + * |
| 106 | + * Schematic: |
| 107 | + * [Ground] -- [thermistor] -------- | -- [15,000 ohm resistor] --[Vcc (5v)] |
| 108 | + * | |
| 109 | + * Analog Pin 0 |
| 110 | + For the circuit above: |
| 111 | + * Resistance = ( Count*RawADC /(1024-Count)) |
| 112 | + */ |
| 113 | + long Resistance; |
| 114 | + float Resistor = 15000; //fixed resistor |
| 115 | +// the measured resistance of your particular fixed resistor in |
| 116 | +// the Vernier BTA-ELV and in the SparkFun Vernier Adapter Shield |
| 117 | +// is a precision 15K resisitor |
| 118 | + float Temp; // Dual-Purpose variable to save space. |
| 119 | + Resistance=( Resistor*Raw /(1024-Raw)); |
| 120 | + Temp = log(Resistance); // Saving the Log(resistance) so not to calculate it 4 times later |
| 121 | + Temp = 1 / (0.00102119 + (0.000222468 * Temp) + (0.000000133342 * Temp * Temp * Temp)); |
| 122 | + Temp = Temp - 273.15; // Convert Kelvin to Celsius |
| 123 | + return Temp; // Return the Temperature |
| 124 | +} |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | + |
0 commit comments