CCCPaste

"Display Readings" rev_01

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: "Display Readings"
	- Source Code NOT compiled for: Arduino Nano
	- Source Code created on: 2024-05-21 17:08:26

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Monitor and display temperature and humidity data */
	/* using a DHT11 sensor connected to an Arduino. The */
	/* sensor is connected to digital pin 2 (D2). The */
	/* system should initialize the sensor and read data */
	/* continuously. */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 
#include   //https://github.com/adafruit/DHT-sensor-library
#include   //https://github.com/marcoschwartz/LiquidCrystal_I2C

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t TempSensor_DHT11_DOUT_PIN_D2 = 2;  // DHT11 sensor connected to digital pin 2 (D2)

/***** DEFINITION OF I2C PINS *****/
const uint8_t myDisplay_LCD1602I2C_I2C_PIN_SDA_A4 = A4;  // I2C SDA pin
const uint8_t myDisplay_LCD1602I2C_I2C_PIN_SCL_A5 = A5;  // I2C SCL pin
const uint8_t myDisplay_LCD1602I2C_I2C_SLAVE_ADDRESS = 0x27;  // I2C address for the LCD

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
DHT dht(TempSensor_DHT11_DOUT_PIN_D2, DHT11);  // Initialize DHT sensor for DHT11
LiquidCrystal_I2C lcd(myDisplay_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2);  // Initialize LCD with address 0x27 for 16 chars and 2 lines

void setup(void)
{
  // Initialize serial communication at 9600 bits per second
  Serial.begin(9600);
  Serial.println(F("DHTxx test!"));

  // Set the DHT11 sensor pin as input with pull-up resistor
  pinMode(TempSensor_DHT11_DOUT_PIN_D2, INPUT_PULLUP);
  dht.begin();  // Initialize the DHT sensor

  // Initialize the LCD
  lcd.init();  
  lcd.backlight();  // Turn on the backlight
}

void loop(void)
{
  // Wait a few seconds between measurements
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  float h = dht.readHumidity();  // Read humidity
  float t = dht.readTemperature();  // Read temperature in Celsius
  float f = dht.readTemperature(true);  // Read temperature in Fahrenheit

  // Check if any reads failed and exit early (to try again)
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Sensor error!");
    return;
  }

  // Compute heat index in Fahrenheit and Celsius
  float hif = dht.computeHeatIndex(f, h);
  float hic = dht.computeHeatIndex(t, h, false);

  // Print the results to the Serial Monitor
  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));
  Serial.print(t);
  Serial.print(F("°C "));
  Serial.print(f);
  Serial.print(F("°F  Heat index: "));
  Serial.print(hic);
  Serial.print(F("°C "));
  Serial.print(hif);
  Serial.println(F("°F"));

  // Display the results on the LCD
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Humidity: ");
  lcd.print(h);
  lcd.print("%");
  lcd.setCursor(0, 1);
  lcd.print("Temp: ");
  lcd.print(t);
  lcd.print("C ");
  lcd.print(f);
  lcd.print("F");
}

/* END CODE */