How to Use IR Infrared Sensor With Arduino

by STEAM-DIY in Circuits > Arduino

811 Views, 10 Favorites, 0 Comments

How to Use IR Infrared Sensor With Arduino

How-to-use-IR-Sensor-Module-with-Arduino-1000x562.jpg
Screenshot 2024-10-11 095535.png

Welcome back to my blog. We described the PIR(Motion) sensor in the previous post. Today we are going to talk about the IR sensor modules in this post. This sensor is the most important for making robots. I promise to present those projects in the next articles.




1st Sensor type

This sensor has four pins. The DO pin of the sensor returns a digital value and the AO pin returns an analog value. If you want to change the sensing range, do so with the preset control.

The PIN structure of this sensor


Supplies

  1. Arduino Uno board 
  2. IR sensor 
  3. LED bulb x 2
  4. Buzzer x 1
  5. Breadboard 
  6. Jumper Wires

Firstly, identify these components. 

Diagram

10-7.png

Diagram

Code

copy and paste the following program to the Arduino IDE.

void setup() {
Serial.begin(9600);//enable serial monitor
pinMode(2, INPUT);//define arduino pin
pinMode(3, OUTPUT);//define arduino pin
pinMode(4, OUTPUT);//define arduino pin
pinMode(5, OUTPUT);//define arduino pin
}

void loop() {
balckwhite();//Identification of black and white
obstacle();//Identification obstacles.

}
void balckwhite() {
bool led = digitalRead(2);
if (led == 0) {
Serial.println("White");
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
} else {
Serial.println("Black");
digitalWrite(4, HIGH);
digitalWrite(3, LOW);
}
}
void obstacle() {
int buzzer = analogRead(A0);
if (buzzer <= 80) {
digitalWrite(5, HIGH);
Serial.println("Buzzer on");
} else {
digitalWrite(5, LOW);
Serial.println("Buzzer off");
}
}

This code sets the 2nd pin as the input pin and the 3,4,5 pin as the output pin.

void setup() {
Serial.begin(9600);//enable serial monitor
pinMode(2, INPUT);//define arduino pin
pinMode(3, OUTPUT);//define arduino pin
pinMode(4, OUTPUT);//define arduino pin
pinMode(5, OUTPUT);//define arduino pin
}


This code takes the digital value and puts it into the Boolean variable. The Boolean variable is named ‘led’. This value is checked using the IF condition. When the digital value is 0, the green LED bulb turns on and when the digital value is 1, the yellow LED bulb turns on.

void balckwhite() {
bool led = digitalRead(2);
if (led == 0) {
Serial.println("White");
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
} else {
Serial.println("Black");
digitalWrite(4, HIGH);
digitalWrite(3, LOW);
}
}


This code takes the analog value and it put into the integer variable. The integer variable is named “buzzer”. This value is checked using the IF condition. If the analog value is less than or equal to 80, the buzzer turns on.

void obstacle() {
int buzzer = analogRead(A0);
if (buzzer <= 80) {
digitalWrite(5, HIGH);
Serial.println("Buzzer on");
} else {
digitalWrite(5, LOW);
Serial.println("Buzzer off");
}
}


Now, select the board and port. After, click the upload button.

2nd Type of IR Sensor

2nd sensor type

Ok, let’s see how to use another type of IR sensor with Arduino.

The PIN structure of this sensor

So, step by step we will learn how to connect this sensor with Arduino. For that, the required components are given below.

  1. Arduino Nano board 
  2. IR sensor
  3. LED bulb
  4. Breadboard 
  5. Jumper Wires  

Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.


Step 1

Firstly, identify these components.

Step 2

Attach the components to your breadboard.

Step 3

Connect these components to your Arduino board using the jumper wires. Do it according to the circuit diagram below.

Connect the sensor OUT pin to the Arduino board D2 pin and connect the LED anode pin to the D3 pin.

Step 4

Now, copy and paste the following program to the Arduino IDE.

  1. The complete program of this project – Download
