-
Notifications
You must be signed in to change notification settings - Fork 8
/
ReadPlot.ino
45 lines (38 loc) · 1.48 KB
/
ReadPlot.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* DHT11 Sensor Serial Plotter Reader
* This sketch reads temperature and humidity data from the DHT11 sensor and prints the values to the Arduino IDE serial plotter.
*
* Author: Dhruba Saha
* Version: 2.1.0
* License: MIT
*/
// Include the DHT11 library for interfacing with the sensor.
#include <DHT11.h>
// Create an instance of the DHT11 class.
// - For Arduino: Connect the sensor to Digital I/O Pin 2.
// - For ESP32: Connect the sensor to pin GPIO2 or P2.
// - For ESP8266: Connect the sensor to GPIO2 or D4.
DHT11 dht11(2);
void setup() {
// Initialize serial communication to allow debugging and data readout.
// Using a baud rate of 9600 bps.
Serial.begin(9600);
// Uncomment the line below to set a custom delay between sensor readings (in milliseconds).
// dht11.setDelay(500); // Set this to the desired delay. Default is 500ms.
}
void loop() {
int temperature, humidity;
// Attempt to read the temperature and humidity values from the DHT11 sensor.
int result = dht11.readTemperatureHumidity(temperature, humidity);
// Check the results of the readings.
// If the reading is successful, print the temperature and humidity values in CSV format.
if (result == 0) {
Serial.print("Temperature:");
Serial.print(temperature);
Serial.print(",Humidity:");
Serial.println(humidity);
} else {
// Print error message based on the error code.
Serial.println(DHT11::getErrorString(result));
}
}