Project Introduction
In this project, we will build a complete system using an Arduino board to control multiple sensors and components. Our goal is to create a smart monitoring and control system that integrates various modules into one cohesive unit. The project begins with an overview of the hardware setup, including an image showing the project connections and a basic LED. Each section of this guide explains the working principle, wiring method, and sample code for a specific component.

Each section covers:
- Working Principle: How the component operates.
- Wiring Method: Detailed steps on connecting the component.
- Sample Code: Code snippets to demonstrate how to program the component using Arduino.
Step 1: LED
In this step, we explain how an LED works and how to connect it in the project.

LED Leads:
- Anode (Positive): Longer lead
- Cathode (Negative): Shorter lead, often marked with a flat edge
How It Works: The LED lights up when current flows from the anode to the cathode.
Wiring Steps:
- Choose a Digital Pin: Select an Arduino digital pin to control the LED.
- Connect the Resistor: Attach one end of a resistor (e.g., 220Ω) to the chosen pin to limit current.
- Connect the LED Anode: Link the resistor's other end to the LED’s anode.
- Connect the LED Cathode: Connect the LED’s cathode directly to Arduino’s GND.

CODE :
// Step 1: Simple LED Blink
const int ledPin = 5; // LED connected to digital pin 5
void setup() {
pinMode(ledPin, OUTPUT); // Set the pin as an output
}
void loop() {
digitalWrite(ledPin, HIGH); // Turn LED on
delay(1000); // Wait for 1 second
digitalWrite(ledPin, LOW); // Turn LED off
delay(1000); // Wait for 1 second
}
Step 2: PIR Sensor
In this step, we explain how the PIR motion sensor works and how to connect it to detect movement.

What is a PIR Sensor?
- Detects motion by measuring changes in infrared radiation from surrounding objects.
- Outputs a digital HIGH signal when a warm object (like a person) is detected.
Wiring Steps:
- Power Connection: Connect the VCC pin of the PIR sensor to the Arduino’s 5V.
- Ground Connection: Connect the GND pin to the Arduino’s GND.
- Output Connection: Connect the sensor’s output pin to an Arduino digital input.

CODE :
// Step 2: Integrating PIR Sensor with an LED Indicator
const int ledPin = 5; // LED on pin 5
const int pirPin = 3; // PIR sensor on pin 3
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(pirPin, INPUT); // Initialize PIR sensor as input
}
void loop() {
int motion = digitalRead(pirPin);
if (motion == HIGH) {
digitalWrite(ledPin, HIGH); // Turn LED on when motion is detected
} else {
digitalWrite(ledPin, LOW);
}
delay(500);
}
Step 3: DHT11 Sensor
This step explains how to use the DHT11 temperature and humidity sensor.

What is DHT11?
- Measures ambient temperature and humidity.
- Uses a thermistor and humidity sensor to output a digital signal.
Wiring Steps:
- VCC: Connect the DHT11 VCC pin to the Arduino's 5V.
- GND: Connect the GND pin to the Arduino’s GND.
- Data: Connect the data pin to one of the Arduino’s digital pins.
Library Installation:
- Install the "DHT" library from this link: DHT library
- Install the "Adafruit Unified Sensor" library via the IDE.

CODE :
#include 'DHT.h'
#define DHTPIN A0 // DHT11 sensor on pin A0
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
Serial.print("Temp: "); Serial.print(temperature);
Serial.print(" °C, Hum: "); Serial.print(humidity);
Serial.println(" %");
delay(2000);
}
Step 4: LCD Display
In this step, we explain how to connect an LCD display (I2C) to show information.

What is an LCD I2C?
- Communicates with the Arduino using the I2C protocol (only 2 data lines needed).
- Simplifies wiring and saves pins.
Wiring Steps:
- VCC: Connect the LCD's VCC to Arduino’s 5V.
- GND: Connect the LCD's GND to Arduino’s GND.
- SDA: Connect the LCD’s SDA to Arduino’s SDA line (pin A4).
- SCL: Connect the LCD’s SCL to Arduino’s SCL line (pin A5).
Library Installation: Install "LiquidCrystal_I2C" from the Library Manager.

CODE :
#include 'LiquidCrystal_I2C.h'
LiquidCrystal_I2C lcd(0x27, 16, 2); // Adjust address as needed
void setup() {
lcd.init();
lcd.backlight();
}
void loop() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("T:"); lcd.print(temperature); lcd.print("C ");
lcd.print("H:"); lcd.print(humidity); lcd.print("%");
delay(2000);
}
Step 5: Red LED for Alert
Here we explain how to add an alert using a red LED.
- Connect the red LED (with a suitable resistor) to the designated Arduino pin.

CODE :
const int redLEDPin = 4; // Red LED for alert
float tempThreshold = 30.0;
float humThreshold = 70.0;
void setup() {
pinMode(redLEDPin, OUTPUT);
}
void loop() {
bool alarm = false;
if (temperature > tempThreshold || humidity > humThreshold) {
alarm = true;
}
if (alarm) {
digitalWrite(redLEDPin, HIGH);
} else {
digitalWrite(redLEDPin, LOW);
}
delay(1000);
}
Step 6: Gas Sensor
In this step, we explain how to connect and use the gas sensor (MQ2) to detect gas leakage.

What is the MQ2 Sensor?
- Detects combustible gases and smoke (e.g., LPG, methane, propane).
- Provides an analog voltage output proportional to the gas concentration.
Wiring Steps:
- VCC: Connect the sensor's VCC to Arduino’s 5V.
- GND: Connect the sensor's GND to Arduino’s GND.
- AO (Analog Output): Connect the AO pin to an analog input.

CODE :
const int gasPin = A1; // MQ2 sensor connected to A1
void setup() {
Serial.begin(9600);
}
void loop() {
int gasValue = analogRead(gasPin);
lcd.setCursor(1, 0);
lcd.print("Gas:");
lcd.print(gasValue);
delay(1000);
}
Step 7: Buzzer
In this step, we explain how to connect a buzzer to produce an audible alert.
What is a Buzzer?
- Produces sound when voltage is applied.
- Commonly used in alarm systems and notifications.
Wiring Steps:
- VCC: Connect the positive terminal (through a resistor if needed) to a digital pin.
- GND: Connect the negative terminal to Arduino’s GND.

CODE :
const int buzzerPin = 7; // Buzzer for alert
int gasThreshold = 600;
void setup() {
pinMode(buzzerPin, OUTPUT);
}
void loop() {
int gasValue = analogRead(gasPin);
bool alarm = false;
if (gasValue > gasThreshold) {
alarm = true;
}
if (alarm) {
digitalWrite(buzzerPin, HIGH);
} else {
digitalWrite(buzzerPin, LOW);
}
delay(1000);
}
Step 8: RFID Module
In this step, we explain the RFID system and how to read cards for identification.

What is RFID?
- Uses radio waves to capture information stored on a tag.
- Commonly used for access control and identification.
Wiring Steps:
- VCC: Connect to 3.3V or 5V (check your module’s requirements).
- GND: Connect to Arduino’s GND.
- SDA (SS): Connect to a digital pin (pin 10).
- SCK: Connect to the Arduino’s SCK (pin 13).
- MOSI: Connect to the Arduino’s MOSI (pin 11).
- MISO: Connect to the Arduino’s MISO (pin 12).
- RST: Connect to a digital pin (pin 9).
Install the "MFRC522" library via the IDE.

CODE :
#include 'MFRC522.h'
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
// Authorized UID: 03 DD D7 24
byte authorizedUID[] = {0x03, 0xDD, 0xD7, 0x24};
const byte authorizedUIDSize = sizeof(authorizedUID);
// LCD for displaying message
LiquidCrystal_I2C lcd(0x27, 16, 2);
String userName = "AMMAR";
void setup() {
SPI.begin();
mfrc522.PCD_Init();
}
bool checkRFID() {
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
bool authorized = true;
if (mfrc522.uid.size != authorizedUIDSize) {
authorized = false;
} else {
for (byte i = 0; i < mfrc522.uid.size; i++) {
if (mfrc522.uid.uidByte[i] != authorizedUID[i])
authorized = false;
}
}
mfrc522.PICC_HaltA();
return authorized;
}
return false;
}
void loop() {
unsigned long startTime = millis();
bool validCard = false;
// Wait for RFID card for 5 seconds
while (millis() - startTime < 5000) {
if (checkRFID()) {
validCard = true;
break;
}
}
if (validCard) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Welcome,");
lcd.setCursor(0, 1);
lcd.print(userName);
// Open gate using servo (explained in the Servo section)
openGate();
}
delay(500);
}
Step 9: Servo Motor
This step explains how to use a servo motor and control its movement using the Arduino.

