Introduction
ThingSpeak is a cloud IoT platform used for storing, visualizing and analyzing sensor data online.
In this tutorial, ESP32 reads temperature and humidity from DHT11 sensor and uploads data to ThingSpeak dashboard.
Required Components
| Component | Quantity |
|---|---|
| ESP32 Board | 1 |
| DHT11 Sensor | 1 |
| Jumper Wires | Few |
| WiFi Connection | 1 |
DHT11 Connection with ESP32
| DHT11 Pin | ESP32 Connection |
|---|---|
| VCC | 3.3V |
| GND | GND |
| DATA | GPIO 4 |

Create ThingSpeak Account
Step 1:
Open ThingSpeak website and create free account.
Step 2:
Login and click:
Channels > New Channel
Step 3:
Enter Channel Name and enable:
- Field 1 → Temperature
- Field 2 → Humidity
Step 4:
Save the channel.
Get Write API Key
After creating the channel:
Open:
Channel > API Keys
Copy the Write API Key.
Install Required Libraries
Install these libraries from Arduino IDE:
- ThingSpeak Library
- DHT Sensor Library
- Adafruit Unified Sensor
Open:
Sketch > Include Library > Manage Libraries
ESP32 ThingSpeak Program
#include <WiFi.h>
#include "ThingSpeak.h"
#include "DHT.h"
#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";
unsigned long channelID = YOUR_CHANNEL_ID;
const char* writeAPIKey = "YOUR_WRITE_API_KEY";
WiFiClient client;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while(WiFi.status() != WL_CONNECTED){
delay(1000);
Serial.println("Connecting...");
}
Serial.println("WiFi Connected");
ThingSpeak.begin(client);
dht.begin();
}
void loop() {
float temp = dht.readTemperature();
float hum = dht.readHumidity();
ThingSpeak.setField(1, temp);
ThingSpeak.setField(2, hum);
int status = ThingSpeak.writeFields(channelID, writeAPIKey);
if(status == 200){
Serial.println("Data Sent Successfully");
}
else{
Serial.println("Error Sending Data");
}
delay(15000);
}
Program Explanation
| Code | Purpose |
|---|---|
| WiFi.begin() | Connects ESP32 to WiFi |
| dht.readTemperature() | Reads temperature |
| dht.readHumidity() | Reads humidity |
| ThingSpeak.setField() | Stores data in channel fields |
| ThingSpeak.writeFields() | Uploads data to cloud |
Output
After uploading the code:
- ESP32 connects to WiFi
- DHT11 sensor values are read
- Temperature and humidity appear on ThingSpeak graphs
- Cloud dashboard updates every 15 seconds
Important Notes
- Free ThingSpeak accounts allow updates every 15 seconds.
- Use correct WiFi credentials.
- Use correct Channel ID and API Key.
- ESP32 must remain connected to internet.
