Smart Home Automation System
📘 Individual Debugging Code (Serial Monitor Testing One by One)
🎯 Objective of This Lesson
Students will:
-
Test each sensor individually
-
Verify correct wiring
-
Calibrate thresholds
-
Debug hardware issues
-
Build confidence before full integration
We will test in this order:
1️⃣ DHT11
2️⃣ Ultrasonic Sensor
3️⃣ MQ3 Gas Sensor
4️⃣ Servo Motor
5️⃣ Relay Modules
🔹 STEP 1 – Test DHT11 (Temperature Sensor)
📌 Debug Code – DHT11
#include "DHT.h"
#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
dht.begin();
Serial.println("DHT11 Test Started...");
}
void loop() {
float temp = dht.readTemperature();
float hum = dht.readHumidity();
Serial.print("Temperature: ");
Serial.print(temp);
Serial.print(" °C | Humidity: ");
Serial.print(hum);
Serial.println(" %");
delay(2000);
}
✅ Expected Output
Temperature: 28°C
Humidity: 60%
🔧 If Problem Occurs
-
Check VCC (3.3V)
-
Check DATA pin
-
Add pull-up resistor if needed
🔹 STEP 2 – Test Ultrasonic Sensor (Water Level)
📌 Debug Code – Ultrasonic
#define TRIG 5
#define ECHO 18
long duration;
float distance;
void setup() {
Serial.begin(115200);
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
Serial.println("Ultrasonic Test Started...");
}
void loop() {
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
duration = pulseIn(ECHO, HIGH);
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(1000);
}
✅ Expected Output
Distance: 25 cm
🔧 If Reading is 0 or Very Large
-
Check wiring
-
Ensure sensor facing object
-
Verify 5V supply
🔹 STEP 3 – Test MQ3 Gas Sensor
📌 Debug Code – MQ3
#define MQ3_PIN 34
void setup() {
Serial.begin(115200);
Serial.println("MQ3 Test Started...");
delay(30000); // warm-up
}
void loop() {
int gasValue = analogRead(MQ3_PIN);
Serial.print("Gas Value: ");
Serial.println(gasValue);
delay(1000);
}
✅ Expected Values
Clean air: 300–600
Alcohol vapor: 1500+
🔧 Calibrate Threshold
Observe both values and choose midpoint.
🔹 STEP 4 – Test Servo Motor (Door Lock)
📌 Debug Code – Servo
#include <ESP32Servo.h>
Servo myServo;
void setup() {
Serial.begin(115200);
myServo.attach(13);
Serial.println("Servo Test Started...");
}
void loop() {
Serial.println("Lock Position");
myServo.write(0);
delay(3000);
Serial.println("Unlock Position");
myServo.write(90);
delay(3000);
}
✅ Expected Behavior
Servo rotates between 0° and 90°.
🔧 If Servo Vibrates
-
Use external 5V supply
-
Check common ground
-
Avoid powering from ESP32 5V
🔹 STEP 5 – Test Relay Modules
📌 Debug Code – Relay
#define AC_RELAY 26
#define PUMP_RELAY 27
void setup() {
Serial.begin(115200);
pinMode(AC_RELAY, OUTPUT);
pinMode(PUMP_RELAY, OUTPUT);
Serial.println("Relay Test Started...");
}
void loop() {
Serial.println("AC ON");
digitalWrite(AC_RELAY, LOW);
delay(3000);
Serial.println("AC OFF");
digitalWrite(AC_RELAY, HIGH);
delay(3000);
Serial.println("Pump ON");
digitalWrite(PUMP_RELAY, LOW);
delay(3000);
Serial.println("Pump OFF");
digitalWrite(PUMP_RELAY, HIGH);
delay(3000);
}
⚠ Important
Many relay modules are:
LOW = ON
HIGH = OFF
Test and confirm.
🧠 Why This Lesson Is Critical
If something fails in final integration:
You will know exactly which module is faulty.
This prevents:
-
Blind debugging
-
Frustration
-
Random code editing
🎓 Learning Outcomes
After this lesson, students will:
-
Test sensors individually
-
Calibrate readings
-
Debug hardware problems
-
Build confidence
-
Follow professional workflow