0% found this document useful (0 votes)
5 views5 pages

Smart Door Control System Code

jgdawdgr

Uploaded by

yohannestafete
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Smart Door Control System Code

jgdawdgr

Uploaded by

yohannestafete
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Software design

The software design includes the following key components:


 Sensor Detection Code: The microcontroller is programmed to constantly check the
sensor’s state (whether motion is detected) and take action when needed.
 Actuator Control Code: The code for controlling the actuator, i.e., sending signals to
open or close the door.
 Remote Control Interface (Optional): The remote interface code, allowing the user to
monitor the door status and open/close it remotely.
Programming Languages and Tools:
 Programming Language:
o For Arduino: C++ (using Arduino IDE).
o For Raspberry Pi: Python (using libraries like GPIO Zero or [Link]).
 Development Tools:
o Arduino IDE (for coding, uploading, and debugging).
o Thonny IDE or VS Code (for Raspberry Pi development).

Algorithm for Door Control


The following steps outline the core algorithm:
1. Initialize the sensor, actuator, and any communication modules.
2. Continuously read sensor data.
3. If the sensor detects motion:
o Send a signal to the actuator to open the door.
o Start a delay timer.
4. If no motion is detected after the delay timer expires:
o Send a signal to the actuator to close the door.
5. Repeat the process.

Code Implementation
Example Code for Arduino
cpp
#include <Servo.h>
// Pin Definitions
#define PIR_PIN 2 // PIR sensor connected to pin 2
#define SERVO_PIN 9 // Servo motor connected to pin 9

// Global Variables
Servo doorServo; // Servo motor object
int sensorValue = 0; // Variable to store sensor reading
int doorOpenAngle = 90; // Angle to open the door
int doorCloseAngle = 0; // Angle to close the door
unsigned long openTime = 0; // Tracks when the door was opened
const unsigned long delayTime = 5000; // 5 seconds delay before closing

void setup() {
pinMode(PIR_PIN, INPUT);
[Link](SERVO_PIN);
[Link](doorCloseAngle); // Start with door closed
[Link](9600);
}

void loop() {
sensorValue = digitalRead(PIR_PIN);

if (sensorValue == HIGH) { // Motion detected


[Link]("Motion Detected!");
[Link](doorOpenAngle); // Open the door
openTime = millis(); // Record the time
}

// Close the door after delay if no motion


if ((millis() - openTime) > delayTime && sensorValue == LOW) {
[Link]("Closing the door.");
[Link](doorCloseAngle);
}
}
Optional: Bluetooth Integration (Using HC-05)
For remote control using a mobile app:
1. Establish a Bluetooth connection using the HC-05 module.
2. Add commands to open/close the door manually.
Example Addition to Code:
cpp
Copy code
#include <SoftwareSerial.h>

// Bluetooth Module Pins


SoftwareSerial BTSerial(10, 11); // RX, TX

void setup() {
[Link](9600); // Start Bluetooth communication
...
}

void loop() {
// Check for Bluetooth command
if ([Link]()) {
char command = [Link]();
if (command == 'O') { // 'O' for Open
[Link](doorOpenAngle);
} else if (command == 'C') { // 'C' for Close
[Link](doorCloseAngle);
}
}
...
}

For Raspberry Pi (Python)


Here’s a similar implementation using Python:
python
Copy code
import [Link] as GPIO
import time

# Pin Definitions
PIR_PIN = 17
SERVO_PIN = 18

# Setup
[Link]([Link])
[Link](PIR_PIN, [Link])
[Link](SERVO_PIN, [Link])

# Servo Setup
servo = [Link](SERVO_PIN, 50) # 50 Hz PWM frequency
[Link](0)

def set_servo_angle(angle):
duty = 2 + (angle / 18) # Convert angle to duty cycle
[Link](SERVO_PIN, True)
[Link](duty)
[Link](1)
[Link](SERVO_PIN, False)
[Link](0)
try:
while True:
if [Link](PIR_PIN): # Motion detected
print("Motion Detected!")
set_servo_angle(90) # Open door
[Link](5) # Delay before closing
else:
print("No motion, closing door.")
set_servo_angle(0) # Close door
[Link](1)

except KeyboardInterrupt:
print("Exiting program.")
[Link]()

You might also like