Go Down

Topic: Connecting MLX90614 with 16x02 LCD (Read 910 times) previous topic - next topic

columbuslee

Jul 10, 2015, 11:41 am Last Edit: Jul 21, 2015, 08:55 am by columbuslee
hi,
i got a problem in combining both files together.
i got the MLX90614 (Non-Contact Thermal sensor) working using the serial monitor.
and i got the 16x02 LCD working.

http://www.melexis.com/Infrared-Thermometer-Sensors/Infrared-Thermometer-Sensors/MLX90614-615.aspx

but i could not put them in place to get them working.

attached will be the coding which i used:
Code: [Select]
#include <FastIO.h>
#include <I2CIO.h>
#include <LCD.h>
#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
#include <LiquidCrystal_SR.h>
#include <LiquidCrystal_SR2W.h>
#include <LiquidCrystal_SR3W.h>
#include <Wire.h>
/**
 * Infrared Thermometer MLX90614
 * by Jaime Patarroyo
 * based on 'Is it hot? Arduino + MLX90614 IR Thermometer' by bildr.blog
 *
 * Returns the temperature in Celcius and Fahrenheit from a MLX90614
 * Infrared Thermometer, connected to the TWI/I²C pins (on the Wiring v1
 * board 0 (SCL) and 1 (SDA) and on Wiring S board 8 (SCL) and 9 (SDA)).
 */

#include <i2cmaster.h>
#include <LiquidCrystal.h>


int deviceAddress = 0x5A<<1;    // From MLX906114 datasheet's, 0x5A is
                                // the default address for I²C communication.
                                // Shift the address 1 bit right, the
                                // I²Cmaster library only needs the 7 most
                                // significant bits for the address.

float celcius = 0;              // Variable to hold temperature in Celcius.
float fahrenheit = 0;           // Variable to hold temperature in Fahrenheit.
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
 
  Serial.begin(9600);           // Start serial communication at 9600bps.

  i2c_init();                               // Initialise the i2c bus.
  PORTC = (1 << PORTC4) | (1 << PORTC5);    // Enable pullups.
}

void loop() {
  celcius = temperatureCelcius(deviceAddress);  // Read's data from MLX90614
                                                // with the given address,
                                                // transform's it into
                                                // temperature in Celcius and
                                                // store's it in the Celcius
                                                // variable.
 
  fahrenheit = (celcius*1.8) + 32;     // Converts celcius into Fahrenheit
                                       // and stores in Fahrenheit variable.

  Serial.print("Celcius: ");           // Prints both readings in the Serial
  Serial.println(celcius);             // port.
  Serial.print("Fahrenheit: ");
  Serial.println(fahrenheit);
  delay(1000);                         // Wait a second before printing again.

  lcd.begin(16, 2);
  lcd.print ("123");
  lcd.print ("degrees");

    delay (1000);
    lcd.setCursor(0, 1);
}

float temperatureCelcius(int address) {
  int dev = address;
  int data_low = 0;
  int data_high = 0;
  int pec = 0;

  // Write
  i2c_start_wait(dev+I2C_WRITE);
  i2c_write(0x07);

  // Read
  i2c_rep_start(dev+I2C_READ);
  data_low = i2c_readAck();       // Read 1 byte and then send ack.
  data_high = i2c_readAck();      // Read 1 byte and then send ack.
  pec = i2c_readNak();
  i2c_stop();

  // This converts high and low bytes together and processes temperature,
  // MSB is a error bit and is ignored for temps.
  double tempFactor = 0.02;       // 0.02 degrees per LSB (measurement
                                  // resolution of the MLX90614).
  double tempData = 0x0000;       // Zero out the data
  int frac;                       // Data past the decimal point

  // This masks off the error bit of the high byte, then moves it left
  // 8 bits and adds the low byte.
  tempData = (double)(((data_high & 0x007F) << 8) + data_low);
  tempData = (tempData * tempFactor)-0.01;
  float celcius = tempData - 273.15;
 
  // Returns temperature un Celcius.
  return celcius;
}
 

 

floresta

Did you notice the smiley faces in your code?  You can get rid of them and make your code more legible in the process by using code tags?

You can find out about code tags by reading this thread: -->   How to use this forum - please read

Don

Paul__B

And while you are at it, post a link to whatever a "MLX90614" is.

columbuslee

i have updated the things that i tried to do and nothing seems to work. and i have also include the MLX90614 website. it is a non contact thermal sensor.

Thanks In Advance!!

Paul__B

A very sophisticated device, it seems.

OK, you need to post the two versions of the code that works, in addition to your combined code which is I believe, what you have above - didn't look too closely.

There seem to be a whole lot of "LiquidCrystal" defines.  Why is that? - you should only need one.

columbuslee

Code for MLX 90614 (Non-Contact Thermal Sensor):
Code: [Select]
/**
 * Infrared Thermometer MLX90614
 * by Jaime Patarroyo
 * based on 'Is it hot? Arduino + MLX90614 IR Thermometer' by bildr.blog
 *
 * Returns the temperature in Celcius and Fahrenheit from a MLX90614
 * Infrared Thermometer, connected to the TWI/I²C pins (on the Wiring v1
 * board 0 (SCL) and 1 (SDA) and on Wiring S board 8 (SCL) and 9 (SDA)).
 */

#include <i2cmaster.h>


int deviceAddress = 0x5A<<1;    // From MLX906114 datasheet's, 0x5A is
                                // the default address for I²C communication.
                                // Shift the address 1 bit right, the
                                // I²Cmaster library only needs the 7 most
                                // significant bits for the address.

float celcius = 0;              // Variable to hold temperature in Celcius.
float fahrenheit = 0;           // Variable to hold temperature in Fahrenheit.

void setup() {
  Serial.begin(9600);           // Start serial communication at 9600bps.

  i2c_init();                               // Initialise the i2c bus.
  PORTC = (1 << PORTC4) | (1 << PORTC5);    // Enable pullups.
}

void loop() {
  celcius = temperatureCelcius(deviceAddress);  // Read's data from MLX90614
                                                // with the given address,
                                                // transform's it into
                                                // temperature in Celcius and
                                                // store's it in the Celcius
                                                // variable.
 
  fahrenheit = (celcius*1.8) + 32;     // Converts celcius into Fahrenheit
                                       // and stores in Fahrenheit variable.

  Serial.print("Celcius: ");           // Prints both readings in the Serial
  Serial.println(celcius);             // port.
  Serial.print("Fahrenheit: ");
  Serial.println(fahrenheit);

  delay(1000);                         // Wait a second before printing again.
}

float temperatureCelcius(int address) {
  int dev = address;
  int data_low = 0;
  int data_high = 0;
  int pec = 0;

  // Write
  i2c_start_wait(dev+I2C_WRITE);
  i2c_write(0x07);

  // Read
  i2c_rep_start(dev+I2C_READ);
  data_low = i2c_readAck();       // Read 1 byte and then send ack.
  data_high = i2c_readAck();      // Read 1 byte and then send ack.
  pec = i2c_readNak();
  i2c_stop();

  // This converts high and low bytes together and processes temperature,
  // MSB is a error bit and is ignored for temps.
  double tempFactor = 0.02;       // 0.02 degrees per LSB (measurement
                                  // resolution of the MLX90614).
  double tempData = 0x0000;       // Zero out the data
  int frac;                       // Data past the decimal point

  // This masks off the error bit of the high byte, then moves it left
  // 8 bits and adds the low byte.
  tempData = (double)(((data_high & 0x007F) << 8) + data_low);
  tempData = (tempData * tempFactor)-0.01;
  float celcius = tempData - 273.15;
 
  // Returns temperature un Celcius.
  return celcius;
}


Code for LCD 1602:
Code: [Select]
#include <Wire.h>
#include <LiquidCrystal_I2C.h>


/* Initialise the LiquidCrystal library. The default address is 0x27 and this is a 16x2 line display */
LiquidCrystal_I2C lcd(0x27,16,2);


void setup()
{
  /* Initialise the LCD */
  lcd.init();
  lcd.init();
}

/* Main program loop */
void loop()
{
  /* Make sure the backlight is turned on */
  lcd.backlight();

  /* Output the test message to the LCD */
  lcd.setCursor(0,0);
  lcd.print("HOBBY COMPONENTS");
  lcd.setCursor(0,1);
  lcd.print("**HELLO WORLD**");

 
  /* Do nothing */
  while(1);
}

floresta

Which I2C library are you using?

Why did you initialize the device twice.

If you only want to do something once such as turn the backlight on and display the messages you have shown in 'Code for LCD 1602' that is typically done in setup().   Just move your first five statements from loop() into setup(), get rid of the while() statement, and leave loop() empty for now.

Don




Go Up