DHT22 + DS18B20 using Arduino UNO — Arduino Uno simulation

An interactive Arduino Uno circuit simulation you can run free in your browser on Velxio, by dwisatriya32.

Components

Sketch code

// Gabungan: DS18B20 (multi sensor, 1 bus) + DHT + Titik Embun + LCD I2C
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
#include <math.h>
#include "DHT.h"

// ----- LCD -----
const byte I2C_ADDR    = 0x27;      // ganti 0x3F jika LCD tidak tampil
const byte LCD_COLUMNS = 20;
const byte LCD_LINES   = 4;
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);

// ----- DS18B20 -----
const byte ONE_WIRE_BUS = 12;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

// ----- DHT -----
#define DHTPIN  2
#define DHTTYPE DHT22        // ganti ke DHT22 jika pakai AM2302
DHT dht(DHTPIN, DHTTYPE);

// ----- Buffer LCD -----
const byte LCD_BUFFER_LENGTH = 21;   // 20 char + NULL
char lcdBuffer[LCD_BUFFER_LENGTH];
const char LCD_BUFFER_FORMAT[] = "  %d: %s\xDF\x43            ";

byte numDevices = 0;
const byte MAX_LCD_SENSORS = 3;      // baris 1..3 untuk DS18B20

// ----- Rumus titik embun (Magnus) -----
float dewPoint(float t, float h)
{
  if (h < 1.0) h = 1.0;              // hindari log(0)
  const float a = 17.27;
  const float b = 237.7;
  float alpha = log(h / 100.0) + (a * t) / (b + t);
  return (b * alpha) / (a - alpha);
}

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

  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Temperatures:");

  sensors.begin();
  dht.begin();

  // Enumerasi semua DS18B20 di bus
  byte address[8];
  while (oneWire.search(address))
  {
    Serial.print("Bus index=");
    Serial.print(numDevices);
    Serial.print(";  Device address=");
    for (byte i = 0; i < 8; i++)
    {
      Serial.print(address[i], HEX);
      Serial.print(" ");
    }
    Serial.println();
    numDevices++;
  }
}

void loop()
{
  // ================= DHT =================
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float f = dht.readTemperature(true);

  lcd.setCursor(0, 0);
  if (isnan(h) || isnan(t) || isnan(f))
  {
    lcd.print("DHT: read failed    ");
    Serial.println("Failed to read from DHT sensor!");
  }
  else
  {
    float dp = dewPoint(t, h);
    float hi = dht.computeHeatIndex(f, h);

    char tbuf[7], hbuf[5], dbuf[7];
    dtostrf(t,  4, 1, tbuf);          // contoh: "25.3" / "-5.3"
    dtostrf(h,  2, 0, hbuf);          // contoh: "60"
    dtostrf(dp, 4, 1, dbuf);          // contoh: "17.2"

    char dhtBuf[LCD_BUFFER_LENGTH];
    snprintf(dhtBuf, sizeof(dhtBuf),
             "T%s\xDF\x43 H%s%% DP%s\xDF\x43",
             tbuf, hbuf, dbuf);

    lcd.print(dhtBuf);
    for (byte i = strlen(dhtBuf); i < LCD_COLUMNS; i++) lcd.print(' ');

    Serial.print("DHT Humidity: "); Serial.print(h);  Serial.print(" %  ");
    Serial.print("Temperature: ");  Serial.print(t);  Serial.print(" *C  ");
    Serial.print(f);                Serial.print(" *F  ");
    Serial.print("Dew Point: ");    Serial.print(dp); Serial.print(" *C  ");
    Serial.print("Heat index: ");   Serial.print(hi); Serial.println(" *F");
  }

  // ================= DS18B20 =================

More projects by dwisatriya32