You are on page 1of 5

In this tutorial, I am going show how we can measure the distance over a short range

using infrared sensors. Such application is very much useful in robotics, transportation, etc.,

For example, A robotic car which take diversions based upon the distance
measure between the obstacle and the car. An application like, snake robot can measure the
diameter of the hole and make a decision according to that. Similarly in transportation, inter-
vehicular-distance is very important for a safe driving. The inter-vehicular-distance can be
measured by using IR or UV rays sensors.

1. Construction:

1.1 simple IR transmitter:

The IR transmitter can be designed by using simple IR diode and a 220 ohms resistor,
which is shown on the circuit diagram figure 1.

In this application, the main objective is to measure the distance between obstacle and the
IR receiver. Due to that the analog variation of the IR receiver output is taken and then
connected it to an Analog to Digital converter. Here the receiver diode and a 10k ohms
resistor are connected in series between supply and ground to form a voltage divider network
which is shown in the circuit diagram. The principle of operation is very simple; whenever
the IR light is falls on the receiver diode, the resistance offered by the IR receiver is decreases
and allow more current through it. The IR receiver offers more resistance when no IR light is
falls on it. Keep in mind that both IR transmitter and receiver is connected side by side, so
that the reflected light from the obstacle is easily catch by the receiver.

1.3 Analog to Digital converter connections:

The IR receiver produces different voltage variations as output, depend upon the amount
of IR light is reflected back by the obstacle. By using an Analog to digital converter the
analog voltage variations of the IR receiver are converted into digital values. Here I used
a single channel ADC (ADC0804) which contains single input channel. The output of the IR
receiver is connected to pin6 (input channel VIN (+)) of the ADC0804 IC and digital output
is taken from the pin 11(LSB) to 18(MSB). And pin7 VIN (-) is connected to ground.
A clock circuit is made by using simple resistor and capacitor connected to 4 th and 9th pins
of IC, similarly an external clock can be connected instead of R and C circuit between 4 and
19th pins of the ADC IC. Here, the product of R and C values should result 600 kHz at 5V
check the data sheet, hence R = 10k and c = 150pF is used in the clock circuit. The maximum
clock frequency that ADC0804 can operate at 5V is 800 kHz, whereas at 6v the clock
frequency is 1200khz.

Frequency F = 1/(1.1RC).

A 10k pot is used as external reference voltage for the ADC IC, if no external reference is
used then the reference voltage = VREF/2 (or Vcc/2). This is useful to adjust the step size. If
the VREF = 5V (vcc) then the step size is VREF/MAXIMUM DIGITAL OUTPUT value.

Step size = 5v/255 = 19.33mV.

The internal conversion process is completed in 64 clock cycles, but 1 to 8 clock periods
are used after the write pin low to high transition and before conversion starts. And also 1
clock period is taken before INTR pin goes low. Approximately the maximum conversion
time taken by the ADC is equal to (t conversion = 8 clock cycles+ 64 clock cycles + 1 clock
cycle = 73 clock cycles).

Therefore in this example, t conversion = 73 * 1/(606khz) = 120 microseconds.

Finally we three control pins for the ADC operation Read (2 nd pin of ADC), Write (3rd pin
of ADC), INTR (5th pin of ADC). All these three pins are active low signals. By providing a
high to low logic on the Write pin will START the conversion process. If a low to high logic
will eventually set the INTR pin to high. The INTR pin is used to indicate the END of
conversion process. Finally a low signal on Read pin will latch the internal converted data on
to the data port of the ADC.

1.4 Steps to perform the conversion process on ADC:

Provide a high to low logic (>200ns) on write pin to start the conversion process.
Again provide a low to high logic on write pin to set the INTR pin to high.
Check for INTR pin, if it is low means the conversion process is completed.
Finally provide a logic low on the read pin, to read the converted data from the ADC.
After all the steps are completed make read pin.
Make sure that CS, AGND, v- and GND are grounded before performing all the above steps.

1.5 16x2 LCD interface:

The LCD connections and its interfacing programming with 8051 is already discussed on this
website. Therefore, here I encourage you to read the related topics on LCD provided in this
site.
1.6 Ciruit Diagram for IR sensor based Distance measurement system:

Figure1 Ciruit Diagram for IR sensor based Distance measurement system

Note: 1. White color reflects more IR light than any other colour
2. One major problem with the IR distance measure is going below the minimum
sensor range means an object is so close the sensor cannot get an accurate

1.7 software code:

?
/*********************************************************************************
// IR_Distance_Measurement.c - measuring the obstacle distance using infrared sens
during testing
// so don't confuse with the video output.
// Author - lovakiranvarma, M.tech
/*********************************************************************************
#include<reg52 .h="">
#define LCD_PORT P2
// function prototypes
void converttochar(void);
void lcd_data_string(unsigned char *);
void delay(unsigned int );
void lcd_cmd(unsigned char );
void lcd_data(unsigned char );
void LCD_Init(void);
// LCD control pins declaration
sbit RS = P3^7; // Register Select line
sbit ENABLE = P3^6; // Enable line
int value1=0;
void main()
{
ADC_DATA = 0xff; // make P1 as input port
LCD_PORT = 0x00; //make P2 as outport
LCD_Init();
lcd_cmd(0x85); // Setting cursor location at 5th position of the first line
delay(2);
lcd_data_string("IR BASED DISTANCE MEASURE");
delay(100);
lcd_cmd(0x01);
delay(5);
lcd_cmd(0x80);
delay(5);
lcd_data_string("DISTANCE:");
while(1)
{delay(5);
ADC_WRITE=0; // make WRITE to high to low for start of conversion
delay(5);
while(ADC_INTR==1); // check for END of conversion
delay(5);
converttochar();
}
}void LCD_Init()
{
lcd_cmd(0x38); //2 Line, 5X7 Matrix
delay(5);
delay(5);
lcd_cmd(0x06);
delay(5);
lcd_cmd(0x01); // Clear Screen
delay(5);
}
void lcd_cmd(unsigned char Command) // LCD Command Sending Function declaration
{
LCD_PORT = Command;
RS= 0;
RW=0;
ENABLE = 1;
delay(1);
ENABLE = 0;
return;
}
void lcd_data(unsigned char data_value) // LCD data Sending Function declaration
{
LCD_PORT = data_value;
RS= 1;
RW=0;
ENABLE = 1;
delay(1);
ENABLE = 0;
return;
}
void lcd_data_string(unsigned char *string) // LCD Command Sending String declarati
{
int i=0,j=0;
while(string[i]!='\0')
{
if(i>=9)
{
// If the number of characters in the string > 16, then the below command automa
lcd_cmd(0xc0+j++); // Shift the display right side
}
lcd_data(string[i]);
i++;
delay(2);
}
return;
}
void converttochar()
{
int M;
M=value1/100;
M=M/10;
value1=value1%100;
value1=value1%10;
lcd_cmd(0x8a);
if(M!=0)
lcd_data(M+48);
else
lcd_cmd(0x06);
M=value1/10;
value1=value1%10;
lcd_data(M+48);
lcd_data(value1+48);
lcd_data(' ');
delay(5);
}
void delay(unsigned int DELAY_VALUE ) // delay function
{
int i ,j ;
for(i=0;i<=DELAY_VALUE;i++)
for(j=0; j<1000 data-blogger-escaped-j="" data-blogger-escaped-pre="">

<!--1000--></reg52>