This smart safety monitor uses an ESP32 microcontroller to track environmental conditions and detect hazardous situations in real-time. It combines temperature and humidity sensing with door/window monitoring, infrared motion detection, and electrical current measurement to provide comprehensive safety oversight across a space.
The guide includes a complete wiring diagram, parts list, and step-by-step assembly instructions for integrating the DS18B20 temperature probe, DHT22 sensor, reed switch door alarm, current sensor, relay-controlled fan, and status indicators. The provided firmware handles sensor calibration, multi-sensor polling, and automated responses like triggering alarms or activating cooling systems based on detected conditions.
Wiring diagram
Gather all the parts
Assemble it in 6 steps
1. Check the power before connecting electronics
Use only the adapter's isolated DC output. Verify connector polarity. With electronics disconnected, adjust the LM2596 output to 5.0 V with a meter. Connect capacitor positive to 12 V and striped negative to GND. Connect the 5 V output to VIN, never 12 V. Join all grounds.
- Reversing the capacitor can make it rupture. Disconnect power and discharge it before changing wires.
- Do not simultaneously connect external 5 V VIN and USB 5 V. Deploy on USB with external VIN wire disconnected; for powered serial use a USB connection with its 5 V conductor disconnected.
2. Connect low-voltage sensors
Power both temperature sensors and IR modules from 3.3 V. Probe data goes to GPIO4 and DHT data to GPIO18, each with a 4.7 kΩ pull-up to 3.3 V. IR digital outputs go to GPIO13 and GPIO14; leave AO disconnected. Door contact goes between GPIO19 and GND. The magnet closes the contact.
- Identify waterproof probe wires from their supplied labels, not color alone. IR output polarity depends on the board and target; serial prints raw levels.
3. Protect the analog input
ACS712 VCC to 5 V and GND to GND. Output through 10 kΩ to GPIO34; 15 kΩ from GPIO34 to GND. This limits a 5 V output to 3.0 V. Connect IP+ to 5 V and IP- to motor positive: only motor current is measured. Boot calibration requires motor OFF.
- Never connect ACS712 output directly to the ESP32. ADC current is approximate; calibrate against a meter.
4. Wire the door alarm
GPIO32 through 220 Ω to red LED anode, cathode to GND. Active 5 V buzzer positive to 5 V, negative to 2N2222 collector. Emitter to GND, GPIO25 through 1 kΩ to base, 10 kΩ base to GND. Buzzer diode band to positive, other end to collector.
- Added parts are required: two additional 2N2222 transistors and an additional 1N4007. Check actual transistor lead order; it differs between packages. Chosen buzzer is active, not passive, and must draw at most 30 mA.
5. Connect relay control and fan
For each active-low 5 V relay, use a 2N2222 interface: GPIO26 or GPIO27 through 1 kΩ to base; 10 kΩ base to ground; emitter to ground; collector to IN. Add 10 kΩ IN to 5 V. Relay VCC to 5 V and GND to ground. Relay1 COM to 12 V, NO to fan positive, fan negative to ground. Leave unused contacts disconnected. GPIO HIGH turns these interfaces on.
- Use active-low modules with onboard coil protection. Verify relay action before attaching fan. These contacts switch low-voltage DC only, never mains.
6. Replace the undersized motor transistor
Use the added IRLZ44N: source to ground, drain to motor negative; GPIO33 through 100 Ω to gate and 10 kΩ gate to ground. Motor positive comes from ACS712 IP-. Flyback diode band to motor positive, anode to drain. Firmware leaves motor OFF because no motor behavior was requested.
- The listed 2N2222 with 1 kΩ base drive is not a safe geared-motor driver. IRLZ44N performance at 3.3 V must be checked under the actual stall load; use a 3.3 V-rated motor driver if it heats. Do not enable motor until checked.
- Adapter budget is 24 W total. Check fan starting current and motor stall current; LM2596 modules may need cooling well below advertised 3 A. Keep load power wiring off breadboard contacts; use secure terminal wiring.
Review all connections
1. Connections between "probe" and "ESP32"
2. Connections between "probe_pullup" and "ESP32"
3. Connections between "dht" and "ESP32"
4. Connections between "dht_pullup" and "ESP32"
5. Connections between "ir1" and "ESP32"
6. Connections between "ir2" and "ESP32"
7. Connections between "door" and "ESP32"
8. Connections between "current" and "ESP32"
9. Connections between "adc_top" and "ESP32"
10. Connections between "adc_bottom" and "ESP32"
11. Connections between "led" and "ESP32"
12. Connections between "led_r" and "ESP32"
13. Connections between "buzzer" and "ESP32"
14. Connections between "buzz_driver" and "ESP32"
15. Connections between "buzz_driver_base" and "ESP32"
16. Connections between "buzz_driver_off" and "ESP32"
17. Connections between "relay1_driver" and "ESP32"
18. Connections between "relay1_driver_base" and "ESP32"
19. Connections between "relay1_driver_off" and "ESP32"
20. Connections between "relay2_driver" and "ESP32"
21. Connections between "relay2_driver_base" and "ESP32"
22. Connections between "relay2_driver_off" and "ESP32"
23. Connections between "relay1" and "ESP32"
24. Connections between "relay1_pullup" and "ESP32"
25. Connections between "relay2" and "ESP32"
26. Connections between "relay2_pullup" and "ESP32"
27. Connections between "fan" and "ESP32"
28. Connections between "motor_driver" and "ESP32"
29. Connections between "motor_gate" and "ESP32"
30. Connections between "motor_off" and "ESP32"
31. Connections between "motor_diode" and "ESP32"
32. Connections between "buzz_diode" and "ESP32"
33. Connections between "adapter" and "ESP32"
34. Connections between "cap" and "ESP32"
35. Connections between "buck" and "ESP32"
Deploy the firmware
#include <Arduino.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <DHT.h>
// Safe pin changes: GPIO5 -> 18 and GPIO12 -> 19 (boot straps avoided).
constexpr int PROBE=4, DHT_PIN=18, IR1=13, IR2=14, DOOR=19;
constexpr int ADC_PIN=34, LED_PIN=32, BUZZ=25, FAN=26, SPARE=27, MOTOR=33;
// NPN interfaces invert active-low relay inputs: GPIO HIGH energizes relay.
OneWire wire(PROBE);
DallasTemperature ds(&wire);
DHT dht(DHT_PIN,DHT22);
float zeroMv=1500; // overwritten at boot with motor OFF
uint32_t sampleAt=0, conversionAt=0, changedAt=0;
bool converting=false, rawDoor=true, doorOpen=true, fanOn=false;
float probeC=NAN;
float adcMv(){
uint32_t sum=0;
for(int i=0;i<64;i++) sum+=analogReadMilliVolts(ADC_PIN);
return sum/64.0f;
}
void setup(){
Serial.begin(115200);
for(int p: {LED_PIN,BUZZ,FAN,SPARE,MOTOR}){
digitalWrite(p,LOW); pinMode(p,OUTPUT);
}
pinMode(IR1,INPUT_PULLUP); pinMode(IR2,INPUT_PULLUP);
pinMode(DOOR,INPUT_PULLUP); pinMode(ADC_PIN,INPUT);
analogReadResolution(12); analogSetPinAttenuation(ADC_PIN,ADC_11db);
ds.begin(); ds.setResolution(12); ds.setWaitForConversion(false);
dht.begin();
// Only motor current passes through ACS712. Motor MUST remain off here.
delay(500); zeroMv=adcMv();
rawDoor=doorOpen=digitalRead(DOOR); changedAt=millis();
ds.requestTemperatures(); conversionAt=millis(); converting=true;
sampleAt=millis();
Serial.println("Test ready. Motor and spare relay remain OFF. ACS712 measures motor branch only.");
}
void loop(){
uint32_t now=millis();
// Door alarm responds independently of the two-second sensor report.
bool reading=digitalRead(DOOR);
if(reading!=rawDoor){rawDoor=reading; changedAt=now;}
if(now-changedAt>=40) doorOpen=rawDoor;
digitalWrite(LED_PIN,doorOpen); digitalWrite(BUZZ,doorOpen);
if(converting && now-conversionAt>=750){
float t=ds.getTempCByIndex(0);
probeC=(t!=DEVICE_DISCONNECTED_C && t>=-55 && t<=125)?t:NAN;
// Fail-safe cooling on missing probe; threshold exactly as requested.
fanOn=isnan(probeC) || probeC>30.0f;
digitalWrite(FAN,fanOn); converting=false;
}
if(now-sampleAt>=2000){
sampleAt=now;
float air=dht.readTemperature(), humidity=dht.readHumidity();
// 10k/15k divider factor = 0.6; ACS712-5A sensitivity = 185mV/A.
float current=(adcMv()-zeroMv)/(0.6f*185.0f);
Serial.printf("DS18B20=%.2f C%s | DHT22=%.2f C %.1f %%RH%s | IR1 raw=%d IR2 raw=%d | Door=%s | Motor current=%.3f A | Fan=%s\n",
probeC,isnan(probeC)?" [FAULT cooling ON]":"",air,humidity,
(isnan(air)||isnan(humidity))?" [READ ERROR]":"",digitalRead(IR1),digitalRead(IR2),
doorOpen?"OPEN":"CLOSED",current,fanOn?"ON":"OFF");
ds.requestTemperatures(); conversionAt=millis(); converting=true;
}
}Remix this project
Make it yours in one click
Open a full copy of this project in your own Schematik workspace, diagram, code, parts, and assembly steps included. Swap the sensor, add features, or redesign the whole thing with AI. The author's original stays untouched.




