Question about IR Thermometer with 2 Line LCD on Uno

New to this, got everything working and the two line LCD displays "robojax MLX90614" and, below that, switches between Object and Ambient Temp. What i want the LCD to display is the Object temp in the first line and the ambient temp in the second line. Please help.

link to code below
https://robojax.com/learn/arduino/?vid=robojax_MLX90614_LCD

Post your best attempt (in code tags), and describe any problems you're having

1 Like

i dont know how to copy and paste the link please see the link in initial post and scroll down to see the code. or tell me how to correctly paste the code into the thread. i apologize again but thank you for the help

Read the forum guidelines to see how to properly post code and some good information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

#include <Wire.h>
#include <Adafruit_MLX90614.h>
char *typeName[]={"Object:","Ambient:"};


Adafruit_MLX90614 mlx = Adafruit_MLX90614();

#include <LiquidCrystal_I2C.h>
const uint8_t I2C_ADDRESS =0x27;
const uint8_t LCD_CHAR= 20;
const uint8_t LCD_ROW= 4;
char type ='C';// what to display. Watch video for details
bool ambient=1;//1=yes , 0=no
// C = Celsius
//  K = Keliven
// F = Fahrenheit

LiquidCrystal_I2C lcd(I2C_ADDRESS, LCD_CHAR,LCD_ROW);


void setup() {
  Serial.begin(9600);

  Serial.println("Robojax MLX90614 test");  
  
  
 mlx.begin();

  Wire.begin();
  lcd.begin();
  lcd.backlight();  
      lcd.print("Robojax MLX90614");
      lcd.setCursor(0,1);
      lcd.print("Infrared Temp.");          
      delay(2000);       
      clearCharacters(1,0, LCD_CHAR-1);
      
      lcd.setCursor(0,1);
      lcd.print(typeName[0]);
      lcd.setCursor(0,2);
      lcd.print(typeName[1]);             
  
}//setup() end

void loop() {
  //Robojax.com code for MLX90614 Infrared Sensor on LCD
  if(type =='C')
  {
  printTemp('C');//object temperature in C    
  }
  //Robojax Example for MLX90614 with LCD
  if(type =='F')
  {
  printTemp('F');//object temperature in F    
  }

  if(type =='K')
  {
  printTemp('K');//object temperature in K    
  }

  if( getTemp('C')>40)
  {
    //do something here
  }
  
  delay(2000);  


  //Robojax Example for MLX90614 with LCD2004
}

/*
 * @brief returns temperature or relative humidity
 * @param "type" is character
 *     C = Object Celsius
 *     D = Ambient Celsius
 *     
 *     K = Object Keliven
 *     L = Ambient in Keilven
 *     
 *     F = Object Fahrenheit
 *     G = Ambient in Fahrenheit

 * @return returns one of the values above
 * Usage: to get Fahrenheit type: getTemp('F')
 * to print it on serial monitor Serial.println(getTemp('F'));
 * Written by Ahmad Shamshiri on Mar 30, 2020. 
 * in Ajax, Ontario, Canada
 * www.Robojax.com 
 */
float getTemp(char type)
{
     //Robojax.com code for MLX90614 Infrared Sensor on LCD
  float value;
    float tempObjec = mlx.readObjectTempC();//in C object
    float tempAmbient = mlx.readAmbientTempC();
   if(type =='F')
   {
    value = mlx.readObjectTempF(); //Fah. Object
   }else if(type =='G')
   {
    value = mlx.readAmbientTempF();//Fah Ambient
   }else if(type =='K')
   {
    value = tempObjec + 273.15;// Object Kelvin
   }else if(type =='L')
   {
    value = tempAmbient + 273.15;//Ambient Kelvin
   }else if(type =='C')
   {
    value = tempObjec;
   }else if(type =='D')
   {
    value = tempAmbient;
   }
   return value;
      //Robojax.com code for MLX90614 Infrared Sensor on LCD
}//getTemp

