How to calculate the value from DS18B20 on LCD?

Hi
I have 2 temp sensors DS18B20 and a LCD. It works but i want to do some calculation of the temperature value from “Probe01”.

I am new to this but i can’t figure out how to do. I want to: Probe01/22-1 and then print the result on the LCD.

This is not working:
lcd.print ((Probe01/2)-1);

Best regards
Ole

/* YourDuino Multiple DS18B20 Temperature Sensors on 1 wire
Questions: terry@yourduino.com
V1.01 01/17/2013 …based on examples from Rik Kretzinger

/-----( Import needed libraries )-----/
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
/-----( Declare Constants and Pin Numbers )-----/
#define ONE_WIRE_BUS_PIN A0

LiquidCrystal lcd(11, 10, 9, 8, 7, 6);

/-----( Declare objects )-----/
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS_PIN);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

/-----( Declare Variables )-----/
DeviceAddress Probe01 = { 0x28, 0x63, 0xBC, 0x5B, 0x02, 0x00, 0x00, 0xE9 };
DeviceAddress Probe02 = { 0x28, 0xD7, 0xB6, 0x5B, 0x02, 0x00, 0x00, 0x8F };

void setup() /****** SETUP: RUNS ONCE ******/
{
// Set Analog A0 as digital in
pinMode(A0, INPUT);
// start serial port to show results
Serial.begin(9600);
// set up the LCD’s number of columns and rows:
lcd.begin(20, 4);

// Initialize the Temperature measurement library
sensors.begin();
// initialize the library with the numbers of the interface pins LiquidCrystal lcd(11, 10, 9, 8, 7, 6);

// set the resolution to 10 bit (Can be 9 to 12 bits … lower is faster)
sensors.setResolution(Probe01, 10);
sensors.setResolution(Probe02, 10);

}//–(end setup )—

void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
delay(1000);

// Command all devices on bus to read temperature
sensors.requestTemperatures();
//lcd.clear(); // Reset the display
//lcd.home();
lcd.setCursor(0,0); //Start at character 0 on line 0
lcd.print(“Inne C”);
lcd.setCursor(5,0);
printTemperature(Probe01);
lcd.setCursor(0,1);
lcd.print(“Ute C”);
lcd.setCursor(5,1);
printTemperature(Probe02);
lcd.setCursor(0,2);
lcd.print ((Probe01/2)-1);

}//–(end main loop )—

/-----( Declare User-written Functions )-----/
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00)
{
lcd.print("Error getting temperature ");
}
else
{
lcd.print(tempC);

}

}

//( THE END )**

This is not working:
lcd.print ((Probe01/2)-1);

Why would you want to divide the address of the device by two and then subtract 1?

Doing something with tempC would make sense.

Thanks PaulS
That is what i want, I want to do something with the two values on the one wire to use in an if statement. But i can't fint the variable. I have tried:

lcd.print(tempC);

But get.

"tempC" was not declared in this scope

Best regards
Ole

depending on the lcd you use there might be a chance it does not support printing of floats.

try lcd.print( int(tempC) );

if that works you need to manually print the decimal and fractional part

BTW Please use code tags

Thank you robtillaart
Great, that works. But it only print the result of the last temp sensor (Probe02). How can i get the result from (Probe01)?

Best regards
Ole

/* YourDuino Multiple DS18B20 Temperature Sensors on 1 wire
Questions: terry@yourduino.com
V1.01 01/17/2013 …based on examples from Rik Kretzinger

/-----( Import needed libraries )-----/
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
/-----( Declare Constants and Pin Numbers )-----/
#define ONE_WIRE_BUS_PIN A0

LiquidCrystal lcd(11, 10, 9, 8, 7, 6);

float tempC =0;

/-----( Declare objects )-----/
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS_PIN);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

/-----( Declare Variables )-----/
DeviceAddress Probe01 = { 0x28, 0x63, 0xBC, 0x5B, 0x02, 0x00, 0x00, 0xE9 };
DeviceAddress Probe02 = { 0x28, 0xD7, 0xB6, 0x5B, 0x02, 0x00, 0x00, 0x8F };

void setup() /****** SETUP: RUNS ONCE ******/
{
// Set Analog A0 as digital in
pinMode(A0, INPUT);
// start serial port to show results
Serial.begin(9600);
// set up the LCD’s number of columns and rows:
lcd.begin(20, 4);

// Initialize the Temperature measurement library
sensors.begin();
// initialize the library with the numbers of the interface pins LiquidCrystal lcd(11, 10, 9, 8, 7, 6);

// set the resolution to 10 bit (Can be 9 to 12 bits … lower is faster)
sensors.setResolution(Probe01, 10);
sensors.setResolution(Probe02, 10);

}//–(end setup )—

void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
delay(1000);

// Command all devices on bus to read temperature
sensors.requestTemperatures();
//lcd.clear(); // Reset the display
//lcd.home();
lcd.setCursor(0,0); //Start at character 0 on line 0
lcd.print(“Inne C”);
lcd.setCursor(5,0);
printTemperature(Probe01);
lcd.setCursor(0,1);
lcd.print(“Ute C”);
lcd.setCursor(5,1);
printTemperature(Probe02);
lcd.setCursor(0,2);
lcd.print( int(tempC) );

}//–(end main loop )—

/-----( Declare User-written Functions )-----/
void printTemperature(DeviceAddress deviceAddress)
{
tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00)
{
lcd.print("Error getting temperature ");
}
else
{
lcd.print(tempC);

}

}

//( THE END )**

10 Please use code tags,...
20 GOTO 10

you must use the same commands for both probes, but use separate variables for them...

you need only to add
lcd.print( int(tempC) );

in the right spot to get something working (not clean code but it would work)

You need to highlight the printTemperature() function, AND DELETE IT!

Create a function that RETURNS the temperature, so you can HAVE two different temperatures.

Hi thronborg,
I too have used Pic and Picaxe (a Pic with a bootloader like Arduino) Still struggling with C and I can give you about 10 years.

Here’s my code for my Seed propagator that reads a DS18b20 and sends it to an LCD, with two or more DS18b20’s you need to give them a different varible/name? You will need to install the Dallas Library, etc.

My Code:

#include <FastIO.h>
#include <I2CIO.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
 
// DS18b20 Data line is plugged into pin 12 on the Arduino
#define ONE_WIRE_BUS 12
 
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
 
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire); 
/*
Connections:
Heater1 pin 3
Heater2 pin 4
SDA to Arduino Analog pin 4
SCL to Arduino Analog pin 5
LightPin Analog pin A7
TempPin pin 12
*/

#define DS1307_I2C_ADDRESS 0x68
LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7,3, POSITIVE);  // Set the LCD I2C address 

//*****constants*****
int Light,LightPin=A7,TargetTemp,Heater1=3,Heater2=4,TempPin=12;
int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract;
float TEMP;
void setup()
//----------------Start of Set up---------------------
{
TargetTemp=23;  //Set Normal/Target temp to 23c
pinMode(Heater1,OUTPUT);
pinMode(Heater2,OUTPUT);
pinMode(13,OUTPUT);
Wire.begin();
lcd.begin(20,4); // tells Arduino the LCD dimensions
// start serial port
Serial.begin(9600);
//Serial.println("Dallas Temperature IC Control Library Demo"); 
// Start up the library
sensors.begin(); // IC Default 9 bit. If you have troubles consider upping it 12. Ups the delay giving the IC more time to process the temperature measurement
}
//------------End of Setup loop------------------------
//--------------Start of Main loop------------------
void loop()  
{
  GetTime();
  GetTemp();
  TestIt();
  digitalWrite(13,HIGH);    //Flash LED
  delay(100);               //For 100mS
  digitalWrite(13,LOW);     //LED off
}
//------------End of main loop-----------------------------
void GetTime()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
//if (second <=5)
{
//lcd.clear(); // clear LCD screen
lcd.setCursor(0,0);
lcd.print(" ");
lcd.print(hour, DEC);
lcd.print(":");
if (minute<10)
{
lcd.print("0");
}
lcd.print(minute, DEC);
lcd.print(":");
if (second<10)
{
lcd.print("0");
}
lcd.print(second, DEC);
lcd.print(" ");
lcd.print(dayOfMonth, DEC);
lcd.print("/");
lcd.print(month, DEC);
lcd.print("/");
lcd.print("20");
lcd.print(year, DEC);
//delay(1000);
}
}
//------------------------------------------------------------------------------------------
void GetTemp()
{
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
sensors.requestTemperatures(); // Send the command to get temperatures
TEMP=(sensors.getTempCByIndex(0)); // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
}
//-----------------------------------------------------------------------------------------------------------
void TestIt()
 {
  Light=analogRead(LightPin);
  Light=Light/4;
  if(Light>200)
  {TargetTemp=25;}
  if(Light>160 && Light<200)
  {TargetTemp=18;}
  if(Light<160)
  {TargetTemp=14;}
// Set Temp******
 
   lcd.setCursor(1,1);
   lcd.print("Target Temp:");
   lcd.print(TargetTemp);
   lcd.print(".00c ");
   lcd.setCursor(1,2);
   lcd.print("Current Temp:");
   lcd.print(TEMP); 
   lcd.print("c ");
   
   if (TEMP < TargetTemp)
  {
   digitalWrite(Heater1,LOW);    //Heater ON
   digitalWrite(Heater2,LOW);
   lcd.setCursor(1,3);
   lcd.print("Heater ON ");
   lcd.print("Lite=");
   lcd.print(Light);
   lcd.print("  ");
  }
  if (TEMP > TargetTemp)
  {
    digitalWrite(Heater1,HIGH);
   digitalWrite(Heater2,HIGH);  //Heater OFF
   lcd.setCursor(1,3);
   lcd.print("Heater OFF ");
   lcd.print("Lite=");
   lcd.print(Light);
   lcd.print("  ");
 //lcd.clear();
}
}
//*************************************************************************************************************

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return ( (val/10*16) + (val%10) );
}
//-----------------------------------------------------------------
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}
//------------------------------------------------------------------
// Gets the date and time from the ds1307
void getDateDs1307
(
byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
// A few of these need masks because certain bits are control bits
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm
*dayOfWeek = bcdToDec(Wire.read() &0x07);
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
//==================================================================================

Hope it helps.
Regards

Mel.

I have patched some of my working code into your sketch. I haven’t tested it, because I don’t have the hardware set up to do it at this time, but it should work.

Some changes I made:

Declare the probe address as byte arrays instead of the library class function. I have had problems in the past passing reference to the different sensors when declaring them as type DeviceAddress.

Add float variables and sensors.getTemp in loop. Removed your function.

/*-----( Import needed libraries )-----*/
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>

/*-----( Declare Constants and Pin Numbers )-----*/
#define ONE_WIRE_BUS_PIN A0

LiquidCrystal lcd(11, 10, 9, 8, 7, 6);

/*-----( Declare objects )-----*/
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS_PIN);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

/*-----( Declare Variables )-----*/
byte Probe01[] = { 
  0x28, 0x63, 0xBC, 0x5B, 0x02, 0x00, 0x00, 0xE9 }; 
byte Probe02[] = { 
  0x28, 0xD7, 0xB6, 0x5B, 0x02, 0x00, 0x00, 0x8F };

