-
Notifications
You must be signed in to change notification settings - Fork 0
/
DS18B20_Get_Temp_without_Delay.ino
82 lines (68 loc) · 2.31 KB
/
DS18B20_Get_Temp_without_Delay.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <OneWire.h> // OneWire library to make the OneWire bus usable
#include <DallasTemperature.h> // DS18B20 library for the DS18B20 temperature sensor
#define TEMPERATURE_SENSOR_PIN 11
#define MEASUREMENT_DELAY_MS 1000
#define TEMP_SENSOR_RESOLUTION 12
#define DEBUG_MODE_IS_ACTIVE true
OneWire oneWireObject(TEMPERATURE_SENSOR_PIN); // Create OneWire object
DallasTemperature tempSensor(&oneWireObject); // Declare temperature sensor
DeviceAddress addressTempSensor; // Sensor address is ascertained in findAndSetupTempSensor()
unsigned long lastRequestTime = 0;
// Ueberpruefe ob Sensoren vorhanden sind
void findAndSetupTempSensor()
{
// Initialize temperature sensor
tempSensor.begin();
tempSensor.getAddress(addressTempSensor, 0);
tempSensor.setResolution(addressTempSensor, TEMP_SENSOR_RESOLUTION);
tempSensor.setWaitForConversion(false); // Enables temperature polling without blocking
tempSensor.requestTemperatures();
delay(10); // A short delay is recommended after requesting the current temperature
bool tempSensorFound = false;
// Check if temperature sensor can be found
if (tempSensor.getDeviceCount() == 1)
{
tempSensorFound = true;
}
else
{
tempSensorFound = false;
}
if (DEBUG_MODE_IS_ACTIVE)
{
Serial.print("Temperature sensor found? ");
Serial.println(tempSensorFound ? "YES" : "NO");
}
}
float getTemperature()
{
// ATTENTION!
// A 4.7k resistor must be installed between 5V and the sense wire (pull-up resistor).
float temperature = tempSensor.getTempCByIndex(0); // The index must be changed if there are more than one sensor on the same wire/input pin.
// Tell the sensor to make a temperature conversion. This may happen a bit later.
tempSensor.requestTemperatures();
return temperature;
}
void setup() {
if (DEBUG_MODE_IS_ACTIVE)
{
Serial.begin(9600);
}
findAndSetupTempSensor();
lastRequestTime = millis();
}
void loop() {
// Check if enough time has passed since last temperature request
if (millis() >= (lastRequestTime + MEASUREMENT_DELAY_MS))
{
lastRequestTime = millis();
float currentTemp = getTemperature();
if (DEBUG_MODE_IS_ACTIVE)
{
Serial.print(currentTemp);
Serial.print(" C - (");
Serial.print(millis());
Serial.println(" ms)");
}
}
}