Advertisement
pleasedontcode

Temperature Control rev_01

Feb 14th, 2024
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Temperature Control
  13.     - Source Code compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-02-14 14:05:25
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* if user selects heat mode , turn on rely which */
  21.     /* turns on peltier. Maintain temperature at 60 deg */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include <DHT.h>
  26.  
  27. /****** FUNCTION PROTOTYPES *****/
  28. void setup(void);
  29. void loop(void);
  30.  
  31. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  32. const uint8_t Temp_DHT11_DOUT_PIN_D4 = 4;
  33. const uint8_t HEAT_RELAY_PIN = 5; // Pin number for heat relay control
  34.  
  35. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  36. DHT dht(Temp_DHT11_DOUT_PIN_D4, DHT11);
  37.  
  38. /****** SYSTEM REQUIREMENTS *****/
  39. const float targetTemperature = 60.0; // Target temperature in degrees Celsius
  40.  
  41. void setup(void)
  42. {
  43.   // put your setup code here, to run once:
  44.   pinMode(Temp_DHT11_DOUT_PIN_D4, INPUT_PULLUP);
  45.   pinMode(HEAT_RELAY_PIN, OUTPUT);
  46.   digitalWrite(HEAT_RELAY_PIN, LOW); // Start with heat relay off
  47.   dht.begin();
  48. }
  49.  
  50. void loop(void)
  51. {
  52.   // put your main code here, to run repeatedly:
  53.   float temperature = dht.readTemperature();
  54.  
  55.   // Check if read failed and exit early (to try again).
  56.   if (isnan(temperature))
  57.   {
  58.     Serial.println("Failed to read temperature from DHT sensor!");
  59.     return;
  60.   }
  61.  
  62.   // Print temperature value.
  63.   Serial.print("Temperature: ");
  64.   Serial.print(temperature);
  65.   Serial.print(" °C\t");
  66.  
  67.   // Check if temperature is below target temperature and turn on relay if true.
  68.   if (temperature < targetTemperature)
  69.   {
  70.     digitalWrite(HEAT_RELAY_PIN, HIGH);
  71.   }
  72.   else
  73.   {
  74.     digitalWrite(HEAT_RELAY_PIN, LOW);
  75.   }
  76.  
  77.   delay(2000); // Wait for 2 seconds before next reading.
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement