Here : I have connected 4 MLX90614 temp sensors which work on i2c protocol I have got the code worked out but, I am facing some weird problem the problem is:
Initially the program runs well but if I power off the arduino and repower it the data gets hanged and the results are:
Setup...
> Read address
MLX address: 0, Data: Setup...
> Read address
MLX address: 0, Data:
It gets stuck here and doesnot proceed... BUT, I interchanged as follows:
A4--->SDA A5--->SCL (initial connections)
changed to:
A4--->SCL A5----->SDA resetted the controller and then back to:
A4--->SDA A5--->SCL and started to work back I tried it more than 5 times the same thing I needed to do.
Now how do I resolve this issue???
here is my code:
#include "i2cmaster.h"
float ReadTemp(byte);
void setup() {
Serial.begin(9600);
Serial.println("Setup...");/**/
// Initialise the i2c bus, enable pullups and then wait
i2c_init();
PORTC = (1 << PORTC4) | (1 << PORTC5);
delay(5000);
// Read current address bytes
ReadAddr(0);
Serial.println("**---DONE---**");
}
void loop() {
ReadTemp(0X0D);
delay(2000);
ReadTemp(0X0A);
delay(2000);
ReadTemp(0X0C);
delay(2000);
ReadTemp(0X0B);
delay(2000);
}
float ReadTemp(byte address) {
int data_low = 0;
int data_high = 0;
int pec = 0;
byte MLXAddress = address<<1;
Serial.print("> [ReadTemp] Read temperature ");
if (MLXAddress == 0) {
Serial.print("using MLX univeral address");
} else {
Serial.print("using MLX address ");
Serial.print(address, HEX);
};
Serial.print(": ");
i2c_start_wait(MLXAddress + I2C_WRITE);
i2c_write(0x07);
i2c_rep_start(MLXAddress + I2C_READ);
data_low = i2c_readAck();
data_high = i2c_readAck();
pec = i2c_readNak();
i2c_stop();
float temp = 0x0000;
temp = (float)(((data_high & 0x007F) << 8) + data_low);
temp = (temp * 0.02) - 273.16;
Serial.print(temp);
Serial.println(" C");
return temp;
}
void ReadAddr(byte Address) {
Serial.println("> Read address");
// Inform the user
Serial.print(" MLX address: ");
Serial.print(Address, HEX);
Serial.print(", Data: ");
// Send start condition and write bit
i2c_start_wait(Address + I2C_WRITE);
// Send command for device to return address
i2c_write(0x2E);
i2c_rep_start(Address + I2C_READ);
// Read 1 byte and then send ack (x2)
Serial.print(i2c_readAck(), HEX);
Serial.print(", ");
Serial.print(i2c_readAck(), HEX);
Serial.print(", ");
Serial.println(i2c_readNak(), HEX);
i2c_stop();
}
the output I got was :
Setup...
> Read address
MLX address: 0, Data: Setup...
> Read address
MLX address: 0, Data: Setup...
> Read address
MLX address: 0, Data: 8, 0, 80
**---DONE---**
> [ReadTemp] Read temperature using MLX address D: 30.46 C
> [ReadTemp] Read temperature using MLX address A: 26.98 C
> [ReadTemp] Read temperature using MLX address C: 26.02 C
> [ReadTemp] Read temperature using MLX address B: 26.10 C
> [ReadTemp] Read temperature using MLX address D: 27.40 C
> [ReadTemp] Read temperature using MLX address A: 27.10 C
> [ReadTemp] Read temperature using MLX address C: 26.02 C