connecting a 16x2 LCD to display 3(or more) 18B20

Hi, bit of a n00b, hopefully not for too long.

basically ive taken the Dallas 18B20 code, slightly modified it and got it to read 3 sensors, will be more later. but what i want to do is output the readings from the sensors to a LCD, but ive been trying all sorts of things with no prevail.

here is my code (without and LCD ouput failings) and works perfectly

#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 10 //digital pin 10
#define TEMPERATURE_PRECISION 12 //12byte precision so i get more than just XX.50 Celcius etc

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

// arrays to hold device addresses
DeviceAddress Tture1, Tture2, Tture3;

void setup(void)
{
  // start serial port
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");

  // Start up the library
  sensors.begin();

  // locate devices on the bus
  Serial.print("Locating devices...");
  Serial.print("Found ");
  Serial.print(sensors.getDeviceCount(), DEC);
  Serial.println(" devices.");

  // report parasite power requirements
  Serial.print("Parasite power is: "); 
  if (sensors.isParasitePowerMode()) Serial.println("ON");
  else Serial.println("OFF");


  if (!sensors.getAddress(Tture1, 0)) Serial.println("Unable to find address for Device 0"); 
  if (!sensors.getAddress(Tture2, 1)) Serial.println("Unable to find address for Device 1"); 
  if (!sensors.getAddress(Tture3, 2)) Serial.println("Unable to find address for Device 2"); 

  // show the addresses we found on the bus
  Serial.print("Device 0 Address: ");
  printAddress(Tture1);
  Serial.println();

  Serial.print("Device 1 Address: ");
  printAddress(Tture2);
  Serial.println();
  
  Serial.print("Device 2 Address: ");
  printAddress(Tture3);
  Serial.println();

  sensors.setResolution(Tture1, 12);
  sensors.setResolution(Tture2, 12);
  sensors.setResolution(Tture3, 12);

  Serial.print("Device 0 Resolution: ");
  Serial.print(sensors.getResolution(Tture1), DEC); 
  Serial.println();

  Serial.print("Device 1 Resolution: ");
  Serial.print(sensors.getResolution(Tture2), DEC); 
  Serial.println();
  
  Serial.print("Device 2 Resolution: ");
  Serial.print(sensors.getResolution(Tture3), DEC); 
  Serial.println();
}

// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
  for (uint8_t i = 0; i < 8; i++)
  {
    // zero pad the address if necessary
    if (deviceAddress[i] < 8) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
  }
}

// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  Serial.print("Temp C: ");
  Serial.print(tempC);
  Serial.print(" Temp F: ");
  Serial.print(DallasTemperature::toFahrenheit(tempC));
}

// function to print a device's resolution
void printResolution(DeviceAddress deviceAddress)
{
  Serial.print("Resolution: ");
  Serial.print(sensors.getResolution(deviceAddress));
  Serial.println();    
}

// main function to print information about a device
void printData(DeviceAddress deviceAddress)
{
  Serial.print("Device Address: ");
  printAddress(deviceAddress);
  Serial.print(" ");
  printTemperature(deviceAddress);
  Serial.println();
}

void loop(void)
{ 
  // call sensors.requestTemperatures() to issue a global temperature 
  // request to all devices on the bus
  Serial.print("Requesting Temperatures...");
  sensors.requestTemperatures();
  Serial.println("DONE");

  // print the device information
  printData(Tture1);
  printData(Tture2);
  printData(Tture3);
}

my only question is how would i output it to the LCD?

Cheers

What LCD do you have and how is it connected to the Arduino?

just a regular 16 pin interface LCD, with the data being sent over pins

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

Ok, you're using the standard LiquidCrystal library. You can then use the functions as described here to write to teh LCD:

Or did I misunderstand your question?

... (without and LCD ouput failings) ...

We can help you more if you supply us with the code that didn't work as you expected and with a description or photograph of what result you did get.

Have you been able to get the Hello, world example to work with your LCD?

Don

One more thing... The lcd library only support char or char[] in the write function. So you need to convert any numericals into text first.

possibly, but i have modified it more slightly so that it will display the temp on the LCD, however it opnly displays it on one line and doesn't seem to want to play nice.

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);



// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  Serial.print("Temp C: ");
  Serial.print(tempC);

 float TempC = sensors.getTempC(deviceAddress);
 lcd.print("T:");
 lcd.print(tempC);

thats what i have added so that it prints to the serial port and the LCD

but when it prints it has T:24.51T:24.5T:2
this is becoming a neusence slightly, what i want it to display (more or less) is something like:

CPU Temp: 45.51C
CASE Tmp: 25.32C

with a slight delay then clears the screen and will then print

INTERNAL: 32.65C
EXTERNAL: 28.54C

if that makes sense

you need to do a lcd.setCursor(0,0) before each run, otherwise it will just carry on printing after what has been printed before.

that semi works, but now it only shows

T:25.25

which is the nimmediately overwriten by

T:25.37

etc and doesn't move in the slightest.

