cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L4 - SMBus and Melexis MLX90614

Miguel Antunes
Associate II
Posted on May 23, 2018 at 17:49

Hello,

I'm trying to read temperature values from a Melexis MLX90614 sensor with SMBus but something seems to be wrong.

I used the HAL_SMBUS_IsDeviceReady to see if I can connect to the sensor and I can.

#define MLX90614_ADDRESS (0x5A << 1)

#define MLX90614_TA 0x06

(...)

return_value = HAL_SMBUS_Master_Transmit_IT(&hsmbus1,MLX90614_ADDRESS,(uint8_t*)MLX90614_TA,1,SMBUS_FIRST_FRAME);

while(HAL_SMBUS_GetState(&hsmbus1) != HAL_SMBUS_STATE_READY);

if (return_value != HAL_OK)

{

   return return_value;

}

return_value = HAL_SMBUS_Master_Receive_IT(&hsmbus1,MLX90614_ADDRESS,(uint8_t*)data_read,2,SMBUS_LAST_FRAME_NO_PEC);

while(HAL_SMBUS_GetState(&hsmbus1) != HAL_SMBUS_STATE_READY);

if(return_value != HAL_OK)

{

   return return_value;

}

This is the code I'm using to read to bytes from that sensor.

The code seems to stop in the first while.

The SMBus peripheral is configured in SMBus-Two-Wire-Interface in CubeMX.

I don't understand what should I put in the XferOptions field in either one of those functions and I don't know if that's the problem.

In

Thank you in advance.

Miguel Antunes

29 REPLIES 29
thomasFromBrittany
Associate II

In my case, I need to calculate PEC with : SA + Command (EEPROM Address) + 2 bytes of data otherwise it doesn't work !

It must be said that the component datasheet is not very detailed..

Regards

FIlse
Associate II

Ah okay, thanks for sharing! When changing the slave-address, it is just necessary to calculate the PEC of Command (EEPROM address) + 2 bytes of data to make it work.

But very good to know, that I have to consider this when dealing with emissivity.

And you're absolutely right - the datasheet has some huge mistakes in it. For example you can only access the EEPROM if you add +20 to the address given in the datasheet (for example emissivity: in datasheet it says 0x004h, but in fact, you have to add "+20" to that address, so that the resulting EEPROM-address is 0x24 etc.).

Regards

Nidhi Rao
Associate II

Hello

int main(void){

while (1)

{

HAL_I2C_Mem_Read(&hi2c1, 0x5A<<1, 0x07, 1, buffer, 2, 100);

 int_temp = (buffer[1] <<8 | buffer[0]);

  temperature = int_temp*0.02 - 273.15;

  HAL_Delay(200);

}

according to the datasheet you have to write 0x27. and I have MLX90615 so please can you help.

Nidhi,

Be sure to double check and make sure you are writing the correct commands as they are different for both chips and the thread here is referencing the 90614 not the 90615.

Also, the I2C slave address for the 90615 is 0x5B not 5A.

Nidhi Rao
Associate II

Thank you. it started working. There was some other hardware issue. I am able to see temperature only once. after waking up from standby mode I cant see. Any help with that?

Hi Thomas, Can I ask you how did you managed to read back from EEPROM.

I've tried the same as reading from RAM but it didn't work. It seems like is performing a write instead of a read and after I've to reprogram some regs to make it works again.

ty.locke
Associate II

Be sure to change the op code when reading from EEPROM and you should be able to read the register values without issue. If you're trying to change the values be sure to erase the EEPROM address first before setting the new values.

Be sure to change the op code when reading from EEPROM and you should be able to read the register values without issue. If you're trying to change the values be sure to erase the EEPROM address first before setting the new values

OP code and erase was OK.

I've fixed the problem by changing the number of bytes to read in the mem_read function.

Anyway I've also made a library fully functional, with PEC calculus and check on readded data!

Can find it on this Github repo.

APand.7
Associate II

Hello Everyone

I am trying to interface MLX90614 with stm32F401 Discovery board using I2C communication my code snippet are as follow and i have used logic analyser to analyse SCL and SDA signals.

The code works fine and i am getting sensor values. However, in the logic analyser i am always getting 0x3A NACK in stop condition. I have attached images of my logic analyser signal as well.

i am using standard mode in CubeMX

Can anyone help me how i can convert this NACK to ACK

uint8_t I2C_ADDRESS = 0x00;

int main(void)

{

HAL_StatusTypeDef status = HAL_ERROR;

uint8_t buf[3] = {0,};

uint32_t temp_data_a;

uint32_t temp_data_o;

__IO float T;

__IO float To;

 status = HAL_I2C_IsDeviceReady(&hi2c1, I2C_ADDRESS, 3, 1000); 

 while (1)

 {

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

 // HAL_I2C_Mem_Read(&hi2c1, 0X00, 0X06, 3, buf, 3, 1000);

 HAL_I2C_Mem_Read(&hi2c1, 0x00, 0x06, 1, buf, 2, 1000); // Read Ambient temperature from Register 0x06

 temp_data_a = buf[1]*256 + buf[0];

 HAL_I2C_Mem_Read(&hi2c1, 0x00, 0x07, 1, buf, 2, 1000); // Read Object temperature from Register 0x07

 temp_data_o = buf[1]*256 + buf[0];

 To = temp_data_o*0.02;

 if(To >= 273.15)

  {

  To = To - 273.15;

  }

  else

  {

  To = -(273.15 - To);

  }

 T = temp_data_a*0.02;

if(T >= 273.15)

{

T = T - 273.15;

}

else

{

T = -(273.15 - T);

}

 HAL_Delay(500);