我目前正在尝试(几个小时)在我的Adafruit LEDBackpack上显示从DS18B20获得的温度。但是,当我尝试在设置( matrix.begin(0x070))中初始化显示时,传感器返回的温度始终 -127
你能帮我理解我做错了吗?
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Bridge.h>
#include <Wire.h> // Enable this line if using Arduino Uno, Mega, etc.
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
#define ONE_WIRE_BUS 2
#define TEMP_DELAY 2000 // Request temp every two seconds
Adafruit_7segment matrix = Adafruit_7segment();
unsigned long time, lastTempCheck = 0;
float temp = 0;
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
void setup(void)
{
// start serial port
Serial.begin(9600);
// Start up the library
sensors.begin();
matrix.begin(0x70); // If I comment this and do not use the matrix, the temperature is correct.
}
void loop(void)
{
time = millis();
if((time - lastTempCheck) > TEMP_DELAY){
lastTempCheck = time;
processTemp();
}else {
matrix.print(100);
matrix.writeDisplay();
}
}
void processTemp(void){
sensors.requestTemperatures(); // Send the command to get temperatures
temp = sensors.getTempCByIndex(0);
Serial.print("Temperature: ");
Serial.println(temp);
}
答案 0 :(得分:0)
尝试为每个组件提供自己的电源(即LED - 3V和传感器 - 5v)。每个引脚只能输出足够的功率来阻止对电路板的损坏。 LED可能正在从传感器获取电力,传感器可能没有足够的电力来正常工作。
答案 1 :(得分:0)
Your circuit diagram shows a short-circut over the (+) and (-) pins (legs) of the DS18B20 - that short horizontal wire below the resisitor. The value -127.0 indicates there is a problem with your temperature sensor too.
So if that's an actual problem (not just in the diagram), remove that wire to fix it. Also, in your setup(), you may want to add some kind of check that your sensors are A-OK before looping:
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2 // Arduino pin D2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
setup()
{
Serial.begin(115200);
Serial.println("setup() runs ...");
sensors.begin();
if (sensors.getDeviceCount() < 1)
{
Serial.println("DS18B20 Error - No sensors found");
}
}
// ... rest of code