Skip to content

Commit 79c6fad

Browse files
committed
Sample code for retrieving temp from DS18S20
1 parent 219363c commit 79c6fad

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

ds18s20/ds18s20.ino

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <OneWire.h>
2+
#include <DallasTemperature.h>
3+
4+
#include <ThreeWire.h>
5+
#include <dht.h>
6+
7+
// Data wire is plugged into digital pin 7 on the Arduino
8+
#define ONE_WIRE_BUS 7
9+
10+
// Setup a oneWire instance to communicate with any OneWire device
11+
OneWire oneWire(ONE_WIRE_BUS);
12+
13+
// Pass oneWire reference to DallasTemperature library
14+
DallasTemperature sensors(&oneWire);
15+
16+
dht DHT;
17+
#define DHT11_PIN 2
18+
int deviceCount = 0;
19+
20+
void setup() {
21+
Serial.begin(9600);
22+
Serial.println("Hello dS18S20");
23+
sensors.begin(); // Start up the library
24+
// sensors.setResolution(10);
25+
Serial.println("Starting");
26+
27+
Serial.print("Locating devices...");
28+
Serial.print("Found ");
29+
deviceCount = sensors.getDeviceCount();
30+
Serial.print(deviceCount, DEC);
31+
Serial.println(" devices.");
32+
}
33+
34+
35+
void loop() {
36+
// Send command to all the sensors for temperature conversion
37+
sensors.requestTemperatures();
38+
39+
// Display temperature from each sensor
40+
for (int i = 0; i < deviceCount; i++)
41+
{
42+
Serial.print("Sensor ");
43+
Serial.print(i + 1);
44+
Serial.print(" : ");
45+
float tempC = sensors.getTempCByIndex(i);
46+
Serial.print(tempC);
47+
Serial.print("C | ");
48+
Serial.print(DallasTemperature::toFahrenheit(tempC));
49+
Serial.println("F");
50+
}
51+
//print the temperature in Celsius
52+
// Serial.print("DS18 Temperature: ");
53+
// Serial.print(sensors.getTempCByIndex(0));
54+
// Serial.print("C | ");
55+
//
56+
// //print the temperature in Fahrenheit
57+
// Serial.print((sensors.getTempCByIndex(0) * 9.0) / 5.0 + 32.0);
58+
// Serial.println("F");
59+
60+
int chk = DHT.read11(DHT11_PIN);
61+
double c = DHT.temperature;
62+
if (c == -999) {
63+
Serial.println("Waiting for sensor");
64+
delay(1000);
65+
return;
66+
}
67+
Serial.print("DHT11 Temperature: ");
68+
Serial.print(c);
69+
Serial.print("C | ");
70+
double f = (c * 9.0 / 5.0) + 32.0;
71+
Serial.print(f);
72+
Serial.println("F");
73+
74+
Serial.print("Relative humidity ");
75+
double rh = DHT.humidity;
76+
Serial.println(rh);
77+
78+
delay(5000);
79+
}

0 commit comments

Comments
 (0)