IR Sensor Interfacing With 8051

In our previous tutorial, we have seen how to interface the PIR sensor with 8051. In this tutorial, we are going to see IR Sensor Interfacing with 8051.


Before starting this tutorial we should know the below topics. If you know already, please go further.

Components Required

  • 8051 Development Board
  • IR Sensor
  • LCD Module (To print the Sensor output)


Infrared is light that has a wavelength longer than visible red light. The ranges of infrared include near-infrared, mid-infrared, and far-infrared, spanning wavelengths from about 710 nanometers (near-infrared) to 100 micrometers (far infrared).

All objects emit light according to their temperature–this is called “black body radiation.” The hotter the object, the shorter wavelength of light it emits. The Earth emits infrared light at a peak of about nine to 10 micrometers–and so do warm-blooded animals like humans. This light can be used to detect motion or warmth.

IR Sensor

The infrared Obstacle Sensor Module has a built-in IR transmitter and IR receiver that sends out IR energy and looks for reflected IR energy to detect the presence of any obstacle in front of the sensor module. The PCB of this electronic circuit has a potentiometer. That onboard potentiometer lets users adjust the detection range. The sensor has a very good and stable response even in ambient light or in complete darkness.


  • Operating Voltage: 3.0V – 5.0V
  • Detection range: 2cm – 30cm (Adjustable using potentiometer)
  • Current Consumption:at 3.3V : ~23 mA,at 5.0V: ~43 mA
  • Active output level: Outputs Low logic level when an obstacle is detected
  • Onboard Obstacle Detection LED indicator

Working Principle of IR Obstacle Sensor

An IR sensor consists of an IR LED and an IR Photodiode; together they are called as Photo–Coupler or Opto–Coupler. As said before, the Infrared Obstacle Sensor has a built-in IR transmitter and IR receiver. An infrared Transmitter is a light-emitting diode (LED) that emits infrared radiations. Hence, they are called IR LED. Even though an IR LED looks like a normal LED, the radiation emitted by it is invisible to the human eye.

IR Sensor Interfacing

Infrared receivers are also called as infrared sensors as they detect the radiation from an IR transmitter. IR receivers come in the form of photodiodes and phototransistors. Infrared Photodiodes are different from normal photodiodes as they detect only infrared radiation. When the IR transmitter emits radiation, it reaches the object and some of the radiation reflects back to the IR receiver. Based on the intensity of the reception by the IR receiver, the output of the sensor is defined.

IR Sensor Interfacing with 8051


IR Sensor

  • Vcc – 5v
  • GND – Ground
  • Vout – P1.0


  • RS – P2.0
  • RW – P2.1
  • EN – P2.2
  • Data Lines – P3.0 – P3.7

IR Sensor Interfacing with 8051

Source Code

If it is detecting an object in front of this sensor, LCD will display “Obstacle Detected”.

#define lcd P3

sbit IR=P1^0;

sbit rs=P2^0; //register select
sbit rw=P2^1; //RW
sbit en=P2^2; //enable

void lcd_init();
void cmd(unsigned char);
void dat(unsigned char);
void delay();
void lcd_string(char *s);

void main()
    lcd_string("  EmbeTronicX   ");
    while(1) {
        if(IR == 0) {
            lcd_string("Obstacle Detected");
        } else {
            lcd_string("                ");

void lcd_init()

void cmd(unsigned char a)

void dat(unsigned char b)

void lcd_string(char *s)
    while(*s) {

void delay()
    unsigned int i;

If you want to sense more distance you can use below IR sensor. You can also adjust the distance using this. This is an Infrared Transmitter and receiver which together make up a photoelectric sensor. The sensor has a long detection distance and has less interference by visible light because it uses modulated Infrared light. This sensor has a screwdriver adjustment to set the detected distance, then gives a digital output when it senses something within that range. This sensor does not return a distance VALUE.



In our next tutorial, we will see how to interface the GPS with 8051.

You can also read the below tutorials.

Linux Device Driver TutorialsC Programming Tutorials
FreeRTOS TutorialsNuttX RTOS Tutorials
RTX RTOS TutorialsInterrupts Basics
I2C Protocol – Part 1 (Basics)I2C Protocol – Part 2 (Advanced Topics)
STM32 TutorialsLPC2148 (ARM7) Tutorials
PIC16F877A Tutorials8051 Tutorials
Unit Testing in C TutorialsESP32-IDF Tutorials
Raspberry Pi TutorialsEmbedded Interview Topics
Reset Sequence in ARM Cortex-M4BLE Basics
VIC and NVIC in ARMSPI – Serial Peripheral Interface Protocol
STM32F7 Bootloader TutorialsRaspberry PI Pico Tutorials
STM32F103 Bootloader TutorialsRT-Thread RTOS Tutorials
Zephyr RTOS Tutorials - STM32Zephyr RTOS Tutorials - ESP32
AUTOSAR TutorialsUDS Protocol Tutorials
Product ReviewsSTM32 MikroC Bootloader Tutorial
VHDL Tutorials
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
Table of Contents