Welcome to Solarduino , A blog about DIY Solar PV and Arduino projects

Infrared (IR) Sensor Module with Arduino

Infrared (IR) Sensor Module is a distance proximity sensor “switch”.  When there is an object or obstacles that are close enough to block the view in front of 2 LEDs, it triggers the infrared trans-receiver module. The clear LED is the IR emitter while the black LED is the IR receiver. It uses the electromagnetic reflection principle where when the reflective surface (object) is closer, the receiver will receive stronger signal from transmitter due to shorter distance traveled of reflected of wave. 

When there is a object that is close enough, the IR electromagnetic detection received by the IR receiver is higher than the threshold level (user pre-set level), the sensor will change the output switch mode so that microprocessor board such as Arduino can execute what is going to do next. IR Sensor Module has only 1 main output signal which is Digital Output. Digital Output either go high (5V or 3.3V depends on the input voltage) or low (0V), thus this module cannot be used as a distance measurement but just as a trigger switch. 

When there is no obstacles or object within the detection distance, the output is at HIGH position (5V or 3.3V). When the distance shorter than or equal to the threshold set, the output signal will change to position LOW (0V). The distance threshold can be set by adjusting the potentiometer / trimpot on the board. This sensor module only able to detect distance between 2cm and 30cm within the view of the IR LED and Photoresistor. The trigger distance is somehow very subjective to object’s surface material, color and shape. Practically I would recommend this sensor switch for application less than 10cm distance. Thus this module is suitable for very close range of detection such as obstacle avoidance and virtual touch switch application.

Infrared (IR) Sensor Module

It is an object or obstacle detection Switch module. It detects object or obstacle within 30cm (recommend up to about 20cm) in front of the trans-receiver IR LEDs. The threshold of detection level can be adjusted by onboard potentiometer or trimpot. You can grab it at our affiliate link here !!!

Arduino has the ability to detect voltage value as a ON/OFF switch by using Digital input pin. For Arduino UNO, there are 12 digital input / output pins (2-13) where you can use one of the pins to detect voltage switch. Arduino NANO has 11 pins while Arduino MEGA has 54 pins. The voltage input pins will map input voltages between 0 (LOW) and 3.3V / 5V (HIGH). Voltage more than 3.3V is consider high. Do not reverse the voltage polarity which may damage the pins.

 

Arduino UNO (compatible board)

If you still not yet own an Arduino Micro-controller Board, you can get it cheap at our affiliate link here !!!

Hardware Connection

Once you get your IR sensor module and Arduino Board ready, you may start to do hardware wiring. Below is the schematic of the whole wiring. You may also standby all the tools and accessories by the side for the work.

The IR sensor module is very sensitive especially when you changed the threshold to larger distance (more than 15cm). It might create fake or electrical noise signal. Be sure your connection cable is very tight and module shall be installed in such a way no movement on the sensor. 

Arduino with IR Sensor Module Wiring Diagram

Dupont Line Wires

You may need Dupont Line Wires to connect Arduino board and IR  Sensor Module. It is available at our affiliate link here !!! 

LCD Display Shield 

This is a shield that allows the output value of your Arduino board to be displayed on the screen. Since it is a shield, you can just stack it on Arduino board without the need of extra wiring for the LCD Display. You can get the LCD Display board at our affiliate link here !!!.

Software Codes

The final step would be adding source code onto Arduino board. I assume you have installed the Arduino Software. If you still have not installed the software, the link here can bring you to the official download site. Once you have downloaded the software, you may download the code file (.ino) for this application below (right click save link). 

There are two source code files attached, the first source code is normal IR Sensor source code for people out there without the LCD Display Shield. The output can be shown in Serial Monitor using Arduino Software. The second code is the IR Sensor Module code with LCD display shield display. Once the code is uploaded to the Arduino board, the output will be shown on the LCD Display. 

Screw Shield / Expansion Shield

When there are a lot of wiring around especially more than 1 sensor, sharing pins will be difficult as existing pins (ground and 5V) are limited. This shield provides a lot of convenient terminals for each of the input and output pins. The shield can be mounted directly on top of the Arduino Uno board or in between the shields which made it very convenient to use. You can get it at our affiliate link here !!!

Codes for IR Sensor Module with LCD Display Shield. Note: the codes shown here may not be 100% correct due to translation error. For accurate code, kindly download the .ino file. 

// Infrared (IR) Sensor Module By Solarduino

// 1- IR Sensor Module

int LEDOutputPin = 13;
int IRInputPin = 2;
int IROutputState;

unsigned long startMillisIR;
unsigned long currentMillisIR;
const unsigned long periodIR = 200;

// 2 - LCD Display

#include<LiquidCrystal.h>
LiquidCrystal LCD(8,9,4,5,6,7);
unsigned long startMillisLCD;
unsigned long currentMillisLCD;
const unsigned long periodLCD = 200;

void setup()
{
// 0- General

Serial.begin(9600);

// 1- IR Sensor Module

pinMode(LEDOutputPin, OUTPUT);
pinMode(IRInputPin, INPUT);
startMillisIR = millis();

// 2 - LCD Display

LCD.begin(16,2);
LCD.setCursor(0,0);
startMillisLCD = millis();

}

void loop()
{

// 1- IR Sensor Module

currentMillisIR = millis();
if(currentMillisIR - startMillisIR >= periodIR)
{
IROutputState = digitalRead(IRInputPin);
if (IROutputState == LOW)
{
Serial.println("There is obstacle !!!");
digitalWrite(LEDOutputPin, HIGH);
}
else
{
Serial.println("The path is clear");
digitalWrite(LEDOutputPin, LOW);
}
startMillisIR = millis();
}

// 2 - LCD Display

currentMillisLCD = millis();
if (currentMillisLCD - startMillisLCD >= periodLCD)
{
if (IROutputState == LOW)
{
LCD.setCursor(0,0);
LCD.print(" Warning !! ");
LCD.setCursor(0,1);
LCD.print("Obstacle ahead !");
}
else
{
LCD.setCursor(0,0);
LCD.print(" It is safe !! ");
LCD.setCursor(0,1);
LCD.print(" No obstacle ! ");
}
startMillisLCD = currentMillisLCD ;
}

}

Datalogger Shield

If you plan to record the data in a proper way, you may consider this Datalogger Shield. It allows your arduino to record your data in SD Card. Datalogger shield is often installed together with LCD Display shield. Please find it at our affiliate link here !!! For more about this Datalogger Shield, kindly visit our post here.

Before we end, we would like to give gratitude to you for taking the time to read the post. We would need readers like you to support us in order to keep growing. You can support us in the following ways :

Donate & Fund Raising

If you like my work, please send me a donation to encourage me to do more. Thanks

Aliexpress Affiliate

We are the member of Aliexpress affiliate marketing. Do support us by clicking the affiliate product links if you do wish to purchase them.

Like and Share

If you like our post, we need your support to like and share our posts or videos so that it can reach more and more people like you !!

Result – In Serial Monitor

Result – In LCD Display

For Arduino Code Files, Remember to Right Click > Save Link As … You may alter the internal code as you wish. Happy coding !! 

IR Sensor Module.ino
IR Sensor Module with LCD Display.ino