Advertisement
pleasedontcode

"Display Sensor" rev_02

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