/*
 * @brief nothing
 * @param "type" is character
 *     C = Object Celsius
 *     D = Ambient Celsius
 *     
 *     K = Object Keliven
 *     L = Ambient in Keilven
 *     
 *     F = Object Fahrenheit
 *     G = Ambient in Fahrenheit

 * @return prints temperature value in serial monitor
 * Usage: to get Fahrenheit type: getTemp('F')
 * to print it on serial monitor Serial.println(getTemp('F'));
 * Written by Ahmad Shamshiri on Mar 30, 2020 at 21:51
 * in Ajax, Ontario, Canada
 * www.Robojax.com 
 */
void printTemp(char type)
{

    //Robojax.com code for MLX90614 Infrared Sensor on LCD

  float tmp =getTemp(type);    
  if(type =='C')
  {

      clearCharacters(1,strlen(typeName[0])-1, LCD_CHAR-1 );   
      lcd.setCursor(strlen(typeName[0])-1,1);      
      lcd.print(": ");
      lcd.print(tmp);        
      lcd.print((char)223);// 
      lcd.print("C");   
      

      clearCharacters(2,strlen(typeName[1])-1, LCD_CHAR-1 );   
      lcd.setCursor(strlen(typeName[1])-1,2);      
      lcd.print(": ");
      lcd.print(getTemp('D'));        
      lcd.print((char)223);// 
      lcd.print("C");            
              

  }else if(type =='F')
  {
      clearCharacters(1,strlen(typeName[0])-1, LCD_CHAR-1 );   
      lcd.setCursor(strlen(typeName[0])-1,1);      
      lcd.print(": ");
      lcd.print(tmp);        
      lcd.print((char)223);// 
      lcd.print("F");   
      

      clearCharacters(2,strlen(typeName[1])-1, LCD_CHAR-1 );   
      lcd.setCursor(strlen(typeName[1])-1,2);      
      lcd.print(": ");
      lcd.print(getTemp('F'));        
      lcd.print((char)223);// 
      lcd.print("F");           
  }
  else if(type =='K')
  {
      clearCharacters(1,strlen(typeName[0])-1, LCD_CHAR-1 );   
      lcd.setCursor(strlen(typeName[0])-1,1);      
      lcd.print(": ");
      lcd.print(getTemp('K'));        
      lcd.print((char)223);// 
      lcd.print("K");   
      

      clearCharacters(2,strlen(typeName[1])-1, LCD_CHAR-1 );   
      lcd.setCursor(strlen(typeName[1])-1,2);      
      lcd.print(": ");
      lcd.print(getTemp('L'));        
      lcd.print((char)223);// 
      lcd.print("K");                  
  }  
  

  //Robojax.com code for MLX90614 Infrared Sensor on LCD
}//printTemp(char type)


/*
   clearCharacters(uint8_t row,uint8_t start, uint8_t stop)
 * @brief clears a line of display (erases all characters)
 * @param none
 * @return does not return anything
 * Written by Ahmad Shamshiri
 * www.Robojax.com code May 28, 2020 at 16:21 in Ajax, Ontario, Canada
 */
void clearCharacters(uint8_t row,uint8_t start, uint8_t stop )
{
    for (int i=start; i<=stop; i++)
    {
    lcd.setCursor (i,row); //  
    lcd.write(254);
    } 

}//clearCharacters

im looking to to get the object temp on the top line of LCD and the ambient temp on the bottom line of LCD.

thank you all

That line sets the cursor to line 2, the third row. Change that number to 3 for the bottom line on a 4 line display.

i only have a 2 line LCD display.

Sorry, the above led me to believe otherwise. Shouldn't that be 16 and 2 for a 16 character by 2 line display?

The line numbers on a 2 line display are 0 and 1. The top row is row 0 and the bottom line is row 1.

The setCursor reference.

