IR SENSOR AND ARDUINO

An infrared sensor is an electronic module which is used to sense certain physical appearance of its surroundings by either emitting and/or detecting infrared radiation. IR sensors are also capable of determining the heat being emitted by an object and detecting motion. Now lets learn interfacing of IR Sensor and Arduino.

IR SENSOR WORKING:

Here we are using IR sensor for detecting obstacle. IR transmitter transmits IR signal, as that signal detects any obstacle in its path, the transmitted IR signal reflects back from the obstacle and received by the receiver.

This is similar to topic Interface of Switch as an input to Arduino board and gets output, but here we replace switch by IR sensor this is like automatic switch which gives signal on obstacle detection.

PINOUT:

  1. VCC: 3.3V-5V power input pin
  2. GND: 0V power pin
  3. OUT: Digital Output Pin

IR Sensor Module Pin Out

TECHNICAL SPECIFICATION/RATING

  • Operating Voltage: 3.0V – 6.0V
  • Current: at 3.3V : ~23 mA & at 5.0V: ~43 mA

REQUIRED HARDWARE

S.No. Item Quantity
1 Arduino Uno 1
2 Breadboard 1
3 IR Sensor 1
4 LED 1
5 Resistor 220 ohm 1
6 Male to Male jumper 5

BUILDING CIRCUIT IR SENSOR AND ARDUINO

Here we connected Pins describe below

Arduino pin LED
13 Anode (+)
GND Cathode (-)

 

Arduino pin IR Sensor
VCC VCC
GND GND
10 OUTPUT (O)

Make circuit connection as shown

IR Sensor and Arduino Interface 

IR SENSOR ARDUINO CODE

int LEDpin = 13;
int obstaclePin = 10;
int hasObstacle = LOW;  // LOW MEANS NO OBSTACLE

void setup() {
  pinMode(LEDpin, OUTPUT);
  pinMode(obstaclePin, INPUT);
  Serial.begin(9600);
}

void loop() {
  hasObstacle = digitalRead(obstaclePin);

  if (hasObstacle == HIGH) {
    Serial.println("Stop something is ahead!!");
    digitalWrite(LEDpin, HIGH);
  }
  else {
    Serial.println("Path is clear");
    digitalWrite(LEDpin, LOW);
  }
  delay(200);
}

IR SENSOR ARDUINO CODE EXPLANATION

Here we define an integer type variable LED and assigns 13 to it. Then we define another integer type variable obstaclePin and assign 10 to it. Then we define another variable hasObstacle and set its state to LOW (0), this is used for the reading status of pin number 7 of the arduino. Status either LOW or HIGH means is obstacle present then HIGH if not then LOW.

int LEDpin = 13;
int obstaclePin = 10;
int hasObstacle = LOW;  // LOW MEANS NO OBSTACLE

Now in void setup(), define pin-mode that is LED as output and obstaclePin as input means where we use term LED it is 13 there and where we use obstaclePin it is 10 there. Then for serial communication we are setting baud rate of 9600 by using function Serial.begin(). We will discuss about baud rate and serial communication in later chapter (Communication System).

void setup() {
  pinMode(LEDpin, OUTPUT);
  pinMode(obstaclePin, INPUT);
  Serial.begin(9600);
}

In void loop(), first we are reading obstaclePin(10) state is it either HIGH or LOW, as we connected IR sensor over there, as IR sensor detect any obstacle by transmitting and receiving waves it gives HIGH signal which in turn sets obstaclePin(10) of arduino to HIGH and If sensor does not detect any obstacle it gives LOW signal which in turn sets obstaclePin(10) of arduino to LOW.This signal will be stored in variable hasObstacle.So there are two conditions for hasObstacle either HIGH or LOW.

Then we will check itin ‘if block’, is it is HIGH, if condition matched then we print a message on serial monitor “Stop something is ahead!”& Turns LED on by doing LEDpin(13) HIGH.

Otherwise the signal will we LOW, so in ‘else block’ we print message on serial monitor “Path is clear”& Turns LED off.

This complete cycle will be delayed by 200 milliseconds or 0.2 seconds.

void loop() {
  hasObstacle = digitalRead(obstaclePin);

  if (hasObstacle == HIGH) {
    Serial.println("Stop something is ahead!!");
    digitalWrite(LEDpin, HIGH);
  }
  else {
    Serial.println("Path is clear");
    digitalWrite(LEDpin, LOW);
  }
  delay(200);
}

OUTPUT

As obstacle will detected a message will print on serial monitor & LED turned ON and if there is no obstacle detected, LED will be in turned off condition.

Total Visits: 2049
0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Translate »
0
Would love your thoughts, please comment.x
()
x