Projects / Sensor fault detector

Sensor fault detector

Intermediate50 minGuided buildReviewed
ESP32 DevKitGPIO4GPIO15GPIO2GNDDHT22DS18B20FaultA 26.1 B 26.4 okSerial
Serial at 115200 baud

Tap a part to see what it does.

Tap the parts to see what each one does. A teaching simulation, not a circuit simulator.

Build it step by step

0 of 4 done
  1. ESP32DHT2224.7°C

    Sensor A

    DHT22 on GPIO4 with its pull-up.

  2. ESP32DS18B2024.7°C

    Sensor B

    DS18B20 on GPIO15 with 4.7 kΩ.

  3. ESP3212.9 mA

    Fault light

    GPIO2 through 220 Ω (also the onboard LED on many boards).

  4. ESP32

    Upload, then break things

    Unplug one, warm one, and watch the fault messages.

In short

How do you build a sensor fault detector?

Two temperature sensors watch the same air. The ESP32 flags a fault when one goes missing, gets stuck, reads impossible values or disagrees with the other. You need ESP32 DevKit V1, DHT22 Sensor, DS18B20 Temperature Sensor, Resistor 10 kΩ, Resistor 4.7 kΩ, LED 5 mm red, Resistor 220 Ω and Breadboard 830 Tie Points. It takes 4 steps and about 50 minutes.

Level
Intermediate
Time
About 50 min
Parts
8

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
DHT22 DATAGPIO4 with 10 kΩ to 3V3Sensor A
DS18B20 dataGPIO15 with 4.7 kΩ to 3V3Sensor B
Both VCC / GND3V3 / GNDPower
GPIO2 → 220 Ω → LED → GNDFault light

Code

Libraries: DHT sensor library, OneWire, DallasTemperature. The 2 °C agreement limit comes from the two datasheets' accuracies (±0.5 °C each) plus margin for placement.

Runs on a virtual ESP32 Dev Module in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

#include <DHT.h>
#include <OneWire.h>
#include <DallasTemperature.h>
DHT dht(4, DHT22);
OneWire bus(15); DallasTemperature ds(&bus);
float lastA = NAN; int sameCount = 0;

String check(float a, float b) {
  if (isnan(a)) return "DHT22 missing";
  if (b == DEVICE_DISCONNECTED_C) return "DS18B20 missing";
  if (a < -20 || a > 60 || b < -20 || b > 60) return "impossible value";
  if (a == lastA && ++sameCount > 30) return "DHT22 stuck"; 
  if (a != lastA) sameCount = 0;
  lastA = a;
  if (fabs(a - b) > 2.0) return "sensors disagree by " + String(fabs(a - b), 1) + " C";
  return "";
}

void setup() { Serial.begin(115200); dht.begin(); ds.begin(); pinMode(2, OUTPUT); }

void loop() {
  float a = dht.readTemperature();
  ds.requestTemperatures(); float b = ds.getTempCByIndex(0);
  String fault = check(a, b);
  digitalWrite(2, fault.length() > 0);
  Serial.printf("A %.1f  B %.1f  %s\n", a, b, fault.length() ? ("FAULT: " + fault).c_str() : "ok");
  delay(2000);
}

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual ESP32 Dev Module with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

A 26.5  B 24.5  ok

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Both connected, side by side"ok" with readings within 1 °C
Unplug the DHT22 data wire"DHT22 missing", LED on
Hold only the DS18B20"sensors disagree" after a few readings

If it does not work

Always disagree

One sensor next to the ESP32 or in the sun: place them together.

DS18B20 −127

Missing 4.7 kΩ pull-up.

Stuck alarm in very stable rooms

Raise the stuck count or compare the second decimal.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the sensor fault detector?

ESP32 DevKit V1, DHT22 Sensor, DS18B20 Temperature Sensor, Resistor 10 kΩ, Resistor 4.7 kΩ, LED 5 mm red, Resistor 220 Ω and Breadboard 830 Tie Points.

How long does the sensor fault detector take to build?

About 50 minutes for a intermediate maker, in 4 steps.

How do I know the sensor fault detector works?

Both connected, side by side: "ok" with readings within 1 °C. Unplug the DHT22 data wire: "DHT22 missing", LED on. Hold only the DS18B20: "sensors disagree" after a few readings.

Always disagree: what should I check?

One sensor next to the ESP32 or in the sun: place them together.

DS18B20 −127: what should I check?

Missing 4.7 kΩ pull-up.

Stuck alarm in very stable rooms: what should I check?

Raise the stuck count or compare the second decimal.