Want to see more?
► App Reviews
► Product Reviews
► Tutorials
Trending
Blog Archive
 RSS feed

October 16, 2010 | 11:28 am

Arduino: LCD Thermometer with optional Alarm

Arduino: LCD Thermometer with optional Alarm

This tutorial will help you construct a thermometer that will display the temperature on an LCD display.  Optionally, you can add an alarm that can be toggled via a pushbutton.  This alarm can alert you if the temperature passes a certain threshold temperature, which could be useful for monitoring the internal temperature of a computer, for example.

Equipment

Optional Equipment (for Alarm)

Procedure

  • Download the Arduino IDE for your operating system.

    Hardware
  • Ensure the headers are soldered onto your LCD display.  Read this tutorial if you need information about assembling your character LCD.  I am using a 16x2 LCD.

  • Now, we will connect our LCD to the Arduino.  First, to make things easier, connect +5V from the Arduino to the red rail of your breadboard.  Connect GND from the Arduino to the blue rail.  Connect pin16 of the LCD to ground, and pin15 to +5V.  Figure1, below, shows what you will have on your desk.  Note, I am using a tiny breadboard to hold my LCD, but you can just as easily use the main breadboard

    Figure1: LCD Backlight Power Connection


  • Next, we will hook up our 10k potentiometer.  Connect one side of the pot to +5V and the other side to GND.  The middle pin connects to pin3 of the LCD, as shown in Figure2.

    Figure2: Potentiometer Connection


  • Now, we will provide power for the the logic of the LCD.  Pin1 connects to ground and pin2 connects to +5V.  See Figure3.

    Figure3: LCD Logic Power Connections


  • Then, we will hook up the control lines.  Connect pin5 of the LCD to ground.  This pin is the R/W pin which determines whether we will be reading from or writing to our LCD module.  Logic level low (ground) means we are writing to the LCD, which is all we will be doing, hence we can simply tie it to ground.

  • Next, connect the LCD’s pin4, the register select (RS) pin, to the Arduino’s digital pin7.  (brown wire in Figure4)

  • Connect the LCD’s pin6, the Enable (EN) pin, to the Arduino’s digital pin8. (white wire in Figure4)

  • Finally, we will hook up the data bus lines.  We will be using DB7, DB6, DB5, and DB4.  Connect LCD pins14,13,12 and 11 to Arduino digital pins12,11,10, and 9 respectively.  Figure4 shows what you should have on your desk.  (orange, yellow, green, blue wire in Figure4— respectively)

    Figure4: Final LCD wiring connections


  • Now, plug in your Arduino and fire up the Arduino software you downloaded earlier.  Let’s test our LCD.  Open up the File->Examples->LiquidCrystal->HelloWorld example sketch.  We’ll have to modify the sketch to reflect the pins we used.  Look for the line:
    LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
    This calls the included LiquidCrystal library, so we don’t have to manually control when we write to the LCD. The format for this is
    LiquidCrystal LiquidCrystal lcd(RS, EN, DB4, DB5, DB6, DB7);
    If we make a table of the Arduino pins we connected to these LCD pins, it would look as follows:
    LCD pin name RS EN DB4 DB5 DB6 DB7
    Arduino pin # 7 8 9 10 11 12
    Table1: LCD Pin Configuration

    As you can now see, we must call the function with the following command
    LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
    Now you can compile and upload the sketch. Adjust the contrast if necessary with the potentiometer, and you should see hello world on your LCD.

  • Now that the LCD is running, we can connect our temperature sensor. Remove power from the Arduino, and plug the TMP36 module into the breadboard. With the flat side facing you, the leftmost pin is the source voltage pin, the middle pin is the output voltage, and the rightmost pin is ground. Pin1 and pin3 connect to the +5V rail and GND rail, respectively. Connect pin2 to the analog0 input on the Arduino. As far as hardware is concerned, your circuit is now complete. Figure5 illustrates what you should have on your desk.

    Figure5: LCD module with temperature sensor


    Software
  • Now for software. Plug in your Arduino to your computer. Paste the following code into your Arduino IDE. I have commented it well, so be sure you understand what is going on. Note: If your program won’t compile, you may have to manually retype quotation marks in code. After you upload the sketch to your board, the temperature should be displayed on the screen. See Figure6 for a photograph.
    #include <LiquidCrystal.h> 
    float tempC; 
    float tempF; 
    float voltage; 
    int reading; 
    int tempPin=0; //analog channel 
    // initialize the library with the numbers of the interface pins 
    LiquidCrystal lcd(7, 8, 9, 10, 11, 12); 
    void setup() { 
    // set up the LCD’s number of rows and columns:  
    lcd.begin(16, 2); 
    // Print a message to the LCD. 
    lcd.print("Temperature:"); 
    } 
    void loop() { 
    //TMP36 voltage at the output varies linearly with ambient temperature. 
    //As a result,it can be used as a thermometer according to the equation:
    // ᵒC = (Vout(V) - .5)*100 
    //To find Vout in V, we use the following equation for a 5V input 
    // Vout(V) = AnalogReading*(5/1024) 
    reading = analogRead(tempPin);   //read the value from the sensor 
    voltage = reading*(5.0/1024);  //convert reading to voltage (in V), for 5V input 
    tempC = (voltage-0.5)*100;   //convert voltage to temperature 
    tempF = ((tempC*9/5)+32); //convert C temperature to F 
    // (note: line 1 is the second row, since counting begins with 0): 
    lcd.setCursor(0, 1);// set the cursor to column 0, line 1 
    lcd.print(tempF);  //Print Fahrenheit temperature to LCD 
    lcd.print((char)223); // degree symbol 
    lcd.print("F "); 
    lcd.print(tempC);  //Print Celsius temperature to LCD 
    lcd.print((char)223); // degree symbol 
    lcd.print("C"); 
    delay(1000);  //wait one second                   
    }

    Figure6:  Photo of finished product

