EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
You are here: Home / Topics / Interfacing AT89S51 with ultrasonic sensor and LCD

Interfacing AT89S51 with ultrasonic sensor and LCD

|

Microcontroller › 8051 › Interfacing AT89S51 with ultrasonic sensor and LCD

  • This topic has 40 replies, 28 voices, and was last updated 4 years, 6 months ago by Shirsendu Das.
Viewing 15 posts - 1 through 15 (of 41 total)
1 2 3 →
  • Author
    Posts
  • January 17, 2013 at 7:56 pm #2076
    zulnorain
    Participant

    I want to make a project to measure distance using ultrasonic sensor HC-S04 and LCD to display the distance in cm. I use AT89S51, the trigger pin for ultrasonic sensor is P3.5 and the echo pin is P3.2. The echo pin is connected to P3.2 because I use interrupt so that whenever there is a HIGH it will activate timer 0 and the duration of HIGH condition (in microsecond) will be converted to cm (divided by 59). I already make sure that the ultrasonic sensor works well. The problem is the LCD always display output the same number 45. It is not varying although the distance between object and ultrasonic sensor change. Can anyone help me to solve this problem?

     

    #include<REGX51.h>
    #include<intrins.h>// for using _nop_() function
    #define port P2
    #define dataport P0
    sfr16 DPTR =0x82;
    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) //to generate 10 microseconds delay
    {
    TH0=0x00;TL0=0x00;
     trig=1;
     _nop_();_nop_();_nop_();_nop_();_nop_();
     _nop_();_nop_();_nop_();_nop_();_nop_();
     trig=0;
       
    }
     
    unsigned int get_range(void)
    {
     int range=0;
     int s;
     send_pulse();
     while(!INT0);//         in sake of these lines you can generate a delay of 40 Milli seconds=40000 micro
        while (INT0);//         seconds

     DPH=TH0;DPL=TL0;  
     TH0=0xFF;TL0=0xFF;
     lcd_cmd(0x81);
     delay(2);
     lcd_data_string("output:");
     lcd_cmd(0x8a);
     if(DPTR<35000)
     {//actually you need to use 38000 but the sensor may not work at higher levels
      range=DPTR/59;
      s=range/100;
      range=range%100;
      if(s!=0)
      {
      lcd_data(s+48);
      }
      else
      {
      lcd_cmd(0x06);
      s=range/10;
      range=range%10;
      lcd_data(s+48);
      lcd_data(range+48);
      lcd_data(' ');
      }
     }
     else
     {
     range=0; // indicates that there is no obstacle in front of the sensor
     lcd_cmd(0x06);
     lcd_data(0);
     }
     return range;
    }
     
    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);  
     }
    }

     

    January 18, 2013 at 10:03 am #8976
    nidhin.k
    Participant

     

    Try this code….. it will work….
     
     
    #include<reg52.h>
     
    #define First_Line 0x80
    #define Second_Line 0xc0
    #define Curser_On  0x0f
    #define Curser_Off 0x0c
    #define Clear_Display 0x01
    #define Data_Port P0
     
    sbit Lcd_rs = P2^7;
    sbit Lcd_rw = P2^6;
    sbit Lcd_en = P2^5;
     
    sbit RX= P3^2; 
    sbit TX= P3^5;
     
    void Distance_Measure();
    void Lcd8_Init();
    void Lcd8_Command(unsigned char);
    void Lcd8_Write(unsigned char,unsigned char);
    void Lcd8_Display(unsigned char,const unsigned char*,unsigned int);
    void Lcd8_Decimal2(unsigned char,unsigned char);
    void Lcd8_Decimal3(unsigned char,unsigned char);
    void Lcd8_Decimal4(unsigned char,unsigned int);
    void Delay(unsigned int);
    void del();  
     
    unsigned char i,inch1,inch2,result,obj_distance,rept,sec,j,a=0,c=0,kc,cu=0xc0,l,e[20],num[20],n,v[20];
    unsigned long int count=0,count1=0;
     
    void main()
    {
     
    EA=1;
    ET0=1;
    TMOD=0x01;
    TH0=TL0=0x00;
    TR0=1;
    relay2=0;
    Lcd8_Init();Delay(65000); 
        Lcd8_Display(0x80,”Ultrasonic      “,16);
    Lcd8_Display(0xc0,”                “,16);
    Delay(65000);Delay(65000);
    Lcd8_Display(0xC0,”Distance:       “,16);
     
    while(1)
    {
     
    Distance_Measure();
     
    }
    }
     
     
    void Distance_Measure()
    {
    count=count1=0;
    TH0=0x00;TL0=0x00; 
    TR0=0;
    for(i=0;i<10;i++)         
    {
    TX=0;           
    Delay(1000);
    TX=1;           
    Delay(10);
    TX=0;   
    while(RX==0);
    TR0=1; 
    while(RX==1);
    TR0=0;
    count=TH0<<8;      
    count=count|TL0;    
    TH0=TL0=0x00; 
    count1=count1+count;
    Delay(5000);        
    }
    count=count1/10;
    inch1=count%10000/1000;
    inch2=count%1000/100;
    result=(inch1*10)+inch2;
     
    obj_distance=result-2;  
    Lcd8_Decimal3(0XCb,obj_distance); 
     
    }
     
    void Lcd8_Init()
    {
    Lcd8_Command(0x38); //to select function set
    Lcd8_Command(0x06); //entry mode set
    Lcd8_Command(0x0c); //display on
    Lcd8_Command(0x01); //clear display
    }
     
    void Lcd8_Command(unsigned char com)
    {
    Data_Port=com;
    Lcd_en=1;
    Lcd_rs=Lcd_rw=0;
    Delay(125);
    Lcd_en=0;
    Delay(125);
    }
     
    void Lcd8_Write(unsigned char com,unsigned char lr)
    {
    Lcd8_Command(com);
     
    Data_Port=lr; // Data 
    Lcd_en=Lcd_rs=1;
    Lcd_rw=0;
    Delay(125);
    Lcd_en=0;
    Delay(125);
    }
     
    void Lcd8_Display(unsigned char com,const unsigned char *word,unsigned int n)
    {
    unsigned char Lcd_i;
     
    for(Lcd_i=0;Lcd_i<n;Lcd_i++)
    { 
    Lcd8_Write(com+Lcd_i,word[Lcd_i]);
      }
    }
     
    void Lcd8_Decimal2(unsigned char com,unsigned char val)
    {
    unsigned int Lcd_hr,Lcd_t,Lcd_o;
     
    Lcd_hr=val%100;
    Lcd_t=Lcd_hr/10;
    Lcd_o=Lcd_hr%10;
     
    Lcd8_Write(com,Lcd_t+0x30);
    Lcd8_Write(com+1,Lcd_o+0x30);
    }
     
     
    void Lcd8_Decimal3(unsigned char com,unsigned char val)
    {
    unsigned int Lcd_h,Lcd_hr,Lcd_t,Lcd_o;
     
    Lcd_h=val/100;
    Lcd_hr=val%100;
    Lcd_t=Lcd_hr/10;
    Lcd_o=Lcd_hr%10;
     
    Lcd8_Write(com,Lcd_h+0x30);
    Lcd8_Write(com+1,Lcd_t+0x30);
    Lcd8_Write(com+2,Lcd_o+0x30);
    }
     
     
     
    void Delay(unsigned int del)
    {
    while(del–);
    }
     
     
    January 18, 2013 at 12:06 pm #8978
    AJISH ALFRED
    Participant

    I suspect the working of this function.

     

    void send_pulse(void) //to generate 10 microseconds delay
    {
    TH0=0x00;TL0=0x00;
     trig=1;
     _nop_();_nop_();_nop_();_nop_();_nop_();
     _nop_();_nop_();_nop_();_nop_();_nop_();
     trig=0;
       
    }

    How can you make sure that this code will generate exactly 10 microsecond delay? It depends on your crystal frequency and how your compiler compiles the code.

    Suggest you use a timer for that also

    January 19, 2013 at 7:19 pm #8992
    zulnorain
    Participant

    hey ,

    nidhin.k can you please explain the code ?? and in what units the range is displayed(cm/m)

    January 21, 2013 at 5:59 am #9003
    nidhin.k
    Participant

    hai zulnorain…..

     

     

        in that program, i just high low the transitter pin and waiting for the  recieve high low pulse.

    during that time i will on the timer and counter for calulating the receieve timing. the values in TH0 and TL0 will give the exact timing.

     

    after some calculation it will convert it to inch.

     

     

    u can check and caliber it into cm….

    January 21, 2013 at 7:01 pm #9023
    zulnorain
    Participant

    nidhin.k thanx for the code buddy its working fine….now if i want to introduce leds and buzzer in it how will that be done?

    for example 4 red leds (P0^0 to P0^3) glow when distance is <= 20 inches and buzzer (P1.3) is sound…if distance > 20

    4 green leds glow(p0.4 to p0.7)

     

    please help…

    January 22, 2013 at 4:32 am #9025
    nidhin.k
    Participant

    hai zulorain….

     

     

    u just edit that code…

     

     

    sbit red1=P0^0;

    sbit red2=P0^1;

    sbit red3=P0^2;

    sbit red3=P0^3;

     

    sbit green1=P0^4;

    sbit green2=P0^5;

    sbit green3=P0^6;

    sbit green4=P0^7;

    sbit buzzer=P1^3;

     

     

     

    if(obj_distance<20){red1=red2=red3=red4=1;green1=green2=green3=green4=0;buzzer=1;}

    else{red1=red2=red3=red4=0;green1=green2=green3=green4=1;buzzer=0;}

     

    the conditional statements are placed in while condition.

    January 25, 2013 at 7:50 am #9049
    zulnorain
    Participant

    hey nidhin.k,

     

    if i introduce dc motor (with L293D ic) in this project how can i control motor speed using ultrasonic sensor.?

    so at a certain say (40 inches) motor speed reduces and at 20 inches motor stops.?

    January 28, 2013 at 4:43 am #9070
    nidhin.k
    Participant

    hai zulnorain….

     

     

    u can control the speed of a DC motor… by using PWM signal…. 

     

    try… and if any problem occur.. post here….

     

    all the best…..

     

     

     

    February 3, 2013 at 6:00 pm #9090
    zulnorain
    Participant

    hey nidhin.k can you explain this section of the code:

     

    void Distance_Measure()
    {
    count=count1=0;
    TH0=0x00;TL0=0x00; 
    TR0=0;
    for(i=0;i<10;i++)         
    {
    TX=0;           
    Delay(1000);
    TX=1;           
    Delay(10);
    TX=0;   
    while(RX==0);
    TR0=1; 
    while(RX==1);
    TR0=0;
    count=TH0<<8;      
    count=count|TL0;    
    TH0=TL0=0x00; 
    count1=count1+count;
    Delay(5000);        
    }
    count=count1/10;
    inch1=count%10000/1000;
    inch2=count%1000/100;
    result=(inch1*10)+inch2;
     
    obj_distance=result-2;  
    Lcd8_Decimal3(0XCb,obj_distance); 
     
    }
    March 7, 2013 at 11:17 am #9273
    B V RAjeswararao
    Participant

    can u help me the ultrasonic code using serial communication

     

    March 20, 2013 at 9:03 am #9353
    rohit pandey
    Participant

    nidhin i need ur help in making a similar project but i dont have to display it i just have to convert the range into vibration like maximum range gives minimum vibrations and min range gives maximum vibration using pwm can you provide somthing on the it will be highly appriciable i am using 6 volt dc motor using maimum 5 volt range as entier circuit will be powered by a 9 volt battry and i am using hc sr04 ultrasonic range module and atmel 89s51 as processor please help as i am new to c programming and dont know how to do all this thanks in advance

    March 20, 2013 at 9:21 am #9354
    rohit pandey
    Participant

    and the cicuit diagram if possible for you or zulhoriz please provide the schematics for connection of sensor to micro and to dc motor thnx in advance

    March 21, 2013 at 6:51 am #9360
    rohit pandey
    Participant

     

    #include<REGX51.h>
    #include<intrins.h>// for using _nop_() function
    sfr16 DPTR =0x82;
    sbit trig=P3^1;
    void send_pulse(void) //to generate 10 microseconds delay
    {
    TH0=0x00;TL0=0x00; 
     trig=1;
     _nop_();_nop_();_nop_();_nop_();_nop_();
     _nop_();_nop_();_nop_();_nop_();_nop_();
     trig=0;
        
    } 
     
    unsigned char get_range(void) 
    {
     unsigned char range;
     send_pulse();
        while(!INT0);//         in sake of these lines you can generate a delay of 40 Milli seconds=40000 micro 
        while (INT0);//         seconds
     DPH=TH0;DPL=TL0; 
     TH0=0xFF;TL0=0xFF;
     if(DPTR<35000)//actually you need to use 38000 but the sensor may not work at higher levels 
      range=DPTR/59;
      else
      range=0; // indicates that there is no obstacle in front of the sensor 
      return range; 
    }
    void main()
    {
      TMOD=0x09;//timer0 in 16 bit mode with gate enable
      TR0=1;//timer run enabled
      TH0=0x00;TL0=0x00;
     
     P1=0xff;
      P3|=0x04;//setting pin P3.2    
             
    }
     
    this is my code please help me in howing distance on led as above mentioned led code it is giving me errors please patch them into this 
    March 26, 2013 at 3:34 am #9372
    Hussein
    Participant

    hello Mr Zulnorain,,, 
    or his helpful friend,,

    can someone email me or paste the last edited full code for ultrasonic with leds and motor ??
     

    i really appreciate it @@ thanks so much
    Hussein-Yemen
     

     

  • Author
    Posts
Viewing 15 posts - 1 through 15 (of 41 total)
1 2 3 →
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • Seeking Help to compile code August 17, 2022
  • Nokia 5110 HW in Oshonsoft August 17, 2022
  • Digital Display Information August 17, 2022
  • 2nd pcb design program? August 17, 2022
  • Door exit button August 17, 2022

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2022 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools