Advertisement
pleasedontcode

"Humidity Display" rev_01

Jun 1st, 2024
594
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: "Humidity Display"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-06-01 11:04:48
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Build a project with a humidity sensor and LCD */
  21.     /* screen. Get the data from the humidity sensor and */
  22.     /* display it on the LCD monitor as follows:    If */
  23.     /* the humidity is 25%, a symbol of 1 drop should */
  24.     /* appear on the screen, if 50% - a symbol of two */
  25.     /* drops,if 75 */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <Wire.h>
  30. #include <LiquidCrystal_I2C.h>  //https://github.com/marcoschwartz/LiquidCrystal_I2C
  31. #include <DHT.h>  //https://github.com/adafruit/DHT-sensor-library
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36. void displayHumidityOnLCD(float humidity);
  37.  
  38. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  39. const uint8_t teni_DHT11_DOUT_PIN_D2 = 2;
  40.  
  41. /***** DEFINITION OF I2C PINS *****/
  42. const uint8_t lcd_LCD1602I2C_I2C_PIN_SDA_A4 = A4;
  43. const uint8_t lcd_LCD1602I2C_I2C_PIN_SCL_A5 = A5;
  44. const uint8_t lcd_LCD1602I2C_I2C_SLAVE_ADDRESS = 0x27;  // Corrected to 0x27 based on the example
  45.  
  46. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  47. LiquidCrystal_I2C lcd(lcd_LCD1602I2C_I2C_SLAVE_ADDRESS, 20, 4);  // Initialize the LCD with address and dimensions
  48.  
  49. #define DHTPIN 2     // Pin where the DHT sensor is connected
  50. #define DHTTYPE DHT22   // DHT 22 (AM2302)
  51.  
  52. DHT dht(DHTPIN, DHTTYPE);  // Initialize DHT sensor
  53.  
  54. void setup(void)
  55. {
  56.   // put your setup code here, to run once:
  57.  
  58.   pinMode(teni_DHT11_DOUT_PIN_D2, INPUT_PULLUP);
  59.  
  60.   lcd.init();  // Initialize the LCD
  61.   lcd.backlight();  // Turn on the backlight
  62.  
  63.   // Create custom characters for humidity levels
  64.   uint8_t drop1[8] = {0x04, 0x0A, 0x0A, 0x11, 0x11, 0x11, 0x0E, 0x00}; // 1 drop
  65.   uint8_t drop2[8] = {0x04, 0x0A, 0x0A, 0x11, 0x11, 0x1F, 0x0E, 0x00}; // 2 drops
  66.   uint8_t drop3[8] = {0x04, 0x0A, 0x0A, 0x11, 0x1F, 0x1F, 0x0E, 0x00}; // 3 drops
  67.  
  68.   lcd.createChar(0, drop1);
  69.   lcd.createChar(1, drop2);
  70.   lcd.createChar(2, drop3);
  71.  
  72.   lcd.home();
  73.  
  74.   Serial.begin(9600);
  75.   Serial.println(F("DHTxx test!"));
  76.  
  77.   dht.begin();  // Initialize the DHT sensor
  78. }
  79.  
  80. void loop(void)
  81. {
  82.   // put your main code here, to run repeatedly:
  83.   delay(2000);
  84.  
  85.   float h = dht.readHumidity();
  86.   float t = dht.readTemperature();
  87.   float f = dht.readTemperature(true);
  88.  
  89.   if (isnan(h) || isnan(t) || isnan(f)) {
  90.     Serial.println(F("Failed to read from DHT sensor!"));
  91.     return;
  92.   }
  93.  
  94.   float hif = dht.computeHeatIndex(f, h);
  95.   float hic = dht.computeHeatIndex(t, h, false);
  96.  
  97.   Serial.print(F("Humidity: "));
  98.   Serial.print(h);
  99.   Serial.print(F("%  Temperature: "));
  100.   Serial.print(t);
  101.   Serial.print(F("°C "));
  102.   Serial.print(f);
  103.   Serial.print(F("°F  Heat index: "));
  104.   Serial.print(hic);
  105.   Serial.print(F("°C "));
  106.   Serial.print(hif);
  107.   Serial.println(F("°F"));
  108.  
  109.   displayHumidityOnLCD(h);
  110. }
  111.  
  112. void displayHumidityOnLCD(float humidity)
  113. {
  114.   lcd.clear();
  115.   lcd.setCursor(0, 0);
  116.   lcd.print("Humidity: ");
  117.   lcd.print(humidity);
  118.   lcd.print("%");
  119.  
  120.   lcd.setCursor(0, 1);
  121.   if (humidity >= 75) {
  122.     lcd.write(byte(2)); // 3 drops
  123.   } else if (humidity >= 50) {
  124.     lcd.write(byte(1)); // 2 drops
  125.   } else if (humidity >= 25) {
  126.     lcd.write(byte(0)); // 1 drop
  127.   } else {
  128.     lcd.print("Low");
  129.   }
  130. }
  131.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement