ArduinoESPHow ToTutorials/DIY

ESP8266 Arduino Integration: Installation & DHT11 Sensor Test

Arduino Support for ESP8266 with Simple Test Code

The NodeMCU is more than just a WiFi module; it includes a powerful microcontroller with a 32-bit Tensilica processor. It boasts a power-saving architecture, compact size, and high durability, making it an excellent choice for IoT applications. In this tutorial, we’ll guide you through setting up Arduino support for the ESP8266 and running a simple test using a DHT11 temperature and humidity sensor.

Installing the ESP8266 Board Package

Step 1: Download Arduino IDE

Before proceeding, download and install the Arduino IDE from the official website: Arduino Software

Step 2: Add ESP8266 Board Support

  1. Open Arduino IDE.
  2. Navigate to File > Preferences.
  3. In the Additional Boards Manager URLs field, enter the following URL:
    http://arduino.esp8266.com/stable/package_esp8266com_index.json

  4. Click OK to save the changes.

Step 3: Install the ESP8266 Package

  1. Go to Tools > Board > Boards Manager.
  2. In the search box, type ESP8266.
  3. Select the ESP8266 by ESP8266 Community package and click Install.

Step 4: Select the ESP8266 Board

  1. Restart the Arduino IDE.
  2. Go to Tools > Board and select Generic ESP8266 Module.

Now, your Arduino IDE is ready to program the ESP8266!

Testing with DHT11 Temperature & Humidity Sensor

The DHT11 sensor is a popular choice due to its affordability, accuracy, and pre-calibration. Below is the pin configuration:

DHT11 Pinout

  • VCC → Connect to 3.3V or 5V on NodeMCU
  • GND → Connect to GND on NodeMCU
  • Data → Connect to D1 (GPIO 5) on NodeMCU

Installing Required Libraries

To use the DHT11 sensor, install the necessary libraries:

  1. Download the DHT sensor library.
  2. Extract the .zip file and rename the folder to DHT.
  3. Move the DHT folder to the Arduino libraries directory.
  4. Download the Adafruit Unified Sensor Driver Library.
  5. Extract the .zip file and rename the folder to Adafruit_sensor.
  6. Move the Adafruit_sensor folder to the Arduino libraries directory.
  7. Restart the Arduino IDE.

Code Example: Reading Temperature & Humidity

Copy and paste the following code into the Arduino IDE. Replace YOUR_NETWORK_NAME and YOUR_NETWORK_PASSWORD with your actual WiFi credentials.

C
#include <ESP8266WiFi.h>
#include "DHT.h"

#define DHTTYPE DHT11
const char* ssid = "YOUR_NETWORK_NAME";
const char* password = "YOUR_NETWORK_PASSWORD";
WiFiServer server(80);
const int DHTPin = 5;
DHT dht(DHTPin, DHTTYPE);

void setup() {
    Serial.begin(115200);
    dht.begin();
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("WiFi connected");
    server.begin();
    Serial.println(WiFi.localIP());
}

void loop() {
    WiFiClient client = server.available();
    if (client) {
        float h = dht.readHumidity();
        float t = dht.readTemperature();
        if (isnan(h) || isnan(t)) {
            Serial.println("Failed to read from DHT sensor!");
            return;
        }
        client.println("HTTP/1.1 200 OK");
        client.println("Content-Type: text/html");
        client.println("Connection: close");
        client.println();
        client.println("<html><body><h1>ESP8266 - Temperature and Humidity</h1>");
        client.println("<h3>Temperature: " + String(t) + " *C</h3>");
        client.println("<h3>Humidity: " + String(h) + " %</h3>");
        client.println("</body></html>");
        delay(1);
        client.stop();
    }
}

Running the Test

  1. Upload the code to your NodeMCU using the Arduino IDE.
  2. Open the Serial Monitor at a baud rate of 115200.
  3. Once connected, the ESP8266’s IP address will appear in the Serial Monitor.
  4. Open a web browser and enter the displayed IP address.
  5. You should see the temperature and humidity data displayed in your browser.

Conclusion

With this setup, you can easily integrate the ESP8266 with the Arduino IDE and interface it with sensors like the DHT11. This tutorial provides a solid foundation for building IoT projects involving temperature and humidity monitoring.

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin

25 thoughts on “ESP8266 Arduino Integration: Installation & DHT11 Sensor Test

Leave a Reply

Your email address will not be published. Required fields are marked *