Project 4: ESP32 External Sensor (Updated with bmp280 sensor)

Alvito Rizqi
6 min readFeb 27, 2022

Hello, it’s me again, Alvito, and in this article I will show you how DHT11 sensor works. DHT11 sensor used to measure temperature and relative humidity.

Requirements

  • ESP32 Devkit V1
  • DHT11 Sensor
  • Jumper wires
  • 10k Ohm Resistor
  • Breadboard
  • Arduino IDE
  • USB Cable

Follow the schematic above and open your Arduino to setup the DHT11. Go to tools Sketch > Include Library > Manage Libraries. Search for DHT sensors library by Adafruit and Adafruit Unified Sensor by Adafruit. Click install and restart your Arduino.

Then, open File > Examples > DHT sensor library > DHTtester.

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
// REQUIRES the following Arduino libraries:
// — DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
// — Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor
#include “DHT.h”#define DHTPIN 4 // Digital pin connected to the DHT sensor
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 —
// Pin 15 can work but DHT must be disconnected during program upload.
// Uncomment whatever type you’re using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 3 (on the right) of the sensor to GROUND (if your sensor has 3 pins)
// Connect pin 4 (on the right) of the sensor to GROUND and leave the pin 3 EMPTY (if your sensor has 4 pins)
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors. This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println(F(“DHTxx test!”));
dht.begin();
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds ‘old’ (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F(“Failed to read from DHT sensor!”));
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Serial.print(F(“Humidity: “));
Serial.print(h);
Serial.print(F(“% Temperature: “));
Serial.print(t);
Serial.print(F(“°C “));
Serial.print(f);
Serial.print(F(“°F Heat index: “));
Serial.print(hic);
Serial.print(F(“°C “));
Serial.print(hif);
Serial.println(F(“°F”));
}

Upload it and open serial monitor. It should show you the humidity, temperature, and Heat index. But in my case, it look like this.

I tried to search the problem and found out the cause (https://randomnerdtutorials.com/solved-dht11-dht22-failed-to-read-from-dht-sensor/). So I tried different schematic (http://indomaker.com/2019/12/02/esp32-mendeteksi-suhu-dan-kelembaban-dht11/ and https://www.youtube.com/watch?v=ebJpXs2-WgM) and it still can’t read my DHT sensor.

My guess is that there is something wrong with the sensor either it broken (because the nonsensical number) or not soldered well.

It’s a sad thing that I can’t finish this project but I learned about DHT11 sensor and what it supposed to show. I even learned about web servers when I search different schematic. I will update this article when the problem is solve or I managed to do it with different sensor. Thank you for reading and let’s hope for the best!

UPDATE 9 March 2022

So, I bought a different sensor which is bmp280 sensor. I’ve tried it and it works! Follow this schematic below.

This is how my series look like.

Then, you have to install the library. Go to Sketch > Include Library > Manage Library > download “Adafruit BMP280 Library”. After you installed the library, go to File > Examples > Adafruit BMP280 Library > bmp280test. The code should look like this.

/***************************************************************************
This is a library for the BMP280 humidity, temperature & pressure sensor
Designed specifically to work with the Adafruit BMP280 Breakout
----> http://www.adafruit.com/products/2651
These sensors use I2C or SPI to communicate, 2 or 4 pins are required
to interface.
Adafruit invests time and resources providing this open source code,
please support Adafruit andopen-source hardware by purchasing products
from Adafruit!
Written by Limor Fried & Kevin Townsend for Adafruit Industries.
BSD license, all text above must be included in any redistribution
***************************************************************************/
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>
#define BMP_SCK (13)
#define BMP_MISO (12)
#define BMP_MOSI (11)
#define BMP_CS (10)
Adafruit_BMP280 bmp; // I2C
//Adafruit_BMP280 bmp(BMP_CS); // hardware SPI
//Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO, BMP_SCK);
void setup() {
Serial.begin(9600);
while ( !Serial ) delay(100); // wait for native usb
Serial.println(F("BMP280 test"));
unsigned status;
//status = bmp.begin(BMP280_ADDRESS_ALT, BMP280_CHIPID);
status = bmp.begin();
if (!status) {
Serial.println(F("Could not find a valid BMP280 sensor, check wiring or "
"try a different address!"));
Serial.print("SensorID was: 0x"); Serial.println(bmp.sensorID(),16);
Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n");
Serial.print(" ID of 0x60 represents a BME 280.\n");
Serial.print(" ID of 0x61 represents a BME 680.\n");
while (1) delay(10);
}
/* Default settings from datasheet. */
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */
Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */
Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */
Adafruit_BMP280::FILTER_X16, /* Filtering. */
Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
}
void loop() {
Serial.print(F("Temperature = "));
Serial.print(bmp.readTemperature());
Serial.println(" *C");
Serial.print(F("Pressure = "));
Serial.print(bmp.readPressure());
Serial.println(" Pa");
Serial.print(F("Approx altitude = "));
Serial.print(bmp.readAltitude(1013.25)); /* Adjusted to local forecast! */
Serial.println(" m");
Serial.println();
delay(2000);
}

Upload it and open your serial monitor. Set the baud rate to 9600 and press EN button in your ESP32. Unfortunately, I have this error.

So, I asked my friend and he said that I need to uncomment the “status” variable assignment in setup() and comment the below expression. Here is how the change looks like.

Re-upload the code and open the serial monitor. It will look like this.

Here is my video showing how the temperature increase as I direct it to a glass of hot water. Thank you for reading!

--

--