DS18B20温度传感器和Arduino
-
23-12-2019 - |
题
我目前正在编写这个简单的代码来检查温度传感器
#include <One Wire . 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
}
.
i m get“sketch_feb27a:14:错误:”gettemp“在此范围内未宣布”
有人能告诉我为什么?
解决方案
在这里你的尝试调用 gettemp(); 。但下面的函数未在素描本身下定义。如果添加与 gettemp()相关的活动; 它必须工作。
float temperature = getTemp();
. 其他提示
要么您没有将OneWire库提取到正确的位置,或者在#include行中犯了一个错误。 在没有空格的情况下包括它
*include<OneWire.h>
.
如果gettemp()不是库onewire的一部分,那么显然您必须制作自己的gettemp()函数
可以使用dallastemperature库。
使用它:
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
OneWire dsOneWire(DS_ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature dsSensorControler(&dsOneWire);
dsSensorControler.begin();
dsSensorControler.requestTemperatures();
float temp = dsSensorControler.getTempCByIndex(0);
.
不隶属于 StackOverflow