9171| 12
|
[常见问题] DS18B20温度传感器无法获得地址 |
本帖最后由 ZhangbeiyiZhang 于 2014-6-9 14:40 编辑 最近使用DFRobot的DS18B20温度传感器,安装库并执行示例代码后发现返回温度总是-1000.简单修改代码调试之后发现时无法获得传感器地址。请问这可能是什么问题?另外我发现我收到的传感器线是黄、红、绿三色,和商品介绍上的不一致。颜色是如何定义的? 我自己测试出结果了。实际上是: 红线=VCC 绿线=Data 黄线=GND 跟商品介绍里面的完全不一致!这个售后的帖子发了这么久,竟然没有一个官方的人出来纠正自己产品的错误! 代码 #include <OneWire.h> int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2 //Temperature chip i/o OneWire ds(DS18S20_Pin); // on digital pin 2 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 = 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; } |
Holiday 发表于 2014-5-29 11:12 我买的是这款https://www.dfrobot.com.cn/goods-799.html 套装,里面有转接板,转接板有上拉电阻 |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed