SE322 /

Lab: Arduino Hardware

Prof. Anis Koubaa
Professor

Prof. Anis Koubaa

SE322: Internet of Things Applications
Spring 2025
College of Engineering, Alfaisal University

Arduino Smart Door - Extended Exercise

📅 Duration: 2 hours
🎯 Objective:
Enhance a basic Arduino door system with advanced features including sound notifications, visual indicators, and smart controls.

Introduction

This exercise is an extension of the Basic Arduino Door project. You have already built a system where an ultrasonic sensor detects objects, and a servo motor acts as a door that opens when an object is close. Now, you will enhance the project by adding additional features.

Circuit Diagram

Arduino Smart Door Circuit Diagram

Circuit diagram showing the connections between Arduino, ultrasonic sensor, servo motor, LEDs, buzzer, and LCD display.

Tasks Overview

1. Buzzer Alert (Sound Notification)

  • Add a buzzer that sounds when the door is opening
  • The buzzer should turn off when the door closes

2. LED Indicator (Visual Notification)

  • Add two LEDs:
    • Green LED turns on when the door is open
    • Red LED turns on when the door is closed

3. Delay Mechanism for Door Closure

  • Modify the code so that once the door opens, it stays open for 5 seconds before closing
  • If an object is still detected within 5 seconds, reset the timer

4. Distance Monitoring on Serial Monitor

  • Store the last 10 distance measurements in an array
  • Print the array to the Serial Monitor every 5 seconds

5. Manual Control via Serial Monitor

  • Allow users to type commands in the Serial Monitor:
    • Type "OPEN" → The door opens
    • Type "CLOSE" → The door closes
  • If no command is given, the system works based on the ultrasonic sensor

6. LCD Display for Real-Time Status

  • Connect a 16x2 LCD with I2C module
  • Display real-time distance and door status
  • Example display format:
    LCD Display Example
    Distance: 85cm
    Door Opening...
    
    Distance: 150cm
    Door Closed

Implementation Guide

Required Libraries and Components

  • LiquidCrystal_I2C library for the LCD display
  • Components needed:
    • Arduino board
    • Ultrasonic sensor (HC-SR04)
    • Servo motor
    • Buzzer
    • 2 LEDs (Green and Red)
    • 16x2 LCD with I2C module
    • Resistors and connecting wires
Arduino Code Template
#include 
#include 

// Pin Definitions
#define TRIG_PIN 9
#define ECHO_PIN 10
#define SERVO_PIN 11
#define BUZZER_PIN 8
#define GREEN_LED 7
#define RED_LED 6

// Constants
const int DOOR_OPEN_ANGLE = 90;
const int DOOR_CLOSE_ANGLE = 0;
const int DISTANCE_THRESHOLD = 30; // cm
const unsigned long DOOR_DELAY = 5000; // 5 seconds
const int ARRAY_SIZE = 10;

// Objects
Servo doorServo;
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set I2C address

// Global Variables
int distances[ARRAY_SIZE];
int distanceIndex = 0;
unsigned long lastPrintTime = 0;
unsigned long doorOpenTime = 0;
bool isDoorOpen = false;

void setup() {
    // Initialize serial communication
    Serial.begin(9600);
    
    // Initialize pins
    pinMode(TRIG_PIN, OUTPUT);
    pinMode(ECHO_PIN, INPUT);
    pinMode(BUZZER_PIN, OUTPUT);
    pinMode(GREEN_LED, OUTPUT);
    pinMode(RED_LED, OUTPUT);
    
    // Initialize servo
    doorServo.attach(SERVO_PIN);
    doorServo.write(DOOR_CLOSE_ANGLE);
    
    // Initialize LCD
    lcd.init();
    lcd.backlight();
    lcd.clear();
    
    // Initialize LED states
    digitalWrite(RED_LED, HIGH);
    digitalWrite(GREEN_LED, LOW);
}

void loop() {
    // Your implementation here
    // 1. Read distance
    // 2. Check for serial commands
    // 3. Update door state
    // 4. Update LED indicators
    // 5. Control buzzer
    // 6. Update LCD display
    // 7. Store and print distance history
}
                                    

Submission Requirements

  • Complete Arduino code with comments explaining each section
  • Circuit diagram showing all connections
  • Screenshots of:
    • Serial Monitor output showing distance history
    • LCD display showing different states
  • Brief report explaining your implementation choices

Grading Criteria

  • Functionality (40%)
    • All features working as specified
    • Proper integration of components
  • Code Quality (30%)
    • Well-structured and commented code
    • Efficient implementation
  • Documentation (20%)
    • Clear circuit diagram
    • Complete submission requirements
  • Creativity (10%)
    • Additional features or improvements
    • Innovative solutions

Review Questions

Question 1

Explain what baud rate is and why it's important in serial communication between Arduino and Python.

Question 2

Describe the process of reading sensor data from Arduino using Python. Include the key functions used on both sides.

Question 3

How would you modify the provided temperature sensor code to add humidity sensing? Show the changes needed in both Arduino and Python code.

Question 4

What are the common issues that might arise when working with serial communication, and how would you troubleshoot them?

Question 5

Design a system that uses both the ultrasonic sensor and LED. Explain how you would structure the data exchange between Arduino and Python.