我正在尝试使用 I2C 将 Arduino Uno 连接到温度传感器。起初,Arduino 可以发送数据流,但几分钟后就停止了……
如果我重新启动开发板,Arduino 可以发送数据流,但几分钟后又会停止。我想知道我的代码是否错误。请帮我。
/*
Program akses DT-SENSE Temp SENSOR - Arduino
Vizard Vision @ 2013
*/
#include <Wire.h>
void setup() {
Wire.begin();
Serial.begin(38400);
}
int buffer = 0;
int count = 0;
void loop() {
if(count >= 6000) {
count = 0;
}
Wire.beginTransmission(112); // transmit to device #112 (0x70)
// the address specified in the datasheet is 224 (0xE0) --> 1110 0000 = E0H
// but i2c adressing uses the high 7 bits so it's 112 --> 0111 0000 = 70H
Wire.send(0x00);
// command sensor to measure 16 Byte of Temperature Data
Wire.endTransmission();
delay(100);
Wire.requestFrom(112, 2);
// request 2 bytes from slave device #112
if(2 <= Wire.available()) {
// if two bytes were received
buffer = Wire.receive();
// receive high byte (overwrites previous reading)
buffer = buffer << 8;
// shift high byte to be high 8 bits
buffer |= Wire.receive();
// receive low byte as lower 8 bits
buffer = (buffer - 400)/10;
// Conversion data to Temperature (from datasheet)
Serial.print(count);
Serial.print(" Suhu = ");
Serial.print(buffer);
Serial.write(176);
// Unicode value of Degree Symbol
Serial.println("Celsius");
count ++;
Serial.flush();
}
delay(40);
}