test — ESP32-S3 simulation

An interactive ESP32-S3 circuit simulation you can run free in your browser on Velxio, by mr-natair55.

Components

Sketch code

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <RTClib.h>
#include <Adafruit_BME280.h>

// กำหนดขา I2C สำหรับ DS3231 และ BME280
#define I2C_SDA 8
#define I2C_SCL 9

// กำหนดขา SPI สำหรับจอ ILI9341
#define TFT_CS   10
#define TFT_DC   13
#define TFT_RST  14
#define TFT_MOSI 11
#define TFT_CLK  12

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST);
RTC_DS3231 rtc;
Adafruit_BME280 bme;

void setup() {
  Serial.begin(115200);
  Wire.begin(I2C_SDA, I2C_SCL);

  // 1. เริ่มต้นจอสี ILI9341
  tft.begin();
  tft.setRotation(1); // หมุนจอเป็นแนวนอน (320 x 240)
  tft.fillScreen(ILI9341_BLACK);

  // 2. เริ่มต้นโมดูลนาฬิกา DS3231
  if (!rtc.begin()) {
    tft.setTextColor(ILI9341_RED);
    tft.setTextSize(2);
    tft.setCursor(10, 10);
    tft.println("DS3231 Not Found!");
    for (;;);
  }

  // 3. เริ่มต้นเซนเซอร์ BME280
  if (!bme.begin(0x76)) {
    tft.setTextColor(ILI9341_RED);
    tft.setTextSize(2);
    tft.setCursor(10, 40);
    tft.println("BME280 Not Found!");
    for (;;);
  }

  // วาดโครงสร้างหน้าจอคงที่ (วาดครั้งเดียวเพื่อไม่ให้จอกระพริบ)
  tft.drawRect(5, 5, 310, 230, ILI9341_DARKGREY);
  tft.drawFastHLine(5, 95, 310, ILI9341_DARKGREY);
  tft.drawFastHLine(5, 165, 310, ILI9341_DARKGREY);

  tft.setTextSize(2);
  tft.setTextColor(ILI9341_CYAN);
  tft.setCursor(20, 110);
  tft.print("HUMIDITY :");

  tft.setTextColor(ILI9341_GREEN);
  tft.setCursor(20, 180);
  tft.print("ALTITUDE :");
}

void loop() {
  DateTime now = rtc.now();
  float humidity = bme.readHumidity();
  float altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);

  // --- 1. แสดงเวลา (ตัวใหญ่สีเหลือง) ---
  // ใช้ setTextColor(สีตัวอักษร, สีพื้นหลัง) เพื่อเขียนทับเลขเดิมโดยไม่กระพริบ
  tft.setTextSize(5);
  tft.setTextColor(ILI9341_YELLOW, ILI9341_BLACK);
  tft.setCursor(40, 30);

  if (now.hour() < 10) tft.print('0');
  tft.print(now.hour());
  tft.print(':');
  if (now.minute() < 10) tft.print('0');
  tft.print(now.minute());
  tft.print(':');
  if (now.second() < 10) tft.print('0');
  tft.print(now.second());

  // --- 2. แสดงค่าความชื้น (สีฟ้าอ่อน) ---
  tft.setTextSize(3);
  tft.setTextColor(ILI9341_CYAN, ILI9341_BLACK);
  tft.setCursor(150, 106);
  tft.print(humidity, 1);
  tft.print(" %  "); // เคาะวรรคปิดท้ายเพื่อลบเศษตัวเลขเก่า

  // --- 3. แสดงค่าความสูง (สีเขียว) ---
  tft.setTextColor(ILI9341_GREEN, ILI9341_BLACK);
  tft.setCursor(150, 176);
  tft.print(altitude, 1);
  tft.print(" m  ");

  delay(500);
}

More projects by mr-natair55