infrared Liquid level detector

Thread Starter

fantabulous68

Joined Nov 3, 2007
51
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

Rich (BB code):
//////////////////////CALCULATING DISTANCE///////////////////////

void enable_interrupt(void)
//This function is basically designed to set the specific bits of the timer and capture registers
{
	CCP1CON=0x05; //bits 3-0 of the capture control register are set to 0101.
				  //This mode is to capture every rising edge of the pulse being
				  //transmitted and then received 
	TMR1IF = 0;   //The interrupt flag bit of the PIR1 register is cleared before
				  //enabling the interrupt.
	CCP1IF = 0;   //Before a capture is made, the Interrupt Request Flag bit
				  //CCP1IF of the PIR1 register is cleared.
	CCP1IE = 1;   //The capture interrupt enable bit is set to enable the interrupt
	TMR1IE = 1;

//An interrupt will occur immediately provided that the GIE and PEIE bits of the INTCON
//register are set.
	PEIE = 1;	  
	 GIE = 1;	 
}

void interrupt ISR(void)
{
 if (TMR1IF)  //if there is an overflow
 {		
  TMR1IF = 0; //and the flag is then cleared
  TMR1_OF++;	
 }
 
 if (CCP1IF) //if there is a capture
 {
   CCP1IF = 0; //Zero Capture flag
   if (Cap_1st) //allow only 1 capture to be taken
     {
       	Cap_1st=0;    		 //Cap_1st is cleared as if another value is captured before the previous value is read
					  		 //then the 
  	 	t_capL=CCPR1L; 	    //lower byte of the capture register
       	t_capH=CCPR1H; 		//higher byte of the capture register
        capture_status=1;	//signal that a capture occured is enabled.
     }
 }
}

void calc_distance(void)
{
	long sample=0;
	i=1; j=0;
	 
	Cap_1st = 1;
	capture_status=0;    //signal that a capture occured is disabled
	TMR1ON = 0;         //stop timer
	//tH=TMR1H; tL=TMR1L;
	tH=0; tL=0; t_capL=0; t_capH=0;  //initialise capture
	TMR1_OF=0;
	
    	Transmit40khz();
	DelayUs(100);

	enable_interrupt();  //Interrupt is enabled to capture time when the rising edge occurs
	TMR1ON = 1; 		// start timer 1

 //when timer 1 starts, the enable_interrupt function will be called to 
//capture the time from the 40kHz signal once the first rising edge of the transmitted
//pulse occurs
	
//A continuous loop is run such that when the signal to state that a capture has occurred
// is 0, then the 40 kHz will be transmitted and received when sending out 10 pulses. 
//Once this condition is satisfied, the global interrupt enable bit will be cleared to 
//prevent the interrupt from occurring when an interruption is in progress. 
//The timer is then stopped by disabling TMR1ON and TMR1IE. 

	while((capture_status==0)&&(TMR1_OF<1));
	GIE = 0; //disable global interrupt
	TMR1ON = 0;  //stop timer 1
  	TMR1IE = 0;   // disable timer 1 interrupts on overflow//

//Another condition arises such that if the signal to state that a capture has occurred
// is then enabled, then the sample time is calculated and stored in an array. The sample
// time is calculated via the difference between high byte of the capture time and the high
// byte of Timer1 added to the low byte of the capture time and the low byte of Timer1. 
//The index is incremented. This then deduces the end of the function to get the samples.
//However, if the index is less than 20, then the function will continue.

    if (capture_status==1)
		{
		 sample = ((t_capH)*0x100)+(t_capL);
		 i = 2;
			GIE=0;   //Global interrupt is disabled to avoid another interrupt from occurring while
					// an interrupt is taking place
			t_capH = 0;
			t_capL = 0;
			
		}

	if (i==1)
		distance = 0;


//if and only if a capture is made will the process of distance calculation continue.
	if (i==2)
	{
		time = sample * 1e-6; //since each count takes 1us to complete, this value is multiplied
							  //to the sample time.
	

		//Distance is calculated via multiplying the time and distance light travels
		//light travels 300m in 1 us.This result is divided by 2
		//as speed travels twice the measured distance.

		distance=(time*30000)/2; 

		//liquidLEVEL=heightOFtank-distance im still working on this part
	}
	return; 
}
////////////////////END CALCULATING DISTANCE//////////////////////
 

hgmjr

Joined Jan 28, 2005
9,027
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.

hgmjr
 

jpanhalt

Joined Jan 18, 2008
11,087
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.

John
 

AlexR

Joined Jan 16, 2008
732
well i want to use a 50cm deep tank.


im not allowed to use ultrasonic.
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?
 

Mass

Joined Apr 9, 2009
26
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.
 

jpanhalt

Joined Jan 18, 2008
11,087
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.
Would waves on the surface affect the angle of reflection?

John
 

Thread Starter

fantabulous68

Joined Nov 3, 2007
51
Infrared liquid level detector
specifications of design:

The design should be useful in liquid level or proximity detection. It should operate by detecting the distance from the target by reflection of an infra-red beam. It should safely detect the level of a liquid in a tank without any contact with the liquid itself. The device's range should be variable, from a couple of cm. to about 50 cm.

It is a continuous liquid level detector.

I must be able to display the results on an LCD screen.
ie. The level of the liquid in cm
tank empty
tank full
error message etc

The device should also be reconfigurable and able to work on any tank
 

rjenkins

Joined Nov 6, 2005
1,013
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.

Edit:
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:

jpanhalt

Joined Jan 18, 2008
11,087
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

John
 

Thread Starter

fantabulous68

Joined Nov 3, 2007
51
How fast is your processor running, and how deep is your tank?.
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
 

Thread Starter

fantabulous68

Joined Nov 3, 2007
51
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.
My IR receiver is a TSOP48, it has a DIGITAL output which is active low
 

rjenkins

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

jpanhalt

Joined Jan 18, 2008
11,087
Another possible one; attach the emitter to a model control servo and 'scan' it across the surface, noting the reflected signal pickup point.
Same problem as noted before. What if there are ripples on the liquid?

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

John
 

Thread Starter

fantabulous68

Joined Nov 3, 2007
51
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:

jpanhalt

Joined Jan 18, 2008
11,087
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?
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?

John
 

AlexR

Joined Jan 16, 2008
732
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.
 
Top