Advertisement
Ogomegbunam

Fan speed temperature regulator

Oct 21st, 2023 (edited)
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1.  
  2.  
  3. #include <LiquidCrystal.h>
  4.  
  5. // Initialize the LCD display
  6. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  7.  
  8. // Define the fan and temperature sensor pin numbers
  9. const int fanPin = 9;
  10. const int tempPin = A1;
  11.  
  12. void setup() {
  13. lcd.begin(16, 2); // Initialize the LCD with 16x2 characters
  14. pinMode(fanPin, OUTPUT);
  15. pinMode(tempPin, INPUT);
  16.  
  17. Serial.begin(9600);
  18.  
  19. digitalWrite(fanPin, LOW); // Start with the fan off
  20. lcd.print("Fan Status: OFF");
  21. lcd.setCursor(0, 1);
  22. lcd.print("LOADING ...");
  23. for (int i = 0; i < 16; i++) {
  24. delay(1500);
  25. lcd.scrollDisplayLeft();
  26. }
  27. lcd.clear();
  28. lcd.setCursor(0, 0);
  29. lcd.print("GROUP 6");
  30.  
  31. }
  32.  
  33. void loop() {
  34. lcd.setCursor(0, 0);
  35.  
  36. Serial.print("ITS WORKING");
  37.  
  38. // Read temperature from the sensor
  39. int temperature = analogRead(tempPin);
  40. float voltage = temperature * (5.0 / 1023.0);
  41. float celsius = (voltage) * 100.0;
  42.  
  43. // Adjust the fan speed based on the temperature
  44. int fanSpeed = 0; // Default fan speed
  45.  
  46. if (celsius <= 28.0) {
  47. fanSpeed = 0; // Fan off
  48. } else if (celsius <= 30.0) {
  49. fanSpeed = 64; // 25% fan speed (out of 255)
  50. } else if (celsius <= 32.0) {
  51. fanSpeed = 128; // 50% fan speed (out of 255)
  52. } else if (celsius <= 34.0) {
  53. fanSpeed = 192; // 75% fan speed (out of 255)
  54. } else {
  55. fanSpeed = 255; // 100% fan speed (out of 255)
  56. }
  57.  
  58. analogWrite(fanPin, fanSpeed);
  59.  
  60. // Display the temperature at the top row
  61. lcd.print("Temperature: ");
  62. lcd.print(celsius);
  63. lcd.print(" C");
  64.  
  65. // Display the fan speed at the bottom row
  66. lcd.setCursor(0, 1);
  67. lcd.print("Fan Speed: ");
  68. lcd.print(map(fanSpeed, 0, 255, 0, 100));
  69. lcd.print("%");
  70.  
  71. delay(500); // Update the status every 1 second
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement