我目前正在尝试(几个小时)在我的 Adafruit LEDBackpack 上显示从 DS18B20 获得的温度。但是当我尝试在设置(matrix.begin(0x070))中初始化显示时,传感器返回的温度始终为-127。
你能帮我理解我做错了什么吗?
用例
- 只有温度传感器:温度正确
- 只有屏幕:屏幕按预期工作
- 两者:屏幕工作并显示预期但返回的温度始终为-127。
成分:
- Adafruit LEDBackpack 使用 I2C,因此它连接到 SCL、SDA、5v、GND
- 温度传感器是 DS18B20(1-Wire 总线)。它连接到 D#2, 5v, GND
代码
#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);
}