Search This Blog

Monday, 9 June 2025

PENGUKUR RUANGAN DENGAN ESP32 PLUS LCD 16X2 DENGAN SENSOR NTC






Komponen yang Dibutuhkan:

  • ESP32

  • Sensor NTC + resistor (untuk voltage divider)

  • LCD 16x2 + I2C module

  • Breadboard dan kabel jumper

  • Internet (WiFi)


(Vcc) ---- [10K Resistor] ----+---- [NTC] ---- GND
                             |
                         (Analog input ke ESP32, misal GPIO34)

Kode Program ESP32 (Arduino IDE)

Instalasi Library:

  • LiquidCrystal_I2C untuk LCD

  • Gunakan Arduino IDE dengan board ESP32 diaktifkan



#include <WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Konfigurasi WiFi
const char* ssid = "NAMA_WIFI";
const char* password = "PASSWORD_WIFI";

// Konfigurasi sensor NTC
const int ntcPin = 34; // Analog pin
const float seriesResistor = 10000.0;
const float nominalResistance = 10000.0;
const float nominalTemperature = 25.0;
const float bCoefficient = 3950.0;

// LCD I2C
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Server web ESP32
WiFiServer server(80);

// Fungsi untuk menghitung suhu dari pembacaan NTC
float readTemperature() {
  int analogValue = analogRead(ntcPin);
  float voltage = analogValue / 4095.0 * 3.3;
  float resistance = (3.3 - voltage) * seriesResistor / voltage;
  float steinhart;
  steinhart = resistance / nominalResistance;     // (R/Ro)
  steinhart = log(steinhart);                     // ln(R/Ro)
  steinhart /= bCoefficient;                      // 1/B * ln(R/Ro)
  steinhart += 1.0 / (nominalTemperature + 273.15); // + (1/To)
  steinhart = 1.0 / steinhart;                    // Invert
  steinhart -= 273.15;                            // convert to Celsius
  return steinhart;
}

void setup() {
  Serial.begin(115200);
  Wire.begin(21, 22); // SDA, SCL
  lcd.begin();
  lcd.backlight();

  // Koneksi WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Connected to WiFi");
  Serial.println(WiFi.localIP());

  server.begin();
}

void loop() {
  float temperature = readTemperature();
  lcd.setCursor(0, 0);
  lcd.print("Suhu: ");
  lcd.print(temperature, 1);
  lcd.print(" C  ");

  WiFiClient client = server.available();
  if (client) {
    Serial.println("Client connected");
    String response = "<!DOCTYPE html><html><head><meta http-equiv='refresh' content='5'/>";
    response += "<title>Suhu Ruangan</title></head><body>";
    response += "<h1>Suhu: ";
    response += temperature;
    response += " &deg;C</h1></body></html>";

    client.println("HTTP/1.1 200 OK");
    client.println("Content-type:text/html");
    client.println();
    client.println(response);
    client.stop();
  }

  delay(1000);
}


Cara Akses Web Interface (AJAX)

  1. Upload kode ke ESP32.

  2. Buka Serial Monitor → Catat alamat IP (misalnya 192.168.1.42)

  3. Buka browser dan akses IP tersebut.

  4. Web akan menampilkan suhu yang diperbarui otomatis setiap detik tanpa reload



No comments:

Post a Comment

CARGER AKI CONTROLER INA226-LIBARY WE ROTARI SW+OLED 0.96 SUKSES TAMPILAN LEBIH BAIK

 #include <Wire.h>  #include <Adafruit_GFX.h>    // Library grafis Adafruit #include <Adafruit_SSD1306.h> // Library untuk...