Seial data does not display

I have started using arduino recently. I was given a sketch to read the data from MLX90614 IR sensor. Until now everything work but something happen the serial data does not display anymore.

I have tried sample sketch from arduino forum and this display accordingly for example “hello world” etc.

The sketch below does not even display “Hello”

Any suggestion of how to rectify this?

Below is the sketch that has been working so far.
#include <i2cmaster.h>

void setup()
{
Serial.begin(9600);
Serial.println(“Hello!”);
i2c_init(); //Initialise the i2c bus
Serial.println(“Return from i2c_init”);
PORTC = (1 << PORTC4) | (1 << PORTC5);//enable pullups
}
void loop()
{
int dev1 = 0x50<<1; //Address must be bit shifted left, library does not automatically do it
int data_low1 = 0;
int data_high1 = 0;
int pec1 = 0;
int devres_low1 =0; // device response address
int devres_high1 =0; // device response address

int dev2 = 0x5A<<1; //Address must be bit shifted left, library does not automatically do it
int data_low2 = 0;
int data_high2 = 0;
int pec2 = 0;
int devres_low2 =0; // device response address
int devres_high2 =0; // device response address

i2c_start_wait(dev1+I2C_WRITE); //send start condition and write bit
//Function issues a start condition and sends address and transfer direction.
i2c_write(0x07); //send command for device to action

i2c_rep_start(dev1+I2C_READ); // Function issues a repeated start condition
//and sends address and transfer direction
//device will ack
data_low1 = i2c_readAck(); //Read 1 byte and then send ack
data_high1 = i2c_readAck(); //Read 1 byte and then send ack
pec1 = i2c_readNak(); //Read error check byte and send Nack to tell device to stop
i2c_stop(); //Release bus, end transaction

//Read address

i2c_start_wait(dev1+I2C_WRITE); //send start condition and write bit
//Function issues a start condition and sends address and transfer direction.
i2c_write(0x2D); //send command for device to return address

i2c_rep_start(dev1+I2C_READ); // Function issues a repeated start condition
//and sends address and transfer direction
//device will ack
devres_low1 = i2c_readAck()>>1; //Read 1 byte and then send ack
devres_high1 = i2c_readAck()>>1; //Read 1 byte and then send ack
pec1 = i2c_readNak(); //Read error check byte and send Nack to tell device to stop
i2c_stop(); //Release bus, end transaction

//second device

i2c_start_wait(dev2+I2C_WRITE); //send start condition and write bit
//Function issues a start condition and sends address and transfer direction.
i2c_write(0x07); //send command for device to action

i2c_rep_start(dev2+I2C_READ); // Function issues a repeated start condition
//and sends address and transfer direction
//device will ack
data_low2 = i2c_readAck(); //Read 1 byte and then send ack
data_high2 = i2c_readAck(); //Read 1 byte and then send ack
pec2 = i2c_readNak(); //Read error check byte and send Nack to tell device to stop
i2c_stop(); //Release bus, end transaction

//Read address

i2c_start_wait(dev2+I2C_WRITE); //send start condition and write bit
//Function issues a start condition and sends address and transfer direction.
i2c_write(0x2D); //send command for device to return address

i2c_rep_start(dev2+I2C_READ); // Function issues a repeated start condition
//and sends address and transfer direction
//device will ack
devres_low2 = i2c_readAck()>>1; //Read 1 byte and then send ack
devres_high2 = i2c_readAck()>>1; //Read 1 byte and then send ack
pec2 = i2c_readNak(); //Read error check byte and send Nack to tell device to stop
i2c_stop(); //Release bus, end transaction

//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
double tempData = 0x0000;
int frac;
// This masks off the error bit of the high byte, then moves it left 8 bits and adds the low byte.
tempData = (double)(((data_high1 & 0x007F) << 8) + data_low1);
tempData = (tempData * tempFactor)-0.01;
tempData = tempData - 273.15;
Serial.print(“Temeperature from Sensor A”);
Serial.print(devres_high1);
Serial.print(devres_low1);
Serial.print(" is = “);
Serial.print((int)tempData); //Print temp in degrees C to serial
Serial.print(”.");
tempData=tempData-(int)tempData;
frac=tempData*100;
Serial.print(frac);

Serial.print(" and B");

tempData = (double)(((data_high2 & 0x007F) << 8) + data_low2);
tempData = (tempData * tempFactor)-0.01;
tempData = tempData - 273.15;

Serial.print(devres_high2);
Serial.print(devres_low2);
Serial.print(" is “);
Serial.print((int)tempData); //Print temp in degrees C to serial
Serial.print(”.");
tempData=tempData-(int)tempData;
frac=tempData*100;
Serial.println(frac);
delay(1000);
}

Have you opened the serial monitor or a terminal emulator?

(When posting code, please use the # icon on the editor's toolbar to give you [code] [/code] tags, and avoid smileys.)

Read this before posting a programming question

I opened the serial monitor but nothing is showing. As mentioned, I had used the same code to measure temperature and I could see the measured temperature using Serial monitor. Now, nothing shows on the serial monitor.

void setup ()
{
  Serial.begin (9600);
  Serial.println ("Hello world!");
}

void loop ()
{
}

What does that do?

ftosin: I opened the serial monitor but nothing is showing.

Did you decide not to read my link? Which mentions to use code tags? I can't compile code with smiley faces in it.

In a new sketch, I could see “Hello World” on the serial monitor. But when I run the sketch for the MLX sensor, “Hello” does not show on the serial monitor nor “Return from i2c_init” .

The MLX sensor sketch is below:

#include <i2cmaster.h>

void setup() 
{ 
Serial.begin(19200); 
Serial.println("Hello!"); 
i2c_init(); //Initialise the i2c bus 
Serial.println("Return from i2c_init"); 
PORTC = (1 << PORTC4) | (1 << PORTC5);//enable pullups 
} 
void loop() 
{ 
int dev1 = 0x50<<1; //Address must be bit shifted left, library does not automatically do it
int data_low1 = 0; 
int data_high1 = 0; 
int pec1 = 0; 
int devres_low1 =0;  // device response address
int devres_high1 =0;  // device response address

int dev2 = 0x5A<<1; //Address must be bit shifted left, library does not automatically do it
int data_low2 = 0; 
int data_high2 = 0; 
int pec2 = 0; 
int devres_low2 =0;  // device response address
int devres_high2 =0;  // device response address

i2c_start_wait(dev1+I2C_WRITE); //send start condition and write bit
                    //Function issues a start condition and sends address and transfer direction. 
i2c_write(0x07);   //send command for device to action


i2c_rep_start(dev1+I2C_READ); // Function issues a repeated start condition 
                            //and sends address and transfer direction
                            //device will ack
data_low1 = i2c_readAck(); //Read 1 byte and then send ack 
data_high1 = i2c_readAck(); //Read 1 byte and then send ack 
pec1 = i2c_readNak(); //Read error check byte and send Nack to tell device to stop
i2c_stop();          //Release bus, end transaction

//Read address

i2c_start_wait(dev1+I2C_WRITE); //send start condition and write bit
                    //Function issues a start condition and sends address and transfer direction. 
i2c_write(0x2D);   //send command for device to return address


i2c_rep_start(dev1+I2C_READ); // Function issues a repeated start condition 
                            //and sends address and transfer direction
                            //device will ack
devres_low1 = i2c_readAck()>>1; //Read 1 byte and then send ack 
devres_high1 = i2c_readAck()>>1; //Read 1 byte and then send ack 
pec1 = i2c_readNak(); //Read error check byte and send Nack to tell device to stop
i2c_stop();          //Release bus, end transaction

//second device

i2c_start_wait(dev2+I2C_WRITE); //send start condition and write bit
                    //Function issues a start condition and sends address and transfer direction. 
i2c_write(0x07);   //send command for device to action


i2c_rep_start(dev2+I2C_READ); // Function issues a repeated start condition 
                            //and sends address and transfer direction
                            //device will ack
data_low2 = i2c_readAck(); //Read 1 byte and then send ack 
data_high2 = i2c_readAck(); //Read 1 byte and then send ack 
pec2 = i2c_readNak(); //Read error check byte and send Nack to tell device to stop
i2c_stop();          //Release bus, end transaction

//Read address

i2c_start_wait(dev2+I2C_WRITE); //send start condition and write bit
                    //Function issues a start condition and sends address and transfer direction. 
i2c_write(0x2D);   //send command for device to return address


i2c_rep_start(dev2+I2C_READ); // Function issues a repeated start condition 
                            //and sends address and transfer direction
                            //device will ack
devres_low2 = i2c_readAck()>>1; //Read 1 byte and then send ack 
devres_high2 = i2c_readAck()>>1; //Read 1 byte and then send ack 
pec2 = i2c_readNak(); //Read error check byte and send Nack to tell device to stop
i2c_stop();          //Release bus, end transaction

//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 
double tempData = 0x0000; 
int frac; 
// This masks off the error bit of the high byte, then moves it left 8 bits and adds the low byte. 
tempData = (double)(((data_high1 & 0x007F) << 8) + data_low1); 
tempData = (tempData * tempFactor)-0.01; 
tempData = tempData - 273.15; 
Serial.print("A");
Serial.print(devres_high1);
Serial.print(devres_low1);
Serial.print(" is ");
Serial.print((int)tempData); //Print temp in degrees C to serial 
Serial.print("."); 
tempData=tempData-(int)tempData; 
frac=tempData*100; 
Serial.print(frac); 
Serial.print(" and B");

tempData = (double)(((data_high2 & 0x007F) << 8) + data_low2); 
tempData = (tempData * tempFactor)-0.01; 
tempData = tempData - 273.15; 

Serial.print(devres_high2);
Serial.print(devres_low2);
Serial.print(" is ");
Serial.print((int)tempData); //Print temp in degrees C to serial 
Serial.print("."); 
tempData=tempData-(int)tempData; 
frac=tempData*100; 
Serial.println(frac); 
delay(10); 
}
Serial.begin(19200);
Serial.begin (9600);

See the difference? Your Arduino and your serial monitor have to agree what settings to use on the serial port. If your Arduino is using a non-default speed then you will have to configure the serial monitor to match it.

Thanks. I changed baud rate in the code to 9600 but no data shows on the serial monitor, when I run my program. I checked the port settings. It is also set at9600 bits per second. Any other suggestions?

ftosin: In a new sketch, I could see "Hello World" on the serial monitor. But when I run the sketch for the MLX sensor, "Hello" does not show on the serial monitor nor "Return from i2c_init" .

I uploaded your code, set the serial monitor to 19200 baud and saw the messages:

Hello!
Return from i2c_init

Thanks. I changed to another laptop, where arduino is also installed. I did see "Hello!" and "Return from i2c_init" but after this no data. It seems that the program stop after i2c is initialized.

Any further suggestion.

Why are you doing it this complicated way? How about using the Wire library?

http://www.gammon.com.au/i2c

Thanks. Now, I have to change the code to use wire library. I've never done this before.

Hi I used these sensors for my final year project. Have a look at the thread I started ( on my phone so can't really link it) named multiple mlx90614 on i2c or something like that. Your code should work it's just the sensors have a limit to the rate at which they can transmit data to the serial monitor. 9600 is too slow and just bottlenecks and you won't recive data from them.

Drop me a message if you like and I can help you out more and can help you rename/edit device addresses or individual sensors so that you can just define at the start of the code and forgot about it

Hope this helps

Info from dr of engineering at Uni and about 6 months work :)

Thanks for your reply.
I did almost the same thing and I got the serial display to work. I understand the problem was the device address. I started using another MLX sensor. It seems that there was conflict with two devices on default address.
I used the following sketch to give each device and address:
Below is Change Sensor Address sketch:

#include <i2cmaster.h>
// Pins: Standard: SDA:A4  SCL:A5
//       Mega:     SDA:D20 SCL:D21

byte MLXAddr = 0x5A<<1;           // Default address
//byte MLXAddr = 0;               // Universal address

void setup(){
  Serial.begin(9600);
  Serial.println("Setup...");
  
  i2c_init();                              //Initialise the i2c bus
  PORTC = (1 << PORTC4) | (1 << PORTC5);   //enable pullups
  
  delay(5000);                    // Wait to allow serial connection
  ReadAddr(0);                    // Read current address bytes
  ChangeAddr(0x56, 0x00);         // Change address to new value
  //ChangeAddr(0x5A, 0xBE);       // Change address to default value
  ReadAddr(0);                    // Read address bytes
  delay(5000);                    // Cycle power to MLX during this pause
  ReadTemp(0);                    // Read temperature using default address
  ReadTemp(MLXAddr);              // Read temperature using new address
}

void loop(){
    delay(1000); // wait a second
}

word ChangeAddr(byte NewAddr1, byte NewAddr2) {

  Serial.println("> Change address");

  i2c_start_wait(0 + I2C_WRITE);    //send start condition and write bit
  i2c_write(0x2E);                  //send command for device to return address
  i2c_write(0x00);                  // send low byte zero to erase
  i2c_write(0x00);                  //send high byte zero to erase
  if (i2c_write(0x6F) == 0) {
    i2c_stop();                     //Release bus, end transaction
    Serial.println("  Data erased.");
  }
  else {
    i2c_stop();                     //Release bus, end transaction
    Serial.println("  Failed to erase data");
    return -1;
  }

  Serial.print("  Writing data: ");
  Serial.print(NewAddr1, HEX);
  Serial.print(", ");
  Serial.println(NewAddr2, HEX);

  for (int a = 0; a != 256; a++) {
    i2c_start_wait(0 + I2C_WRITE);  //send start condition and write bit
    i2c_write(0x2E);                //send command for device to return address
    i2c_write(NewAddr1);            // send low byte zero to erase
    i2c_write(NewAddr2);            //send high byte zero to erase
    if (i2c_write(a) == 0) {
      i2c_stop();                   //Release bus, end transaction
      delay(100);                   // then wait 10ms
      Serial.print("Found correct CRC: 0x");
      Serial.println(a, HEX);
      return a;
    }
  }
  i2c_stop();                       //Release bus, end transaction
  Serial.println("Correct CRC not found");
  return -1;
}

void ReadAddr(byte Address) {

  Serial.println("> Read address");

  Serial.print("  MLX address: ");
  Serial.print(Address, HEX);
  Serial.print(", Data: ");

  i2c_start_wait(Address + I2C_WRITE);  //send start condition and write bit
  i2c_write(0x2E);                  //send command for device to return address
  i2c_rep_start(Address + I2C_READ);
  
  Serial.print(i2c_readAck(), HEX); //Read 1 byte and then send ack
  Serial.print(", ");
  Serial.print(i2c_readAck(), HEX); //Read 1 byte and then send ack
  Serial.print(", ");
  Serial.println(i2c_readNak(), HEX);
  i2c_stop();
}

float ReadTemp(byte Address) {
  int data_low = 0;
  int data_high = 0;
  int pec = 0;

  Serial.println("> Read temperature");

  Serial.print("  MLX address: ");
  Serial.print(Address, HEX);
  Serial.print(", ");

  i2c_start_wait(Address + I2C_WRITE);
  i2c_write(0x07);                  // Address of temp bytes
  
  // read
  i2c_rep_start(Address + 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
  float Temperature = 0x0000;       // zero out the data
  
  // This masks off the error bit of the high byte, then moves it left 8 bits and adds the low byte.
  Temperature = (float)(((data_high & 0x007F) << 8) + data_low);
  Temperature = (Temperature * 0.02) - 273.16;
  
  Serial.print(Temperature);
  Serial.println(" C");
  return Temperature;
}

After this in the original MLX read sketch, I indicated the new device address that I assign for each sensor. Everything works.
The above sketch was found on one of the forum.
Thanks.

ahh ye i found that one too! i also found a i2c scanner that tells ou the devices connected to the bus which you may find usefull ill add the link when i get abck from work.