Advertisement
pleasedontcode

**Sensor Reading** rev_01

Nov 17th, 2024
80
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 Reading**
  13.     - Source Code NOT compiled for: ESP8266 NodeMCU V1.0
  14.     - Source Code created on: 2024-11-18 00:51:26
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The project aims to develop an Arduino-based */
  21.     /* system utilizing specific libraries for sensor */
  22.     /* integration and data processing, ensuring */
  23.     /* efficient communication and control of connected */
  24.     /* components. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /* START CODE */
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <Wire.h>
  31. #include <SPI.h>
  32. #include <Adafruit_Sensor.h>
  33. #include <DHT.h>
  34.  
  35. /****** FUNCTION PROTOTYPES *****/
  36. void setup(void);
  37. void loop(void);
  38.  
  39. /****** GLOBAL VARIABLES *****/
  40. DHT dht(2, DHT22); // Initialize DHT sensor on pin 2 with DHT22 model
  41.  
  42. void setup(void)
  43. {
  44.     // Initialize serial communication for debugging
  45.     Serial.begin(9600);
  46.    
  47.     // Initialize DHT sensor
  48.     dht.begin();
  49. }
  50.  
  51. void loop(void)
  52. {
  53.     // Read temperature and humidity values from DHT sensor
  54.     float humidity = dht.readHumidity();
  55.     float temperature = dht.readTemperature();
  56.  
  57.     // Check if any reads failed and exit early (to try again).
  58.     if (isnan(humidity) || isnan(temperature)) {
  59.         Serial.println("Failed to read from DHT sensor!");
  60.         return;
  61.     }
  62.  
  63.     // Print the results to the Serial Monitor
  64.     Serial.print("Humidity: ");
  65.     Serial.print(humidity);
  66.     Serial.print(" %\t");
  67.     Serial.print("Temperature: ");
  68.     Serial.print(temperature);
  69.     Serial.println(" *C");
  70.  
  71.     // Wait a few seconds between measurements
  72.     delay(2000);
  73. }
  74.  
  75. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement