I have Arduino mega and Lego temperature sensor which I would like to connect to the Arduino. I'm trying to use a I2C connection between them, with no luck. I connected the sensor and Arduino referring to the picture below:wiring I'm using the Wire library for transmitting data. This is my code currently:
#include < Wire.h >
byte ADDRESS=0x98;
byte CONFIG=0x01;
void setup(){
Serial.begin(9600);
Wire.begin();
Wire.beginTransmission(ADDRESS);
Wire.write(CONFIG);
Wire.endTransmission();
delay(100);
}
void loop(){
Wire.beginTransmission(ADDRESS);
Wire.write(CONFIG);
Wire.endTransmission();
Wire.requestFrom(ADDRESS,2);
delay(100);
int avail=Wire.available();
Serial.println(avail);
delay(100);
}
I found the config and address values from RobotC drivers for the same sensor. But all I get is zeros. Is there any way i could check for a defect sensor? Is my code correct?
-
2Start with a i2c scanner : playground.arduino.cc/Main/I2cScannerJot– Jot2017年04月22日 21:29:12 +00:00Commented Apr 22, 2017 at 21:29
-
Looks like this is the datasheet for the actual sensor ti.com/lit/ds/symlink/tmp275.pdf. Reading that and cross referencing with the header file you liked to, should explain how to communicate with it.Mazaryk– Mazaryk2017年04月22日 21:40:27 +00:00Commented Apr 22, 2017 at 21:40
-
I2C scanner helped me to use the correct address, which was different from the one in drivers. Now my sensor is working. Thank you, Jot!Samelikameli– Samelikameli2017年04月23日 09:03:07 +00:00Commented Apr 23, 2017 at 9:03
1 Answer 1
I managed to get it work with the following code:
float getTemp(){
#define ADDRESS 0x4C
#define CONFIG 0x00
float temp;
Wire.beginTransmission(ADDRESS);
Wire.write(CONFIG);
Wire.endTransmission();
delay(50);
Wire.requestFrom(ADDRESS,2);
temp=Wire.read();
if(Wire.read()==128){
temp+=0.5;
}
return temp;
}
And connect the green wire to 3.3V, not to the 5V.