infrared Liquid level detector

Discussion in 'Embedded Systems and Microcontrollers' started by fantabulous68, Aug 27, 2009.

  1. fantabulous68

    Thread Starter Active Member

    Nov 3, 2007
    What do you guys think about this method for calculating the distance?
    Any simpler method?im using a timer to determine the interval between transmitted and received signals

    Code ( (Unknown Language)):
    2. //////////////////////CALCULATING DISTANCE///////////////////////
    4. void enable_interrupt(void)
    5. //This function is basically designed to set the specific bits of the timer and capture registers
    6. {
    7.     CCP1CON=0x05; //bits 3-0 of the capture control register are set to 0101.
    8.                   //This mode is to capture every rising edge of the pulse being
    9.                   //transmitted and then received
    10.     TMR1IF = 0;   //The interrupt flag bit of the PIR1 register is cleared before
    11.                   //enabling the interrupt.
    12.     CCP1IF = 0;   //Before a capture is made, the Interrupt Request Flag bit
    13.                   //CCP1IF of the PIR1 register is cleared.
    14.     CCP1IE = 1;   //The capture interrupt enable bit is set to enable the interrupt
    15.     TMR1IE = 1;
    17. //An interrupt will occur immediately provided that the GIE and PEIE bits of the INTCON
    18. //register are set.
    19.     PEIE = 1;    
    20.      GIE = 1;    
    21. }
    23. void interrupt ISR(void)
    24. {
    25.  if (TMR1IF)  //if there is an overflow
    26.  {     
    27.   TMR1IF = 0; //and the flag is then cleared
    28.   TMR1_OF++;   
    29.  }
    31.  if (CCP1IF) //if there is a capture
    32.  {
    33.    CCP1IF = 0; //Zero Capture flag
    34.    if (Cap_1st) //allow only 1 capture to be taken
    35.      {
    36.         Cap_1st=0;           //Cap_1st is cleared as if another value is captured before the previous value is read
    37.                              //then the
    38.         t_capL=CCPR1L;      //lower byte of the capture register
    39.         t_capH=CCPR1H;      //higher byte of the capture register
    40.         capture_status=1;   //signal that a capture occured is enabled.
    41.      }
    42.  }
    43. }
    45. void calc_distance(void)
    46. {
    47.     long sample=0;
    48.     i=1; j=0;
    50.     Cap_1st = 1;
    51.     capture_status=0;    //signal that a capture occured is disabled
    52.     TMR1ON = 0;         //stop timer
    53.     //tH=TMR1H; tL=TMR1L;
    54.     tH=0; tL=0; t_capL=0; t_capH=0;  //initialise capture
    55.     TMR1_OF=0;
    57.         Transmit40khz();
    58.     DelayUs(100);
    60.     enable_interrupt();  //Interrupt is enabled to capture time when the rising edge occurs
    61.     TMR1ON = 1;         // start timer 1
    63.  //when timer 1 starts, the enable_interrupt function will be called to
    64. //capture the time from the 40kHz signal once the first rising edge of the transmitted
    65. //pulse occurs
    67. //A continuous loop is run such that when the signal to state that a capture has occurred
    68. // is 0, then the 40 kHz will be transmitted and received when sending out 10 pulses.
    69. //Once this condition is satisfied, the global interrupt enable bit will be cleared to
    70. //prevent the interrupt from occurring when an interruption is in progress.
    71. //The timer is then stopped by disabling TMR1ON and TMR1IE.
    73.     while((capture_status==0)&&(TMR1_OF<1));
    74.     GIE = 0; //disable global interrupt
    75.     TMR1ON = 0;  //stop timer 1
    76.     TMR1IE = 0;   // disable timer 1 interrupts on overflow//
    78. //Another condition arises such that if the signal to state that a capture has occurred
    79. // is then enabled, then the sample time is calculated and stored in an array. The sample
    80. // time is calculated via the difference between high byte of the capture time and the high
    81. // byte of Timer1 added to the low byte of the capture time and the low byte of Timer1.
    82. //The index is incremented. This then deduces the end of the function to get the samples.
    83. //However, if the index is less than 20, then the function will continue.
    85.     if (capture_status==1)
    86.         {
    87.          sample = ((t_capH)*0x100)+(t_capL);
    88.          i = 2;
    89.             GIE=0;   //Global interrupt is disabled to avoid another interrupt from occurring while
    90.                     // an interrupt is taking place
    91.             t_capH = 0;
    92.             t_capL = 0;
    94.         }
    96.     if (i==1)
    97.         distance = 0;
    100. //if and only if a capture is made will the process of distance calculation continue.
    101.     if (i==2)
    102.     {
    103.         time = sample * 1e-6; //since each count takes 1us to complete, this value is multiplied
    104.                               //to the sample time.
    107.         //Distance is calculated via multiplying the time and distance light travels
    108.         //light travels 300m in 1 us.This result is divided by 2
    109.         //as speed travels twice the measured distance.
    111.         distance=(time*30000)/2;
    113.         //liquidLEVEL=heightOFtank-distance im still working on this part
    114.     }
    115.     return;
    116. }
    117. ////////////////////END CALCULATING DISTANCE//////////////////////
  2. AlexR

    Well-Known Member

    Jan 16, 2008
    So how many miles above the liquid surface will your sensor be?
  3. hgmjr


    Jan 28, 2005
    As Alex has indicated, the speed of light is such that the "time of flight" for short distances is in the tens of nanosecond range. Such a short distance is far beyond the microsecond resolution of the microcontroller.

    This may be an application better suited to use of ultrasonic range finding. A system based on ultrasonic techniques would give you "time of flight" measurements in the hundreds of microseconds. This a resolution with which your microcontroller can easily cope.

  4. fantabulous68

    Thread Starter Active Member

    Nov 3, 2007
    well i want to use a 50cm deep tank.

    im not allowed to use ultrasonic.
  5. jpanhalt

    AAC Fanatic!

    Jan 18, 2008
    What is the liquid? What wavelength of IR are you planning to use?
    How broad is the tank? With what precision do you need to make the measurement? Can you attach anything to the tank or insert any sort of probe? In other words, a pressure sensor or capacitance-based sensor would probably be easier than a TOF light sensor, particularly if you need to measure the depth to within a few millimeters.

    If a few cm will suffice, Stanley Tools makes a FatMax TOF light-based distance measuring device that might be the cheapest way to do what you want to do. I am not sure it will work perpendicular to water. I will try it later today. I got mine for about $50 new on ebay. It could be hacked.

  6. AlexR

    Well-Known Member

    Jan 16, 2008
    Well the first thing you should do before you start coding is make some rough calculations to see if your approach is feasible.

    For example;

    How long will it take light to travel from the sensor to the bottom of the tank and back to the sensor?

    What timer resolution can you achieve using a microcontroller?

    In the light of the answers to the above question is your approach practical or do you need to change your method of detecting fluid level?
  7. Mass


    Apr 9, 2009
    Another method would be to angle the source slightly so that the infrared is no longer traveling perpendicular to the liquid surface. The depth of the tank could be calculated by measuring the horizontal distance from the source to where the reflected wave is recieved.

    The lower the liquid level of the tank, the farther away the received spot will be.
  8. jpanhalt

    AAC Fanatic!

    Jan 18, 2008
    Would waves on the surface affect the angle of reflection?

  9. fantabulous68

    Thread Starter Active Member

    Nov 3, 2007
    Infrared liquid level detector
  10. rjenkins

    AAC Fanatic!

    Nov 6, 2005
    For 50cm you would probably be best off using the strength of the reflected IR signal.

    Switch the emitter on&off at a steady rate (tens of hz?) and measure the IR sensor output with one of the ADCs in the PIC for both emitter on & emitter off.

    The difference gives you the signal due to the emitter, cancelling any ambient illumination.

    You can then calibrate that (using a look-up table?) to actual distance.

    There is another form of optical ranger;

    It uses a rotating faceted mirror (which could be made from small mirror tiles) to 'scan' a beam of light down and across the target surface, and to the side of this an absolutely vertical, focussed, detector.

    You have another detector at the side of the emitter which gets a reflected pulse each time a mirror facet passes square on to the emitter (or have it at some known reference angle, anyway).

    This gives you a timing reference, both to control the rotation motor speed and timing to when it gets a signal from the main detector.

    If ths surface is near the sensor, the beam will be towards horizontal. If it's far away, it will be nearer vertical.
    The rest is trigonometry..
    Last edited: Aug 27, 2009
  11. jpanhalt

    AAC Fanatic!

    Jan 18, 2008
    I just did a quick and dirty experiment with my FatMax and a basin of water. At a fixed distance, the empty basin read 4'-9" +a fraction of inch. With a few of inches of water (about 3"), the distance was 4'-10" plus a fraction of inch. When drained, it again read 4'-9" plus a fraction.

    Unverified conclusions:
    1) The 630 to 690 nm IR used by the FatMax does not reflect sufficiently off the surface of water to measure its depth.
    2) The refractive index of water may explain the longer distance that was observed. That could lead to some fun experiments for high school students.
    3) Maybe the FatMax can be used for detecting submarines in fresh water, just in case the Canadians decide to attack us from across Lake Erie. :D

  12. fantabulous68

    Thread Starter Active Member

    Nov 3, 2007
    1. OSCCON|=0x60; //set fosc to 4Mhz

    2. I want to use a 50cm deep tank

    3. the calculation of distance is in centimeters

    Wikipedia says "299,792,458 metres per second", so in 1uS it travels 300m.

    how long will it take light to travel 100cm?

    666 picoseconds
  13. fantabulous68

    Thread Starter Active Member

    Nov 3, 2007
    My IR receiver is a TSOP48, it has a DIGITAL output which is active low
  14. rjenkins

    AAC Fanatic!

    Nov 6, 2005
    I added an alternate solution in my previous post; three new posts appeared in the meantime..
  15. rjenkins

    AAC Fanatic!

    Nov 6, 2005
    Another possible one; attach the emitter to a model control servo and 'scan' it across the surface, noting the reflected signal pickup point.
  16. jpanhalt

    AAC Fanatic!

    Jan 18, 2008
    Same problem as noted before. What if there are ripples on the liquid?

    Again I ask, what liquid are you trying to measure?

  17. fantabulous68

    Thread Starter Active Member

    Nov 3, 2007
    well i have to test it on various liquids and determine which its best suited to.

    And im going to assume that the liquid is still.

    I already have my hardware for the proximity detector circuit working.
    An LED blinks when an object is detected. An object is detected when the receiver module outputs a digital low.

    All i have to do now is work on the software. I want to use timers to calculate the distance, i provided the code in my previous code. Am i on the right track?
    Last edited: Aug 27, 2009
  18. jpanhalt

    AAC Fanatic!

    Jan 18, 2008
    If the liquid is still, why do you need to measure its level? It won't be changing. In reality, liquids such as water and petroleum pick up vibrations or show effects of flow quite readily. Thus, you are likely to have ripples.

    The second part of your response having to do with proximity detection is confusing. Proximity detection with a TSOP48xx is a world different than measuring distance using time of flight. Which is it that you are trying to do?

  19. AlexR

    Well-Known Member

    Jan 16, 2008
    How do you expect to be able to calculate distance?
    By your own calculations you will need to be able to time periods of less than 1 nanosecond. The microcontroller timer cannot resolve any time period less than 1 microsecond. In other words with your hardware you can't determine distances in the cm range by timing a light pulse reflection.
  20. fantabulous68

    Thread Starter Active Member

    Nov 3, 2007