Project 24 – LCD Temperature Display

This project is a simple demonstration of using an LCD to present useful information to the user—in this case, the temperature from an analog temperature sensor. You will add a button to switch between displaying the in Celsius or Fahrenheit. Also, the maximum and minimum temperature will be displayed on the second row.

Parts Required

The parts required are the same as for Project 23, plus a button and an analogue temperature sensor. Make sure that the temperature sensor only outputs positive values.

16×2 Backlit LCD images

Current Limiting Resistor (Backlight) images

Current Limiting Resistor (Contrast) images

Pushbutton images

Analogue Temperature Sensor images

Connect It Up

Use the exact same circuit that you set up for Project 23. Then add a pushbutton and temperature sensor as shown in Figure 8-2.

images

Figure 8-2. The circuit for Project 24 – LCD Temperature Display (see insert for color version)

I have used an LM35DT temperature sensor, which has a range from 0°C to 100°C. You can use any analogue temperature sensor. The LM35 is rated from -55°C to +150°C. You will need to adjust your code accordingly (more on this later).

Enter The Code

Check your wiring, then upload the code from Listing 8-2.

Listing 8-2. Code for Project 24

// PROJECT 24
#include <LiquidCrystal.h>

// Initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Create an lcd object and assign the pins
int maxC=0, minC=100, maxF=0, minF=212;
int scale = 1;
int buttonPin=8;

void setup() {
        lcd.begin(16, 2); // Set the display to 16 columns and 2 rows
        analogReference(INTERNAL);
        pinMode(buttonPin, INPUT);
        lcd.clear();
}

void loop() {
        lcd.setCursor(0,0); // Set cursor to home position
        int sensor = analogRead(0); // Read the temp from sensor
        int buttonState = digitalRead(buttonPin); // Check for button press
        switch (buttonState) { // Change scale state if pressed
                case HIGH:
                        scale=-scale; // Invert scale
                        lcd.clear();
        }

        delay(250);
        switch (scale) { // Decide if C or F scale
                case 1:
                        celsius(sensor);
                        break;
                case -1:
                        fahrenheit(sensor);
        }
}

void celsius(int sensor) {
        lcd.setCursor(0,0);
        int temp = sensor * 0.09765625; // Convert to C
        lcd.print(temp);
        lcd.write(B11011111); // Degree symbol
        lcd.print("C  ");
        if (temp>maxC) {maxC=temp;}
        if (temp<minC) {minC=temp;}
        lcd.setCursor(0,1);
        lcd.print("H=");
        lcd.print(maxC);
        lcd.write(B11011111);
        lcd.print("C L=");
        lcd.print(minC);
        lcd.write(B11011111);
        lcd.print("C  ");
}

void fahrenheit(int sensor) {
        lcd.setCursor(0,0);
        float temp = ((sensor * 0.09765625) * 1.8)+32; // convert to F
        lcd.print(int(temp));
        lcd.write(B11011111); // Print degree symbol
        lcd.print("F ");
        if (temp>maxF) {maxF=temp;}
        if (temp<minF) {minF=temp;}
        lcd.setCursor(0,1);
        lcd.print("H=");
        lcd.print(maxF);
        lcd.write(B11011111);
        lcd.print("F L=");
        lcd.print(minF);
        lcd.write(B11011111);
        lcd.print("F ");
}

When you run the code the current temperature will be displayed on the top row of the LCD. The bottom row will display the maximum and minimum temperatures recorded since the Arduino was turned on or the program was reset. By pressing the button, you can change the temperature scale between Celsius and Fahrenheit.

Project 24 – LCD Temperature Display – Code Overview

As before, the LiquidCrystal library is loaded into your sketch:

#include <LiquidCrystal.h>

A LiquidCrystal object called lcd is initialized and the appropriate pins set:

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Create an lcd object and assign the pins

Some integers to hold the maximum and minimum temperatures in degrees C and F are declared and initialized with impossible max and min values. These will be changed as soon as the program runs for the first time.

int maxC=0, minC=100, maxF=0, minF=212;

A variable called scale of type int is declared and initialized with 1. The scale variable will decide if you are using Celsius or Fahrenheit as your temperature scale. By default, it's set to 1, which is Celsius. You can change this to -1 for Fahrenheit.

int scale = 1;

An integer to store the pin being used for the button is declared and initialized:

int buttonPin=8;

In the setup() loop, you set the display to be 16 columns and 2 rows:

lcd.begin(16, 2); // Set the display to 16 columns and 2 rows

The reference for the analogue pin is then set to INTERNAL:

analogReference(INTERNAL);

This gives you a better range on the Arduino's ADC (Analogue to Digital Convertor). The output voltage of the LM35DT at 100°C is 1v. If you were using the default reference of 5 volts, then at 50°C, which is half of the sensors range, the reading on the ADC would be 0.5v = (0.5/5)*1023 = 102, which is only about 10% of the ADC's range. When using the internal reference voltage of 1.1 volts, the value at the analogue pin at 50°C is now 0.5v = (0.5/1.1)*1023 = 465.

As you can see, this is almost half way through the entire range of values that the analogue pin can read (0 to 1023), therefore the resolution and accuracy of the reading has increased, as has the sensitivity of the circuit.

The button pin is now set to an input and the LCD display cleared:

pinMode(buttonPin, INPUT);
lcd.clear();

In the main loop, the program starts off by setting the cursor to its home position:

lcd.setCursor(0,0); // Set cursor to home position

Then you read a value from the temperature sensor on Analogue Pin 0:

int sensor = analogRead(0); // Read the temp from sensor

Then you read the state of the button and store the value in buttonState:

int buttonState = digitalRead(buttonPin); // Check for button press

Now you need to know if the button has been pressed or not and if so, to change the scale from Celsius to Fahrenheit or vice-versa. This is done using a switch/case statement:

        switch (buttonState) { // change scale state if pressed
                case HIGH:
                        scale=-scale; // invert scale
                        lcd.clear();
        }

This is a new concept: The switch/case command controls the flow of the program by specifying what code should be run based on what conditions have been met. The switch/case statement compares the value of a variable with values in the case statements, and if true, runs the code after that case statement.

For example, if you had a variable called var and you wanted things to happen if its value was either 1, 2, or 3, then you could decide what to do for those values like so:

switch (var) {
        case 1:
                // run this code here if var is 1
                break;
        case 2:
                // run this code here if var is 2
                break;
        case 3:
                // run this code here if var is 3
                break;
        default:
                // if nothing else matches run this code here
        }

The switch/case statement will check the value of var. If it's 1, it will run the code within the case 1 block up to the break command. The break command is used to exit out of the switch/case statement. Without it, the code would carry on executing until a break command is reached or the end of the switch/case statement is reached. If none of the checked-for values are reached, then the code within the default section will run. Note that the default section is optional and not necessary.

In your case, you only check one case: if the buttonState is HIGH. If so, the value of scale is inverted (from C to F or vice-versa) and the display is cleared.

Next is a short delay:

delay(250);

Then another switch/case statement to check if the value of scale is either a 1 for Celsius or a -1 for Fahrenheit and if so to run the appropriate functions:

        switch (scale) { // decide if C or F scale
                case 1:
                        celsius(sensor);
                        break;
                case -1:
                        fahrenheit(sensor);
        }
}

Next you have the two functions to display the temperatures on the LCD. One is for working in Celsius and the other for Fahrenheit. The functions have a single parameter. You pass it an integer value that will be the value read from the temperature sensor:

void celsius(int sensor) {

The cursor is set to the home position:

lcd.setCursor(0,0);

Then you take the sensor reading and convert it to degrees Celsius by multiplying by 0.09765625:

int temp = sensor * 0.09765625; // convert to C

This factor is reached by taking 100, which is the range of the sensor and dividing it by the range of the ADC, which is 1024:

100/1024=0.09765625.

If your sensor had a range from -40 to +150 degrees Celsius, the calculation would be the following (presuming you had a sensor that did not output negative voltages):

190 / 1024 = 0.185546875

You then print that converted value to the LCD, along with char B11011111, which is a degree symbol, followed by a C to indicate that you are displaying the temperature in Celsius:

lcd.print(temp);
lcd.write(B11011111); // degree symbol
lcd.print("C  ");

Then the current temperature reading is checked to see if it is greater than the currently stored values of maxC and minC. If so, the values of maxC or minC are changed to the current value of temp. This will keep a running score of the highest or lowest temperatures read since the Arduino was turned on.

if (temp>maxC) {maxC=temp;}
if (temp<minC) {minC=temp;}

On the second row of the LCD you print H (for HIGH) and the value of maxC and then an L (for LOW) followed by the degree symbol and a letter C:

lcd.setCursor(0,1);
lcd.print("H=");
lcd.print(maxC);
lcd.write(B11011111);
lcd.print("C L=");
lcd.print(minC);
lcd.write(B11011111);
lcd.print("C  ");

The Fahrenheit function does exactly the same, except it converts the temperature in Celsius to Fahrenheit by multiplying it by 1.8 and adding 32:

float temp = ((sensor * 0.09765625) * 1.8)+32; // convert to F

Now that you know how to use an LCD to display useful information, you can create your own projects to display sensor data or to create a simple user interface.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.5.38