Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
TIMOTHY MWALA
Published © GPL3+

​​​Connect XIAO ESP32 C3 with the DHT11 via I2C

DIY smart home device based on XIAO ESP32C3 connecting DHT11 sensor and an OLED display to monitor humidity and temperature.

IntermediateFull instructions provided2 hours1,411
​​​Connect XIAO ESP32 C3 with the DHT11 via I2C

Things used in this project

Hardware components

DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1
Seeed Studio XIAO ESP32C3
Seeed Studio XIAO ESP32C3
XIAOESP32_C3
×1
OLED Display 128x64 0.96 inch, I2C Interface
DIYables OLED Display 128x64 0.96 inch, I2C Interface
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

10 Pc. Jumper Wire Kit, 5 cm Long
10 Pc. Jumper Wire Kit, 5 cm Long

Story

Read more

Schematics

Set up scheme

Code

XIAOesp32_c3_DHT11_0.96OLED-Display

C/C++
Before using this code In your Arduino IDE, ensure you have installed latest version of these libraries;
"DHT.h" // DHT Library
<Wire.h>
<Adafruit_GFX.h>
<Adafruit_SSD1306.h>
#include "DHT.h" // DHT Library
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

const int OLED_RESET = 0;
Adafruit_SSD1306 display(OLED_RESET);

// Pin to which the DHT11 sensor is connected.
// If using the DHT11 Shield, it's digital Pin D4.
const int DHTPIN = D6;

// Specify the type of DHT sensor being used.
#define DHTTYPE DHT11

// Initialize the sensor with the pin and type.
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(115200); // Begin serial communication at 9600 Baud.
  Wire.begin();
  
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.display();
  delay(2000);
  display.clearDisplay(); 
  dht.begin(); // Start DHT communication.
}

void loop() {
  // The DHT11 sensor provides a new reading every 2 seconds,
  // so there's no need to constantly loop in the program.
  delay(2000);

  // Read humidity value.
  double humidity = dht.readHumidity();
  // Read temperature in Celsius.
  double temperatureC = dht.readTemperature();
  // Read temperature in Fahrenheit.
  // The boolean parameter controls whether
  // the temperature is displayed in Fahrenheit or Celsius.
  double temperatureF = dht.readTemperature(true);

  // Check if the values were read successfully.
  if (isnan(humidity) || isnan(temperatureC) || isnan(temperatureF)) {
    Serial.println("Error reading data.");
    return;
  }

  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);

  display.setCursor(5, 0);
  display.println("Hygrometer-1");

  display.setCursor(5, 15);
  String tempValue = String(temperatureC);
  display.println("Temp: " + tempValue + "C");

  display.setCursor(5, 23);
  String humValue = String(humidity);
  display.println("Humidity: " + humValue + "%");

  display.display();
  delay(500);
}

Credits

TIMOTHY MWALA

TIMOTHY MWALA

25 projects • 15 followers
I am an Embedded engineer who like prototyping

Comments