{
  float tempC = sensors.getTempC(deviceAddress);
  Serial.print("Temp C: ");
  Serial.print(tempC);

lcd.setCursor(0,0);

 float temp = sensors.getTempC(deviceAddress);
 lcd.print("T:");
 lcd.print(temp);

You use different values in a second lcd.setCursor line before you print the next value.

yeah, do a lcd.setCursor(0,1) before writing to the 2nd line. You need to tell the display where you want to write something... :wink:

i see exactly what ive done wrong, ive just re-defined each display value and told it to calculate it seperately (take longer and possible much less efficient, but its working.

heres what ive got if youd like to have a look:

#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 10 //digital pin 10
#define TEMPERATURE_PRECISION 12 //12byte precision so i get more than just XX.50 Celcius etc

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

// arrays to hold device addresses
DeviceAddress Tture1, Tture2, Tture3;

void setup(void)
{
  // start serial port
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");

  // Start up the library
  sensors.begin();

  // locate devices on the bus
  Serial.print("Locating devices...");
  Serial.print("Found ");
  Serial.print(sensors.getDeviceCount(), DEC);
  Serial.println(" devices.");

  // report parasite power requirements
  Serial.print("Parasite power is: "); 
  if (sensors.isParasitePowerMode()) Serial.println("ON");
  else Serial.println("OFF");


  if (!sensors.getAddress(Tture1, 0)) Serial.println("Unable to find address for Device 0"); 
  if (!sensors.getAddress(Tture2, 1)) Serial.println("Unable to find address for Device 1"); 
  if (!sensors.getAddress(Tture3, 2)) Serial.println("Unable to find address for Device 2"); 

  // show the addresses we found on the bus
  Serial.print("Device 0 Address: ");
  printAddress(Tture1);
  Serial.println();

  Serial.print("Device 1 Address: ");
  printAddress(Tture2);
  Serial.println();

  Serial.print("Device 2 Address: ");
  printAddress(Tture3);
  Serial.println();

  sensors.setResolution(Tture1, 12);
  sensors.setResolution(Tture2, 12);
  sensors.setResolution(Tture3, 12);

  Serial.print("Device 0 Resolution: ");
  Serial.print(sensors.getResolution(Tture1), DEC); 
  Serial.println();

  Serial.print("Device 1 Resolution: ");
  Serial.print(sensors.getResolution(Tture2), DEC); 
  Serial.println();

  Serial.print("Device 2 Resolution: ");
  Serial.print(sensors.getResolution(Tture3), DEC); 
  Serial.println();
}

// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
  for (uint8_t i = 0; i < 8; i++)
  {
    // zero pad the address if necessary
    if (deviceAddress[i] < 8) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
  }
}

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


/*
device 0 = 286A98BA0200005E
device 1 = 28AEA2BA02000024
device 2 = 288195BA02000063
*/
// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress); //the LOT
  Serial.print("Temp C: ");
  Serial.print(tempC);

delay(750);

 lcd.begin(16, 2);
 
  lcd.print("CPU Temp: ");
   float temp1 = sensors.getTempC(Tture1);  //single reading for single set :D
   lcd.print(temp1);
   lcd.print("C");

//new line

 lcd.setCursor(0,1);
  
  lcd.print("CASE Tmp: ");
   float temp2 = sensors.getTempC(Tture2);
   lcd.print(temp2);
   lcd.print("C");

delay(1000);

 lcd.clear();
  
 
  lcd.begin(16, 2);
  
   lcd.print("INTERNAL: ");
   float temp3 = sensors.getTempC(Tture3);
   lcd.print(temp3);
   lcd.print("C");
   
   delay(1000);
   

}
// function to print a device's resolution
void printResolution(DeviceAddress deviceAddress)
{
  Serial.print("Resolution: ");
  Serial.print(sensors.getResolution(deviceAddress));
  Serial.println();    
}


// main function to print information about a device
void printData(DeviceAddress deviceAddress)
{
  Serial.print("Device Address: ");
  printAddress(deviceAddress);
  Serial.print(" ");
  printTemperature(deviceAddress);
  Serial.println();
}



void loop(void)
{ 
  // call sensors.requestTemperatures() to issue a global temperature 
  // request to all devices on the bus
  Serial.print("Requesting Temperatures...");
  sensors.requestTemperatures();
  Serial.println("DONE");

  // print the device information
  printData(Tture1);
  printData(Tture2);
  printData(Tture3);

}

its on-going but im getting there now

Take out the second 'lcd.begin(16,2)'. You should only use lcd.begin() once as it defines how to initialize the LCD controller and the controller should only be initialized once.

Don

right, cheers, but its still quite slow at displaying the temps, i have them serial output aswell as then putting those reading on the LCD, but they only get (in series) the temps one after the otherafter the total delay has been met, any ideas as to why?

Have you changed your code since? If yes, please post the new code.

Based on the code provided, there is a few slow-downs.

First, as floresta said, don't keep calling lcd.begin(). This should be called only once in setup(). Do not call it in loop() or any function called from loop(). My guess is that you are calling it to get the cursor to the top left. Use lcd.setCursor(0,0) instead.

lcd.clear() is very slow. I found it far quicker to just overwrite what's on the display. You may have to pad the numbers with spaces though...

Your function printTemperature() only returns after it has printed temperatures to the serial port, waited 750ms, displayed CPU and case temperatures, waited 1 second, displayed internal temperature and waited another 1 second. The Arduino will not do anything else during that time. This is not a multitasking environment.

If you want the display to alternate between showing different temperatures, you need to do that differently. Show the first temperatures, exit the function, do something else for a while, call the function again and have it display the other temperatures.

To avoid that I have just squeezed more info on the LCD and avoid the need to alternate. On a 16x2 LCD you usually can't afford to print "INTERNAL" That word alone takes up a quarter of your space on the LCD.

Or you can get a different display all together. I have a 20x4 now and they are really really cheap. I think the LCD library supports up to 4 rows and can easily be extended.

Just to give you an idea of how cheap these displays are: :wink:

http://cgi.ebay.co.uk/20X4-Character-LCD-Module-Display-LCM-White-Backlight-/190345879489?pt=UK_BOI_Electrical_Components_Supplies_ET&hash=item2c517f9fc1