-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino_ds18b20.ino
67 lines (52 loc) · 1.7 KB
/
arduino_ds18b20.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
/*
Arduino DS18B20
Created: 04/20/2016
By Gus
Modified N/A
By Gus
https://pimylifeup.com/arduino-ds18b20-temperature-sensor/
*/
#include <OneWire.h>
#include <DallasTemperature.h>
int greenLedPin = 2; // Pin Green LED is connected to
int yellowLedPin = 3; // Pin Yellow LED is connected to
int redLedPin = 4; // Pin Red LED is connected to
int temp_sensor = 5; // Pin DS18B20 Sensor is connected to
float temperature = 0; //Variable to store the temperature in
int lowerLimit = 15; //define the lower threshold for the temperature
int upperLimit = 35; //define the upper threshold for the temperature
OneWire oneWirePin(temp_sensor);
DallasTemperature sensors(&oneWirePin);
void setup(void){
Serial.begin(9600);
//Setup the LEDS to act as outputs
pinMode(redLedPin,OUTPUT);
pinMode(greenLedPin,OUTPUT);
pinMode(yellowLedPin,OUTPUT);
sensors.begin();
}
void loop(){
Serial.print("Requesting Temperatures from sensors: ");
sensors.requestTemperatures();
Serial.println("DONE");
temperature = sensors.getTempCByIndex(0);
digitalWrite(redLedPin, LOW);
digitalWrite(greenLedPin, LOW);
digitalWrite(yellowLedPin, LOW);
Serial.print("Temperature is ");
Serial.print(temperature);
//Setup the LEDS to act as outputs
if(temperature <= lowerLimit){
Serial.println(", Yellow LED is Activated");
digitalWrite(yellowLedPin, HIGH);
}
else if(temperature > lowerLimit && temperature < upperLimit){
Serial.println(", Green LED is Activated");
digitalWrite(greenLedPin, HIGH);
}
else if(temperature >= upperLimit){
Serial.println(", Red LED is Activated");
digitalWrite(redLedPin, HIGH);
}
delay(500);
}