IR With Motor

What is Needed?

IR Sensor
Relay 5v
9V Battery



  1. IR Sensor, 5V Relay, Motor and 9V Battery
  2. Connections
  3. Program
  4. Check obstacles

IR Sensor

An infrared sensor is an electronic device, that emits in order to sense some aspects of the surroundings. An IR sensor can measure the heat of an object as well as detects the motion.

DC Motor

The DC motor is a machine that transforms electric energy into mechanical energy in form of rotation.

5V Relay

A relay is an electromagnetic switch operated by a relatively small electric current that can turn on or off a much larger electric current.

9V Battery

A battery is a device consisting of one or more electrochemical cells with external connections for powering electrical devices such as flashlights, mobile phones, and electric cars.


·        Connect the IR’s VCC to 4th pin.

·       Connect the IR’s Ground(GND) to 6th pin.

·       Connect the IR’s Digital out(D0) to 8th pin.

·       Connect the Relay’s VCC / 5V to 2nd pin.

·       Connect the Relay’s Ground(GND) to 9th pin

·       Connect the Relay’s Input(I) to 12th pin

·       Connect one end of the motor with Relay’s NO(Normally Open)

·       Connect the other end of the motor to Negative(-) side of 9V battery.

Connect Relay’s Common(COM) to Positive(+) side of 9V battery.


import RPi.GPIO as GPIO

import time             #Import Librarys


GPIO.setmode(GPIO.BOARD)    # Set a Mode BOARD Or BCM

GPIO.setup(8,GPIO.IN)      # Set 8th pin is Input

GPIO.setup(12,GPIO.OUT)     # Set 12th Pin is Output

while True:

a=GPIO.input(8)   # Get Input and store the value in variable 'a'

if a==GPIO.HIGH:  # check the Variable High or Low if its High

print(“Obstacle")     # Print Obstacle and Motor will be ON


else:                      # If 'a' is Low

print(“No Obstacle")     # There is No obstacle

GPIO.output(12,False)     # Motor will be OFF

Obstacle detection

If there is no Obstacle in front of the IR Sensor it will show “No Obstacle”, When an IR Sensor Detects an Obstacle the DC Motor will start running.

Social media & sharing icons powered by UltimatelySocial