Arduino Proximity Detector Project

In this project, a simple proximity detector will be constructed using a Keyes Infrared (IR) Proximity Sensor Module, an HW-480 Red/Green 2-Colour LED Module and an Arduino. Based on whether the IR sensor detects any obstacles/objects at a given distance, the LED will change colour. The LED will be green if the IR sensor detects no objects in range but the moment an object comes in range of the sensor, the LED will switch to a red colour.

Completed proximity detector
Table of Contents

Arduino Proximity Detector Operation Principle

The Keyes IR Sensor is based on a pair of infrared LEDs, one IR LED that acts as the emitter and a Vishay HS0038B receiver module which receives reflected IR rays. Upon operation of this sensor module, the IR LED will emit IR light and if there are objects in range, the IR rays will bounce off the object and back towards the sensor, being received by the HS0038B receiver module. The sensor will then send a signal to the Arduino indicating the presence of an object in the range of the sensor.

In terms of the construction of this sensor module, aside from the two IR LEDs (emitter and receiver), there are two on-board LEDs: a sense and power LED. The sense LED will light up when a LOW signal is sent from the sensor to the Arduino (i.e. when an object is detected in the range of the sensor). The power LED will stay on continuously whenever the sensor receives power from the Arduino, regardless of its digital output signal.

Furthermore, two potentiometers are further located at the bottom of the sensor module, marked R5 and R6. By adjusting R5, you can adjust the range of the sensor (how far it can detect an object) from 2 to 40 cm. Although it is recommended to leave R6 to its default setting, adjusting R6 will alter the sensitivity of the sensor. Together, adjusting both R5 and R6 will alter the range and sensitivity of the sensor.

Regarding the so-called ‘brains’ of the sensor, it all runs on a NE555 chip, which acts as an oscillator/timer to generate a square wave pattern of 38 kHz that powers up the IR LED. The potentiometers are essentially put in place to modulate the output of the 555 chip and to change the frequency of the signal (R6) or the amplitude/current that is powering the IR LED (R5).

Components for Project Preparation

Another noticeable difference that you may have observed within the construction of this sensor is the fact that there are four pins, compared to the usual 3-pin configuration on other sensors. The Keyes IR sensor module that is featured in this project has a 4-pin configuration but only three of the four pins will be used in this project: the digital output, positive (+) and GND (-) pin. The fourth pin is marked as the enable (EN) pin and can be used if you would like to control when the sensor is active. With the current three-pin configuration, the Keyes IR sensor will always be active and sense proximity continuously. It is important to note that if you would like to use the EN pin, it is essential to remove the on-board jumper. However, for this project, the EN pin will not be used and therefore, the jumper should remain on the two pins at all times. With all that said, these are the components needed to build this proximity detector:

  • Arduino Nano (other Arduino-compatible boards will work)
  • USB cable (compatible with the Arduino board)
  • Breadboard
  • Male-Male Jumper Wires (6)
  • Keyes Infrared Proximity Sensor Module
  • HW-480 Red/Green 2-Colour LED Module (common cathode)
  • 220Ω resistors (2)

How to Complete the Wiring

Depending on your Arduino board, you may or may not require a breadboard to plug your board in. In this example, an Arduino Nano is used, thus requiring a breadboard but if you are using an Arduino Uno, for example, the jumper wires can be plugged in from the components on the breadboard directly to the board pins. The wiring for this project is fairly simple as aside from the main modules, not a lot of extra components are needed.

Regarding the Keyes IR sensor module, only three of the four pins will be used in this project: the digital output (OUT) pin, positive (+) pin and GND (-) pin. In terms of the LED module, the reason that two 220Ω resistors are connected in series with the two outputs from this module is to prevent the LEDs from burning out when applying a supply voltage of +5 volts. A circuit diagram of the wiring is also featured below.

Keyes Infrared Proximity Sensor Module: Connect the output (OUT) pin to D2 on your Arduino board, the positive (+) pin to +5v and the GND (-) pin to GND.

HW-480 Red/Green 2-Colour LED Module: Insert one 220Ω resistor in the breadboard in series with the output pin for the red colour LED signal (R) and insert another 220Ω resistor in series with the output pin for the green colour LED signal (G). Connect the red colour output pin (R) to D3 and the green colour output pin (G) to D4. Connect the negative (-) pin to GND on your Arduino.

Now, you can plug in your Arduino board via the USB cable to the computer.

    Arduino proximity detector wiring schematic

    Arduino Proximity Sensor Code

    					int sensorPin = 2;
    int redPin = 3;
    int greenPin = 4;
    boolean val;
    void setup()
     pinMode(redPin, OUTPUT);
     pinMode(greenPin, OUTPUT);
     pinMode(sensorPin, INPUT); 
    void loop()
     val = digitalRead(sensorPin);
     if(val == LOW)
       digitalWrite(redPin, HIGH);
       digitalWrite(greenPin, LOW);
       digitalWrite(redPin, LOW);
       digitalWrite(greenPin, HIGH);

    About Code

    The first block of the code is where we declare all the variables that will be used in this project. This includes integer variables for the digital pin 2 (D2) which is used by the Keyes IR sensor module, in addition to digital pins 3 & 4 (D3 & D4) that are utilized by the HW-480 LED module. The boolean variable ‘val’ is then declared and is used to store the digital output (HIGH/LOW) signal from the Keyes IR sensor module.

    In the void setup section, we first define the LED module pins as output devices since information from the Arduino is being sent out to control the LED module. Then, we define the Keyes IR sensor pin as an input device since digital information is being fed from the sensor into the Arduino for processing.

    To start the void loop section, we use the digitalRead() function to set the boolean variable ‘val’ equal to whatever digital output signal (HIGH/LOW) is being received from the Keyes IR sensor module. Then, an if-else statement is introduced where we state that if the sensor sends a LOW signal (when an object is detected), the red LED will be switched on only. Otherwise, when no object is detected, the green LED will be switched on only.


    Now that you understand the capabilities of the Keyes Infrared Proximity Sensor Module and how it can be used with the Arduino, you can apply these concepts into expanding upon this project. Some common applications for a sensor module like this may be for robotic purposes, automobile devices (e.g. parking/reverse sensors), home automation systems, etc. Due to the sensor using a very simple digital interface, interfacing it with any microcontroller (e.g. Arduino, Raspberry Pi, Micro:bit, etc) is simply effortless. It is important to note that if you do plan on adjusting the potentiometers for altered range and sensitivity, fully rotating the R5 potentiometer, especially fully clockwise, may induce damage to the sensor module during its operation. Hence, it is recommended to only rotate R5 in small increments until your target range is achieved. The R6 potentiometer should be fine when left in its default setting but if you do plan on altering the sensor’s sensitivity, using a frequency counter or oscilloscope will greatly help.

    More content you may be interested in

    Arduino MQ-3 Alcohol Sensor Project
    Arduino MQ-3 Alcohol Sensor Project

    Creating an alcohol sensing project with an MQ-3 sensor and Arduino allows for accurate alcohol concentration monitoring in environments like bars and factories. The MQ-3

    Arduino Temperature Alarm Project
    Arduino Temperature Alarm Project

    This Arduino temperature alarm project effectively demonstrates how to create a practical temperature monitoring system. By utilizing the LM35 temperature sensor and a buzzer, it

    Arduino Breathing LED Project
    Arduino Breathing LED Project

    A breathing light is an LED lighting effect that simulates the human breathing process, gradually increasing or decreasing in brightness within a specific cycle. This

    arduino vibration sensor
    Arduino Vibration Sensor Project

    This Arduino project employs a vibration sensor, ball switch, and LED to detect tilt or collision events. The LED lights up when vibrations occur and

    arduino smart robot car
    Arduino Smart Robot Car DIY Tutorial

    In this project, TechSparks will use Arduino to create a smart car project that can achieve Bluetooth remote control, obstacle avoidance, and line tracking functions.

    Scroll to Top