// ============================================================================
// PROJECT: Temperature & Humidity Experiment in 5°C Steps
// PURPOSE: Generate clean data pairs for graphing in Microsoft Excel
// BOARD: ESP32 DevKit V1 | SENSOR: DHT22
// ============================================================================
#include "DHT.h"
#define DHTPIN 4 // Data pin connected to GPIO 4
#define DHTTYPE DHT22 // DHT22 sensor model
DHT dht(DHTPIN, DHTTYPE);
float lastRecordedTemp = -999.0;
void setup() {
Serial.begin(115200);
dht.begin();
Serial.println(F("=========================================================="));
Serial.println(F(" EXPERIMENT: HUMIDITY VARIATION IN 5°C STEPS "));
Serial.println(F("=========================================================="));
Serial.println(F(" Instructions: Click on the DHT22 and adjust the temperature."));
Serial.println(F(" The system will log data every time temperature changes by 5°C."));
Serial.println(F("=========================================================="));
Serial.println();
}
void loop() {
delay(2000); // Wait 2 seconds between readings
// 1. Read the raw temperature set by the student in Wokwi
float rawTemp = dht.readTemperature();
if (isnan(rawTemp)) {
Serial.println(F("Error reading from DHT22 sensor. Check your wiring connections."));
return;
}
// 2. Round the temperature to the nearest 5°C step (e.g., 0, 5, 10, 15, 20, 25...)
int temp5C = round(rawTemp / 5.0) * 5;
// Limit the range between 0°C and 50°C for the activity
if (temp5C < 0) temp5C = 0;
if (temp5C > 50) temp5C = 50;
// 3. Detect if the temperature has moved into a new 5°C interval
if (abs(temp5C - lastRecordedTemp) >= 5.0) {
lastRecordedTemp = temp5C;
// 4. Calculate proportional humidity (Inverse relationship: higher temp = lower humidity)
// Formula: 100% humidity at 0°C decreasing down to 20% humidity at 50°C
float calculatedHumidity = 100.0 - (temp5C * 1.6);
// 5. Print clean data for students to log in Excel
Serial.println(F("----------------------------------------------------------"));
Serial.print(F(" 🌡️ Recorded Temperature: "));
Serial.print(temp5C);
Serial.println(F(" °C"));
Serial.print(F(" 💧 Estimated Humidity: "));
Serial.print(calculatedHumidity, 1);
Serial.println(F(" %"));
Serial.println(F(" 📝 Write down this data pair (Temp, Humidity) in Excel!"));
Serial.println(F("----------------------------------------------------------"));
Serial.println();
}
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4