logo i-r-genius.com


A Simple Arduino LCD Min/Max Thermometer

As a simple first Arduino experiment I interfaced a two line LCD (a Displaytech 162B) and an LM35DZ to make a simple Min/Max thermometer.

Step 1 - Interfacing the Temperature Sensor

This is really very simple. The LM35DZ has three pins, +5V, ground and a variable voltage output to indicate the temperature. I plugged it into breadboard and wired the output straight to the Arduino's analogue input 0.

To test we can run a program to read the sensor, do the conversion to degrees celcius and then output it to the serial port (since we don't have an LCD quite yet).

int potPin = 0;            // select the input pin for the LM35
float temperature = 0;  

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  if (Serial.available())
  {
    val = analogRead(potPin);    // read the value from the sensor
    temperature = (5.0 * val * 100.0)/1024.0;
    Serial.println((long)temperature);
  }
  delay(1000);
}

Remember to use the arduino IDE's serial monitor to look for the output when run - it won't appear unless you select it!

Step 2 - The LCD module

The Displaytech 162B is a HD44780 compatible device, and it's easy to find guides on how to interface them with the Arduino. They can be driven with a eight data lines - which uses up most of the digital I/O pins on the Arduino, or more sensibly you can use 4 bit mode.

I started with 8 bit mode, and whilst it worked it's so trivial to use 4 bit instead it's not worth the extra wires! I also couldn't get both lines of the display working in 8 bit mode.

I connected the LCD like so - remember yours may differ :

LCD Pin                    Connected to 
1  Backlight Anode         +5V (via small resistor)
2  Backlight Cathode       GND
3  Ground                  GND
4  Supply V for Logic      +5V
5  Input Voltage for LCD   Middle pin of 50K potentiometer
6  RS (Data/Instruction)   Ard pin 12
7  R/W (Read/Write)        GND
8  E (Enable)              Ard pin 2
9  Data bit 0
10 Data bit 1
11 Data bit 2
12 Data bit 3
13 Data bit 4              Ard pin 7
14 Data bit 5              Ard pin 8
15 Data bit 6              Ard pin 9
16 Data bit 7              Ard pin 10

The other two pins of the potentiometer are connected to +5V and GND and provide a contrast control (and you will need it at least initally). 50K is over the top for the purpose, 10K is more typical, but it isn't an issue.

The resistor on the backlight was added because the backlight LED on the board was getting rather warm. The display stays bright enough and runs a bit cooler with a resistor on the supply.

The pin allocations on the Arduino were dictated by the LCD4Bit library, but you could easily change them in the library if you needed to.

You can take the test code and library straight from the Arduino Playground 4-bit LCD page.

Run it and verify your LCD is printing "arduino" as the program runs - you will probably need to twist the potentiometer to get a readable display.

The code

#include <LCD4Bit.h>
#undef int
#include <stdio.h>

LCD4Bit lcd = LCD4Bit(2); 
const int lm35pin=0;
int mintemp=255;
int maxtemp=-255;
int pin13 = 0;
   
void setup(void){
	lcd.init(); //initialize the LCD
}

void loop(void){
   int temp;
   int val=0;
   char buffer[64];

   lcd.commandWrite(2); //bring the cursor to the starting position
   val = analogRead(lm35pin);
   temp = (5*val*100/1024);
   if(maxtemp < temp)
     maxtemp=temp;
   if(mintemp > temp)
     mintemp=temp;
   // 337 in octal (223 decimal) is the degree symbol in the character
   // map of the 162B
   sprintf(buffer,"Current: %i\337C",temp);
   lcd.printIn(buffer);
   lcd.commandWrite(128+0x40); // move to 1st char on 2nd line

   sprintf(buffer,"Min %i\337C Max %i\337C",mintemp, maxtemp);
   lcd.printIn(buffer); //send the string to the LCD

   // set & invert pin 13 to flash LED
   digitalWrite(13,pin13 ? HIGH : LOW); 
   pin13=!pin13;
   
   delay(1000);
}

The

#undef int
fixes an issue in the libraries supplied with my installed version of the dev kit, you may not need it.

Future additions

  • The LM35 has an 0.5°C accuracy which we're not displaying - if with a few changes we could.
  • We could add multiple sensors, especially by using Dallas 1-wire sensors.



xargle@eh.org
Last modified on: Sat Jan 30 09:55:25 2010
N.T.K.