Obstacle Detector by IR Sensor & ATmega328 Arduino

1,098 views

We all have surely observed that our phone display turns off during phone calls when we put it near our ears. So, do you ever wonder how does this happen???

It is not a hi-tech thing but a simple IR sensor to detect the obstacle near the phone we are also making the same project in this article, we are going to make an “Obstacle Detector” with an Infrared IR Sensor and ATmega328p Chip. This commonly used IR Sensor can also be used as Line Follower Robot we will discuss that later. Let’s start making it a simple Obstacle Detector.

JLCPCB is the foremost PCB prototype & manufacturing company in China, providing us with the best service we have ever experienced regarding (Quality, Price Service & Time).

Hardware Required

You will require the following hardware to make this obstacle detector.

Sr NoValueQty
1ATmega328p IC1
2USB cable for uploading the code1
3IR sensor1
4Buzzer and an LED1
5220-ohm resistor1
7Jumper wires and a breadboard1

Atmega328p Pinout

Obstacle Detector Circuit

Connection Table

Arduino UNOIR Sensor
( +5V ) VCC
GND GND
D2 PinOUT Pin
Arduino UNOBuzzer
D7 PinPositive
GND Negative
ArduinoLED R220 Ohm Resistor
D6 PinAnode Pin 
GND Terminal 1
 Cathode PinTerminal 2

Working Explanation

As the code in the setup section, we define the pin along with the type to which the sensor is connected and also initialize the pin and type for the led and buzzer.

In the loop section, we read the digital value on pin 2 of the microcontroller and according to the value, we apply conditions either 1 or 0 to turn the led and buzzer on or off. In the analog code in the loop section, we read the analog value i.e., 0-1023 values on pin A0 and according to the value of 200 or more, we turn the led and buzzer on or off.

Atmega328p Code

 int val = 0 ;  
 void setup()  
 {  
 Serial.begin(9600); // sensor buart rate  
 pinMode(2,INPUT);  // IR sensor output pin connected  
 pinMode(6,OUTPUT);  // LED  
 pinMode(7,OUTPUT);  // BUZZER  
 }  
 void loop()  
 {  
 val = digitalRead(2);  //  IR sensor output pin connected  
 Serial.println(val);  // see the value in serial monitor in Arduino IDE  
 delay(500);  
 if(val == 1 )  
 {  
 digitalWrite(6,HIGH);  // LED ON  
 digitalWrite(7,HIGH);  // BUZZER ON  
 }  
 else  
 {  
 digitalWrite(6,LOW);  // LED OFF  
 digitalWrite(7,LOW);  // BUZZER OFF  
 }  
 }  

Applications

  • To detect the presence of objects nearby
  • Security Alarm
  • Smart Gadgets