Advertisement
pleasedontcode

Sensor Readings rev_01

Jan 9th, 2024
99
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 compiled for: Arduino Uno
  14.     - Source Code created on: 2024-01-09 08:50:10
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Display in serial monitor the values */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /****** DEFINITION OF LIBRARIES *****/
  24. #include <Arduino.h>
  25. #include <DHT.h>
  26.  
  27. /****** SYSTEM REQUIREMENTS *****/
  28. /****** SYSTEM REQUIREMENT 1 *****/
  29. /* Display the values in the serial monitor */
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34.  
  35. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  36. const uint8_t DHTPIN = 2;
  37.  
  38. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  39. DHT dht(DHTPIN, DHT22);
  40.  
  41. void setup(void)
  42. {
  43.   // Initialize serial communication
  44.   Serial.begin(9600);
  45.   Serial.println(F("DHTxx test!"));
  46.  
  47.   // Set the pin mode for DHT sensor
  48.   pinMode(DHTPIN, INPUT_PULLUP);
  49.  
  50.   // Initialize the DHT sensor
  51.   dht.begin();
  52. }
  53.  
  54. void loop(void)
  55. {
  56.   // Read the humidity and temperature values from DHT sensor
  57.   float h = dht.readHumidity();
  58.   float t = dht.readTemperature();
  59.  
  60.   if (isnan(h) || isnan(t))
  61.   {
  62.     Serial.println(F("Failed to read from DHT sensor!"));
  63.     return;
  64.   }
  65.  
  66.   // Display the humidity and temperature values in the serial monitor
  67.   Serial.print(F("Humidity: "));
  68.   Serial.print(h);
  69.   Serial.print(F("%\tTemperature: "));
  70.   Serial.print(t);
  71.   Serial.print(F("°C"));
  72.   Serial.println();
  73.  
  74.   // Delay for 2 seconds before reading again
  75.   delay(2000);
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement