How to work most commonly used IR Infrared sensors with Arduino

How to work most commonly used IR Infrared sensors with Arduino

Hello, welcome back. In this tutorial, we will learn how the two most commonly used IR Infrared sensors work with Arduino. Also, you can easily understand how these sensors work, as they are explained step by step separately. These sensors can use to detect black, white, and obstacles. Therefore, these are can be used mainly to build line-following robots, obstacle-avoiding robots, and edge detection robots. Keep reading.

Okay, let’s learn how these sensors work step by step. The required components are as follows.

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

Firstly, identify these components.

The first sensor type

How to work most commonly used IR Infrared sensors with Arduino

Identifying black and white

Step 1

Firstly, connect these components as follows.

How to work most commonly used IR Infrared sensors with Arduino

Step 2

Secondly, let’s creates the program for this project. It is as follows.

  • The complete program of this project – download
/*IR sensor tutorial
 * https://srituhobby.com
 */
 
#define LED1 2
#define LED2 3
#define Sensor 5

void setup() {
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(Sensor, INPUT);
}
void loop() {
  bool value = digitalRead(Sensor);
  if (value == 1) {
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
  } else if (value == 0) {
    digitalWrite(LED2, HIGH);
    digitalWrite(LED1, LOW);
  }
}
Code explanation

First, the LED, buzzer and sensor PINs are defined.

#define LED1 2
#define LED2 3
#define Sensor 5

In the setup function, these PINs are set as Input and Output PINs.

void setup() {
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(Sensor, INPUT);
}

In the loop function,

void loop() {
//Gets the sensor values and puts them into the variable
  bool value = digitalRead(Sensor);
//These values are checked using IF function. If the value is 1, the LED1 is turns ON.
  if (value == 1) {
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
//If the value is 0, the LED2 is turns OFF.
  } else if (value == 0) {
    digitalWrite(LED2, HIGH);
    digitalWrite(LED1, LOW);
  }
}

Step 3

Lastly, select board and port. After, upload this code to the Arduino board. Then test this sensor using a black and white surface.

The second sensor type

How to work most commonly used IR Infrared sensors with Arduino

Identifying black, white and obstacles

Step 1

Firstly, connect this sensor as follows.

How to work most commonly used IR Infrared sensors with Arduino

Step 2

Secondly, let’s creates the program for this project. It is as follows.

  • The complete program of this project – download
/*IR sensor tutorial
 * https://srituhobby.com
 */
 
#define LED1 2
#define LED2 3
#define Buzzer 4
#define SensorD 5
#define SensorA A0

void setup() {
  Serial.begin(9600);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(Buzzer, OUTPUT);
  pinMode(SensorD, INPUT);
}

void loop() {
  bool value = digitalRead(SensorD);
  if (value == 1) {
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
  } else if (value == 0) {
    digitalWrite(LED2, HIGH);
    digitalWrite(LED1, LOW);
  }

  int value1 = analogRead(SensorA);
  if (value1 <= 50) {
    digitalWrite(Buzzer, HIGH);
  } else {
    digitalWrite(Buzzer, LOW);
  }
}
Code explanation

Firstly, the LED, buzzer, and sensor pins are defined.

#define LED1 2
#define LED2 3
#define Buzzer 4
#define SensorD 5
#define SensorA A0

In the setup function, these pins are set as input and output pins.

void setup() {
  Serial.begin(9600);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(Buzzer, OUTPUT);
  pinMode(SensorD, INPUT);
}

The loop function includes black, white, and obstacle detection codes. These are described below.

void loop() {
//Black and white detection code
//Gets the digital values through this sensor
  bool value = digitalRead(SensorD);
//These values are checked using the IF condition. If the value is 1, the LED1 is turns ON.
  if (value == 1) {
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
//If the value is 0, the LED2 is turns OFF.
  } else if (value == 0) {
    digitalWrite(LED2, HIGH);
    digitalWrite(LED1, LOW);
  }

//Obstacle detection code
//Gets the analog values through this sensor
  int value1 = analogRead(SensorA);
//These values are checked using the IF condition. If the value is less than or equal to 50, the buzzer is turns ON.
  if (value1 <= 50) {
    digitalWrite(Buzzer, HIGH);
//Otherwise, the buzzer is turns OFF.
  } else {
    digitalWrite(Buzzer, LOW);
  }
}

Step 3

Lastly, select board and port. After, upload this code to the Arduino board.

OK, enjoy this project. The full video guide is given below. So, we will meet in the next tutorial.

How to work most commonly used IR Infrared sensors with Arduino

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *