ultrasonic reading problem

Discussion in 'Embedded Systems and Microcontrollers' started by walid el masry, May 20, 2011.

  1. walid el masry

    Thread Starter Active Member

    Mar 31, 2009
    i wanna build a distance measure using ultrasonic and i have the module vm125 by vellemanusa on this link here

    it suppose to measure distance between range: 0.66-8.20ft or 20.12-249.94cm

    the module has an analog output which represent the distance and it has rating of voltage output 0-5v

    am using pic microcontroller and in my case 16f876 but am confused i can't measure the right distance and i guess there is some thing wrong in my calculation

    the adc has a resolution of 10bit and the analog output is 5v so the analog reading will represent the distance using this formula

    distance = adc reading * (5/1024) but when i tested it gives me wrong result can any one help me here plz

    i include her the code and photos of the circuit

    Code ( (Unknown Language)):
    2. void initialize(){
    3.  PORTA=0x00;
    4.  TRISA=0x01;
    5.  ADCON1=0b10001110;
    6.  ADCON0=0b10000000;
    7.  ADCON0.f0=1;
    9.  Lcd_Init(&PORTB);
    10.  Lcd_Cmd(Lcd_CLEAR);
    11.  Lcd_Cmd(Lcd_CURSOR_OFF);
    12. }
    14. void ad_read(){
    15.  ADCON0.f2=1;
    16.  while(ADCON0.f2==1){}
    17.    ad_res=0;
    18.    ad_res=ADRESH;
    19.    ad_res=ad_res<<8;
    20.   ad_res=ad_res+ADRESL;
    21.   distance=ad_res*(5/1024);
    22.   Lcd_Cmd(Lcd_Clear);
    23.   FloatToStr(distance, ad_res_txt);
    24.   Lcd_Out_Cp(ad_res_txt);
    25.   Lcd_Out_Cp("cm");
    26.   Lcd_Cmd(LCD_SECOND_ROW);
    27.   WordToStr(ad_res, ad_res_txt);
    28.   Lcd_Out_Cp(ad_res_txt);
    29.  }


  2. Markd77

    Senior Member

    Sep 7, 2009
    instead of

    I think there is something else wrong because I would expect a value between 0-5 with your current calculation.
    Maybe you have the ADC result in the left shifted instead of right shifted format.
  3. walid el masry

    Thread Starter Active Member

    Mar 31, 2009
    the adc result is already fight justified since i wrote ADCON1=0b10001110; so the bit7 ADFM is equal 1 which is a right justified

    and when i used you formula the result gives me zero !!!