+ Reply to Thread
Page 1 of 5
1 2 3 4 5 Last
Results 1 to 15 of 72

Thread: infrared Liquid level detector-SharpGP2D12

  1. #1
    fantabulous68 Newbie
    Join Date
    Feb 2007
    Posts
    280

    Default infrared Liquid level detector-SharpGP2D12

    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

    I went to Nigels page to just check out how to interface the GP2D12 to a micro but when i click the tutorial nothing comes up...
    Last edited by fantabulous68; 28th August 2009 at 08:58 PM.


  2. #2
    Mr RB Excellent Mr RB Excellent Mr RB Excellent Mr RB Excellent Mr RB Excellent Mr RB Excellent Mr RB Excellent
    Join Date
    Jul 2008
    Location
    Out there
    Posts
    1,761

    Default

    There's a project on this page that uses the Sharp GP2 sensor;
    Connect JuneBug to serial port and Sharp GP2 sensor
    (see project 4)

    It is in C, and has some very simple refined math for converting the logarithmic sensor output to linear range reading in cm (or inches).

  3. #3
    fantabulous68 Newbie
    Join Date
    Feb 2007
    Posts
    280

    Default

    Wow that looks really useful. Thanks alot Mr RB

  4. #4
    fantabulous68 Newbie
    Join Date
    Feb 2007
    Posts
    280

    Default

    lol OMG i didnt realise that you are the MAN himself. RB-Roman Black Pleasure to meet you.

  5. #5
    Noggin Good Noggin Good
    Join Date
    Jul 2003
    Location
    Austin, TX
    Posts
    436

    Default

    Any idea how well the sensor works with condensation on either lens?
    A rectangular bear is just a polar bear after a coordinate transform. -- I dunno who.
    A recent study shows that research causes cancer in rats. -- I dunno who said that one either.

  6. #6
    Mr RB Excellent Mr RB Excellent Mr RB Excellent Mr RB Excellent Mr RB Excellent Mr RB Excellent Mr RB Excellent
    Join Date
    Jul 2008
    Location
    Out there
    Posts
    1,761

    Default

    Probably badly. The datasheet specifically mentions the lens/lenses must be kept clean.

  7. #7
    bryan Newbie
    Join Date
    Jun 2003
    Location
    Vancouver, Canada
    Posts
    177

    Default

    Can the sensor measure the level of water in a vessel, or does it need a colored surface?

  8. #8
    ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent
    Join Date
    Jan 2007
    Location
    Hampshire. England.
    Posts
    10,836
    Blog Entries
    13

    Default

    Quote Originally Posted by bryan View Post
    Can the sensor measure the level of water in a vessel, or does it need a colored surface?
    hi bryan,
    In another thread by the OP, he states that the liquid dosnt have to be water, he can choose the liquid.
    I also raised the point about poor reflection from clear water
    Eric " Good enough is Perfect "
    I will NOT answer PM's requesting technical help, please use the Forum
    PIC tutorials: Nigel's www.winpicprog.co.uk/ Bill's: www.blueroomelectronics.com/

    Link to my Articles: http://www.electro-tech-online.com/a...icgibbs-55450/

  9. #9
    fantabulous68 Newbie
    Join Date
    Feb 2007
    Posts
    280

    Default

    Im a girl not a HE

  10. #10
    ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent
    Join Date
    Jan 2007
    Location
    Hampshire. England.
    Posts
    10,836
    Blog Entries
    13

    Default

    Quote Originally Posted by fantabulous68 View Post
    Im a girl not a HE
    hi Girl,
    No offence intended, I'm always pleased to see young women taking up electronics... good luck with you difficult project.
    Eric " Good enough is Perfect "
    I will NOT answer PM's requesting technical help, please use the Forum
    PIC tutorials: Nigel's www.winpicprog.co.uk/ Bill's: www.blueroomelectronics.com/

    Link to my Articles: http://www.electro-tech-online.com/a...icgibbs-55450/

  11. #11
    fantabulous68 Newbie
    Join Date
    Feb 2007
    Posts
    280

    Default

    I tested the GP2 sensor last night. I entered values of Analogue output Voltage Vs distance(cm) into a table in Excel and plotted its relationship.....The shape is similar to that of the datasheet for the sensor....So the sensor does work on liquids. Ill attach the graphs onto this thread sometime when im home....

    Im programming in C, using MPLAB IDE Hi-Tech universal toolsuite ANSi C compiler....

    Now i need to program the PIC16f690 to manipulate the output of the sensor and give me continuous measurements in cm of a (varying) liquid level in a container....MR RB has been very kind and directed me to his project page which basically does what i need my sensor to do...but he uses the junebug 18F1320.....

    I dont need to send that data as a text string to PC(for now maybe i could add it later) because im using a LCD to display the measurements....

    Now there are differences with the PIC16f690 and 18F1320 and im a beginner...and tend to get lost along the way......


    Code:
    //*********************************************************
    /*  Junebug_Serial4.c    RomanBlack.com 19th July 2009.
    
    	Junebug 18F1320, uses my "zero error 1 second timer"
    	system to generate a 2 second interval, then every
    	2 seconds it reads an analog voltage from a
    	Sharp GP2 distance sensor and converts it to decimal
    	distance, then sends that data as a text string to PC
    	via bitbanged serial out. It also flashes Junebug LED1
    	everytime it takes a distance sample.
    
    	Code for MikroC, config handled by MikroC compiler;
    	_INTI02_OSC_1H
    	_WDT_OFF_2H
    	-MCLRE_ON_3H
    	_LVP_OFF_4L
    	_PWRT_ON_2L
    
    */
    //*********************************************************
    
    // functions declared
    void calc_distance(void);
    void send_serial_message(void);
    void send_serial_byte(unsigned char);
    
    #define PIN_SER_OUT LATA.F3		// which pin for serial out (PORTA.F3)
    #define SER_BAUD 51				// TMR3 (1Mhz/19200 baud) = 52
    								// tested; works from 49 to 53, using 51
    
    unsigned char i;		// used for looping
    
    unsigned char cm10;		//
    unsigned char cm;		//
    
    unsigned int math;		// used for voltage calculations
    unsigned long bres;		// for bresenham 2-second timer system
    
    
    //---------------------------------------------------------
    void main()
    {
    	// setup the PIC 18F1320
    	OSCCON = 0x72;      	// internal osc, 8MHz
    
    	PORTA = 0;
    	TRISA = 0b10000010; 	// RA7 high imp, RA3 is serial out,
    							// RA1 is ADC input measuring VR1
    	PORTB = 0;
    	INTCON2 = 0;            // PORTB pullups ON
    	TRISB = 0b00000000; 	// PORTB not used
    
    	ADCON0 = 0b00000101;	// ADC ON, RA1 is ADC input
    	ADCON1 = 0b01111101;	// AN1 is ADC input
    	ADCON2 = 0b10100010;	// right justify, 8Tad, 32Tosc
    	T1CON = 0b00010001;     // TMR1 is ON, 1:2 prescale, =1MHz
    
    	T3CON = 0b00010001;     // TMR3 is ON, 1:2 prescale, =1MHz
    
    	// main loop here;
    	while(1)
    	{
    		// wait for 2 seconds, uses TMR1 free running at 1Mhz
    		while(!PIR1.TMR1IF);    // wait for TMR1 overflow
            PIR1.TMR1IF = 0;		// clear overflow flag
    		LATA = 0;				// all LEDs off
    		
    		bres += 65536;			// add 65536uS to bres value
    		if(bres >= 2000000)		// if reached 2 seconds!
    		{
    			bres -= 2000000;	// subtract 2 seconds, keep error
    		
    			// flash Junebug LED1,  RA0=1 RA6=0 RA7=hiimpedance
    			LATA.F0 = 1;			// LED on
    
    		    // read the ADC voltage RA1 (Sharp GP2 sensor)
    		    ADCON0.F1 = 1;		// set GO bit, start doing ADC
    		    while(ADCON0.F1);	// wait until ADC done
    			calc_distance();		// convert ADC value to distance
    			send_serial_message();	// send message back to PC!
    		}
    
    	}
    }
    
    
    
    //---------------------------------------------------------
    void calc_distance(void)
    {
    	// from the Sharp datasheet the analog voltage is
    	// the inverse of distance, so distance can be calculated
    	// d = (1 / volts) then just scaled to suit the sensor
    
    	// load ADC value in 16bit math var
    	math = ADRESH;
    	math = (math * 256);
    	math += ADRESL;
    
    	// now invert it; (1 / volts) use (6050 / volts) for scaling
    	math = (6050 / math);
    	if(math >= 2) math -=2;		// fix linear error (-2)
    	if(math > 99) math = 99;	// max limit at 99cm
    
    	// convert from 0-99 to 2 decimal digits, 0-99cm
    	cm10=0;
    	while(math >= 10)
    	{
    		cm10++;
    		math -= 10;
    	}
    	cm = math;
    }
    
    
    //---------------------------------------------------------
    void send_serial_message(void)
    {
    	// send message and number to serial port
    	send_serial_byte('J');			// send ascii text
    	send_serial_byte('u');
    	send_serial_byte('n');
    	send_serial_byte('e');
    	send_serial_byte('b');
    	send_serial_byte('u');
    	send_serial_byte('g');
    	send_serial_byte(' ');
    
    	send_serial_byte('G');
    	send_serial_byte('P');
    	send_serial_byte('2');
    	send_serial_byte('=');
    
    	send_serial_byte('0'+ cm10);	// send number as ascii text 0-9
    	send_serial_byte('0'+ cm);	// send number as ascii text 0-9
    	send_serial_byte('c');
    	send_serial_byte('m');
    
    	send_serial_byte(13);			// send carriage return/linefeed pair
    	send_serial_byte(10);			//  to start a new text line
    
    }
    
    
    //---------------------------------------------------------
    void send_serial_byte(unsigned char data)
    {
    	// this manually sends a serial byte out any PIC pin.
    	// NOTE! serial is inverted to connect direct to PC serial port.
    	// baud timing is done by using TMR3L and removing
    	// timer error after each baud.
    
    	i=8;						// 8 data bits to send
    
    	PIN_SER_OUT = 1;			// make start bit
    	TMR3L = (256 - SER_BAUD);	// load TMR3 value for first baud;
    	while(TMR3L.F7);			// wait for baud
    
    	while(i)			// send 8 serial bits, LSB first
    	{
    		if(data.F0) PIN_SER_OUT = 0;    // invert and send data bit
    		else		PIN_SER_OUT = 1;
    
    		data = (data >> 1);			// rotate right to get next bit
    		i--;
    		TMR3L -= SER_BAUD;      // load corrected baud value
    		while(TMR3L.F7);        // wait for baud
    	}
    
    	PIN_SER_OUT = 0;		// make stop bit
    	TMR3L -= SER_BAUD;		// wait a couple of baud for safety
    	while(TMR3L.F7);
    	TMR3L -= SER_BAUD;
    	while(TMR3L.F7);
    }
    


    I highlighted the 3 lines im getting confused with....
    Code:
    
    TRISA = 0b10000010; 	// RA7 high imp, RA3 is serial out,
    			// RA1 is  
    Question1:The 16f690 doesnt have a RA7. What does makin the pin a high impedance mean? Im assuming imp is impedance. I know TRIS sets pins as inputs and outputs.



    Code:
    ADCON2 = 0b10100010;	// right justify, 8Tad, 32Tosc 
    Question2:
    I dont know how to set 8Tad on the 16f690. Could someone please tell me how to do that? I looked at the datasheets and i couldnt see any bit in a register that does sets it.....


    Question3:
    Does analogue channel select bits bits5-2 0001=AN1 mean that AN1 is ADC input?


    Well for now thats giving me trouble...so if any1 knows....Please help me out...Thanks...

  12. #12
    fantabulous68 Newbie
    Join Date
    Feb 2007
    Posts
    280

    Default

    This was just a rough test i did with the GP2 and various liquids....
    tomorrow i plan to do a more accurate test so each liquid will have the same type of container
    Attached Files

  13. #13
    Mr RB Excellent Mr RB Excellent Mr RB Excellent Mr RB Excellent Mr RB Excellent Mr RB Excellent Mr RB Excellent
    Join Date
    Jul 2008
    Location
    Out there
    Posts
    1,761

    Default

    Yeah your chart shows a couple of funky readings (obvious on that milk reading that jumps about).

    I have used 3 types of the Sharp GP2 sensors with no real stability problems but I had a good +5v supply and short wiring. Lots of people do report erratic readings, (probably noise?). Putting a 10uF cap right at the sensor power between +5v and ground is supposed to help, and also doing some low pass filtering of the analog voltage out would help especially if you have long wires between the sensor and the PIC.

    All I did was try doing a single ADC reading, then tried using multiple readings averaged. There was never much discernable difference between the 2 systems and my readings were stable whenever there was no movement of sensor or target. So that was good enough for me.

    I'd love to see your results with different liquids AND different heights of liquid to see if there is a back reflaction from the bottom of the container.

    And why use a look up table to correct the nonlinearity?? My version of the Sharp formula dist = (1 / volts)+n worked really nice. I think lookup tables are another reason people get jumpy values, (unless they are using a 1024 entry table).

  14. #14
    fantabulous68 Newbie
    Join Date
    Feb 2007
    Posts
    280

    Default


    I have used 3 types of the Sharp GP2 sensors with no real stability problems but I had a good +5v supply and short wiring. Lots of people do report erratic readings, (probably noise?). Putting a 10uF cap right at the sensor power between +5v and ground is supposed to help, and also doing some low pass filtering of the analog voltage out would help especially if you have long wires between the sensor and the PIC.

    All I did was try doing a single ADC reading, then tried using multiple readings averaged. There was never much discernable difference between the 2 systems and my readings were stable whenever there was no movement of sensor or target. So that was good enough for me.

    I'd love to see your results with different liquids AND different heights of liquid to see if there is a back reflaction from the bottom of the container.

    And why use a look up table to correct the nonlinearity?? My version of the Sharp formula dist = (1 / volts)+n worked really nice. I think lookup tables are another reason people get jumpy values, (unless they are using a 1024 entry table).

    Hello Mr RB.....I manually took the readings with a tape measure...i never implement your code yet.....And thanks for the advice.......

    My wires are LoooonG...ill shorten them and ad that capacitor and LPF...

    Here are more new measurements i HAD taken b4 reading your advice( so these readings dont have a capacitor or LPF or short wires)............See the attached document.............this was not rushed; i measured it pretty accurately...

    Taking these readings are time consuming.....

    But i have to redo them with short wires...etc. At least ill have more to talk about in my demonstration to my lecturer(or ill reach that 50 page requirement for my report submission lol-just looking on the bright side of taking another 40 readings)

    I dont plan on using a look up table any more.....i think your method is much better....Mr RB.


    Well ill try and take the new readings later tonight....i have to prepare a presentation on JOSEPHSON junctions and what they good for...My turn is on tuesday so i better make a start with that.....
    Attached Files
    Last edited by fantabulous68; 12th September 2009 at 11:01 AM.

  15. #15
    jpanhalt Excellent jpanhalt Excellent jpanhalt Excellent jpanhalt Excellent jpanhalt Excellent jpanhalt Excellent
    Join Date
    Jun 2006
    Location
    Cleveland, OH, USA
    Posts
    1,589

    Default

    Good afternoon.

    Interesting data. I am not so worried about the quantitative differences, but the qualitative difference (i.e., the dip at distances <10 cm) with milk and not with water/oil/red wine needs some more thought.

    We are concerned about reflection from surfaces or interfaces other than the liquid's top surface. Just a short digression... In analytical science, one must show that the response is actually due the the analyte, not just proportional to it. As a trivial example, assume you have a substance, "A" that has a little impurity, "B" in it. So, you develop an assay that correlates perfectly with A. Problem is, your assay actually measured B, but because the ratio was constant, it correlated with A very well. If you were to put that assay into practice, a different batch of material will likely have a different ratio of A and B and your assay will not give accurate results for A with other batches of material. Believe it or not, I have seen real life examples of that error.

    In your case, you need to show that the reflective surface being measured is the air-liquid interface ("A" in the above example), not some other surface ("B" in the example).

    1) When you do these experiments, do you have a stable way to position the detector, like a ring stand and clamps, or is it just being hand held?

    2) What are the dimensions of your liquid container(s)? Is your detector situated over the center or near an edge? Do your results vary if you move the detector off center? I looked briefly at the datasheet and suspect you may be able to move one direction, but not the other, without an effect from the edge of the container.

    3) The experiment I suggested earlier was designed to detect secondary reflections that might influence your results. In other words, at a set distance from the liquid's surface, the value you get should be exactly the same (within experimental error) regardless of the depth of the liquid. A water depth of 1 to 2 cm was chosen, as that would allow a significant percentage of your IR light to penetrate to the wall. I have no idea about the IR transmissivity of milk or wine. I suspect wine may be nearly as transparent as
    water at 940nm; purified mineral oil (mineral, not vegetable) certainly is as transparent or more so. The fact that the three liquids gave identical results is consistent with that. Vegetable oil may also be pretty clear too.

    4) What was the "green" solution? Was it green-tinted water? Is it colored with a dye or a pigment?

    I have to get the morning shopping done. It is a little after 0800 where I am. Sorry about the lengthy response. It is great to see someone on these forums who is willing to put forth the effort and is not just an "I want the answer type." If I seem picky, it is because most of my career entailed designing analyses and making decisions based on the results.

    John

+ Reply to Thread
Page 1 of 5
1 2 3 4 5 Last

Similar Threads

  1. Liquid level sensing
    By Bizzarri in forum Electronic Projects Design/Ideas/Reviews
    Replies: 17
    Latest: 11th September 2008, 12:27 PM
  2. Liquid Level Measurement/2
    By Joe McGivern in forum General Electronics Chat
    Replies: 17
    Latest: 15th May 2007, 08:12 AM
  3. Liquid level measurement.
    By Joe McGivern in forum Chit-Chat
    Replies: 8
    Latest: 3rd May 2007, 12:03 PM
  4. Liquid Level detector and indicator
    By Langat in forum Electronic Projects Design/Ideas/Reviews
    Replies: 4
    Latest: 27th September 2006, 02:13 PM
  5. About and Liquid level display
    By mishranirmal in forum Electronic Projects Design/Ideas/Reviews
    Replies: 1
    Latest: 13th September 2003, 02:48 AM

Tags for this Thread