Build Your Own ESP32 DIY Weather Station: Track Temperature, Humidity & Air Quality

A top-down view of a DIY weather station prototype built on a breadboard over a green grid mat. The setup features an ESP32 microcontroller, an MQ gas sensor with a glowing red LED, a blue temperature sensor, and a small OLED display showing "Air Quality Monitor" with live temperature and humidity data. A white USB cable provides power to the system.

The “What’s in My Air?” Moment | DIY Weather Station

I used to wonder about the air in my room.

Is it too humid? Too dry? Is the air quality actually good?

I bought an air purifier. It had a little light. Green meant good. Red meant bad.

But that’s all it told me. No numbers. No trends. No idea what was actually happening.

So I built my own.

An ESP32. A few sensors. An OLED display.

Now I know exactly what’s in my air. Temperature in degrees. Humidity as a percentage. Air quality from “Good” to “Hazardous.”

And the best part? I can check it from my phone. Anywhere. Anytime.

This is the perfect intermediate project. More complex than a simple LED blinker. But not so hard that you’ll give up.

Let’s build it.

What You’ll Build

A complete diy weather station that measures:

 
 
MeasurementSensorRange
🌡️ TemperatureDHT22-40°C to 80°C (±0.5°C accuracy)
💧 HumidityDHT220% to 100% (±2-5% accuracy)
🌫️ Air Quality (CO2, smoke, NH3)MQ2Relative measurement (Good → Hazardous)

Display options:

  • Local OLED screen shows readings instantly

  • Web server lets you check from any device on your WiFi

  • Data updates every 2 seconds

A technical block diagram for an ESP32-based diy weather station. It shows an ESP32 microcontroller connected to a DHT22 temperature/humidity sensor and an MQ135 air quality sensor. The data flows into an OLED display and simultaneously transmits via WiFi to a smartphone and a computer monitor for remote tracking.

What You’ll Need

Hardware Components

 
 
ComponentSpecs / NotesPrice (USD)
ESP32 Development BoardAny 30-pin version$5.00 – $7.00
DHT22 Temperature/Humidity SensorMore accurate than DHT11 (±0.5°C)$3.00 – $5.00
MQ135 Air Quality SensorDetects CO2, smoke, NH3, benzene$5.00 – $8.00
0.96″ OLED DisplayI2C, 128×64 pixels$2.00 – $4.00
10kΩ ResistorFor DHT22 data line (pull-up)$0.05 – $0.10
Breadboard400 points$1.00 – $2.00
Jumper WiresMale-to-female & male-to-male$1.00 – $2.00
USB CableFor power and programming$2.00 – $4.00

Total: ~$19.00 – $32.00 USD

Quick Buy Links

 
 
ComponentWhere to Find
ESP32 Development BoardAliExpress
DHT11 SensorAliExpress
MQ2 Air Quality SensorAliExpress
0.96″ OLED DisplayAliExpress
Breadboard + Jumper KitAliExpress
A collection of electronics components for a diy weather station project laid out on a white surface. The set includes a large white breadboard, a small green breadboard, an MQ gas sensor, a DHT temperature and humidity sensor, an OLED display module, a small buzzer, resistors, and several multi-colored jumper wires.

Sensor Comparison: DHT11 vs DHT22

If you’ve seen the DHT11, you might wonder why I chose the DHT22.

 
 
FeatureDHT11DHT22 (AM2302)
Temperature range0-50°C-40 to 80°C
Temperature accuracy±2°C±0.5°C
Humidity range20-80%0-100%
Humidity accuracy±5%±2-5%
Sampling rate1 Hz (1 read/sec)0.5 Hz (1 read/2 sec)
Price~$2-3~$3-5
Best forBasic projectsDIY Weather stations, accurate monitoring

For a DIY weather station that actually tells you useful information, spend the extra dollar on the DHT22.

How the Sensors Work

DHT11 (Temperature + Humidity)

The DHT11 uses a capacitive humidity sensor and a thermistor to measure temperature. It sends digital data over a single wire.

Important: The DHT11 needs a 10kΩ pull-up resistor on the data line. Without it, readings will fail or be erratic.

A technical wiring diagram in text format for a diy weather station project. It lists the connections between an MQ-2 sensor and an ESP32: VCC to 5V (or 3.3V), GND to the GND rail, A0 (Analog) to GPIO 34, and D0 (Digital) to GPIO 35.

 

MQ135 (Air Quality)

The MQ135 is a chemical sensor. It has a heating element inside that makes it sensitive to various gases:

 
 
GasDetected
Carbon dioxide (CO2)✅ Yes
Smoke✅ Yes
Ammonia (NH3)✅ Yes
Benzene✅ Yes
Formaldehyde✅ Yes

The sensor outputs a higher voltage when it detects more pollutants. We read this analog value and convert it to a simple quality rating.

Important: The MQ135 needs to “warm up” for 5-10 minutes before giving stable readings. This is normal.

Wiring It Up

I2C OLED Display – DIY Weather Station

 
 
OLED PinESP32 Pin
VCC3.3V
GNDGND
SCLGPIO 22
SDAGPIO 21

DHT11 Sensor – DIY Weather Station

 
 
DHT11 PinESP32 PinNotes
VCC3.3VUse 3.3V, not 5V
GNDGND 
DATAGPIO 4Add 10kΩ resistor from DATA to VCC

MQ2 Air Quality Sensor – DIY Weather Station

 
MQ2 PinESP32 Pin
VCC5VMQ135 needs 5V power
GNDGND
AO (Analog Out)GPIO 34ADC input
A hand places a jumper wire into a white breadboard next to a small OLED display and an ESP32 microcontroller. The workspace includes an MQ gas sensor, a DHT temperature sensor, resistors, and a buzzer. An inset box provides the specific OLED I2C pinout: VCC to 3.3V, GND to GND, SCL to GPIO 22, and SDA to GPIO 21 for a diy weather station project.

The Code – DIY Weather Station

Full code available on GitHub: https://github.com/shahrear-ab/roborear/blob/main/weather_station/weather_station.ino

Setting up VS Code and PlatformIO to Control Servo Motors – Robotics for Beginners: Learn ESP32 PlatformIO with VS Code | Blink LED Tutorial

Required Libraries – DIY Weather Station

Install these in Arduino IDE (Sketch → Include Library → Manage Libraries):

 
 
LibraryAuthorPurpose
DHT sensor libraryAdafruitReads DHT22
Adafruit Unified SensorAdafruitRequired for DHT
Adafruit SSD1306AdafruitControls OLED display
Adafruit GFXAdafruitGraphics for OLED
A digital readout from a diy weather station project showing environmental data. The text on a dark background displays a temperature of 26.50°C, humidity at 60.00%, weather as "Sunny," and an advice note saying "Wear sunscreen!". Below this, it lists a smoke level of 293 with "Smoke Detected: NO" and "Smoke Alert: NO".
A top-down view of a DIY weather station prototype built on a breadboard over a green grid mat. The setup features an ESP32 microcontroller, an MQ gas sensor with a glowing red LED, a blue temperature sensor, and a small OLED display showing "Air Quality Monitor" with live temperature and humidity data. A white USB cable provides power to the system.

 

How to Read Air Quality Values – DIY Weather Station

The MQ135 outputs an analog value from 0 to 4095. Here’s what the numbers mean:

 
 
Raw ValueAir Quality StatusWhat It Means
< 300🟢 GoodClean air, normal conditions
300 – 600🟡 ModerateAcceptable, some pollutants present
600 – 900🟠 Unhealthy for SensitiveChildren, elderly, respiratory issues may be affected
900 – 1200🔴 UnhealthyEveryone may experience effects
> 1200⚫ HazardousEmergency conditions

Important: These thresholds are approximate. Your sensor may read higher or lower depending on your environment and calibration.

Identifying resistor values – How To Read Resistor Color Codes (And Never Burn an LED Again) | 4 Easy Steps

Testing Your DIY Weather Station

After Uploading

  1. Open Serial Monitor (115200 baud)

    • Should show temperature, humidity, and air quality every 2 seconds

  2. Check the OLED display

    • Should show all three readings clearly

  3. Test the web server (if WiFi configured)

    • Open browser on phone or computer

    • Enter the ESP32’s IP address (shown in Serial Monitor)

    • See readings in a clean web page

Testing Air Quality Response – DIY Weather Station

  • Normal room air: Baseline reading (usually 100-400)

  • Near a candle or incense: Reading should increase

  • Near cooking: Reading may spike

  • Exhaling directly on sensor: CO2 from breath increases reading

    To get familiar with your MQ135 sensor, try these simple experiments. Light a match or incense stick about 30cm away from the sensor. Watch the Serial Monitor – you should see the air quality value rise within 10-15 seconds. The smoke particles are detected by the sensor, causing the voltage on the AO pin to increase. Once you move the smoke source away, the value should slowly return to baseline after 20-30 seconds.

    You can also test with common household items. A spray of deodorant or perfume will cause a sharp spike. Cooking oil fumes from the kitchen will show a gradual increase. Even opening a bottle of vinegar nearby will register because the MQ135 is sensitive to acidic gases.

    For a fun demonstration, have someone exhale directly onto the sensor from a few inches away. The carbon dioxide in their breath will cause a noticeable jump in readings. This shows how quickly air quality changes with human presence – useful for classroom demonstrations or understanding ventilation needs in a room.

    Remember that extreme values (over 1000) may take several minutes to drop back to normal because the sensor absorbs gases and needs time to “clear” itself. This is normal behavior.

Make It Your Own (Upgrades) – DIY Weather Station

 
 
UpgradeDifficultyWhat You’ll Need
Add more sensors⭐ EasyBMP280 (pressure), MQ7 (CO), MQ2 (combustible gas)
Data logging to SD card⭐⭐ MediumMicroSD card module
Send data to cloud⭐⭐ MediumThingspeak, Blynk, or Firebase
Battery powered⭐⭐ Medium18650 battery + TP4056
3D printed case⭐⭐ Medium3D printer or laser cutter
Telegram alerts⭐⭐⭐ HardTelegram bot API
Historical graphs⭐⭐⭐ HardInfluxDB + Grafana

Troubleshooting

 
 
ProblemLikely CauseFix
DHT22 reads “nan”Missing pull-up resistorAdd 10kΩ resistor between DATA and VCC
MQ135 readings unstableSensor not warmed upWait 5-10 minutes for stabilization
OLED shows nothingWrong I2C addressTry address 0x3C or 0x3D
Air quality always 4095AO pin not connectedCheck jumper from MQ135 AO to ESP32 GPIO 34
Air quality always 0MQ135 not poweredCheck VCC to 5V, GND to GND
WiFi not connectingWrong credentials or 5GHz networkUse 2.4GHz network, check SSID/password

Why DHT22 Over DHT11? – DIY Weather Station

 
 
ReasonExplanation
Accuracy matters±0.5°C vs ±2°C makes a real difference
Better rangeSub-zero temperatures and 0-100% humidity
ReliabilityFewer failed readings
Cost differenceOnly ~$2 more

The DHT11 is fine for learning. But for a real weather station, the DHT22 is worth the extra dollar.

If your DHT22 still shows “nan” after adding the pull-up resistor, try a different GPIO pin. Some ESP32 pins have better performance for one-wire sensors. GPIO 4, 13, and 14 are good alternatives. Also, the DHT22 requires precise timing. If your code has long delays or heavy WiFi operations, the sensor reading may timeout. Move sensor readings to the beginning of your loop and add a 2-second delay between reads – the DHT22 needs at least 1.8 seconds between measurements anyway.

If your MQ135 readings fluctuate wildly even after warm-up, check for airflow. The sensor is sensitive to drafts from fans, air conditioners, or open windows. Place it in a stable environment for consistent readings. You can also add software smoothing by averaging 10 readings and dividing by 10. This reduces noise without losing responsiveness.

If your OLED display works but shows garbage or random pixels, check your I2C connections. Loose wires can cause data corruption. Also verify that you have the correct display driver in your code. Some 0.96″ OLEDs use the SH1106 driver instead of SSD1306. The symptoms look similar but the fix is simply changing the library.

For WiFi issues, try powering your ESP32 from a phone charger instead of your computer’s USB port. Computer ports sometimes provide unstable power that affects the WiFi radio.

A side-by-side comparison of two popular environmental sensors for a diy weather station. On the left is the smaller, blue DHT11 sensor, and on the right is the larger, white DHT22 sensor. Both feature four connection pins and are labeled with their respective model names against a plain gray background.

Real-World Applications – DIY Weather Station

 
 
Use CaseHow It Helps
Home air quality monitorKnow when to open windows or run purifier
Greenhouse controllerMaintain optimal temp/humidity for plants
Server room monitorAlert before equipment overheats
Classroom demonstrationShow how CO2 rises when room is full
Cigar humidor monitorMaintain perfect humidity
Pet enclosure monitorKeep reptiles or amphibians healthy

🎥 Watch the Step-by-Step Video Tutorial – DIY Weather Station

Prefer watching over reading? See the complete DIY weather station build in action:

📺 ESP32 DIY Weather Station with Air Quality Monitor

👉 Don’t forget to Subscribe to Roborear on YouTube for more beginner-friendly electronics and IoT projects every week!

Your Turn – DIY Weather Station

This project taught me that you don’t need expensive commercial air quality monitors. A $20 ESP32 and a few sensors give you more control, more data, and a much better understanding of what’s actually happening.

Now go build yours. Let it run for a day. Notice how the air quality changes when you cook, when you open a window, when the room gets crowded.

That’s the fun part.

FAQs

1. Why is my MQ135 sensor showing constantly high or low readings? - DIY Weather Station

The MQ135 needs time to stabilize. When first powered on, it can take 5 to 10 minutes to reach normal operating temperature. During this warm-up period, readings will be erratic and unusually high. This is completely normal. If readings remain unstable after 15 minutes, check your wiring. The MQ135 requires 5V power, not 3.3V. Connecting to 3.3V will result in very low or no readings. Also, the sensor is affected by airflow. If it's near an air conditioner vent, fan, or open window, readings will fluctuate. For stable readings, place it away from direct airflow. Finally, the thresholds in the code may need adjustment for your specific sensor and environment. Run the sensor for 30 minutes in clean air, note the average reading, and adjust the threshold values accordingly.

2. Why does my DHT22 show "nan" or random values? - DIY Weather Station

"nan" means "Not a Number" – the sensor failed to return a valid reading. The most common cause is missing the 10kΩ pull-up resistor on the data line. Connect a 10kΩ resistor between the DATA pin and VCC (3.3V). Another common issue is timing. The DHT22 requires precise timing from the ESP32. WiFi operations can sometimes interfere. If you're using WiFi and getting frequent nan readings, add a delay between WiFi operations and sensor reads, or use a separate sensor read task. Check your wiring as well. The DHT22 has four pins, but only three are used: VCC, GND, and DATA. Make sure you're using the correct pins. Some DHT22 modules come with a built-in pull-up resistor – check your module's documentation. If issues persist, try a different GPIO pin or replace the sensor.

3. How can I make the weather station battery-powered? - DIY Weather Station

Yes, you can make this project portable! The ESP32 has deep sleep modes that draw very little power (as low as 10µA). Power requirements: ESP32 active mode: ~150-250mA DHT22: ~1mA MQ135: ~150mA (heater element) – this is your biggest power draw OLED display: ~10-20mA To run on batteries: Use 2x 18650 lithium batteries (provides ~7.4V) Add a voltage regulator (5V for MQ135, 3.3V for ESP32 and sensors) Or use a 3.7V LiPo battery with a boost converter To save power: Put ESP32 into deep sleep between readings Power MQ135 only when taking measurements (it needs 60 seconds warm-up) Use a MOSFET to completely cut power to sensors when not in use Estimated battery life: With deep sleep and minimal readings, a 2000mAh battery could last several days. Without power saving, 1-2 hours maximum. For a more detailed guide on ESP32 deep sleep, check out the ESP32 deep sleep tutorial link in the internal links section below.

4. Why is my MQ135 air quality sensor showing very high or inconsistent readings even in clean air? - DIY Weather Station

The MQ135 is a chemical sensor that needs time to stabilize. When first powered on, it can take 5 to 15 minutes to reach normal operating temperature. During this warm-up period, readings will be erratic and unusually high. This is completely normal. Another common issue is that the MQ135 is sensitive to many gases including CO2, smoke, ammonia, benzene, and even humidity. If you breath directly on the sensor, the CO2 from your breath will cause a spike. Place the sensor away from air vents, open windows, and your own breathing when testing. The sensor also requires calibration for accurate absolute readings. The thresholds in the code (300 = Good, 600 = Moderate, etc.) are approximate. Let the sensor run for 30 minutes in your environment, note the average reading, then adjust the threshold values in the code accordingly. Finally, check your wiring. The MQ135 needs 5V power – connecting to 3.3V will give very low or no readings. Make sure the AO (Analog Output) pin is connected to an ESP32 ADC pin like GPIO 34.

5. Can I add more sensors to this weather station? What sensors do you recommend? - DIY Weather Station

Yes, absolutely! The ESP32 has multiple ADC pins and I2C capabilities, so you can easily expand your weather station. Here are the most useful sensors to add: BMP280 or BME280 adds barometric pressure measurement. The BME280 also measures temperature and humidity, making it a good backup or replacement for the DHT22. It uses I2C, so it shares the SDA and SCL pins with your OLED display. Price: $3-5. MQ7 carbon monoxide sensor detects CO gas – useful for garage or kitchen safety. It needs preheating time like the MQ135. Price: $6-8. DS18B20 waterproof temperature sensor is great for measuring soil temperature, water temperature, or outdoor conditions. It uses a one-wire protocol and can be placed far from the ESP32. Price: $2-4. Rain drop sensor detects rainfall – perfect for a complete outdoor weather station. It gives analog output like the MQ135. Price: $3-5. To add multiple sensors, you'll need to manage pin assignments carefully. Use GPIO 34, 35, 36, 39 for analog sensors (they are input-only and perfect for this). Use I2C for multiple digital sensors on the same SDA/SCL pins (GPIO 21 and 22). Remember that each sensor adds power consumption, so for battery-powered operation, you may need to power sensors only when taking readings.

Subscribe to The Newsletter

Join robotics enthusiasts getting weekly project ideas:

• 🔧 Under $50 projects

• ⚙️ Under $100 projects

• 🎥 New video tutorials

• 📝 Blog updates

We don’t spam! Read our privacy policy for more info.

Similar Posts

Leave a Reply

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

Roborear Logo
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.