Landing : Athabascau University
  • Blogs
  • Circuit 4B: Temperature Sensor

Circuit 4B: Temperature Sensor

I had no issues with building the circuit. My program displays the temperature in Celsius, Fahrenheit, and kelvins, as well as a message. The possible messages include “Freezing”, “Cold”, “Chilly”, “Nice”, “Warm”, “Hot”, “Too hot”, and “Boiling”. Here is the code:

#include <LiquidCrystal.h> //the liquid crystal library contains commands for printing to the display
LiquidCrystal lcd(7, 12, 11, 10, 9, 8); // tell the RedBoard what pins are connected to the display
float voltage = 0; //the voltage measured from the TMP36
float degreesC = 0; //the temperature in Celsius, calculated from the voltage
float degreesF = 0; //the temperature in Fahrenheit, calculated from the voltage
float kelvins = 0; //the temperature in kelvins
String message = ""; //the message to be displayed
void setup() {
lcd.begin(16, 2); //tell the lcd library that we are using a display that is 16 characters wide and 2 characters high
lcd.clear(); //clear the display
}
void loop() {
voltage = analogRead(A0) * 0.004882813; //convert the analog reading, which varies from 0 to 1023, back to a voltage value from 0-5 volts
degreesC = (voltage - 0.5) * 100.0; //convert the voltage to a temperature in degrees Celsius
degreesF = degreesC * (9.0 / 5.0) + 32.0; //convert the voltage to a temperature in degrees Fahrenheit
kelvins = degreesC + 273.15; //convert from Celsius to kelvins
//Decide what message to print
if(degreesC <= 0){
message = " Freezing";
}else if(degreesC > 0 && degreesC <= 10){
message = " Cold ";
}else if(degreesC > 10 && degreesC <= 20){
message = " Chilly ";
}else if(degreesC > 20 && degreesC <= 30){
message = " Nice ";
}else if(degreesC > 30 && degreesC <= 40){
message = " Warm ";
}else if(degreesC > 40 && degreesC <= 50){
message = " Hot ";
}else if(degreesC > 50 && degreesC < 100){
message = " Too hot ";
}else if(degreesC >= 100){
message = " Boiling ";
}
lcd.setCursor(0, 0); //set the cursor to the top left position
lcd.print("C:"); //print a label for the data
lcd.print(degreesC); //print the degrees Celsius
lcd.setCursor(0, 1); //set the cursor to the lower left position
lcd.print("F:"); //Print a label for the data
lcd.print(degreesF); //print the degrees Fahrenheit
lcd.setCursor(7, 0); //set the cursor to the position
lcd.print(" K:"); //Print a label for the data
lcd.print(kelvins); //print the kelvins
lcd.setCursor(7, 1); //set the cursor to the position
lcd.print(message); //print the message
delay(1000); //delay for 1 second between each reading (this makes the display less noisy)
}

Most of my code is from the example program. I changed the way the temperatures are labelled to make more room on the display, so instead of “Celsius:” and “Fahrenheit:” it simply displays “C:” and “F:”. I also added “K:” for kelvins. I read in the Kelvin Wikipedia article that a temperature in degrees Celsius is equal to the temperature in kelvins minus 273.15, so to find the temperature in kelvins I added 273.15 to the temperature in Celsius. I also learned from the article that unlike the Celsius and Fahrenheit systems, the Kelvin system measures temperature in kelvins rather than degrees. In the loop() function there is also a series of if-else statements to determine which message to print on the display.

Here is a video of the program in action:  TemperatureSensor

Each of the possible message strings and the “K:” string start with a space, and the cursor is set to the seventh character block before displaying them. This is because I wanted there to be spaces between the last digits of the Celsius and Fahrenheit readings and the first characters of the message and “K:” strings. If I had set the cursor to the eighth character and not included spaces at the beginning of the strings, there would be no spaces between the digits and the characters when the Celsius or Fahrenheit readings get to below zero degrees or greater than or equal to one hundred degrees, because then the temperature reading would take up an extra digit for either the “1” in “100” or the “-“ if the temperature is below zero. Also, I omitted the lcd.clear(); statement in the loop() function because it made the display blink every time the readings were updated. This statement was not needed because all the strings that are printed to the display fill up all the character blocks, so the previous readings and messages are always entirely overwritten. This is the reason for all the message strings having a bunch of spaces at their ends. After running the program, I compared the temperature readings with the readings on an instant thermometer and found the program to be pretty accurate.