i guess you're right it should be 16 however it still works fine with the code given. I'm just trying to get the Object temp on the top line and ambient temp on the bottom line. Right now, it shows the Model number on the top line and on the bottom line it alternates between object and ambient temp.

Please post your best attempt.

I want the Robojax MLX90614 text to go away from the top line and i want the object temp on the top line of the LCD and the ambient temp on the bottom line of the LCD. Currently the object and ambient temp cycle on the bottom line of the LCD.

#include <Wire.h>
#include <Adafruit_MLX90614.h>
char *typeName[]={"Object:","Ambient:"};

Adafruit_MLX90614 mlx = Adafruit_MLX90614();

#include <LiquidCrystal_I2C.h>
const uint8_t I2C_ADDRESS =0x3f;
const uint8_t LCD_CHAR= 16;
const uint8_t LCD_ROW= 2;
LiquidCrystal_I2C lcd(I2C_ADDRESS, LCD_CHAR,LCD_ROW);


void setup() {
//Robojax Step by Step Course http://robojax.com/L/?id=338		
  Serial.begin(9600);

  Serial.println("Robojax MLX90614 test");  

   if (!mlx.begin()) //Begin returns 0 on a good init
  {
      lcd.print("MLX90614 Failed");
      lcd.setCursor(0,1);
      lcd.print("check wiring!");      
    while (1)
      ;
  }  
  Wire.begin();
  lcd.begin();
  lcd.backlight();  
      lcd.print("Robojax MLX90614");
      lcd.setCursor(0,1);
      lcd.print("Infrared Temp.");          
      delay(2000);       
      clearCharacters(LCD_ROW-1,0, LCD_CHAR-1);
       
  
}//setup() end

void loop() {
  //Robojax Example for MLX90614 with LCD
//Robojax Step by Step Course http://robojax.com/L/?id=338	  
  printTemp('C');//object temperature in C
  delay(2000);
  
  printTemp('D');//ambient temperature in C
  delay(2000);
//    
//  printTemp('F'); //object temperature in F
//  delay(2000);
//     
//  printTemp('G'); //ambient temperature in F
//  delay(2000);  
//  if( getTemp('C')>40)
//  {
//    //do something here
//  }
//  
//  printTemp('K'); //object temperature in K
//  delay(2000);    
//  printTemp('L');//ambient temperature in K  
//  delay(2000);  


  //Robojax Example for MLX90614
}

/*
 * @brief returns temperature or relative humidity
 * @param "type" is character
 *     C = Object Celsius
 *     D = Ambient Celsius
 *     
 *     K = Object Keliven
 *     L = Ambient in Keilven
 *     
 *     F = Object Fahrenheit
 *     G = Ambient in Fahrenheit

 * @return returns one of the values above
 * Usage: to get Fahrenheit type: getTemp('F')
 * to print it on serial monitor Serial.println(getTemp('F'));
 * Written by Ahmad Shamshiri on Mar 30, 2020. 
 * in Ajax, Ontario, Canada
 * www.Robojax.com 
 */
float getTemp(char type)
{
	//Robojax Step by Step Course http://robojax.com/L/?id=338	
   // Robojax.com MLX90614 Code
  float value;
    float tempObjec = mlx.readObjectTempC();//in C object
    float tempAmbient = mlx.readAmbientTempC();
   if(type =='F')
   {
    value = mlx.readObjectTempF(); //Fah. Object
   }else if(type =='G')
   {
    value = mlx.readAmbientTempF();//Fah Ambient
   }else if(type =='K')
   {
    value = tempObjec + 273.15;// Object Kelvin
   }else if(type =='L')
   {
    value = tempAmbient + 273.15;//Ambient Kelvin
   }else if(type =='C')
   {
    value = tempObjec;
   }else if(type =='D')
   {
    value = tempAmbient;
   }
   return value;
    // Robojax.com MLX90614 Code
}//getTemp

/*
 * @brief nothing
 * @param "type" is character
 *     C = Object Celsius
 *     D = Ambient Celsius
 *     
 *     K = Object Keliven
 *     L = Ambient in Keilven
 *     
 *     F = Object Fahrenheit
 *     G = Ambient in Fahrenheit

 * @return prints temperature value in serial monitor
 * Usage: to get Fahrenheit type: getTemp('F')
 * to print it on serial monitor Serial.println(getTemp('F'));
 * Written by Ahmad Shamshiri on Mar 30, 2020 at 21:51
 * in Ajax, Ontario, Canada
 * www.Robojax.com 
 */
void printTemp(char type)
{
 clearCharacters(1,0, LCD_CHAR-1 );  
  // Robojax.com MLX90614 Code
  float tmp =getTemp(type);
      lcd.setCursor(0,1);
      
  if(type =='C')
  {

      lcd.print(typeName[0]);  
      lcd.print(" ");
      lcd.print(tmp);        
      lcd.print((char)223);// 
      lcd.print("C");      
              

  }else if(type =='D')
  {
      lcd.print(typeName[1]); 
      lcd.print(" ");
      lcd.print(tmp);        
      lcd.print((char)223);// 
      lcd.print("C");        
  }else if(type =='F')
  {
      lcd.print(typeName[0]); 
      lcd.print(" ");
      lcd.print(tmp);        
      lcd.print((char)223);// 
      lcd.print("F");        
  }else if(type =='G')
  {
      lcd.print(typeName[1]);
      lcd.print(" ");
      lcd.print(tmp);        
      lcd.print((char)223);// 
      lcd.print("F");        
  }

  else if(type =='K')
  {
      lcd.print(typeName[0]); 
      lcd.print(" ");
      lcd.print(tmp);        
      lcd.print((char)223);// 
      lcd.print("K");        
  }  
  else if(type =='L')
  {
      lcd.print(typeName[1]);
      lcd.print(" ");
      lcd.print(tmp);        
      lcd.print((char)223);// 
      lcd.print("K");        

  }

// Robojax.com MLX90614 Code
}//printTemp(char type)


/*
   clearCharacters(uint8_t row,uint8_t start, uint8_t stop)
 * @brief clears a line of display (erases all characters)
 * @param none
 * @return does not return anything
 * Written by Ahmad Shamshiri
 * www.Robojax.com code May 28, 2020 at 16:21 in Ajax, Ontario, Canada
 */
void clearCharacters(uint8_t row,uint8_t start, uint8_t stop )
{
    for (int i=start; i<=stop; i++)
    {
    lcd.setCursor (i,row); //  
    lcd.write(254);
    } 

}//clearCharacters

In that function the type argument chooses which display setup to use. Do you want to replace an existing type or create a new type?

Here's what im looking at. Currently the Robojax MLX90614 is held at the top line. The Ambient and Object temp cycle on the second line. I want the Object temp on the first line and the ambient temp on the second line.

So, delete the code that does that.

I've tried deleting part of this section of code before and it does take away what your talking about but it does not move Object temp to the top line and ambient temp to the bottom line. They still cycle on the second line.

  Wire.begin();
  lcd.begin();
  lcd.backlight();  
      lcd.print("Robojax MLX90614");
      lcd.setCursor(0,1);
      lcd.print("Infrared Temp.");          
      delay(2000);       
      clearCharacters(LCD_ROW-1,0, LCD_CHAR-1);
       
  
}//setup() end

void loop() {
  //Robojax Example for MLX90614 with LCD
//Robojax Step by Step Course http://robojax.com/L/?id=338	  
  printTemp('C');//object temperature in C
  delay(2000);
  
  printTemp('D');//ambient temperature in C
  delay(2000);```

So, you've made a start.
Next, move the reading you want on the top line, to the top line.

forgive me for being a newb but how does one go about doing that?

I understand what you want. Will you answer questions?

And do you want the temperatures to be displayed in C, F or K?

fahrenheit F