OVERVIEW :
 

This project monitors an IR sensor using a Jetson Nano, provides real-time status updates via a Flask web server, and displays a live dashboard with:
● Object Detection Alerts
● Detection Counter
● Detection History (Last 5 events)

Required Components:

● Jetson Nano
● IR Sensor Module (Active Low)
● Jumper Wires

Software Dependencies:

● Python 3
● Flask (for web server)
● Jetson.GPIO (for GPIO control)

Circuit Connections :




Install Required Packages:
pip3 install Jetson.GPIO
pip3 install Jetson.GPIO
Code Implementation:

Create an new directory in jetson nano and inside that directory create a new python file
●main.py (Backend - Flask & Jetson GPIO)
●This script:r
✅ Reads the IR sensor status.
✅ Updates a live counter for object detections.
✅ Stores the last 5 detection events.
✅ Runs a Flask web server for real-time monitoring.

Code

import Jetson.GPIO as GPIO import time from flask import Flask, render_template, jsonify import threading # GPIO Setup IR_SENSOR_PIN = 18 GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) GPIO.setup(IR_SENSOR_PIN, GPIO.IN) # Flask Web App app = Flask(__name__) # Global variables sensor_value = 1 # Default (No object) detection_count = 0 # Count the number of detections detection_history = [] # Store last 5 detections # Function to monitor the IR sensor def monitor_sensor(): global sensor_value, detection_count, detection_history while True: new_value = GPIO.input(IR_SENSOR_PIN) # Read sensor value if new_value == 0 and sensor_value == 1: # Object detected (falling edge) detection_count += 1 detection_history.append(time.strftime("%H:%M:%S")) # Add timestamp if len(detection_history) > 5: # Keep only last 5 records detection_history.pop(0) sensor_value = new_value # Update the global value time.sleep(0.5) # Update every 500ms # Routes @app.route("/") def index(): return render_template("index.html") @app.route("/sensor_data") def sensor_data(): return jsonify({ "sensor_value": sensor_value, "detection_count": detection_count, "detection_history": detection_history }) # Start background sensor monitoring threading.Thread(target=monitor_sensor, daemon=True).start() # Run Flask app if __name__ == "__main__": app.run(host="0.0.0.0", port=5000, debug=True)
 

● Then create a new folder as templates inside that project directory create an new html file and paste the following code it in index.html (Frontend - Web Dashboard) This web interface:
✅ Shows real-time sensor status.
✅ Displays a detection counter.
✅ Lists the last 5 detection events.

Code:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>IR Sensor Dashboard</title>
    <style>
        body { font-family: Arial, sans-serif; text-align: center; padding: 20px; transition: 0.3s; }
        .status-box { font-size: 24px; padding: 20px; border-radius: 10px; display: inline-block; }
        .safe { background: green; color: white; }
        .danger { background: red; color: white; animation: blink 0.5s infinite alternate; }
        @keyframes blink { from { opacity: 1; } to { opacity: 0.5; } }
        .dark-mode { background: #333; color: white; }
        button { padding: 10px; margin: 10px; cursor: pointer; }
    </style>
    <script>
        function updateStatus() {
            fetch('/sensor_data')
                .then(response => response.json())
                .then(data => {
                    let statusBox = document.getElementById("sensorStatus");
                    let counterBox = document.getElementById("detectionCount");
                    let historyList = document.getElementById("detectionHistory");

                    // Update status
                    if (data.sensor_value === 0) {
                        statusBox.innerText = "⚠ Object Detected!";
                        statusBox.className = "status-box danger";
                    } else {
                        statusBox.innerText = "✔ No Object";
                        statusBox.className = "status-box safe";
                    }

                    // Update counter
                    counterBox.innerText = "Detections: " + data.detection_count;

                    // Update history
                    historyList.innerHTML = "";
                    data.detection_history.forEach(time => {
                        let li = document.createElement("li");
                        li.textContent = "Detected at " + time;
                        historyList.appendChild(li);
                    });
                });
        }

        function toggleDarkMode() {
            document.body.classList.toggle("dark-mode");
        }

        setInterval(updateStatus, 500);
    </script>
</head>
<body>
    <h1>IR Sensor Monitoring</h1>
    <button onclick="toggleDarkMode()">🌙 Toggle Dark Mode</button>
    <div id="sensorStatus" class="status-box safe">✔ No Object</div>
    <p id="detectionCount">Detections: 0</p>
    <h3>Detection History</h3>
    <ul id="detectionHistory"></ul>
</body>
</html>

● Setup the Environment
● Ensure you are in the project directory and all dependencies are installed.

● Step 2: Start the Flask Web Server
● Run the Python script:
● python3 main.py
● You'll see an output like:
● Running on http://127.0.0.1:5000 or
● Running on http://192.168.1.X:5000



● Access the Web Dashboard
●Open a browser and go to:
●http://192.168.1.X:5000





« Home

×