Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino UNO-Based Smart Door System with GSM and Ultrasonic Sensor

View and Edit Project In Cirkit DesignerImage of Arduino UNO-Based Smart Door System with GSM and Ultrasonic Sensor

Circuit Documentation

Summary

This circuit is designed to control a door using an Arduino UNO, an MG90S servo motor, an HC-SR04 ultrasonic sensor, a SIM 800L GSM module, and various other components. The system can open and close the door based on button presses, ultrasonic sensor readings, and GSM commands. It also sends SMS notifications when someone is detected at the door.

Component List

  1. Arduino UNO

    • Description: Microcontroller board based on the ATmega328P.
    • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
  2. HC-SR04 Ultrasonic Sensor

    • Description: Ultrasonic distance sensor.
    • Pins: VCC, TRIG, ECHO, GND
  3. MG90S Servo Motor

    • Description: Micro servo motor.
    • Pins: SIG, VCC, GND
  4. SIM 800L V2.0 GSM Module

    • Description: GSM module for sending and receiving SMS.
    • Pins: SIM_TXD, VDD, SIM.RXD, 5V/4V, GND, RST
  5. Buck Converter

    • Description: DC-DC step-down converter.
    • Pins: IN+, IN-, OUT+, OUT-
  6. 2.1mm DC Barrel Jack

    • Description: Power connector.
    • Pins: switch, sleeve, center
  7. Pushbutton

    • Description: Momentary pushbutton switch.
    • Pins: Pin 2, Pin 1, Pin 3, Pin 4
  8. LED: Two Pin (red)

    • Description: Red LED.
    • Pins: cathode, anode
  9. LED: Two Pin (green)

    • Description: Green LED.
    • Pins: cathode, anode
  10. Resistor

    • Description: 200 Ohm resistor.
    • Pins: pin1, pin2
    • Properties: Resistance = 200 Ohms

Wiring Details

Arduino UNO

HC-SR04 Ultrasonic Sensor

MG90S Servo Motor

SIM 800L V2.0 GSM Module

Buck Converter

2.1mm DC Barrel Jack

Pushbutton

LED: Two Pin (red)

LED: Two Pin (green)

Resistor

Code Documentation

#include <Servo.h>
#include <SoftwareSerial.h>

// Pin definitions
const int trigPin = 4;
const int echoPin = 3;
const int greenLEDPin = 5;
const int redLEDPin = 6;
const int buttonPin = 10;
const int servoPin = 9;
const int gsmTXPin = 7;
const int gsmRXPin = 8;

Servo doorServo;
SoftwareSerial gsmSerial(gsmTXPin, gsmRXPin);

const char allowedPhoneNumber[] = "+254701568555";
const long detectionRange = 400; // 4 meters in centimeters

void setup() {
  // Initialize serial communication
  Serial.begin(9600);
  gsmSerial.begin(9600);

  // Initialize pins
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(greenLEDPin, OUTPUT);
  pinMode(redLEDPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP); // Use internal pull-up resistor

  // Attach the servo motor
  doorServo.attach(servoPin);

  // Initialize the door to closed position
  closeDoor();

  // Initialize GSM module
  initializeGSM();
}

void loop() {
  // Check for button press
  if (digitalRead(buttonPin) == LOW) { // Adjust for pull-up resistor
    toggleDoor();
    delay(1000); // Debounce delay
  }

  // Check for GSM command
  if (gsmSerial.available() > 0) {
    handleGSM();
  }

  // Check for ultrasonic sensor trigger
  long distance = measureDistance();
  if (distance > 0 && distance <= detectionRange) {
    sendSMS(allowedPhoneNumber, "Someone is at the door");
    delay(10000); // Wait 10 seconds before checking again to avoid spamming
  }
}

void openDoor() {
  doorServo.write(90); // Rotate servo to 90 degrees
  digitalWrite(greenLEDPin, HIGH); // Turn on green LED
  digitalWrite(redLEDPin, LOW); // Turn off red LED
}

void closeDoor() {
  doorServo.write(0); // Rotate servo to 0 degrees
  digitalWrite(greenLEDPin, LOW); // Turn off green LED
  digitalWrite(redLEDPin, HIGH); // Turn on red LED
}

void toggleDoor() {
  static bool doorOpen = false;
  if (doorOpen) {
    closeDoor();
  } else {
    openDoor();
    sendSMS(allowedPhoneNumber, "The door has been opened manually with button");
  }
  doorOpen = !doorOpen;
}

void initializeGSM() {
  delay(1000); // Give time for GSM module to initialize
  gsmSerial.println("AT"); // Check if the module is ready
  delay(1000);
  gsmSerial.println("AT+CMGF=1"); // Set SMS to text mode
  delay(1000);
  gsmSerial.println("AT+CNMI=1,2,0,0,0"); // Configure the module to send SMS data to the serial port
  delay(1000);
}

void handleGSM() {
  if (gsmSerial.find("+CMT:")) {
    delay(10); // Wait for the rest of the message to arrive
    String senderNumber = gsmSerial.readStringUntil(',');
    senderNumber.trim();
    senderNumber.remove(0, 1); // Remove the leading "
    senderNumber.remove(senderNumber.length() - 1); // Remove the trailing "

    if (senderNumber == allowedPhoneNumber) {
      String message = gsmSerial.readStringUntil('\n');
      message.trim();
      if (message.equalsIgnoreCase("Open")) {
        openDoor();
      }
    }
  }
}

void sendSMS(const char* phoneNumber, const char* message) {
  gsmSerial.print("AT+CMGS=\"");
  gsmSerial.print(phoneNumber);
  gsmSerial.println("\"");
  delay(100); // Wait for the prompt
  gsmSerial.print(message);
  delay(100); // Wait for the message to be sent
  gsmSerial.write