Displaying temp on an lcd from serial issue

Hi every one. Just started with arduino and Im a bit stuck.

I have used code from the net and changed it about to suit my needs but I have hit a stumbling block.

I can read the temperature on the serial but it will not send to the lcd screen and display, every thing else is fine.

Can some one please point me in the right direction.

Any comments much appreciated.

/*
MultiTempSensor
*/
//Include’s
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h> // Library for LCD
#include <Wire.h> // Library for I2C communication

//Constants
#define SENSOR_PIN 5
#define READ_TIMER 10000 //10 seconds
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4); // Change to (0x27,16,2) for 16x2 LCD.
//

OneWire oneWire(SENSOR_PIN);

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

//Sensor Adresses
DeviceAddress Sensor1 = { 0x28, 0x31, 0xAF, 0x78, 0x32, 0x19, 0x01, 0xBD };
//DeviceAddress Sensor2 = { 0x28, 0xFF, 0xE2, 0x0F, 0x61, 0x16, 0x03, 0x21 };
//DeviceAddress Sensor3 = { 0x28, 0xFF, 0x38, 0xB3, 0x60, 0x16, 0x03, 0xEA };
//DeviceAddress Sensor4 = { 0x28, 0xFF, 0xA4, 0xBE, 0x60, 0x16, 0x03, 0xE1 };
//DeviceAddress Sensor5 = { 0x28, 0xFF, 0xFE, 0x13, 0x61, 0x16, 0x03, 0x56 };
//DeviceAddress Sensor6 = { 0x28, 0xFF, 0x89, 0xBA, 0x60, 0x16, 0x03, 0xFB };
//DeviceAddress Sensor7 = { 0x28, 0xFF, 0xFA, 0xBC, 0x60, 0x16, 0x03, 0xD1 };
//DeviceAddress Sensor8 = { 0x28, 0xFF, 0x57, 0xB1, 0x60, 0x16, 0x03, 0x93 };

int amountOfSensors = 1;
const int oilswitch = 2; // the number of the oilswitch input pin

//variable to check amount of sensors on bus once in a while
int counterCheckBus = 0;
int oilState = 0; // variable for reading the oil switch status

void setup()
{
pinMode(oilswitch, INPUT);

// Initiate the LCD:
lcd.init();
lcd.backlight();
Serial.begin(9600);
sensors.begin();

// Startup screen, will show on power up
lcd.setCursor(0,1);
lcd.print(“START UP PLEASE WAIT”);
delay(3000);
lcd.setCursor(0,1);
lcd.print(" “);
delay(1000);
lcd.setCursor(0,1);
lcd.print(” SYSTEM SELF TEST “);
delay(1000);
lcd.setCursor(0,1);
lcd.print(” “);
delay(1000);
lcd.setCursor(0,1);
lcd.print(” SYSTEM SELF TEST “);
delay(1000);
lcd.setCursor(0,1);
lcd.print(” ");
delay(3000);

// start serial port to show results
Serial.begin(9600);
//display_Running_Sketch();
//printProgramName();
lcd.setCursor(1,0);
lcd.print(“Init Temp Cont Lib”);
lcd.setCursor(4,2);
lcd.print(“V14.06.2020”);
delay(3000);
lcd.clear();
delay(1000);

lcd.setCursor(1,1);
lcd.print("Searching for ");
lcd.setCursor(18,1);
lcd.print(amountOfSensors);
lcd.setCursor(2,3);
lcd.println(“sensors…”);
delay(5000);
lcd.clear();
delay(1000);

// Initialize the Temperature measurement library
sensors.begin();

// set the resolution to 10 bit (Can be 9 to 12 bits … lower is faster)
sensors.setResolution(Sensor1, 10);
//sensors.setResolution(Sensor2, 10);
//sensors.setResolution(Sensor3, 10);
//sensors.setResolution(Sensor4, 10);
//sensors.setResolution(Sensor5, 10);
//sensors.setResolution(Sensor6, 10);
//sensors.setResolution(Sensor7, 10);
//sensors.setResolution(Sensor8, 10);

getSensorsOnBus();
Serial.println(“System initialized”);
}

void loop()
{
//Oil pressure status, input from pin 2
oilState = digitalRead(oilswitch);

if (oilState == HIGH) {
lcd.setCursor(11, 0);
lcd.print(“O/P FAULT”);

} else {

lcd.setCursor(11, 0);
lcd.print("O/P OK ");
}

counterCheckBus++;
delay(READ_TIMER);
Serial.println();

if (counterCheckBus == 360)
{
getSensorsOnBus();
counterCheckBus = 0;
}

sensors.requestTemperatures();

Serial.print("Sensor 1: ");
printTemperature(Sensor1);
Serial.println();

// Print ‘Input text’ on the first line of the LCD:
lcd.setCursor(0, 0); // Set the cursor on the first column and first row.
lcd.print("O/T "); // print the oil temp
lcd.setCursor(5,0);
//lcd.write(Serial.read)(Sensor1);
lcd.setCursor(10, 0); //Set the cursor on the third column and the second row (counting starts at 0!).
lcd.print(“c”);

//Serial.print("Sensor 2: ");
//printTemperature(Sensor2);
//Serial.println();

//Serial.print("Sensor 3: ");
//printTemperature(Sensor3);
//Serial.println();

//Serial.print("Sensor 4: ");
//printTemperature(Sensor4);
//Serial.println();

//Serial.print("Sensor 5: ");
//printTemperature(Sensor5);
//Serial.println();

//Serial.print("Sensor 6: ");
//printTemperature(Sensor6);
//Serial.println();

//Serial.print("Sensor 7: ");
//printTemperature(Sensor7);
//Serial.println();

//Serial.print("Sensor 8: ");
//printTemperature(Sensor8);
//Serial.println();

}

void printTemperature(DeviceAddress deviceAddress)
{

float tempC = sensors.getTempC(deviceAddress);

if (tempC == -127.00)
{
Serial.print(“Sensor error!”);
}
else
{
Serial.print("C: ");
Serial.print(tempC);
}
}

void printProgramName() {
String path = FILE;
int slash = path.lastIndexOf(’\’);
String programName = path.substring(slash + 1);
int dot = programName.lastIndexOf(’.’);
programName = programName.substring(0, dot);

Serial.print("\nProgram version: ");
Serial.println(programName);
}

void getSensorsOnBus() {
Serial.print("Number of sensors found on bus: ");
Serial.println(sensors.getDeviceCount());
}

// Print 'Input text' on the first line of the LCD:
lcd.setCursor(0, 0); // Set the cursor on the first column and first row.
lcd.print("O/T "); // print the oil temp
lcd.setCursor(5,0);
lcd.print(Sensor1);
lcd.setCursor(10, 0); //Set the cursor on the third column and the second row (counting starts at 0!).
lcd.print("c");

This doesn't work either.

Do you see the PLEASE WAIT message from setup() ?
Do you see the text labels when printing to the LCD ?

Does the LCD library come with any examples ? Do they work as expected ?

Hi, the lcd works fine, i can print text to it and have had it reading temperature in another example with just one sensor.

I believe that its because it is the way its handling the data the screen won't display.

I need more than one sensor and the code i got from the net does this but only prints the data to serial. I assumed that I could tell it to print that data to the screen but I seems i was wrong.

Im only on chapter 3 of arduino for dummies so have a long way to go.

Many thanks, Steve

Your "printTemperature()" function fetches the temperature from the sensor and prints it to Serial. You need to add code to fetch the temperature and print it to the LCD at the right time and in the right place. You could add the printing to the LCD in "printTemperature()", where the data is already fetched, or write a separate function to fetch and print to the LCD.

Hi, thanks for your reply.

As I made this from examples, im not sure where the data comes from within the function. I just included the dallas temp and 1 wire from the library.

Like I say, im very new to all this so my understanding is limited at the moment.

If i try and fetch the data and print it it just says no matching function for call to 'print(device address)

Steve

sasdiscos:
Hi, thanks for your reply.

As I made this from examples, im not sure where the data comes from within the function. I just included the dallas temp and 1 wire from the library.

Like I say, im very new to all this so my understanding is limited at the moment.

If i try and fetch the data and print it it just says no matching function for call to 'print(device address)

Steve

This line "fetches" the temperature:

 float tempC = sensors.getTempC(deviceAddress);

So if you print tempC to the display after that, mirroring the print to Serial, it should show up there.

Many thanks.

Do you mean like this?

sensors.requestTemperatures();
float tempC = sensors.getTempC(Sensor1);

lcd.setCursor(2,0);
lcd.write(Sensor1);

Im just playing about with it now.

Steve

float tempC = sensors.getTempC(deviceAddress);

if (tempC == -127.00)
{
Serial.print("Sensor error!");
}
else
{
Serial.print("C: ");
Serial.print(tempC);
lcd.setCursor(2,0);
lcd.print(tempC);
}

Put this in and it will read the one temperature, i assume that with more sensors it will roll each one. I wanted to display them separately on screen.

What you suggested works so many thanks but just needs a bit of fettling.

Steve

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

Hi, sorry.

Many thanks for all your help.

I can now read the temp 1. But no others. I can’t work out how to display more than one. It just reads the first one and thats it.

Many Thanks, Steve

/*
MultiTempSensor 
*/
//Include's
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h> // Library for LCD
#include <Wire.h> // Library for I2C communication

//Constants
#define SENSOR_PIN 5
#define READ_TIMER 10000  //10 seconds
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4); // Change to (0x27,16,2) for 16x2 LCD.
//

OneWire oneWire(SENSOR_PIN);

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



//Sensor Adresses 
DeviceAddress Sensor1 = { 0x28, 0x31, 0xAF, 0x78, 0x32, 0x19, 0x01, 0xBD };
DeviceAddress Sensor2 = { 0x28, 0xF9, 0xD8, 0x76, 0x32, 0x19, 0x01, 0xAA };
//DeviceAddress Sensor3 = { 0x28, 0xFF, 0x38, 0xB3, 0x60, 0x16, 0x03, 0xEA };
//DeviceAddress Sensor4 = { 0x28, 0xFF, 0xA4, 0xBE, 0x60, 0x16, 0x03, 0xE1 };
//DeviceAddress Sensor5 = { 0x28, 0xFF, 0xFE, 0x13, 0x61, 0x16, 0x03, 0x56 };
//DeviceAddress Sensor6 = { 0x28, 0xFF, 0x89, 0xBA, 0x60, 0x16, 0x03, 0xFB };
//DeviceAddress Sensor7 = { 0x28, 0xFF, 0xFA, 0xBC, 0x60, 0x16, 0x03, 0xD1 };
//DeviceAddress Sensor8 = { 0x28, 0xFF, 0x57, 0xB1, 0x60, 0x16, 0x03, 0x93 };

int amountOfSensors = 2;
const int oilswitch = 12;     // the number of the oilswitch input pin
const int oilfault = 13;       // the number of the output pin to the relay

//variable to check amount of sensors on bus once in a while
int counterCheckBus = 0;
int oilState = 0;         // variable for reading the oil switch status

void setup()
{
  pinMode(oilswitch, INPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  
  // Initiate the LCD:
  lcd.init();
  lcd.backlight();
  Serial.begin(9600);
  sensors.begin();

  byte customChar[] = {  //Custom char fuel droplet
  B00100,
  B00100,
  B01110,
  B11111,
  B11111,
  B11111,
  B11111,
  B01110
  };
  byte customChar1[] = {  //custom char oil temp
  B00110,
  B00100,
  B00110,
  B00100,
  B00100,
  B01110,
  B11111,
  B01110
  };
  byte customChar2[] = {  //custom char deg c
  B10000,
  B00011,
  B00100,
  B01000,
  B01000,
  B01000,
  B00100,
  B00011
  };
  byte customChar3[] = { //custon char snowflake
  B00100,
  B10001,
  B01010,
  B10101,
  B10101,
  B01010,
  B10001,
  B00100
  };

  lcd.createChar(0, customChar);
  lcd.createChar(1, customChar1);
  lcd.createChar(2, customChar2);
  lcd.createChar(3, customChar2);
  
  
  // Startup screen, will show on power up  
  lcd.setCursor(0,1);
  lcd.print("START UP PLEASE WAIT");
  delay(3000);
  lcd.setCursor(0,1);
  lcd.print("                    ");
  delay(1000);
  lcd.setCursor(0,1);
  lcd.print("  SYSTEM SELF TEST  ");
  delay(1000);
  lcd.setCursor(0,1);
  lcd.print("                    ");
  delay(1000);
  lcd.setCursor(0,1);
  lcd.print("  SYSTEM SELF TEST  ");
  delay(1000);
  lcd.setCursor(0,1);
  lcd.print("                    ");
  delay(3000);
  
  // start serial port to show results
  Serial.begin(9600);
  //display_Running_Sketch();
  //printProgramName();
  lcd.setCursor(1,0);
  lcd.print("Init Temp Cont Lib");
  lcd.setCursor(4,2);
  lcd.print("V14.06.2020");
  delay(3000);
  lcd.clear();
  delay(1000);

  lcd.setCursor(1,1);
  lcd.print("Searching for ");
  lcd.setCursor(18,1);
  lcd.print(amountOfSensors);
  lcd.setCursor(2,3);
  lcd.println("sensors...");
  delay(5000);
  lcd.clear();
  delay(1000);

  // Initialize the Temperature measurement library
  sensors.begin();

  // set the resolution to 10 bit (Can be 9 to 12 bits .. lower is faster)
  sensors.setResolution(Sensor1, 10);
  sensors.setResolution(Sensor2, 10);
  //sensors.setResolution(Sensor3, 10);
  //sensors.setResolution(Sensor4, 10);
  //sensors.setResolution(Sensor5, 10);
  //sensors.setResolution(Sensor6, 10);
  //sensors.setResolution(Sensor7, 10);
  //sensors.setResolution(Sensor8, 10);

  getSensorsOnBus();
  Serial.println("System initialized");
}

void loop()
{
  int sensorValue = analogRead(A0);
  float voltage = sensorValue *(100 / 1023.0);
  lcd.setCursor(16,0);
  lcd.print(voltage,1);
  lcd.setCursor(19,0);
  lcd.print("%  ");



  

 
 


    
  //Oil pressure status, input from pin 2  
  oilState = digitalRead(oilswitch);
  
  lcd.setCursor(14, 0);
  lcd.write(byte(0));  //fuel symbol
  lcd.setCursor(0, 0);  
  lcd.write(byte(1));  //temp symbol
  lcd.setCursor(8, 0); 
  lcd.write(byte(2) ); //oc symbol
  
  if (oilState == HIGH) {
  //lcd.setCursor(17, 0);
  //lcd.print("LOW");
  lcd.setCursor(2,3);
  lcd.print("LOW OIL PRESSURE");
  //delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  } else { 
    
  //lcd.setCursor(17, 0);
  //lcd.print("OK ");
  lcd.setCursor(2,3);
  lcd.print("                ");
  digitalWrite(LED_BUILTIN, HIGH);
  }
  
  counterCheckBus++;
  delay(READ_TIMER);
  Serial.println();

  if (counterCheckBus == 360)
  {
    getSensorsOnBus();
    counterCheckBus = 0;
  }


  sensors.requestTemperatures();

  Serial.print("Sensor 1:   ");
  printTemperature(Sensor1);
  Serial.println();
  

  // Print 'Input text' on the first line of the LCD:
  //lcd.setCursor(0, 0); // Set the cursor on the first column and first row.
  //lcd.write(byte(1));
  //lcd.print("O/T "); // print the oil temp
  //lcd.setCursor(2,0);
  //lcd.print();
  
  //lcd.setCursor(8, 0); //Set the cursor on the third column and the second row (counting starts at 0!).
  //lcd.print("c");

  Serial.print("Sensor 2:   ");
  printTemperature(Sensor2);
  Serial.println();

  //Serial.print("Sensor 3:   ");
  //printTemperature(Sensor3);
  //Serial.println();

  //Serial.print("Sensor 4:   ");
  //printTemperature(Sensor4);
  //Serial.println();

  //Serial.print("Sensor 5:   ");
  //printTemperature(Sensor5);
  //Serial.println();

  //Serial.print("Sensor 6:   ");
  //printTemperature(Sensor6);
  //Serial.println();

  //Serial.print("Sensor 7:   ");
  //printTemperature(Sensor7);
  //Serial.println();

  //Serial.print("Sensor 8:   ");
  //printTemperature(Sensor8);
  //Serial.println();


}

void printTemperature(DeviceAddress deviceAddress)
{

  float tempC = sensors.getTempC(deviceAddress);
  
  if (tempC == -127.00)
  {
    Serial.print("Sensor error!");
  }
  else
  {
    Serial.print("C: ");
    Serial.print(tempC);
    lcd.setCursor(2,0);
    lcd.print(tempC,1);
{
  }    if (tempC <= 30.00)
    {
      lcd.setCursor(4,2);
      lcd.print("OIL TEMP LOW");
    }
      else
      lcd.setCursor(4,2);
      lcd.print("            ");
    }
}

void printProgramName() {
  String path = __FILE__;
  int slash = path.lastIndexOf('\\');
  String programName = path.substring(slash + 1);
  int dot = programName.lastIndexOf('.');
  programName = programName.substring(0, dot);

  Serial.print("\nProgram version: ");
  Serial.println(programName);
}

void getSensorsOnBus() {
  Serial.print("Number of sensors found on bus: ");
  Serial.println(sensors.getDeviceCount());
}

printTemperature prints at a fixed location on the screen...

 lcd.setCursor(2,0);

It also prints the “oil temp low” warning, which may not be appropriate for your other sensors.

Regardless, if you call that function for more than one sensor then the new value will overwrite any previously displayed value. You’ll need to modify that. A simple way would be to also pass into the function the location where the temperature is to be displayed. Or you could copy and paste the function and create a separate one for each sensor: printTemperature1, printTemperature2, .... etc

Please describe how, when displaying more than one temperature, they are to be laid out on your 4x20 LCD.
Are there specific locations where the different temperatures are to be shown, or some way to manually or automatically cycle through the temperatures (e.g. with a button press or every two seconds)? It may help to get a pen and paper and draw a 4x20 grid and decide what is being displayed where.

Hi, thanks for the response.

I want to display 2 temps, oil temp and water temp in the end.

I am going to add a second screen, one screen will display the info and the other will display the fault condition etc.

I also have an input for the fuel level in the top right corner.

It all needs to be automatic.

Im very new to all this but im experienced in ladder logic. I thought id give this a go for my project building a kit car.

Im surprised at how cheap the kit is, and how addictive it is.

Im just struggling to pick it up and fully understand it.

Again, thanks so much for your input.

Steve

Something like this

    else
      lcd.setCursor(4, 2);
    lcd.print("            ");

This section of code will unconditionally print several spaces on the LCD thus erasing what was previously printed there. I am willing to bet that is not what you want to do.

This, however, would only print the spaces if the else clause was executed

    else
   {
      lcd.setCursor(4, 2);
      lcd.print("            ");
  }

Note the use of { and } enclosing the dependant code

Hi, Thank you.

Im sorry but I don't understand.

It will always be executed except for when there is a sensor error, and that is only on the serial.

Both temps need to display at the same time but have independent controls linked to them, for the warning of high or low oil temp and water temp at operation condition.

Maybe there is an easier way of getting 2 temp readouts not using the code i have. There is an example of one with 2 readouts but that is the same, it just prints to serial.

Steve