Back

Smart Monitoring and Control System using Arduino: Step by Step Guide

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.

Project overview with connections and LED

Each section covers:

Finally, all the individual parts are integrated into one complete project code to create a fully functional smart monitoring and control system. Enjoy building your project step by step!

Step 1: LED

In this step, we explain how an LED works and how to connect it in the project.

LED image

LED Leads:

How It Works: The LED lights up when current flows from the anode to the cathode.

Wiring Steps:

LED wiring diagram

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.

PIR sensor image

What is a PIR Sensor?

Wiring Steps:

PIR sensor wiring diagram

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.

DHT11 sensor image

What is DHT11?

Wiring Steps:

Library Installation:

DHT11 wiring diagram

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.

LCD display image

What is an LCD I2C?

Wiring Steps:

Library Installation: Install "LiquidCrystal_I2C" from the Library Manager.

LCD wiring diagram

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.

Red LED wiring diagram

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.

Gas sensor image

What is the MQ2 Sensor?

Wiring Steps:

Gas sensor wiring diagram

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?

Wiring Steps:

Buzzer wiring diagram

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.

RFID module image

What is RFID?

Wiring Steps:

Install the "MFRC522" library via the IDE.

  • Code for RFID card reading card reading
  • RFID wiring diagram

    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.

    Servo motor image

    What is a Servo Motor?

    Wiring Steps:

    Install the "Servo" library via the IDE.

    Servo wiring diagram

    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();
    }