防水水温DS18B20仅返回-127摄氏度 [英] Waterproofed water-temperature DS18B20 only returns -127 for degree in Celsius
问题描述
对于大学中的一个项目,我必须测量各种水质,包括温度.温度传感器是DS18B20,我使用Arduino Mega板进行了整个展示.使用DS18B20的个人只能返回任何有意义的数字.而不是25C(或类似的温度),它返回-127.
For a project in university, I have to measure various quality of the water, including the temperature. The temperature sensor is DS18B20, and I use an Arduino Mega board to run the whole show. Individual running with DS18B20 only fails to return any meaningful number. Instead of 25C (or something like that), it returns -127.
下面的代码来自达拉斯温度(稍有变化,例如有延迟和删除了一些注释行)
The code below is from Dallas Temperature (with small changes, like having delay and removing some comment lines)
// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 48
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
void setup(void)
{
// start serial port
Serial.begin(9600);
Serial.println("Dallas Temperature IC Control Library Demo");
// Start up the library
sensors.begin();
}
void loop(void)
{
delay(500);
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
Serial.print("Temperature for the device 1 (index 0) is: ");
Serial.println(sensors.getTempCByIndex(0));
delay(2500);
}
这实际上来自 DFRobot的Wiki页面(我的传感器起源于此)
This one is actually from the Wiki page of DFRobot (where my sensor is originated from)
#include <OneWire.h>
int DS18S20_Pin = 48; //DS18S20 Signal pin on digital 48
//Temperature chip i/o
OneWire ds(DS18S20_Pin); // on digital pin 48
void setup(void) {
Serial.begin(9600);
}
void loop(void) {
float temperature = getTemp();
Serial.println(temperature);
delay(100); //just here to slow down the output so it is easier to read
}
float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++ ) { // we need 9 bytes
data[i] = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
return TemperatureSum;
}
此特定代码每次都会返回-1000,我认为是与-127等效.
This specific code returns -1000 every time, which I presume is the equivalence of -127 above.
推荐答案
来自库:
// Error Codes
#define DEVICE_DISCONNECTED_C -127
因此您的设备未连接/未通信.检查接线和引脚号.
So you're device is not connected / not communicating. Check your wiring and your pin numbers.
这篇关于防水水温DS18B20仅返回-127摄氏度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!