← Microcontrollers 2

Assignment 2: IoT Security System with Computer Vision

Course: DE6417 Microcontrollers 2

Assessment Type: Assignment

Weighting: This assignment contributes towards the 10% allocated for Assignments/Tests.

Objective

The objective of this assignment is to design and implement a smart security system. You will interface an Arduino Uno R3 with a motion detector (HC-SR501) and a camera module (OV7670) to detect intruders. Upon detection, the system must trigger an alert and send an image to a user's Telegram account.

This advanced assignment requires you to utilize Interrupt Service Routines (ISRs) for efficient event handling and implement off-chip peripheral communication to bridge the microcontroller with an internet-connected gateway (your PC).

Learning Outcomes Assessed

This assignment provides an opportunity to demonstrate competency in the following course learning outcomes:


Part 1: Hardware Implementation

Learning Outcome Met: LO1 & LO2

Justification: This section requires the integration of complex off-chip sensors. You must interface a pyroelectric sensor and a parallel camera module, managing timing constraints and voltage levels (if applicable) in accordance with industry practice.

Task Specification

You are required to assemble a security circuit with the following functionality:

  1. Motion Detection: The PIR sensor interprets infrared changes and signals the MCU.
  2. Image Capture: The OV7670 module captures a frame upon trigger.
  3. Data Transmission: The image data is sent via UART (Serial) to a gateway.

System Overview

The complete system consists of three interconnected components working together:

  1. Hardware Layer (Arduino + Sensors): The physical circuit comprising the Arduino Uno, PIR motion sensor, and OV7670 camera module. This layer is responsible for detecting motion and capturing images.
  2. Firmware Layer (Arduino Sketch): The embedded C/C++ code running on the Arduino that manages interrupts, controls the camera, and handles serial communication.
  3. Gateway Layer (Python Script on PC): A Python application running on your computer that receives image data via USB serial connection and forwards it to Telegram.

Setup Process Overview

To complete this assignment, you will follow these steps in order:

Step Task Description
1 Component Assembly Gather all required components and verify they are functional.
2 Hardware Wiring Connect the PIR sensor and OV7670 camera to the Arduino Uno following the pinout diagram.
3 Power Verification Ensure the 5V and 3.3V power rails are correctly connected. The OV7670 requires 3.3V logic levels.
4 Arduino IDE Setup Install the Arduino IDE and any required libraries for the OV7670 camera.
5 ISR Implementation Write and test the Interrupt Service Routine for motion detection (Stage A).
6 Camera Configuration Configure the OV7670 registers via I2C and implement the image capture routine (Stage B).
7 Serial Transmission Implement the UART protocol to send image data to the PC.
8 Python Gateway Setup Install Python dependencies (pyserial, requests) and create the gateway script (Stage C).
9 Telegram Bot Setup Create a Telegram bot using BotFather and obtain your bot token and chat ID.
10 Integration Testing Test the complete system end-to-end: motion → capture → transmit → Telegram notification.

What You Need to Deliver

By the end of this assignment, you should have:

Hardware Pinout — OV7670 Camera & HC-SR501 PIR → Arduino Uno
SignalArduino PinOV7670 / PIR PinNotes
HC-SR501 PIR Motion Sensor
Power5VVCC5V supply for PIR module
GroundGNDGNDCommon ground
OutputD2OUTExternal interrupt INT0 (RISING edge)
OV7670 Camera Module (3.3 V logic — do not connect data pins directly to 5 V Arduino pins without level-shifting)
VCC3.3 VVCC3.3 V ONLY — use Arduino 3.3 V output or LDO
GNDGNDGNDCommon ground
SDA (I²C)A4SIODOpen-drain I²C Data — add 4.7 kΩ pull-up to 3.3 V; no voltage divider needed
SCL (I²C)A5SIOCOpen-drain I²C Clock — add 4.7 kΩ pull-up to 3.3 V; no voltage divider needed
VSYNCD7VSYNCFrame-sync pulse (active once per frame)
HREFD6HREFLine-sync (HIGH during valid pixel row)
PCLKD5PCLKPixel clock — sample data on each rising edge
XCLKD4XCLK⚠️ Arduino drives this — use voltage divider (5 V → 3.3 V). Master clock to camera (8 MHz via Timer2 OC2B).
D0 (pixel)D8D0Pixel data bit 0 — camera output to Arduino input (no divider needed)
D1 (pixel)D9D1Pixel data bit 1 — camera output to Arduino input (no divider needed)
D2 (pixel)D10D2Pixel data bit 2
D3 (pixel)D11D3Pixel data bit 3
D4 (pixel)D12D4Pixel data bit 4
D5 (pixel)D13D5Pixel data bit 5
D6 (pixel)A0D6Pixel data bit 6 (used as digital)
D7 (pixel)A1D7Pixel data bit 7 (used as digital)
RESETD3RESET⚠️ Arduino drives this — use voltage divider (5 V → 3.3 V). Active-LOW reset.
PWDNGNDPWDNPower-down (active HIGH). Tie to GND to keep camera active.
Voltage Warning: Only two pins need a resistor voltage divider (2.2 kΩ / 3.3 kΩ, 5 V → 3.3 V): XCLK (D4) and RESET (D3) — the Arduino actively drives these as outputs into the camera. Pixel data pins D8–D13, A0–A1 are camera outputs → Arduino inputs and require no level shifting. I²C lines A4/A5 are open-drain — add 4.7 kΩ pull-up resistors to 3.3 V on both lines instead of a voltage divider.

Required Components

Hardware Setup Notes


Part 2: Software Development

Learning Outcome Met: LO5

Justification: You must prioritize fault tolerance and specification compliance. The system must not "poll" the sensor continuously; instead, it must use interrupts to respond to motion events immediately, allowing the main processor to remain in a low-power or idle state otherwise.

Stage A: Motion Detection via ISR

Implement an Interrupt Service Routine (ISR) to handle the PIR sensor trigger.

Example Code Structure:

volatile bool motionDetected = false;

void setup() {
  pinMode(2, INPUT); // PIR connected to Pin 2
  attachInterrupt(digitalPinToInterrupt(2), isr_motion, RISING);
}

void isr_motion() {
  motionDetected = true; // Set flag only
}
Constraint: Do not put heavy logic (like camera capture or serial prints) inside the ISR. Keep it minimal to prevent blocking the processor.

Stage B: Camera Interface & Image Transmission

In your main loop():

  1. Check if the motionDetected flag is set.
  2. If set, initialize the OV7670 capture sequence.
  3. Read the pixel data from the camera.
  4. Send the data via Serial.write() to your PC.
  5. Reset the flag and system state.
System Logic Flowchart — IoT Security System Hardware Layer Firmware (Arduino) Gateway (Python/PC) System Start / setup() attachInterrupt(D2, isr_motion, RISING) PIR sensor wired to D2 (INT0) PIR triggers? (motionDetected flag) NO → loop() YES ISR fires (isr_motion) sets motionDetected = true Capture OV7670 frame Read pixel data via parallel bus Serial.write(imageData) Send bytes via USB to PC (115200 baud) Python: ser.read() → save as detected.jpg pyserial reads bytes from USB port POST to Telegram Bot API requests.post("/sendPhoto", files={'photo': img}) motionDetected = false Return to idle loop() 📱 Intruder image sent to Telegram!

Stage C: Telegram Integration (The Gateway)

Since the Arduino Uno lacks native internet connectivity, you will develop a gateway script in Python running on your computer.

Python Gateway Example:

# pip install pyserial requests
import serial
import requests

# 1. Setup Serial Connection
ser = serial.Serial('COM3', 115200) # Adjust 'COM3' or '/dev/tty...'

# 2. Logic loop
while True:
    if ser.in_waiting > 0:
        # [Add code to read image bytes from serial save to 'detected.jpg']
        
        # 3. Send to Telegram
        bot_token = 'YOUR_BOT_TOKEN'
        chat_id = 'YOUR_CHAT_ID'
        url = f'https://api.telegram.org/bot{bot_token}/sendPhoto'
        
        with open('detected.jpg', 'rb') as img:
            files = {'photo': img}
            requests.post(url, data={'chat_id': chat_id}, files=files)

Implementation Requirements

Submission

You must submit a zip file containing:

  1. Arduino Sketch (.ino): Well-commented code.
  2. Gateway Script: The Python script used for the Telegram interface.
  3. Demonstration Video: A short video showing the motion trigger -> Telegram notification.

Grading Rubric

Criteria Marks LO
Hardware Implementation
Camera connections, sensor usage, logic levels, wiring quality.
20 LO1, LO2
Stage A: Interrupts (ISR)
Correct use of attachInterrupt; Minimal ISR logic (flag only).
20 LO5
Stage B: Camera & Serial
State machine logic; Successful image capture and transmission.
30 LO2
Stage C: Python Gateway
Python serial reading; Telegram API integration.
20 LO5
Code Quality
Register documentation; Non-blocking structure.
10 LO5
Total 100