Advertisement
kowalyshyn_o

Temperature Sensor Conversions

Jun 22nd, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. /* Temperature Printing Program
  2.  * By Orwell Kowalyshyn @ themakery.space
  3.  *
  4.  * This program reads a temperature sensor and calculates four different values of
  5.  * temperature in Celcius, Farenheit, and Kelvin
  6.  *
  7.  * The program also converts the signal into its equivalent in volts.
  8.  *
  9.  * Program assumes standard 500mv offset from thermistor. Consult
  10.  * your own sensor's datasheet for exact range and relationships.
  11.  *
  12.  * Lilypad thermistor is type MCP9700
  13.  * https://www.sparkfun.com/datasheets/DevTools/LilyPad/MCP9700.pdf
  14.  * standard form linear relationship V = (1/100)T + 0.5 for MCP9700
  15.  * standard form linear relationship V = (1.9/100)T + 0.4 for MCP9701
  16.  *
  17.  */
  18.  
  19. //define arduino ports as constants
  20. #define sensorPin A0
  21. #define outputPin 13
  22. #define offsetT 0.5  //MCP9700
  23. #define inverseSlopeT 100.0/1.0 //MCP9700
  24.  
  25.  
  26. void setup() {
  27.   Serial.begin(9600);
  28.   //set up serial port
  29.  
  30.   pinMode(outputPin, OUTPUT);
  31.   //define LED output
  32.  
  33.   digitalWrite(outputPin, LOW);
  34.   //start by turning LED off
  35. }
  36.  
  37. void loop() {
  38.    
  39.   int sensorRead = analogRead(sensorPin);
  40.   //read the raw sensor value between 0-1023, 10-bit ADC
  41.  
  42.   float sensorVoltage = (sensorRead*5.0)/1024.0;
  43.   //convert the sensor value to Volts (5.0V per 1024 steps)
  44.  
  45.   float sensorTempC = (sensorVoltage-offsetT)*inverseSlopeT;
  46.   //convert the voltage into degrees Celcius as per datasheet
  47.  
  48.   float sensorTempF = (sensorTempC*1.8)+32.0;
  49.   //convert degrees Celcius to degrees Farenheit
  50.  
  51.   float sensorTempK = sensorTempC + 273.15;
  52.   //convert degrees Celcius to Kelvin
  53.  
  54.   if (sensorTempC > 22) {   //turn on LED when warm
  55.     digitalWrite(outputPin, HIGH);
  56.   }
  57.  
  58.   else if (sensorTempC <= 22) {   //turn off LED when cold
  59.      digitalWrite(outputPin, LOW);
  60.   }
  61.  
  62.   //Debug display values
  63.   Serial.println(" Voltage: "+String(sensorVoltage)+" Celcius: "+String(sensorTempC)+" Farenheit: "+String(sensorTempF)+" Kelvin: "+String(sensorTempK));
  64.   delay(1000);
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement