Interface Sharp Infrared Distance Measurement Sensor with Raspberry Pi 4

Hello friends, I hope all are fine. Today, we are going to share the 3rd chapter of Section-III in our Raspberry Pi Programming Course. In our previous lecture, we interfaced the Soil Moisture Sensor with Raspberry Pi 4. Today, we are going to Interface the Infrared(IR) sensor with RPi4.

IR Sensor is typically employed for the presence/motion detection of objects in the immediate area. With their low power consumption, straightforward design, and user-friendly features, IR sensors are a popular choice for detection purposes. Infrared(IR) impulses are invisible to the naked eye and lie between the visible and microwave parts of the electromagnetic spectrum. So let's get started:

Components Required

To learn how an IR sensor detects the existence of an object, we'll set up a basic circuit to detect any object. The following electronic components are required for this task:

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

What is IR Sensor?

  • The IR sensor(Infrared sensor) consists of a transmitter and a receiver; the transmitter sends out infrared radiations, which are reflected by the barriers/receiver and get back to the receiver of the sensor.
  • The sensor computes the incoming signal and sets the module's output.
  • With an IR sensor and a Raspberry Pi 4, we can see what's around us, how fast things are moving, and how far away things are from the sensor.
  • There are three connections on this IR sensor: Vcc, Ground, and output.
  • Vcc is linked to 5V, Ground is grounded, and output is the pin, from where we read the sensor's data.

Working Principle

An infrared sensor incorporates an infrared LED(light-emitting diode) and a photodiode, which, when joined together, can function as a photo-coupler or optocoupler. This sensor employs Plank's radiation, Stephan Boltzmann, and Wein's displacement principles of physics.

The infrared LED is a transmitter that produces IR radiations. Visually, the IR LED is identical to a regular LED, although the IR radiation is invisible to the naked eye. Radiation sent out by an infrared source is primarily detected by an infrared receiver. Photodiodes are one physical shape that these infrared detectors can take. Unlike regular photodiodes, IR photodiodes only respond to infrared light. Variations in voltage, wavelength, package size, etc., are the primary categories separating the many types of infrared receivers.

The wavelength of the IR receiver must be the same as that of the IR transmitter whenever the two are utilized together. In this case, an infrared LED acts as the sender, and an infrared photodiode acts as the receiver. An infrared LED produces infrared light, which an infrared photodiode can detect. The amount of IR light collected correlates with the photo-resistance diodes and the resulting shift in output voltage. It is on this concept that the IR detector operates.

Some of the infrared radiation sent out by the transmitter will be reflected back to the receiver. The IR receiver can calibrate the sensor output to the level of the response.

Infrared Detector Varieties

You can choose between active or passive IR sensors when shopping for an infrared detector.

  1. Active IR Sensor

The active infrared sensor, we are going to use in today's project, incorporates both the transmitter and the receiver.

These detectors can receive and process data via radiation from an external source. The signal processor can then be used to extract the data that is required for further analysis. The reflectance sensor and the break beam sensor are the two most common active infrared sensors.

  1. Passive IR Sensor

Passive infrared sensors consist solely of detectors and lack any transmitter. A transmitter or infrared source is used in these sensors. An infrared detector can pick up the object's radiation. The signal is then processed by a signal-understanding device to extract the necessary data.

The pyroelectric detector, the bolometer, the thermocouple-thermopile, etc., are all excellent examples of this type of sensor. Thermal infrared sensors and quantum infrared sensors are two examples of these devices. The wavelength is not a factor for the thermal IR detector. For these sensors to function, they use a hot energy source. Infrared quantum sensors have a fast response and detection times; their sensitivity is wavelength-dependent. To take accurate readings, these sensors must be regularly cooled.

Schematic of an Infrared Detector

The infrared sensor is one of the most common and fundamental sensor circuits in modern electronics. One of the most prominent uses for this sensor is obstacle detection. The following elements are used in IR circuit:

  • LM358 IC 2 IR transmitter and receiver pair
  • Resistors of the range of kilo-ohms.
  • Variable resistors.
  • LED (Light Emitting Diode).

An infrared (IR) sensor located in the transmitter part constantly broadcasts IR waves that are picked up by an infrared (IR) receiver module. How the receiver receives IR photons affects what comes out of its IR output terminal. Because this deviation cannot be examined separately, it can be sent into a comparator circuit. An LM339 operational amplifier (op-amp) is employed in this application as a comparator.

Without a signal, the comparator IC's inverting input has a greater potential than its non-inverting counterpart (LM339). As a result, the LED doesn't light up and the comparator's output drops low. The input's inverting potential will drop when the IR receiver module detects an incoming signal. As a result, the comparator's (LM 339) output gets high, and the LED lights up.

The IR LED Devices, such as the Photodiode, require a current of at least 10 mA, which is maintained by Resistors R1 (100), R2 (10k), and R3 (330). Output terminal adjustments are made with resistor VR2 (preset=5k). Circuit sensitivity is adjusted by resistor VR1 (preset=10k). Learn more about IR detectors by reading up on the topic.

A Transistor-Based Infrared Circuit

Below is a schematic of the IR sensor's transistor-based circuit, demonstrating how to use two transistors for obstacle detection. This circuit's primary function is to detect obstacles in the path of an infrared LED. So, two transistors, such as NPN and PNP types, are all you need to use to construct this circuit. Transistors of the BC547 and BC557 types are utilized, respectively, for NPN and PNP operations.

For the above circuit to work, one IR LED must be ON at all times, while the other IR LED is connected to the base terminal of the PNP transistor. Infrared (IR) LEDs-2, 100-ohm, and 200-ohm resistors, BC547 and BC557 transistors, and a simple LED are all needed for this design.

When an infrared LED is identified, a tiny current is distributed through the sensor, thanks to the object's reflected light. By doing so, the NPN and PNP transistors are turned on, and the LED lights up. You may use this circuit to make automatic lamps that turn on when someone walks into the room.

IR Sensor with Raspberry Pi 4

  • The presence-detection circuit is depicted as follows in the schematic:

  • To construct the circuit depicted in the preceding diagram, we will first attach the Pi 4, the IR sensor, and the LED to the breadboard.

  • We'll hook up the Infrared sensor and LED to the Raspberry Pi using the table as a guide.

Python Code for IR Sensor Object Detection with RPi 4

If you want to use the Raspberry Pi's infrared (IR) sensor to identify an object, you'll need to create a Python script; to do so, launch the nano text editor and navigate to the file named "irsensor.py."

nano ir_sensor.py

This is the Python code we will be writing now:

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 behavior of the sensor as input

GPIO.setup(led,GPIO.OUT)

#set the behavior of led as output

try: 

    while True:

        #initiated an infinite while loop

        if GPIO.input(sensor):

            #checking input on the sensor

            GPIO.output(led, False)

            #led turned on

            while GPIO.input(sensor):

                #checking input on the sensor again

                time.sleep(0.2)

                #generate a time delay of 0.2 seconds

        else:

            GPIO.output(led,True)

            #led turned off if there is no input on the sensor

            except KeyboardInterrupt:

            # terminate the program

            GPIO.cleanup()

            #cleanup the GPIO pins for any other program use

Code explanation

Time and the RPi.GPIO library has been imported, and pins 16 and 18 have been designated with the sensor and the LED, respectively. Setmode = GPIO.BOARD; sensor = input; led = output; these PINs correspond to the board's PINs.

Then, when an object is detected, the sensor's input is checked in an infinite while loop to determine whether or not the LED's brightness should increase. Now run the Python file in your terminal, and you should see the led blink when an object is detected.

When the subject is not in range, and the LED is turned off:

The LED will light up as the object passes over the IR sensor.

Uses for Infrared Sensors

Infrared (IR) sensors can be broken down into several categories depending on their intended use. Examples of how various sensor technologies are typically put to use. It is possible to get numerous motors working in unison using a speed sensor. The sensor's temperature readings are used in factories for climate regulation. The passive infrared (PIR) sensor is employed in an automated door opening system, whereas the ultrasonic (US) sensor measures distance.

The infrared (IR) sensors detailed below can be found in various sensor-based applications and electrical gadgets.

  1. Radiation Thermometers

Features of radiation thermometers, which use infrared (IR) sensors to determine an object's temperature, vary with the object's material and temperature.

  • remote sensing and measurement, where no physical contact is required

  • quicker response

  • Simple Pattern Sizing

  1. Flame Monitors

The light given off by the flames may be detected, and the fire's progress can be tracked with such gadgets. Flames emit a wide spectrum of light, from ultraviolet to infrared. Numerous types of detectors, including PBS, PbSe, Two-color detectors, and pyroelectric detectors, are utilized in flame monitors.

  1. Gas Analyzers

Gas analyzers use IR sensors to analyze gas based on their absorption characteristics in the IR region. Two techniques are utilized: dispersive and non-dispersive. In the dispersive method, the emitted light is separated spectroscopically, and the absorption is analyzed to determine the gas composition and sample quantity. The non-dispersive method, also known as NDIR technology, is the more widely used technique. It employs optical filters similar to sunglasses to filter out unwanted radiation and determines the absorption without spectroscopically separating the emitted light. NDIR analyzers are commonly used for carbonated drinks, while non-dispersive analyzers are frequently used for automotive exhaust gas analysis and fuel leak detection.

  1. IR Imaging Devices

An IR imaging device generally applies IR waves due to its invisible property. It finds use in thermal cameras, night vision equipment, and others. All objects, including water, rocks, soil, vegetation, the atmosphere, and human tissue, emit IR radiation, which the Thermal infrared sensors measure within the IR spectrum to create a map of the object/area's temperature distribution. Thermal cameras are often made up of Sb (indium antimonite), Gd Hg (mercury-doped germanium), and Hg Cd Te (mercury-cadmium-telluride) sensors. 

Conclusion

The IR sensor's transmitter continuously sends out infrared beams, which bounce back when they encounter an object. The receiver of the IR module then detects these beams, resulting in a high output from the IR sensor. IR technology has various applications in daily life and industries, such as televisions, where an IR sensor detects signals from a remote control. In this article, we have integrated the IR module with the Raspberry Pi 4 and demonstrated the functioning of the IR module through a simple object detection project.

So, that was all for today. In the next lecture, we will interface PIR Sensor with Raspberry Pi 4. Till then, take care. Have fun!!!

Syed Zain Nasir

I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Share
Published by
Syed Zain Nasir