Interface single and Dual IR Infrared sensor with Arduino and LCD
Multiple Sensor Interface to Arduino
In this another tutorial on sensors for beginners, we are going to interface single and multiple Infrared IR sensors with Arduino Uno development board, working simultaneously and have the status displayed either on an ( 4X16 LCD ) LCD module (jhd162a) or an LED. We are going to cover three chapters in single tutorial in simple step by step fashion. The purpose of this tutorial is to serve the basics for beginners with Sensor interface on Arduino for being able to use these concepts and to develop various other sensor based applications (robotics, Automation Projects etc.). We will use the same Object Detection Proximity IR Sensor Module that we made in our precedent tutorial. In our previous Proximity Sensor tutorials we have learned how to interface IR sensor with various other microcontrollers such as PIC18F4550 and 18F2550; However, today we are going to learn how to interface the same infrared sensor with the famous Arduino Uno board.
To begin easy, first we are going to interface a Single IR sensor for sensing the distance and read the value of the sensor using an Arduino board and have status displayed on a LED. On the next level we are going to do same with two IR sensors working simultaneously. At last we will interface (add) an LCD (Liquid Crystal Display) and have the status of both the Infrared sensors displayed on a 4X16 LCD Display unit. The Concepts for Reading the IR sensor is still same as explained in previous lessons, the only difference is the Arduino board.
- Single Infrared Sensor Interface to Arduino Microcontroller
- Dual IR Infrared Sensor interface with Arduino
- Dual IR sensor interface to Arduino –Display Result on LCD
Requirements
- Arduino Uno
- Infrared IR sensor Module
- JHD162a 4X16 LCD
- Resistors
- LEDS
Infrared IR Sensor Module
We will use the IR Infrared Sensor Circuit module that we made in our previous project, which is an inexpensive (Low Cost) sensor circuit module. You can find the schematic and PCB design in my previous post. There are three pins in the Schematic - Two pins for providing the input voltage and GND, and the third pin from the IR module is the IR control pin. This Control Pin from the IR sensor Module will be interfaced to the Arduino for reading the value from sensor. We will use two identical copies of the sensor for our tutorial.
As mentioned before we are going to start easy,hence here we going to interface just one infrared sensor circuit with Arduino, the source code is pretty much self-explanatory. Like every microcontroller, we need to define pins either as input or output. We have configured Pin 13 as output, and pin 9 as input for taking input from sensor. The Result of sensor will reflect on the LED connected on pin13 which is configured as output. The source code can be tweaked easily to display the result on serial monitor too.
We are going to leave pin 0 to Pin 7 on Arduino Uno untouched for using it with LCD operation in future. Make the Circuit ready according to the schematics below to proceed further. Arduino will draw power from USB hence there is no need to interface external power supply as long as the board is connected with USB cable.
Schematic (Single Sensor Interface)
USE and GND and +5V pins on the arduino to power the IR sensors. Make the necessary wiring according to the schematic given below.
NOTE: The operation is quiet similar to that of interfacing a simple switch to Arduino or any microcontroller, But here you don’t need to ground the input pin (pull-down) with a resister. In Arduino pin13 by default is interfaced with an On board led for displaying status. We can use that one on board led or you can interface an Extra led on Pin 13 to have the Status displayed.
Source Code: Dual IR Sensor - LED
Copy this sketch below and launch your Arduino IDE and create a new project. Paste, compile and Run the source code. You can also download the Sketch at the end of this source code. After the sketch is compiled and uploaded, any object within a short distance must make the on board led or the LED connected on Pin13 to light up.
Source code: Single IR Interface
/*
* File: Single_IR_Sensor_Interface.ino
* Author: ron
* December 08, 2013, 2:21 AM
*/
int irsensor1 = 6;
int led1 = 13;
void setup()
{
pinMode(irsensor1 , INPUT);
pinMode(led1, OUTPUT);
}
void loop()
{
int readsen = digitalRead( irsensor1 );
if( readsen == HIGH )
{
digitalWrite(led1, HIGH);
}
else
{
digitalWrite(led1, !HIGH);
}
delay(1);
}
/* THE END */
Download source code: Single IR Sensor Arduino(.uno)
Now we are going to interface two IR infrared sensors with the same Arduino board and have result displayed on LES's. With some additional operators on the source code its quiet easy to get the status of both the sensors. Here also we are going to have the status of the sensor displayed on LED’s. We will need two additional pins for input and output operation for the additional sensor. Hence totally we need two Pins for Input from sensors and two output pins for having the status displayed.
PIN 13, Pin12 - Output
PIN 6, PIN 7 - Inputs
Schematic (Dual Sensor Interface- LED)
Make the circuit connection according the schematic given below and proceed forward.
NOTE: The operation is quiet similar to that of interfacing a simple switch to Arduino or any microcontroller, But here you don’t need to ground the input pin (pull-down) with a resister.
Source Code: Dual IR Sensor - LED
/*
* File: Dual_IR_Sensor_Arduino.ino
* Author: ron
* December 08, 2013, 2:21 AM
*/
int irsensor1 = 5;
int irsensor2= 6;
int led1 = 13;
int led2 = 12;
void setup()
{
pinMode(irsensor1 , INPUT); pinMode(irsensor2 , INPUT);
pinMode(led1, OUTPUT); pinMode(led2, OUTPUT);
}
void loop()
{
int readsen = digitalRead( irsensor1 );
int readsen1 = digitalRead( irsensor2 );
if(readsen == HIGH && readsen1 == HIGH )
{
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
}
else if(readsen == !HIGH && readsen1 == HIGH )
{
digitalWrite(led1, !HIGH);
digitalWrite(led2, HIGH);
}
else if(readsen == HIGH && readsen1 == !HIGH )
{
digitalWrite(led1, HIGH);
digitalWrite(led2, !HIGH);
}
else
{
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
}
delay(1);
}
/* THE END */
Download source code: Dual IR Sensor interface(.uno)
At this stage now we are going to get rid of LED’s and have the status of the sensor displayed on a jhd162a 4X16 Liquid Crystal Display (LCD ) module. Arduino provides absolutely great set of LCD libraries which supports various types of character and Graphical LCD’s, which can save a lot of coding and pain to interface an LCD. For LCD we will use pin 0 to pin 7 on the Arduino and configure the source code accordingly.
Interface the LCD to the Arduino board according to the schematics given below and proceed forward.
Schematic (Dual Sensor Interface- LCD )
Source Code: Dual IR Sensor - LCD Interface
Create a new project and compile the sketch below. After the code is successfully uploaded, the Arduino should able to display the status of the sensors on the LCD module with the back light glowing. Both the sensors should be able to work simultaneously if everything is right. You can download the sketch at the end of the source code.
/*
* File: Dual_IR_Sensor_LCD_Arduino.ino
* Author: ron
* December 08, 2013, 2:21 AM
*/
#include <Wire.h>
#include <LiquidCrystal.h>
#define sensor1 x = 9
#define sensor2 y = 8
#define colx 8
int sensor1,sensor2;
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
void setup()
{
pinMode(sensor1, INPUT);
pinMode(sensor2, INPUT);
lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print("Sensor1:");
lcd.setCursor(0,1);
lcd.print("Sensor2:");
}
void set1()
{ lcd.setCursor(colx,0); lcd.print("OK!!"); lcd.setCursor(colx,1); lcd.print("OK!!"); }
void set2()
{ lcd.setCursor(colx,0); lcd.print("OK!!"); lcd.setCursor(colx,1); lcd.print("NA "); }
void set3()
{ lcd.setCursor(colx,0); lcd.print("NA "); lcd.setCursor(colx,1); lcd.print("OK!!"); }
void defaul()
{ lcd.setCursor(colx,0); lcd.print("NA "); lcd.setCursor(colx,1); lcd.print("NA "); }
void loop()
{
int senread1 = digitalRead(sensor1);
int senread2 = digitalRead(sensor2);
if(senread1 == HIGH && senread2 == HIGH )
{ set1(); }
else if(senread1 == !HIGH && senread2 == HIGH )
{ set2(); }
else if(senread1 == HIGH && senread2 == !HIGH )
{ set3(); }
else
{
defaul();
}
delay(1);
}
/* THE END */
Download source code: Dual IR Sensor LCD interface(.uno)
Project Files
- Single IR Sensor Source code
- Dual IR Sensor Source code
- Dual IR Sensor and LCD interface
Thanks for reading
Rakesh Mondal
Related Topics
IR Sensor Interface to PIC18F4550
Proximity Sensors
IR Object detection Sensor Module