Monday, 15 March 2010

Arduino, LM34 temperature sensor, DRobotics Arduino LCD shield and LiquidCrystal.h



One of the problems with the DRobotics Arduino LCD shield is that the documentation suggests the use of a LCD_4Bit_mod 'd library. This leads to all sorts of fun and games when attempting to print floats and anything else other than chars or ints. I gave up. I like to use standard libraries and code wherever possible so decided to wade through the .cpp to find the pins, to allow them to be mapped onto the standard Liquid crystal code (LiquidCrystal.h).

The prescient bit of mapping code is for the shield is:

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

The second problem is that the DRobotics shield doesn't use the nice passthrough pins found on other shields so I ordered the Arduino header pins from coolcomponents.co.uk and soldered them on.

This is my temperature probe test circuit - cribbed from here.



Here is the progress code to tie the whole thing together:



/*
LiquidCrystal library with DRobotics shield.


Pins configured with DRobotics LCD shield.
http://www.droboticsonline.com/index.php/review/product/list/id/7/
Example circuit for printing temperature from an LM34.

The circuit:
* LCD RS pin to digital pin 8
* LCD Enable pin to digital pin 9
* LCD D4 pin to digital pin 4
* LCD D5 pin to digital pin 5
* LCD D6 pin to digital pin 6
* LCD D7 pin to digital pin 7

LM34 into +5V, GND
Middle pin to Analog pin 1 (Analog 0 is used by the DRobotics shield)

Library originally added 18 Apr 2008
by David A. Mellis
library modified 5 Jul 2009
by Limor Fried (http://www.ladyada.net)
example added 9 Jul 2009
by Tom Igoe
modified 25 July 2009
by David A. Mellis
modified 15 Mar 2010
by Tim Gibbon

Modified from
http://www.arduino.cc/en/Tutorial/LiquidCrystal
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1235965293

*/

// include the library code:
#include
int sensor_pin = 1; // the analog pin for LM34 temp sensor - Cannot use pin 0 as it is used for something by DRobotics LCD shield
float sensor_reading = 0.0; // variable to store the value coming from the sensor
float vref = 1.084; // variable to store the voltage reference used (check for validity with a DMM)
int fahrenheit = 0; // variable to store the actual temperature
int acquisition_timer = 1000; // variable to control the time between updates (in ms)
int centigrade = 0;
int loop_counter = 0;

// initialize the library with the numbers of the interface pins - suitable for DRobotics LCD Keypad shield
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16,2);
pinMode( sensor_pin, INPUT ); // set LM34 temp sensor pin as an input
analogReference(INTERNAL); // set the analog reference to the 1.1V internal reference
delay(1000);

}

void loop() {

sensor_reading = analogRead(sensor_pin); // stores the digitized (0 - 1023) analog reading from the LM34
fahrenheit = (100.0 * sensor_reading * vref)/1023; // calculates the actual fahrenheit temperature
centigrade = (((5.0/9.0))*(fahrenheit-32.0)); //conversion to degrees C

// set the cursor to (0,0):
lcd.setCursor(0, 0);

lcd.print("Temp=");
lcd.print(centigrade);
lcd.print("C");
//Print useful counter on the second row

lcd.setCursor(0,1);
lcd.print(loop_counter);
loop_counter++;
delay(5000);
// turn off automatic scrolling
lcd.noAutoscroll();
// clear screen for the next loop:
lcd.clear();
}




Download code here

3 comments:

south said...

Many, many thanks. I've just been banging my head off the desk over the same issue. I guess many folk like me will buy this LCD PCA to get up the learning curve quickly only to stumble badly with the supplied library.

Ta
David

António Oliveira said...

Nice, thanks!

But what about the keyboard support?

tng said...

Antonio,

Sorry about the lack of keyboard (cursor key) support. It wasn't something that I was interested in. I just wanted a cheap LCD shield working with the standard libraries and this seemed like the quickest way to do it. I guess you have two choices - hack the keyboard parts into your own header file or work with the supplied DRobotics library.


If you have any success, please let me know. I'm happy to modify my code as and when people tell me I should!