That’s it!  Just a basic thermometer; nothing really exciting to speak of.  Now, wouldn’t it be nice if your new thermometer could notify you if it is reading a value that is larger or smaller than a certain threshold temperature. I have added some instruction for this.  As an engineer, this is part of the analysis you might have to undergo— analyzing a customer’s wants vs. needs and tailoring a device that will really wow your customer. In fact, if you’re really clever, you can implement this functionality before presenting your design to the customer and have the code commented out. The client may then realize, “hey, wouldn’t it be neat if I could integrate an alarm with this.” At this point, you say “okay, I can do that. Give me a couple weeks.” Then, you simply uncomment the code and you get paid for a few extra weeks of work you’ve already done! (I’m not condoning this)

Optional Functionality
You will see a more advanced coding style that doesn’t use the delay() loops, because while undergoing a delay loop, anything else occurring in the system is ignored.  Also, this alarm will be able to be toggled on and off with a simple pushbutton, so you may also learn how a pushbutton can be utilized as a toggle switch by implementing a state machine.

  • First, we will connect our piezo element. If you use the device I used, it doesn’t matter which pin you connect to ground. However, some piezo buzzers are polarized, so the black wire should be connected to ground. Connect the other wire to digital pin13 on your Arduino. This pin is connected to the LED on the Arduino, so we can utilize that as a flashing light to complement our alarm. You could, conceivably, use a separate LED (don’t forget your current limiting resistor!)

  • Next, connect a pushbutton to the breadboard. Figure7 shows the design. Notice, I have a 10k resistor between my pushbutton and ground. This is a pull-down resistor. It serves to hold the logic level at zero when the button is not pressed.

    Figure7: Final design with piezo and pushbutton
  • Now, we will program the microcontroller. Connect your Arduino and upload the following code:
    #include <LiquidCrystal.h>
    float tempC;
    float tempF;
    float tempVoltage;
    int threshTemp = 80; //Temperature threshold (in F)
    int frequency = 500; //in Hz
    int duration = 750; //in ms
    int pinTemp=0; //analog channel
    int pinSpeaker = 13; //digital
    int pinSwitch = 6; //digital
    int state = HIGH;
    int currSwitch;
    int readingTemp;
    int prevSwitch = LOW;
    int interval = 750;  //in ms (how often screen reprints
    unsigned long int stateTime=0;
    unsigned long int printTime=0;
    unsigned long int buzzTime=0;
    long int debounce = 200;
    // initialize the library with the numbers of the interface pins
    LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
    
    void setup() {
    // set up the LCD’s number of rows and columns: 
    lcd.begin(16, 2);
    // Print a message to the LCD.
    pinMode(pinSpeaker, OUTPUT);
    pinMode(pinSwitch, INPUT);
    }
    
    void loop() {
    //TMP36 voltage at the output varies linearly with ambient temperature. 
    //As a result,it can be used as a thermometer according to the equation:
    // ᵒC = (Vout(V) - .5)*100 
    //To find Vout in V, we use the following equation for a 5V input 
    // Vout(V) = AnalogReading*(5/1024) 
    readingTemp = analogRead(pinTemp);   //read the value from the sensor
    currSwitch = digitalRead(pinSwitch);
    tempVoltage = readingTemp*(5.0/1024);  //convert reading to voltage (in V), for 5V input
    tempC = (tempVoltage-0.5)*100;   //convert voltage to Celsius temperature 
    tempF = ((tempC*9/5)+32); //convert C temperature to F 
    
    // millis() is built-in function that counts time (in ms) since last reset
    // We will utilize this to decide whether the button was actually pressed
    // or if it was just the switch bouncing naturally.
    if(currSwitch == HIGH && prevSwitch == LOW && millis() - stateTime > debounce) {
      //here we will implement a Finite State Machine (FSM)
      //When the button is pressed, we toggle states
        if (state == HIGH)
          state = LOW;
        else
          state = HIGH;
        stateTime = millis();    
    }
    
    prevSwitch = currSwitch;
    if(millis() - printTime > interval){ //reprint every <interval> ms
        lcd.setCursor(0, 0);// set the cursor to column 0, line 0
        if(state == LOW)  //Print (A) to screen if Alarm enabled
          lcd.print("Temperature: (A)");
        else
          lcd.print("Temperature:    ");
        lcd.setCursor(0, 1);// set the cursor to column 0, line 1
        lcd.print(tempF);  //send the data to the computer
        lcd.print((char)223); // degree symbol
        lcd.print("F ");
        lcd.print(tempC);
        lcd.print((char)223); // degree symbol
        lcd.print("C");
        printTime = millis();
        //Next statement plays tone IF alarm is enabled   
        //buzz every <duration> ms when above threshold
    if
    (tempF>=threshTemp && state==LOW && millis() - buzzTime > duration){     playTone(duration,frequency); //buzz pulse high for <duration> ms at <freq>kHz     buzzTime = millis();     } } } void playTone(long dur, int freq) {     dur *= 1000;     int period = (1.0 / freq) * 1000000;     long elapsed_time = 0;     // last condition (in next statement) is to allow for disabling buzzer while buzzing     while (elapsed_time < dur && digitalRead(pinSwitch)!=HIGH) {         //Pulse train signal         digitalWrite(pinSpeaker,HIGH);         delayMicroseconds(period / 2);         digitalWrite(pinSpeaker, LOW);         delayMicroseconds(period / 2);         elapsed_time += (period);     } }
  • There you have it. The alarm will be off by default. If you press the pushbutton once, it will toggle on and the first line of the LCD will display “Temperature (A)” The default alarm conditions are for it to sound if the temperature reading is above 80F. Figure 8 shows what you will have on your desk

    Figure8: Photo of finished product with alarm and pushbutton


    Extras
  • Fritzing Diagram

    References
  • ladyada.net
Creative Commons License
LCD Thermometer with Alarm by Brad Wells is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

For more from Brad Wells, visit

Notes

  1. 5m0k3 posted this