Course: DE6417 Microcontrollers 2
Assessment Type: Assignment
Weighting: This assignment contributes towards the 10% allocated for Assignments/Tests.
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).
This assignment provides an opportunity to demonstrate competency in the following course learning outcomes:
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.
You are required to assemble a security circuit with the following functionality:
The complete system consists of three interconnected components working together:
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. |
By the end of this assignment, you should have:
| Signal | Arduino Pin | OV7670 / PIR Pin | Notes |
|---|---|---|---|
| HC-SR501 PIR Motion Sensor | |||
| Power | 5V | VCC | 5V supply for PIR module |
| Ground | GND | GND | Common ground |
| Output | D2 | OUT | External 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) | |||
| VCC | 3.3 V | VCC | 3.3 V ONLY — use Arduino 3.3 V output or LDO |
| GND | GND | GND | Common ground |
| SDA (I²C) | A4 | SIOD | Open-drain I²C Data — add 4.7 kΩ pull-up to 3.3 V; no voltage divider needed |
| SCL (I²C) | A5 | SIOC | Open-drain I²C Clock — add 4.7 kΩ pull-up to 3.3 V; no voltage divider needed |
| VSYNC | D7 | VSYNC | Frame-sync pulse (active once per frame) |
| HREF | D6 | HREF | Line-sync (HIGH during valid pixel row) |
| PCLK | D5 | PCLK | Pixel clock — sample data on each rising edge |
| XCLK | D4 | XCLK | ⚠️ Arduino drives this — use voltage divider (5 V → 3.3 V). Master clock to camera (8 MHz via Timer2 OC2B). |
| D0 (pixel) | D8 | D0 | Pixel data bit 0 — camera output to Arduino input (no divider needed) |
| D1 (pixel) | D9 | D1 | Pixel data bit 1 — camera output to Arduino input (no divider needed) |
| D2 (pixel) | D10 | D2 | Pixel data bit 2 |
| D3 (pixel) | D11 | D3 | Pixel data bit 3 |
| D4 (pixel) | D12 | D4 | Pixel data bit 4 |
| D5 (pixel) | D13 | D5 | Pixel data bit 5 |
| D6 (pixel) | A0 | D6 | Pixel data bit 6 (used as digital) |
| D7 (pixel) | A1 | D7 | Pixel data bit 7 (used as digital) |
| RESET | D3 | RESET | ⚠️ Arduino drives this — use voltage divider (5 V → 3.3 V). Active-LOW reset. |
| PWDN | GND | PWDN | Power-down (active HIGH). Tie to GND to keep camera active. |
attachInterrupt on the Uno).SIOC and SIOD (I2C) correctly, along with the VSYNC and PCLK synchronization pins.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.
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
}
In your main loop():
motionDetected flag is set.Serial.write() to your PC.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)
115200 or 1000000) to transfer image data quickly.You must submit a zip file containing:
| 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 |