Raspberry Pi

How to interface the infrared (IR) sensor to Raspberry Pi 4

The IR (infrared) sensor is mostly used to detect the motion of the objects in the surroundings as well as the detection of the presence of the objects. We can interface the IR sensor with the Raspberry Pi 4 in different projects and in this write-up we have discussed the method by which it can be interfaced with Raspberry Pi 4.

What is the infrared (IR) sensor and how does it work

The IR sensor is an electronic module which contains the transmitter and the receiver, the transmitter emits the infrared radiation which hits the obstacles and is reflected back by that obstacle which is detected by the receiver of the module and it changes the output to High.

We can use this IR sensor with the Raspberry Pi 4 to detect the objects in the surroundings, also to find out the speed of the moving objects, and to find out the distance of the object using the IR sensor.

This sensor has three terminals, one is Vcc connected to the 5 volts, the other is the Ground which is connected to the ground terminal, and the last one is of output, through which we can find out the output of the IR sensor.

How to find out the presence of the object using the IR sensor with Raspberry Pi 4

To understand the working of the IR sensor, we will configure a simple circuit to find out the presence of the object in front of the IR sensor. For this purpose, we need the following electronic components:

  • Raspberry Pi 4
  • IR sensor module
  • Breadboard
  • LED
  • Jumper wires

The circuit diagram of the circuit to detect the presence of the object is:

For making the circuit following the above circuit diagram, first we will place the Raspberry Pi 4, IR sensor, and a LED on the breadboard:

Now will will connect the IR sensor and the LED with the Raspberry Pi according to the table:

Cathode of the LED Connect it with the ground of Raspberry Pi 4
Anode of the LED Connect it with the GPIO 24 (BCM 18)
Vcc of the IR sensor Connect it with the 5 volts of Raspberry Pi 4
Ground of the IR sensor Connect it with the ground of Raspberry Pi 4
Out of the IR sensor Connect it with the GPIO 23 (BCM 16)

What is the Python code for the detection of object using the IR sensor with Raspberry Pi 4

To detect the object using the IR sensor with the Raspberry Pi, we have to write a Python script for this purpose, we will open the text file with the name of “ir_sensor.py” using a nano text editor:

$ nano ir_sensor.py

Now, we will type the following Python code:

import RPi.GPIO as GPIO

#importing the library of RPi.GPIO

import time

#importing the library of time

sensor = 16

#declaring BCM pin 16 which is GPIO 23 of Raspberry Pi

led = 18

#declaring BCM pin 18 which is GPIO 24 of Raspberry Pi

GPIO.setmode(GPIO.BOARD)

#declaring the BCM mode of pins

GPIO.setup(sensor,GPIO.IN)

#set the behaviour of sensor as input

GPIO.setup(led,GPIO.OUT)

#set the behaviour of led as output

try:

while True:

#initiated a infinite while loop

if GPIO.input(sensor):

#checking input on sensor

GPIO.output(led, False)

#led turned on

while GPIO.input(sensor):

#checking input on sensor again

time.sleep(0.2)

#generate time delay of 0.2 seconds

else:

GPIO.output(led,True)

#led turned off if there is no input on sensor

except KeyboardInterrupt:

#if any key is pressed on keyboard terminate the program

GPIO.cleanup()

#cleanup the GPIO pins for any other program use

Explanation of code: We have imported the time and RPi.GPIO library and then declared pin 16 for the sensor and pin 18 for the LED. These pin numbers are according to the board pin numbers for which we set the setmode to GPIO.BOARD and declared the sensor pin as input and the led pin as output.

Then in the infinite while loop, we check the input of the sensor as it detects some object; its output will become high, the LED will also become high, else the LED will remain off.

To compile and run the ir_sensor.py file, we will use the command:

$ python ir_sensor.py

The hardware working of the project is:

LED is off and the object is not in front of the sensor:

The LED will glow as the object is crossing the IR sensor.

Conclusion

The transmitter of the IR sensor transmits the infrared rays continuously, when the object comes in front of these rays, the rays reflect back due to the obstacle. The rays are detected by the receiver of the IR module and it makes the output of the IR sensor high. In this write-up, we have interfaced the IR module with the Raspberry Pi 4 and understand the working of an IR module by making a simple object detection project.

About the author

Hammad Zahid

I'm an Engineering graduate and my passion for IT has brought me to Linux. Now here I'm learning and sharing my knowledge with the world.