void setup() {
pinMode(2, INPUT);//define arduino pin
pinMode(3, OUTPUT);//define arduino pin
Serial.begin(9600);//enable serial monitor
}
void loop() {
bool value = digitalRead(2);//get value and save it boolean veriable
if (value == 1) { //check condition
Serial.println("ON");//print serial monitor ON
digitalWrite(3,HIGH);//LED on
} else {
Serial.println("OFF");//print serial monitor OFF
digitalWrite(3,LOW);//LED off
}
}


This code sets 2 pins as input pins and 3 pins as output pins.

void setup() {
pinMode(2, INPUT);//define arduino pin
pinMode(3, OUTPUT);//define arduino pin
Serial.begin(9600);//enable serial monitor
}


Takes the digital value and puts it into the Boolean variable. The Boolean variable is named ‘value’.

bool value = digitalRead(2);//get value and save it boolean veriable


This value is checked using the IF condition. When the digital value is 1, the LED bulb turns on and when the digital value is 0, the LED bulb turns off.

if (value == 1) { //check condition
Serial.println("ON");//print serial monitor ON
digitalWrite(3,HIGH);//LED on
} else {
Serial.println("OFF");//print serial monitor OFF
digitalWrite(3,LOW);//LED off
}


Step 5

Now, select the board and port. After, click the upload button.

So, now you can test this sensor. The full video guide is given below.

IR infrared sensor with Arduino – How does work IR infrared sensor

3rd sensor type

This sensor includes three pins. Also, we can get a digital value through this sensor. This sensor can also be used to detect black and white as well as obstacles. Also, we can control the sensing range using the potentiometer on this sensor.

The PIN diagram of this sensor

So, step by step we will learn how to connect this sensor with Arduino. For that, the required components are given below.

  1. Arduino Nano board —
  2. Line tracking sensor — 
  3. LED bulb x 2 —
  4. Buzzer x 1 — 
  5. 180-ohm resistor x 2 — 
  6. Breadboard — 
  7. Jumper Wires — 

Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.

Step 1

Firstly, identify these components.

Step 2

Secondly, connect these components. For that, use the circuit diagram below.

Step 3

Thirdly, we will create the required program for this. It is as follows.

  1. The complete program of this project – Download
/*Line tracking sensor tutorial.
*This code created by the SriTu Hobby team.
* https://srituhobby.com
*/

#define Sensor 2
#define Buzzer 3
#define Red 4
#define Green 5

void setup() {
Serial.begin(9600);
pinMode(Sensor, INPUT);
pinMode(Buzzer, OUTPUT);
pinMode(Green, OUTPUT);
pinMode(Red, OUTPUT);
}
void loop() {
bool value = digitalRead(Sensor);
Serial.println(value);

if (value == 0) {
digitalWrite(Buzzer, HIGH);
digitalWrite(Green, HIGH);
digitalWrite(Red, LOW);
} else if (value == 1) {
digitalWrite(Buzzer, LOW);
digitalWrite(Green, LOW);
digitalWrite(Red, HIGH);
}
}


Code explanation

Firstly, this code defines the sensor, LED, and buzzer-connected pins.

#define Sensor 2
#define Buzzer 3
#define Red 4
#define Green 5

Secondly, this code sets the sensor pin as the input pin and the buzzer, LED pin as the output pin.

void setup() {
Serial.begin(9600);
pinMode(Sensor, INPUT);
pinMode(Buzzer, OUTPUT);
pinMode(Green, OUTPUT);
pinMode(Red, OUTPUT);
}

In the loop function, the sensor values are taken and put into the Boolean variable. After, these values are then checked using the IF condition. Then if the value is 0, the buzzer and green LED bulb will be activated. If the value is 1, the red LED bulb will be activated.

void loop() {
bool value = digitalRead(Sensor);
Serial.println(value);

if (value == 0) {
digitalWrite(Buzzer, HIGH);
digitalWrite(Green, HIGH);
digitalWrite(Red, LOW);
} else if (value == 1) {
digitalWrite(Buzzer, LOW);
digitalWrite(Green, LOW);
digitalWrite(Red, HIGH);
}
}


Step 4

Lastly, select the board and port. Afterward, upload this code.

OK, now you can test this sensor. For that, use a black and white surface. The full video guide is given below. So, we will meet in the next tutorial. Have a good day.



Please subscribe my channel thanks