void setup()   /****** SETUP: RUNS ONCE ******/
{
  // Set Analog A0 as digital in
  pinMode(A0, INPUT);
  // start serial port to show results
  Serial.begin(9600);
  // set up the LCD's number of columns and rows: 
  lcd.begin(20, 4);


  // Initialize the Temperature measurement library
  sensors.begin();
  // initialize the library with the numbers of the interface pins LiquidCrystal lcd(11, 10, 9, 8, 7, 6);

  // set the resolution to 10 bit (Can be 9 to 12 bits .. lower is faster)
  sensors.setResolution(Probe01, 10);
  sensors.setResolution(Probe02, 10);

}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  delay(2000);

  // Command all devices on bus to read temperature  
  sensors.requestTemperatures();
  
  float temps_degreesC[2];
    temps_degreesC[0] = sensors.getTempC(Probe01);
    temps_degreesC[1] = sensors.getTempC(Probe02);
    
  //lcd.clear();  // Reset the display  
  //lcd.home();
  lcd.setCursor(0,0); //Start at character 0 on line 0
  lcd.print("Inne      C");
  lcd.setCursor(5,0);
  lcd.print(temps_degreesC[0]);
  //printTemperature(Probe01);    
  lcd.setCursor(0,1);
  lcd.print("Ute       C");
  lcd.setCursor(5,1);
  lcd.print(temps_degreesC[1]);
  //printTemperature(Probe02);
  //lcd.setCursor(0,2);
  //lcd.print ((Probe01/2)-1);

}

Thank you so much everybody for your help. I think i understand a little bit more. I remake the hole code after your tips and now its working.

But I have some questions:

  1. Why use “void loop(void)” and not only “void loop()”???
  2. When Printing the tempIn and TempOut on the LCD they flicker/flash, any workaround?

Best regards
Ole

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

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS A0

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(11, 10, 9, 8, 7, 6);


// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);
float tempIn;
float tempOut;

void setup(void)
{
   lcd.begin(20, 4);
  sensors.begin();   // Start up the library
}



void loop(void)
{ 
  GetTemp(); // Get the temperatures from the 2 sensors
  lcd.setCursor(0, 0);
  lcd.print("In        C");
  lcd.setCursor(4, 0);
  lcd.print(tempIn);
  lcd.setCursor(0, 1);
  lcd.print("Out       C");
  lcd.setCursor(4, 1);
  lcd.print(tempOut);
}


void GetTemp()
{
// call sensors.requestTemperatures() to issue a global temperature request to all devices on the bus
sensors.requestTemperatures(); // Send the command to get temperatures
tempIn=(sensors.getTempCByIndex(0)); // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
tempOut=(sensors.getTempCByIndex(1));
}
  1. Doesn't matter. Most people leave out the extra void.

  2. Don't update the LCD so often - once a second should do. You could do this crudely with a delay(1000), but that will make the system unresponsive if you decide to add functionality later. In that case, look at the blink without delay example to see how to slow down your updates.

  1. Why use "void loop(void)" and not only "void loop()"

The void in the parentheses makes it clear that the function takes no arguments. I'm not a fan of it being there. The loop() declaration tells me that the function takes no arguments, while main(int argc, char **argv) tells me that the function takes two arguments.

  1. When Printing the tempIn and TempOut on the LCD they flicker/flash, any workaround?

wildbill's suggestion is good. It might be enough, though, to only print the values when they change, regardless of how often that is. If the values change often, the flickering is something you'll have to accept. If the values change infrequently, then there will be no flicker when the values are not overwritten.

in the definition int f(void) tells the function takes no arguments, same as int f() but more explicit

In calling y = f() tells that the function takes no arguments OR it uses default values for the arguments.

int f(int x = 3)
{ 
  return x * x;
}

void setup() 
{
  Serial.begin(115200);
  Serial.println("Start ");
  int y = f();
  int z = f(4);
  Serial.println(y);
  Serial.println(z);
}

void loop() 
{
}

output:
Start
9
16

Note that when overloading functions you have a resolve problem with functions that have default parameters and no parameters.
int f(int x = 3)...
int f(void) ...
y = f(); // can't be resolved. it cannot be forced to use the f(void) implementation