Hi,
I been tinkering with DS18B20 for sometime.
There is unique problem with input. It get accurate temperature until power pin get disrupted for some reason.
Its the same if I hotswap the sensor unit while board is running.
Then it will read a random temperature until I run this code from hackatronics.
any ideas?
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into pin 10 on the Arduino
#define ONE_WIRE_BUS 10
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// Assign the unique addresses of your 1-Wire temp sensors.
// See the tutorial on how to obtain these addresses:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html
DeviceAddress insideThermometer = {0x28, 0x3A, 0x9B, 0x45, 0x92, 0x07, 0x02, 0x2F};
//DeviceAddress insideThermometer = {0x28, 0xD6, 0x39, 0x45, 0x92, 0x0A, 0x02, 0x42};// SOLDERED
void setup(void)
{
// start serial port
Serial.begin(9600);
// Start up the library
sensors.begin();
// set the resolution to 10 bit (good enough?)
sensors.setResolution(insideThermometer, 10);
}
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00) {
Serial.print("Error getting temperature");
} else {
Serial.print("C: ");
Serial.print(tempC);
Serial.print(" F: ");
Serial.print(DallasTemperature::toFahrenheit(tempC));
}
}
void loop(void)
{
delay(2000);
Serial.print("Getting temperatures...\n\r");
sensors.requestTemperatures();
Serial.print("Inside temperature is: ");
printTemperature(insideThermometer);
Serial.print("\n\r");
}
Hi Terry.
My code is getting data from outside sensor DS18B20 and compare with humidity to turn a relay on and off. Here is the full code
#include <DHT.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// DS18B20 Data wire is plugged into pin 10 on the Arduino
#define ONE_WIRE_BUS 10
#define DHTPIN A0 // AM2302 is plugged into pin 4
#define DHTTYPE DHT11
#define Relay 3 // Pin to Activate relay
#define LED 13 // Pin to show relay is on (blue)
#define DEVICE_DISCONNECTED -127
#define DEADBAND 3
DHT dht(DHTPIN, DHTTYPE);
// 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);//https://www.hacktronics.com/Tutorials/arduino-1-wire-tutorial.html
DeviceAddress outsideThermometer = {0x28, 0x3A, 0x9B, 0x45, 0x92, 0x07, 0x02, 0x2F};
int setPoint;
void setup() {
delay(1000);
Serial.begin(9600); //Begin serial communication
sensors.begin();
sensors.setResolution(outsideThermometer, 12);
dht.begin();
pinMode(LED, OUTPUT);
pinMode(Relay, OUTPUT);
digitalWrite(Relay, HIGH);//to keep relay off initially
Serial.println("Arduino Automatic Humidistat "); //Print a message
}
void loop() {
delay(5000); // Wait 5 seconds between measurements.
float tempOut = sensors.getTempC(outsideThermometer);
float h = dht.readHumidity();
float t = dht.readTemperature();
float tt = dht.convertCtoF(t);
//float tempOut; //create a variable to hold the temperature value
//sensors.requestTemperatures();
//tempOut = (sensors.getTempFByIndex(0));
float F = DallasTemperature::toFahrenheit(tempOut);
//float F = (tempOut * 18 + 5) / 10 + 32; //https://forum.arduino.cc/index.php?topic=139006.0
// Check if any reads failed and exit early (to try again).
if (isnan(h) && (tempOut <= -54)) {
Serial.println("Fault: NO SENSORS");
display.display();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 2);
display.print("Fault: NO SENSORS");
for (int x = 0; x < 10; x++) {
digitalWrite(LED, HIGH);
delay(500);
digitalWrite(LED, LOW);
delay(100);
}
return;
}
else if (isnan(h)) {
Serial.println("Failed to read from DHT sensor!");
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(27, 30);
display.print("Fault: DHT Sensor");
for (int x = 0; x < 5; x++) {
digitalWrite(LED, HIGH);
delay(1000);
digitalWrite(LED, LOW);
delay(500);
}
}
if (tempOut == -127.00) { //lower threshold FOR TEMP for DS18B20 IS -55C
Serial.println("Failed to read from OUTDOOR sensor!");
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(27, 30);
display.print("Fault: DS18B20");
// If the sensor fails to read successfully execute for loop
// and flash the RELAY LED for 10 times
// a total of 15 seconds
for (int x = 0; x < 10; x++) {
digitalWrite(LED, HIGH);
delay(1000);
digitalWrite(LED, LOW);
delay(500);
}
return;
}
// Read DS18B20 and convert to humidity values between 25 and 50%
// setPoint = the set point for the target humidity level
{
if (F <= -20) {
setPoint = 20;
}
else if (F > -20 && F <= -10) {
setPoint = 25;
}
else if (F > -10 && F <= 0) {
setPoint = 30;
}
else if (F > 0 && F <= 10) {
setPoint = 35;
}
else if (F > 10 && F <= 20) {
setPoint = 40;
}
else if (F > 20 && F <= 40) {
setPoint = 45;
}
else if (F > 40 && F <= 60) {
setPoint = 50;
}
else {
setPoint = 55;
}
Serial.print ("Humidity Set Point is: ");
Serial.println (setPoint);
Serial.print("Humidity: ");
Serial.print(h);
Serial.println("%");
Serial.print("Temperature Outside C: ");
Serial.print(tempOut);
Serial.print(" degC ");
Serial.print("Temperature Outside F: ");
Serial.print(F);
Serial.println(" degF ");
Serial.print("Temperature inside C: ");
Serial.print(t);
Serial.print(" degC ");
Serial.print("Temperature inside F: ");
Serial.print(tt);
Serial.println(" degF ");
// Test to see if the humidity is less than the set point
// if so turn on relay
// If not turn off relay //[url=https://forum.arduino.cc/index.php?topic=515908.15]https://forum.arduino.cc/index.php?topic=515908.15[/url]
if (h > setPoint + DEADBAND) {
digitalWrite(Relay, HIGH);
digitalWrite(LED, LOW);
Serial.println ("Humidifier OFF");
}
else if (h < setPoint - DEADBAND) {
digitalWrite(Relay, LOW);
digitalWrite(LED, HIGH);
Serial.println ("Humidifier ON");
delay(12000);//00
}
else {
//do nothing
}
Serial.println(" ------------------");
}
}
I don't THINK so. The request temperature methods all ask the device for a new conversion. Whenever you are not using this method, but still reading the sensor you never get recent temperatures. And if the device hows powered off it will not have a current temperature value..