Menu Close

Raspberry Pi – IR proximity & Color Detection

This tutorial of Robo India explains how to concept of  IR LED and photo diode as a color detection sensor and as a proximity detection sensor with Raspberry Pi.

1. Introduction:

An infrared light emitting diode (IR LED) emits light of Infrared range 700 nanometers (nm) to 1 mm. This light is not visible by naked eyes but can be seen by a camera (that is why these are also used in night vision cameras).

A photo diode gives response in term of change in resistance when light falls on it. That change is measured in terms of voltage.

An IR LED and a Photo diode are used in a combination for proximity and color detection. An IR LED (transmitter) emits IR light, that light gets reflected by the object, the reflected light is received by an IR receiver (Photo Diode). Amount of reflection and reception varies with the distance. . This difference causes to change in input voltage through IR input. This variation in input voltage is used for proximity detection.

For color detection application: The amount of reflected light depends upon the color of surface from which it is reflected. The reflection is different for different colored surfaces. This makes it a color detector.

This tutorial is for Raspberry Pi. Please note that the same tutorial can be performed on LUA as well.

1.2 Hardware required

S.No.ItemQuantity
1Raspberry Pi 1
2Pi Widge B+ 1
2Breadboard 1
3IR LED & Photo Diode pair 1
4Resistor 1k 1
5Resistor 10k 1
6Jumper Male to male 7

2. Building Circuit

Schematic Diagram:

Circuit Layout:

3. Programming:

Once the circuit part is done, Raspberry Pi is needed to be programmed. Here is the code to run this circuit on Raspberry Pi.

You may download this code (Python Code) from here.

import RPi.GPIO as GPIO
import time
sensor = 22
GPIO.setmode(GPIO.BOARD)
GPIO.setup(sensor, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
current = GPIO.input(sensor)
previous = current
def printState(current):
    print ('GPIO pin %s is %s' % (sensor, 'HIGH' if current else 'LOW'))
printState(current)
while True:
    current = GPIO.input(sensor)
    if current != previous:
        printState(current)
    previous = current
    time.sleep(0.1)
GPIO.cleanup()

4. Output

Python Coding with Raspberry Pi connects your project to the real world. The easiest introduction to Python is through IDLE, a Python development environment.

Open IDLE (Python Development Environment) from the Desktop or applications menu:

Python 3 is the newest version. Write the above code and save your code on desktop or any other location. Open Command Prompt. Write cd location_of_program > Press Enter.

Then write Python 3 file_name.py > Press Enter. (.py is file extension).

Your Python code is connected with your real world project. Place an object in front of the IR LED & Photo diode. The analog input values from photo diode are displayed on the serial monitor. Try using different colored surfaces and varying distance of object from the IR LED & photo diode.

5. Troubleshooting

Values are not changing: See the IR LED through a camera if it is not emitting any light, try changing polarity if still does not work try changing the IR LED. If values are still not changing by varying the distance or color of the surface then try changing polarity of the photo diode.

If still not working pull the led out and test it may be defective

If you have any query please write us at support@roboindia.com

Thanks and Regards
Content Development Team 
Robo India
https://roboindia.com

Leave a Reply