Jump to content

MDM331AID/Experiment 2

From cognifront

Experiment 2: Obstacle Detection and LED Notification using IR Sensor with Arduino UNO and Raspberry Pi

Course: MDM331AID – Internet of Things

Program: TE Artificial Intelligence and Data Science Engineering

University: Savitribai Phule Pune University


1. Aim

To implement an obstacle detection system using an IR sensor interfaced with Arduino UNO and Raspberry Pi, and notify users about detected obstacles using LEDs.

2. Objectives

  • Understand the working principle of an IR obstacle detection sensor.
  • Interface an IR sensor with Arduino UNO and Raspberry Pi.
  • Read digital sensor signals using GPIO pins.
  • Control LEDs based on obstacle detection.
  • Understand digital input and output operations in IoT devices.

3. Required Components

Sr. No. Component Quantity Kit
1 Arduino UNO 1 COG-U1
2 Raspberry Pi 4 (4 GB) 1 COG-RPI4
3 IR Sensor 1 COG-MSBA
4 LED Bar 1 COG-U1
5 LED 1 COG-RPI4
6 USB Data Cable 1 COG-U1
7 Raspberry Pi Power Supply 1 COG-RPI4
8 Patch Cords As required Available kits

Software Requirements

  • Arduino IDE
  • Raspberry Pi OS
  • Python 3
  • GPIO Zero Python library

4. Theory

4.1 IR Obstacle Detection Sensor

An infrared (IR) obstacle detection sensor detects objects using infrared light. It generally consists of an IR LED transmitter and an IR receiver.

The IR transmitter emits infrared light. When an object is present in the sensing range, some of the emitted light is reflected towards the receiver. The sensor's detection circuit processes the received light and generates a digital output.

The output is commonly used to indicate whether an obstacle has been detected.

4.2 Working Principle

The IR obstacle detection sensor operates using infrared reflection.

  • The IR transmitter continuously emits infrared light.
  • When an object enters the sensing range, infrared light is reflected.
  • The receiver detects the reflected light.
  • The sensor's output changes state according to the detection condition.
  • The controller reads the sensor output and controls the LED accordingly.

Many common IR obstacle modules provide an active-low digital output, meaning the output becomes LOW when an obstacle is detected. Other modules may use the opposite logic.

4.3 GPIO Interfacing

GPIO stands for General Purpose Input/Output. GPIO pins can be configured as digital inputs or outputs.

In this experiment:

  • The IR sensor is connected to a digital input pin.
  • The LED is connected to a digital output pin.
  • The controller reads the IR sensor state.
  • The LED is turned ON or OFF according to the detection state.

5. Pin Connections

5.1 IR Sensor and LED with Arduino UNO

Component Pin Arduino UNO Pin Description
IR Sensor VCC 5V Power supply, if supported by the sensor module
IR Sensor GND GND Common ground
IR Sensor OUT D2 Digital sensor input
LED Bar Input 1 D8 Obstacle indication

Note: The LED bar in COG-U1 is assumed to have compatible input connections and built-in current-limiting resistors. Verify the actual LED bar pin labels and polarity.

5.2 IR Sensor and LED with Raspberry Pi

Component Pin Raspberry Pi Pin Physical Pin
IR Sensor VCC 3.3V 1
IR Sensor GND GND 6
IR Sensor OUT GPIO17 (BCM) 11
LED Anode / signal input GPIO27 (BCM) 13
LED Cathode / ground GND 9

Important: These Raspberry Pi connections assume the IR sensor can operate at 3.3V and its output is compatible with 3.3V GPIO. Never connect a 5V sensor output directly to a Raspberry Pi GPIO pin. Check the LED module's built-in current-limiting arrangement before connecting it directly.

6. Connection Diagram

6.1 Arduino UNO

       +----------------------+
       |      Arduino UNO     |
       |                      |
       | 5V o---------------------- VCC
       |                      |    IR Sensor
       | D2 o---------------------- OUT
       |                      |
       | GND o--------------------- GND
       |                      |
       | D8 o---------------------- LED Bar Input 1
       |                      |
       +----------------------+

6.2 Raspberry Pi

       +----------------------+
       |    Raspberry Pi 4    |
       |                      |
       | 3.3V o------------------- VCC
       |                      |   IR Sensor
       | GPIO17 o----------------- OUT
       |                      |
       | GND o-------------------- GND
       |                      |
       | GPIO27 o----------------- LED Input
       |                      |
       +----------------------+

7. Program Code

7.1 Arduino UNO Program

The following program assumes that the IR sensor output is active LOW and the LED bar input is active HIGH.

#define IR_PIN 2
#define LED_PIN 8

void setup()
{
    pinMode(IR_PIN, INPUT);
    pinMode(LED_PIN, OUTPUT);

    Serial.begin(9600);

    Serial.println("IR Obstacle Detection");
}

void loop()
{
    int sensorState = digitalRead(IR_PIN);

    if (sensorState == LOW)
    {
        digitalWrite(LED_PIN, HIGH);

        Serial.println("Obstacle Detected");
    }
    else
    {
        digitalWrite(LED_PIN, LOW);

        Serial.println("No Obstacle");
    }

    delay(200);
}

7.2 Raspberry Pi Program

Step 1: Install GPIO Zero if it is not already available.

sudo apt update
sudo apt install python3-gpiozero

Step 2: Create a Python file named `ir_obstacle.py`.

from gpiozero import DigitalInputDevice, LED
from time import sleep

ir_sensor = DigitalInputDevice(17, pull_up=None)
led = LED(27)

print("IR Obstacle Detection System")

try:
    while True:
        if ir_sensor.value == 0:
            led.on()
            print("Obstacle Detected")
        else:
            led.off()
            print("No Obstacle")

        sleep(0.2)

except KeyboardInterrupt:
    print("Monitoring Stopped")

finally:
    led.off()
    ir_sensor.close()
    led.close()

Step 3: Execute the program.

python3 ir_obstacle.py

Note: The Raspberry Pi program also assumes an active-low sensor output. If your module is active HIGH, reverse the condition in the program. The `pull_up=None` option assumes that the IR module provides a defined digital output.

8. Code Explanation

8.1 Arduino UNO Program

  • `#define IR_PIN 2` assigns digital pin 2 to the IR sensor.
  • `#define LED_PIN 8` assigns digital pin 8 to the LED indicator.
  • `pinMode(IR_PIN, INPUT)` configures the IR sensor pin as a digital input.
  • `pinMode(LED_PIN, OUTPUT)` configures the LED pin as a digital output.
  • `digitalRead(IR_PIN)` reads the sensor output.
  • `sensorState == LOW` checks for obstacle detection under the assumed active-low logic.
  • `digitalWrite(LED_PIN, HIGH)` turns ON the LED indicator.
  • `digitalWrite(LED_PIN, LOW)` turns OFF the LED indicator.
  • `Serial.println()` displays the detection status.
  • `delay(200)` introduces a short interval between successive readings.

8.2 Raspberry Pi Program

  • `DigitalInputDevice(17)` configures GPIO17 as a digital input.
  • `LED(27)` configures GPIO27 as an LED output.
  • `ir_sensor.value` reads the digital state of the IR sensor.
  • `if ir_sensor.value == 0` checks the active-low detection condition.
  • `led.on()` activates the LED.
  • `led.off()` deactivates the LED.
  • `sleep(0.2)` pauses execution for 200 milliseconds.
  • `KeyboardInterrupt` allows the program to stop safely using Ctrl+C.
  • `finally` ensures the LED is switched off and GPIO resources are released.

9. Procedure

9.1 Arduino UNO

  1. Connect the IR sensor to Arduino UNO using patch cords as per the pin connection table.
  2. Connect the LED bar input to the designated Arduino digital output pin.
  3. Connect Arduino UNO to the computer using the USB data cable.
  4. Open Arduino IDE and select Arduino UNO and the correct COM port.
  5. Enter the Arduino program.
  6. Compile and upload the program.
  7. Open the Serial Monitor at 9600 baud.
  8. Place an object in front of the IR sensor.
  9. Observe the LED indication and serial output.
  10. Remove the object and observe the change in output.

9.2 Raspberry Pi

  1. Connect the IR sensor and LED to Raspberry Pi using patch cords.
  2. Verify the sensor supply voltage and output logic compatibility.
  3. Power ON the Raspberry Pi.
  4. Open the terminal and install GPIO Zero if required.
  5. Create the Python program.
  6. Execute the program.
  7. Place an object in front of the IR sensor.
  8. Observe the LED and terminal output.
  9. Remove the object and observe the change in output.

10. Observation and Output

10.1 Observation Table

Sr. No. Object Position IR Sensor Output LED Status System Response
1 No object
2 Object within sensing range
3 Object removed
4 Object placed at different distance

10.2 Expected Output

Arduino Serial Monitor / Raspberry Pi Terminal

IR Obstacle Detection System
No Obstacle
No Obstacle
Obstacle Detected
Obstacle Detected
No Obstacle

Expected LED Behavior

  • LED ON when an obstacle is detected.
  • LED OFF when no obstacle is detected.

The actual sensing distance depends on the IR sensor module, obstacle surface, ambient lighting and sensor adjustment.

11. Result

An obstacle detection and LED notification system was implemented using an IR sensor interfaced with Arduino UNO and Raspberry Pi.

The system reads the digital output of the IR sensor and controls an LED to indicate the presence or absence of an obstacle.

12. Conclusion

The experiment demonstrates the connectivity of an IR obstacle detection sensor with Arduino UNO and Raspberry Pi. It provides practical knowledge of digital sensor interfacing, GPIO programming, conditional control and visual notification using LEDs.

13. Viva Questions

  1. What is the working principle of an IR obstacle detection sensor?
  2. What are the functions of the IR transmitter and receiver?
  3. What is the difference between active-high and active-low sensor outputs?
  4. Why is an IR sensor used for obstacle detection?
  5. What is GPIO?
  6. What is the purpose of `digitalRead()` in Arduino?
  7. What is the purpose of `digitalWrite()`?
  8. Why should 5V signals not be connected directly to Raspberry Pi GPIO pins?
  9. How does the LED indicate obstacle detection?
  10. Which factors affect the detection range of an IR sensor?
  11. What is the difference between Arduino UNO and Raspberry Pi?
  12. How can the system be extended to provide audible alerts using a buzzer?