8051 Ultrasonic Distance Measurement

The project of 8051 US Distance Measurement will help you measure the distance of object from the position of ultrasonic sensor. Remember, the connections are very important in this project for the successful testing of your circuit.

Connection Details

First understand the connection details of LCD display with 8051 dev. board. They are given as follows:

  • P0.0 to P0.7 → D0 to D7 of LCD display respectively
  • P2.0 → RS of LCD display
  • P2.1 → RW of LCD display
  • P2.2 → E of LCD display

Then connect the remaining pins of LCD display as follows:

  • Vss → ground of 8051 dev. board
  • Vdd → +5V supply of 8051 dev. board
  • Vee → ground of 8051 dev. board

Now connect the ultrasonic sensor, HC-SR-04/05 to 8051 dev. board as follows:

  • Vcc → +5V
  • Echo → P3.2
  • Trigger → P3.5
  • Ground → Ground

We suggest you to do this connections of breadboard by fixing the 16×2 LCD display on it and making the connections using jumper wires.

Important connection details of 16×2 LCD display with 8051 dev. board. The remaining connection details are given above.

The Tested Code

#include
#define port P2
#define dataport P0
int cms;
sbit trig=P3^5;
sbit rs=port^0;
sbit rw=port^1;
sbit e=port^2;

void delay(unsigned int msec)
{
  int i,j;
  for(i=0;i<msec;i++)
  for(j=0;j<1275;j++);
}

void lcd_cmd(unsigned char item) // Function to send command to LCD
{
  dataport = item;
  rs= 0;
  rw=0;
  e=1;
  delay(1);
  e=0;
  return;
}

void lcd_data(unsigned char item) // Function to send data to LCD
{
  dataport = item;
  rs= 1;
  rw=0;
  e=1;
  delay(1);
  e=0;
  return;
}

void lcd_data_string(unsigned char *str) // Function to send string to LCD
{
  int i=0;
  while(str[i]!='')
   {
    lcd_data(str[i]);
    i++;
    delay(1);
   }
return;
}

void send_pulse(void) 
{
  TH0=0x00;TL0=0x00;
  trig=1;                  //Sending trigger pulse
  delay(5);                //Wait for about 10us
  trig=0;                  //Turn off trigger
}

unsigned int get_range(void)
{
  long int timer_val;
  send_pulse();
  while(!INT0);          //Waiting until echo pulse is detected
  while(INT0);           //Waiting until echo pulse changes its state
  timer_val=(TH0<<8)+TL0;
  lcd_cmd(0x81);
  lcd_data_string("output:");
  lcd_cmd(0x8a);
  if(timer_val<38000)
   {
    cms=timer_val/59;
    if (cms!=0)
     {
      lcd_data(cms+48);
     }
   }
  else
  {
   lcd_cmd(0x06);
   lcd_data_string("Object out of range");
  }
  return cms;
}

void main()
{
  lcd_cmd(0x38);
  lcd_cmd(0x0c);
  delay(2);
  lcd_cmd(0x01);
  delay(2);
  lcd_cmd(0x81);
  delay(2);
  lcd_data_string("start");
  delay(20);
  TMOD=0x09;//timer0 in 16 bit mode with gate enable
  TR0=1;//timer run enabled
  TH0=0x00;
  TL0=0x00;
  P3|=0x04;//setting pin P3.2
  while(1)
  {
   get_range();
   delay(2);
  }
}

If you have any problem, don’t hesitate to contact us.

If you liked this post please write Google feedback about us.
Thanks in advance!

One thought on “8051 Ultrasonic Distance Measurement

Leave a comment

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