Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* 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: **Sensor Readings**
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2025-03-08 03:33:11
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* The project shall utilize connected components to */
- /* read sensor data and trigger outputs, ensuring */
- /* seamless communication and functionality within */
- /* the system. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Wire.h> // Include Wire library for I2C communication
- #include <DHT.h> // Include DHT library for temperature and humidity sensor
- #include <Adafruit_Sensor.h> // Include Adafruit Sensor library for sensor abstraction
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- // Define constants for DHT sensor
- #define DHTPIN 4 // Pin where the DHT sensor is connected
- #define DHTTYPE DHT11 // DHT 11 sensor type
- // Create DHT object
- DHT dht(DHTPIN, DHTTYPE);
- // Variables to store sensor data
- float temperature;
- float humidity;
- void setup(void)
- {
- // Initialize serial communication
- Serial.begin(115200);
- // Initialize DHT sensor
- dht.begin();
- }
- void loop(void)
- {
- // Wait a few seconds between measurements
- delay(2000);
- // Read temperature as Celsius
- temperature = dht.readTemperature();
- // Read humidity
- humidity = dht.readHumidity();
- // Check if any reads failed and exit early (to try again).
- if (isnan(temperature) || isnan(humidity)) {
- Serial.println("Failed to read from DHT sensor!");
- return;
- }
- // Print the results to the Serial Monitor
- Serial.print("Temperature: ");
- Serial.print(temperature);
- Serial.print(" °C, Humidity: ");
- Serial.print(humidity);
- Serial.println(" %");
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement