project — ESP32 simulation

An interactive ESP32 circuit simulation you can run free in your browser on Velxio, by hypoxiadream.

Components

Sketch code

#include <DHT.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

// ============================================================
// ESP32 DevKit V1 + DHT22 温湿度检测系统
// 在线仿真网站用这个文件最方便:直接复制到 sketch.ino
// ============================================================

// ---------- 引脚定义 ----------
// DHT22 数据脚:GPIO4
const uint8_t DHT_PIN = 4;
const uint8_t DHT_TYPE = DHT22;

// I2C OLED:ESP32 DevKit V1 常用 SDA=21,SCL=22
const uint8_t OLED_SDA_PIN = 21;
const uint8_t OLED_SCL_PIN = 22;
const uint8_t OLED_ADDRESS = 0x3C;
const int OLED_WIDTH = 128;
const int OLED_HEIGHT = 64;

// 可选报警/状态元件
const uint8_t LED_PIN = 26;
const uint8_t BUZZER_PIN = 27;
const uint16_t BUZZER_FREQUENCY_HZ = 2000;

// 温度达到或超过该值时,LED 亮、蜂鸣器响
const float TEMP_ALARM_C = 30.0;

// 普通 LED 接法:GPIO26 -> 220Ω -> LED 正极,LED 负极 -> GND,保持 false。
// 如果你改成了 3V3 -> LED -> 220Ω -> GPIO26 这种低电平点亮接法,改成 true。
const bool LED_ACTIVE_LOW = false;

// DHT22 不适合高频读取,建议 >= 2s
const unsigned long READ_INTERVAL_MS = 2000;

DHT dht(DHT_PIN, DHT_TYPE);
Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT, &Wire, -1);

bool oledReady = false;
unsigned long lastReadMs = 0;

void showStartupScreen() {
  if (!oledReady) {
    return;
  }

  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.println("DHT22 Monitor");
  display.println("ESP32 DevKit V1");
  display.println();
  display.println("Reading sensor...");
  display.display();
}

void showSensorScreen(float temperature, float humidity, bool alarmOn) {
  if (!oledReady) {
    return;
  }

  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);

  display.setTextSize(1);
  display.setCursor(0, 0);
  display.println("DHT22 Temp & Humi");

  display.setTextSize(2);
  display.setCursor(0, 18);
  display.print("T:");
  display.print(temperature, 1);
  display.println("C");

  display.setCursor(0, 42);
  display.print("H:");
  display.print(humidity, 1);
  display.println("%");

  display.setTextSize(1);
  display.setCursor(90, 0);
  display.print(alarmOn ? "ALARM" : "OK");

  display.display();
}

void showErrorScreen() {
  if (!oledReady) {
    return;
  }

  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.println("DHT22 read failed!");
  display.println();
  display.println("Check wiring:");
  display.println("VCC -> 3V3");
  display.println("GND -> GND");
  display.println("DATA -> GPIO4");
  display.display();
}

void setAlarm(bool on) {
  digitalWrite(LED_PIN, on ? (LED_ACTIVE_LOW ? LOW : HIGH) : (LED_ACTIVE_LOW ? HIGH : LOW));
  if (on) {
    tone(BUZZER_PIN, BUZZER_FREQUENCY_HZ);
  } else {
    noTone(BUZZER_PIN);
  }
}

void testLedAndBuzzer() {
  Serial.println("[TEST] LED and buzzer ON for 1 second");
  setAlarm(true);
  delay(1000);
  setAlarm(false);
  Serial.println("[TEST] LED and buzzer OFF");
}

void setup() {
  Serial.begin(115200);
  delay(500);

  pinMode(LED_

More projects by hypoxiadream