#define LED_pin D7
#define slaveAddress 0x5A
void setup(){
Serial.begin(9600);
// An LED will blink to indicate when we have successfully read the temperature
pinMode(LED_pin, OUTPUT);
digitalWrite(LED_pin, LOW);
Wire.begin();
Wire.beginTransmission(slaveAddress);
}
void loop(){
// Store the two relevant bytes of data for temperature
byte dataLow = 0x00;
byte dataHigh = 0x00;
delay(10);
Wire.write(0x07); // This is the command to view object temperature in the sensor's RAM
delay(10);
Wire.endTransmission(false);
delay(10);
Wire.requestFrom(slaveAddress, 2);
delay(10);
while (Wire.available()){
dataLow = Wire.read();
dataHigh = Wire.read();
digitalWrite(LED_pin, HIGH); // Blink the LED to indicate a successful reading
}
delay(10);
digitalWrite(LED_pin, LOW);
double tempFactor = 0.02; // 0.02 degrees per LSB (measurement resolution of the MLX90614)
double tempData = 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.
tempData = (double)(((dataHigh & 0x007F) << 8) + dataLow);
tempData = (tempData * tempFactor)-0.01;
float celcius = tempData - 273.15;
float fahrenheit = (celcius*1.8) + 32;
Serial.print((String) fahrenheit);
Serial.println("F");
delay(100);
}
Just getting a constant reading of 459.688019F - Using D0 & D1 for SDA & SCL
I’m pretty sure it isn’t the sensor as it works perfectly well with an Arduino.
@craiglockwood, do you have pull-up resistors on the two I2C lines? Are you using the device library I posted on my github? That library and example work fine.
// This #include statement was automatically added by the Spark IDE.
#include "Adafruit_MLX90614.h"
#define LED_pin D7
#define slaveAddress 0x5A
void setup(){
Serial.begin(9600);
// An LED will blink to indicate when we have successfully read the temperature
pinMode(LED_pin, OUTPUT);
digitalWrite(LED_pin, LOW);
Wire.begin();
Wire.beginTransmission(slaveAddress);
}
void loop(){
// Store the two relevant bytes of data for temperature
byte dataLow = 0x00;
byte dataHigh = 0x00;
delay(10);
Wire.write(0x07); // This is the command to view object temperature in the sensor's RAM
delay(10);
Wire.endTransmission(false);
delay(10);
Wire.requestFrom(slaveAddress, 2);
delay(10);
while (Wire.available()){
dataLow = Wire.read();
dataHigh = Wire.read();
digitalWrite(LED_pin, HIGH); // Blink the LED to indicate a successful reading
}
delay(10);
digitalWrite(LED_pin, LOW);
double tempFactor = 0.02; // 0.02 degrees per LSB (measurement resolution of the MLX90614)
double tempData = 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.
tempData = (double)(((dataHigh & 0x007F) << 8) + dataLow);
tempData = (tempData * tempFactor)-0.01;
float celcius = tempData - 273.15;
float fahrenheit = (celcius*1.8) + 32;
Serial.print((String) fahrenheit);
Serial.println("F");
delay(100);
}
With the Adafruit_MLX90614.cpp and Adafruit_MLX90614.H library files added.
I am fairly new to Spark stuff but been doing arduino stuff for some time. I may have made a stupid/simple error somewhere.
I might have missed it, or Wire.endTransmission()/Wire.requestFrom() does it implicitly, but I can only see one single call to Wire.beginTransmission() where Paul’s lib does a fresh begin each time he wants to get a 16bit reading.
uint16_t Adafruit_MLX90614::read16(uint8_t a) {
uint16_t ret;
Wire.beginTransmission(_addr); // start transmission to device
Wire.write(a); // sends register address to read from
Wire.endTransmission(false); // end transmission
Wire.requestFrom(_addr, (uint8_t)3);// send data n-bytes read
ret = Wire.read(); // receive DATA
ret |= Wire.read() << 8; // receive DATA
uint8_t pec = Wire.read();
return ret;
}
Also note that he asks for three not two bytes in Wire.requestFrom(). Since you’re reading in a while(), you might even overwrite your first byte by reading the pec into the same var.
On the other hand: Since you’ve already included Paul’s lib, why do you not just use it?
Please @ScruffR and @craiglockwood, is it possible to use the same code for a photon? Because am experiencing a lot of problems with mine. I am trying to design a body worn temperature sensor using the MLX90614 IR sensor and a photon to transfer the data via wifi to my iPhone using the BLYNK APP. I have tried several times with different photons and it seems like whenever i flash the code to my photon, the photon stops working i.e. it connects to the WIFI but automatically turns off or remain frozen on cyan. I have become depressed in that I have destroyed three photons already with this process of flashing the code. Please below is the code am flashing, Am just a beginner in programming so please don’t laugh lol.