What is a Servo Motor?
- Provides precise control of angular position.
- Commonly used in robotics and automation.
Wiring Steps:
- VCC: Connect the servo’s red wire to Arduino’s 5V.
- GND: Connect the servo’s brown/black wire to Arduino’s GND.
- Control Signal: Connect the signal wire (orange/yellow) to a PWM digital pin.
Install the "Servo" library via the IDE.

CODE :
#include 'Servo.h'
const int servoPin = 2; // Servo controlling the gate
Servo gateServo;
void openGate() {
gateServo.write(90); // Open gate
delay(3000);
gateServo.write(0); // Close gate
}
Step 10: Complete Project Code
Below is the complete code integrating all the sensors and components mentioned above.
// ===================== Required Libraries =====================
#include 'LiquidCrystal_I2C.h'
#include 'DHT.h'
#include 'SPI.h'
#include 'MFRC522.h'
#include 'Servo.h'
// ===================== Pin Definitions & Variables =====================
// -- Simple LED & PIR --
const int pirPin = 3; // PIR sensor
const int pirLED = 5; // LED indicator for motion
// -- DHT11 Sensor --
#define DHTPIN A0 // DHT11 on A0
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
// -- LCD I2C --
LiquidCrystal_I2C lcd(0x27, 16, 2); // Adjust address as needed
// -- Red LED & Buzzer Alert --
const int redLEDPin = 4; // Red LED for alert
const int buzzerPin = 7; // Buzzer for alert
float tempThreshold = 30.0; // Temperature threshold (30°C)
float humThreshold = 70.0; // Humidity threshold (70%)
int gasThreshold = 600; // Gas sensor threshold
// -- Gas Sensor (MQ2) --
const int gasPin = A1; // MQ2 sensor on A1
// -- RFID Module --
#define SS_PIN 10 // RFID SDA/SS pin
#define RST_PIN 9 // RFID RST pin
MFRC522 mfrc522(SS_PIN, RST_PIN);
// -- Servo (Gate Control) --
const int servoPin = 2; // Servo for gate control
Servo gateServo;
// -- Authorized RFID UID --
byte authorizedUID[] = {0x03, 0xDD, 0xD7, 0x24};
const byte authorizedUIDSize = sizeof(authorizedUID);
// -- User Name for Welcome Message --
String userName = "AMMAR";
// ===================== Function Definitions =====================
// Handle simple LED & PIR indicator
void handleLEDandPIR() {
int motion = digitalRead(pirPin);
digitalWrite(pirLED, motion == HIGH ? HIGH : LOW);
delay(200);
}
// Handle environmental sensors and alarm (Red LED & Buzzer)
void handleSensorsAndAlarm() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
int gasValue = analogRead(gasPin);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("T:"); lcd.print(temperature); lcd.print("C ");
lcd.print("H:"); lcd.print(humidity); lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("Gas:"); lcd.print(gasValue);
bool alarm = (temperature > tempThreshold) || (humidity > humThreshold) || (gasValue > gasThreshold);
digitalWrite(redLEDPin, alarm ? HIGH : LOW);
digitalWrite(buzzerPin, alarm ? HIGH : LOW);
delay(1000);
}
// RFID checking function
bool checkRFID() {
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
bool authorized = true;
if (mfrc522.uid.size != authorizedUIDSize) {
authorized = false;
} else {
for (byte i = 0; i < mfrc522.uid.size; i++) {
if (mfrc522.uid.uidByte[i] != authorizedUID[i])
authorized = false;
}
}
mfrc522.PICC_HaltA();
return authorized;
}
return false;
}
// RFID handling and servo control
void handleRFIDAndServo() {
unsigned long startTime = millis();
bool validCard = false;
while (millis() - startTime < 5000) {
if (checkRFID()) {
validCard = true;
break;
}
}
if (validCard) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Welcome,");
lcd.setCursor(0, 1);
lcd.print(userName);
openGate();
}
delay(500);
}
void openGate() {
gateServo.write(90);
delay(3000);
gateServo.write(0);
}
// ===================== Setup & Loop =====================
void setup() {
pinMode(pirPin, INPUT);
pinMode(pirLED, OUTPUT);
dht.begin();
lcd.init();
lcd.backlight();
lcd.clear();
pinMode(redLEDPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
SPI.begin();
mfrc522.PCD_Init();
gateServo.attach(servoPin);
}
void loop() {
handleLEDandPIR();
handleSensorsAndAlarm();
handleRFIDAndServo();
}