In this detailed guide, we will explore how to use an IR (Infrared) proximity sensor with a Raspberry Pi. An IR proximity sensor is a device that detects the presence or absence of objects by emitting and receiving infrared light. We will learn how to wire the IR proximity sensor to the Raspberry Pi and write a Python program to detect objects and take appropriate actions based on their proximity.
An IR (Infrared) sensor works by emitting infrared light and detecting its reflection to determine the presence or absence of objects. The sensor consists of an infrared LED that emits light and a photodiode that receives the reflected light. When an object is present, the reflected light intensity is high, and the photodiode generates an electrical signal. This signal is processed to detect object presence or absence. IR sensors are commonly used for object detection, proximity sensing, and automation applications.
The objective is to create a circuit using an IR proximity sensor that can detect objects in its vicinity. When an object is detected, we want the Raspberry Pi to perform a specific action, such as controlling an output device like an LED.
Here, we will switch On the LED when an object is detected and switch it Off when no object is detected.
Raspberry Pi
Breadboard
LED
IR proximity sensor module
Resistor (220-330 ohms)
Jumper wires
The IR (Infrared) sensor can be utilized with a Raspberry Pi 3B+ to detect objects and provide visual feedback through an LED connected to a breadboard.
Connect the OUT pin of IR sensor to GPIO23 (Pin 16) of RPi.
GND to any GND and VCC to any 5V pin of RPi
Connect anode of the LED to GPIO24 (Pin 18) of RPi via a 220 ohm resistor.
Cathode of the LED to GND.
Now let's write the Python code to detect objects in a vicinity. Follow the steps below:
Connect to your Raspberry Pi using VNC or Desktop environment.
Launch the Geany Python IDE or open a text editor to write the code.
Create a new file named ir_proximity.py and save the following code into it:
You can copy and paste the following code from here into the file.
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM) # Set up BCM GPIO numbering
GPIO.setwarnings(False) # Disable warnings
IR_SENSOR_PIN = 23 # GPIO23 as IR sensor pin
LED_PIN = 24 # GPIO24 as LED pin
GPIO.setup(IR_SENSOR_PIN, GPIO.IN) # IR sensor pin set as input
GPIO.setup(LED_PIN, GPIO.OUT) # LED pin set as output
try:
while True: #sensor may be inverted
# If the IR sensor detects an object
if GPIO.input(IR_SENSOR_PIN):
GPIO.output(LED_PIN, GPIO.HIGH) # Turn on the LED
print("Object Detected!")
else:
GPIO.output(LED_PIN, GPIO.LOW) # Turn off the LED
print("Object not Detected")
time.sleep(0.1)
except KeyboardInterrupt: # If CTRL+C is pressed, exit cleanly:
pass
GPIO.cleanup()
After saving the file execute it by clicking on messenger like icon on geany editor or open terminal and navigate to the folder you saved the file, execute it by typing python ir_proximity.py
LED will turn on when any object is detected by the IR sensor. To stop the program, press Ctrl + C in the terminal where you executed the program.
Note: Please be aware that the performance of the IR sensor may be affected by sunlight. If there is direct sunlight in the room, the sensor may not work as intended.
By following this guide, you have learned how to use an IR proximity sensor with a Raspberry Pi. You have wired the components, written the Python code, and gained the ability to detect objects using the sensor. This opens up a range of possibilities for object detection applications in your Raspberry Pi projects. You can further extend the functionality by integrating it with other devices or implementing specific actions based on object proximity like collision avoiding car or line following car. Enjoy exploring and experimenting with IR proximity sensors on your Raspberry Pi!