Advertisement
pleasedontcode

**Sensor Readings** rev_01

Mar 7th, 2025
303
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: **Sensor Readings**
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-03-08 03:33:11
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The project shall utilize connected components to */
  21.     /* read sensor data and trigger outputs, ensuring */
  22.     /* seamless communication and functionality within */
  23.     /* the system. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /* START CODE */
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <Wire.h>               // Include Wire library for I2C communication
  30. #include <DHT.h>                // Include DHT library for temperature and humidity sensor
  31. #include <Adafruit_Sensor.h>    // Include Adafruit Sensor library for sensor abstraction
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36.  
  37. // Define constants for DHT sensor
  38. #define DHTPIN 4                // Pin where the DHT sensor is connected
  39. #define DHTTYPE DHT11           // DHT 11 sensor type
  40.  
  41. // Create DHT object
  42. DHT dht(DHTPIN, DHTTYPE);
  43.  
  44. // Variables to store sensor data
  45. float temperature;
  46. float humidity;
  47.  
  48. void setup(void)
  49. {
  50.     // Initialize serial communication
  51.     Serial.begin(115200);
  52.    
  53.     // Initialize DHT sensor
  54.     dht.begin();
  55. }
  56.  
  57. void loop(void)
  58. {
  59.     // Wait a few seconds between measurements
  60.     delay(2000);
  61.    
  62.     // Read temperature as Celsius
  63.     temperature = dht.readTemperature();
  64.    
  65.     // Read humidity
  66.     humidity = dht.readHumidity();
  67.    
  68.     // Check if any reads failed and exit early (to try again).
  69.     if (isnan(temperature) || isnan(humidity)) {
  70.         Serial.println("Failed to read from DHT sensor!");
  71.         return;
  72.     }
  73.  
  74.     // Print the results to the Serial Monitor
  75.     Serial.print("Temperature: ");
  76.     Serial.print(temperature);
  77.     Serial.print(" °C, Humidity: ");
  78.     Serial.print(humidity);
  79.     Serial.println(" %");
  80. }
  81.  
  82. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement