Detecting obstacle with IR (Infrared) Sensor Raspberry Pi 3

Concept: IR (Infrared) Sensor

IR (Infrared) Sensor works by emitting infrared signal/radiation and receiving of the signal when the signal bounces back from any obstacle. In other words, the IR Sensor works by continuously sending signal (in a direction) and continuously receive signal, if comes back by bouncing on any obstacle in the way.

IR Sensor
IR (Infra Red) Sensor

Components: IR Sensor

  1. Emitter: This component continuously emits the infrared signal
  2. Receiver: It waits for the signal which is bounced back by obstacle
  3. Indicator: On board LED to signal if obstacle is deducted by the sensor
  4. Output: Could be used as Input for further processing of the signal
  5. Ground: Ground/Negative point of the circuit
  6. Voltage: Input 3.3V

Objective: Detecting obstacle with IR Infrared Sensor

In this tutorial we will be creating a circuit using following components to detect obstacle.

  1. Raspberry Pi 3
  2. IR (Infrared) Sensor
  3. 1 LED
  4. 1 Resistor (330 Ω)
  5. Few jumper cables
  6. 1 Breadboard
Raspberry Pi 3 (B+) GPIO Layout

Circuit: To detect obstacles

We will be creating a circuit which will turn on the LED when an obstacle is detected. And, as soon as the obstacle is removed from the way the LED will turn off. In order to achieve that follow the steps to create required circuit.

Part 1: Connecting IR Sensor

IR Sensor has 3 pins, viz VCC, GND and OUT. We will use GPIO 17 (do not get confused with pin number 17) for receiving input from the sensor.

  1. Connect GPIO 17 from the Raspberry Pi to Breadboard (5a)
  2. Connect OUT pin of the sensor with the Breadboard (5c)
    This will send input received from sensor to GPIO 17, which will be processed further.
  3. Connect GND (any pin from board will work, in this post we are using pin number 9) with negative line on left side of the breadboard
  4. Connect GND of the IR Sensor to Breadboard (10c)
  5. Connect GND from Step 3 to Breadboard (10a)
  6. Connect VCC of the IR Sensor to Breadboard (15c)
  7. Connect 3v3 (Pin #1) to positive line on left side of the breadboard
  8. Connect 3v3 (connected in Step 7) to the Breadboard (15a)
IR Sensor configuration

Now the circuit is complete and sensor will detect the obstacle. It can be tested by putting anything in front of the IR Sensor. On-board LED will be on if obstacle is put in front of the sensor, else it will be off.

Part 2: Connecting LED

Objective is to turn on the LED when obstacle is detected.

  1. Connect GPIO 4 from the board to the Breadboard(20a)
  2. Connect positive point of the LED (longer pin of the LED) to the Breadboard (20c)
  3. Connect negative point of the LED (smaller pin of the LED) to the Breadboard (22c)
  4. Use resistor (330 Ω) to connect negative (row from Part 1: Step 3) to the negative point of the LED(22a)

Now we are ready to send signal based on the input received from IR Sensor to turn on/off the LED.

Part 3: Code to Connect IR Sensor I/P with LED status

from gpiozero import LED
from signal import pause
import RPi.GPIO as GPIO
import time


LED_PIN = 27
IR_PIN = 17 

indicator = LED(LED_PIN)

count = 1

while True:
  got_something = GPIO.input(IR_PIN)
  if got_something:
    print("{:>3} Got something".format(count))
    print("{:>3} Nothing detected".format(count))
  count += 1

Save it by name Code could be found on GitHub.

Part 4: Executing the code

  1. Open terminal (On Pi itself or login through SSH login)
  2. Navigate to the directory where the above code is saved
  3. Type $ python3 and press <enter>

On terminal it will start printing the status based on the conditions.

Video: Working app