Guest User

Untitled

a guest
Oct 23rd, 2020
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. /* DS18B20 1-Wire digital temperature sensor with Arduino example code. More info: https://www.makerguides.com */
  2. // Include the required Arduino libraries:
  3. #include <OneWire.h>
  4. #include <DallasTemperature.h>
  5. // Define to which pin of the Arduino the 1-Wire bus is connected:
  6. #define ONE_WIRE_BUS 2
  7. // Create a new instance of the oneWire class to communicate with any OneWire device:
  8. OneWire oneWire(ONE_WIRE_BUS);
  9. // Pass the oneWire reference to DallasTemperature library:
  10.  
  11. //Relay Pin Variable
  12. int relayPin = 3; // Relay Singnal lead to Digital Pin [D2-D12] (Elegoo Nano v.3)
  13. float maxTemperature = 26.00; // The Max Temperature allowed
  14. float minTemperature = 25.00; // The Min Temperature allowed
  15.  
  16.  
  17. DallasTemperature sensors(&oneWire);
  18. void setup() {
  19. // Begin serial communication at a baud rate of 9600:
  20. Serial.begin(9600);
  21. // Start up the library:
  22. sensors.begin();
  23. pinMode(relayPin, OUTPUT);
  24. }
  25. void loop() {
  26.  
  27. if(sensors.getTempCByIndex(0) >= maxTemperature){
  28. powerOnRelay();
  29. } else if (sensors.getTempCByIndex(0) <= minTemperature) {
  30. powerOffRelay();
  31. }
  32. // Send the command for all devices on the bus to perform a temperature conversion:
  33. sensors.requestTemperatures();
  34. // Fetch the temperature in degrees Celsius for device index:
  35. float tempC = sensors.getTempCByIndex(0); // the index 0 refers to the first device
  36. // Fetch the temperature in degrees Fahrenheit for device index:
  37. float tempF = sensors.getTempFByIndex(0);
  38. // Print the temperature in Celsius in the Serial Monitor:
  39. Serial.print("Temperature: ");
  40. Serial.print(tempC);
  41. Serial.print(" \xC2\xB0"); // shows degree symbol
  42. Serial.print("C | ");
  43. // Print the temperature in Fahrenheit
  44. Serial.print(tempF);
  45. Serial.print(" \xC2\xB0"); // shows degree symbol
  46. Serial.println("F");
  47. // Wait 1 second:
  48. // float temperature = tempC();
  49. delay(1000);
  50. }
  51. void powerOffRelay() {
  52. digitalWrite(relayPin, LOW);
  53. Serial.println("Relay Off");
  54. }
  55. void powerOnRelay() {
  56. digitalWrite(relayPin, HIGH);
  57. Serial.println("Relay On");
  58. }
Add Comment
Please, Sign In to add comment