Sharp GP2D02 Vin connection

lrat

New Member
Dear enthusiasts,
I am working on a robot and I'm using a Sharp GP2D02 infrared distance sensor which is mounted on top of a servo motor. My robot is near finished. Motors, servo and infrared sensor are controlled by a Picaxe28 board with Picaxe 28x1 chip. An L293 steers my 2 motors and the outputs are going through a DIL 330 x 8 resistor array.
I also got a piezo buzzer for sounds hooked up to output 1.
Now, before we continue, I am positioned on a very remote island in the Coral Sea for another 5 months (Willis Island, Queensland, Australia) and can not go to an electronics shop to buy other sensors. I relaize the GP2D02 is now obsolete but it's the only part I've got. I read they are easily destroyed if they are hooked-up incorrectly.
I am puzzled with the Vin function of the GP2D02. Could some of you please explain what this does? From what I understand it needs to be hooked up to a diode (To lower voltage) and then hooked up to a clock-signal of the processor.
Say I want to use Input pin 11 (This has a timer function), how do I do this?
Could you please send me a code snippet to run?
Your help will be much appreciated and as I said before I want to make sure I've got it spot-on before I power it up.
Thanks for your help.
Luc.
 

Dippy

Moderator
Luc,
It's always helpful to others if you provide links for unusual bits.

And it would be really helpful if you would post your schematic (circuit diagram) to show us how far you have got and if you have an understanding of the connections.

What is your electronics and code skill level?
Mega-Novice , Novice or old hand?


I assume you have seen this?
http://www.acroname.com/examples/10066/10066.html#e3

If you have , then the diode is simply acting as a clamp.
If you ran your PICAXE circuit at 3V you wouldn't need that.

I have looked at the Data Sheet and it looks like a SPI type connection. Vin looks like a clock which has to be limited to 3V.
So, you give Vin a pulse and it behaves like the Timing chart on page 3.

If you look at that link you will see some non-PICAXE BASIC code.
That uses a manual clocking method. It may give you some clues how to clock.
Basically set-a-pin , read-input-pin status, shift .. repeat for X number of bits.
Someone with some experience could write you some get-you-startd code I'm sure. I'm afraid I don't have the time.


Oh, did you actually search this Forum for that Sharp part before posting question?
 

Attachments

lrat

New Member
more info

Hi Dippy,
Thanks for your very fast reply and useful suggestions. I am using the same pdf-sheet as what you included.
Ok, here is what I want to do:

-Change Input pin 1 to an output
-Wait for 200 mseconds to give time to the sensor to warm up
-Give 8 pulses 0.2 msec with equal length lows
-Give another pulse of 1.5 msec
-Then wait 70 ms and repeat the cycle.

Back in 2006 I was doing a course in Atmel AVR Mega 8 and wrote the code in C. I have included this code. Basically I want to translate it to Basic Picaxe language. I am also running into problems of changing input 1 to an output. When I write the code output B.1 then this will result in a syntax error.
I hope it makes a bit more sense now.
Here is my C-code:
Code:
/***********************************************
Test program to demonstrate the operation of a Sharp 
GP2D02 using AVR Mega8
input clock to GP2D02 through a SI diode, is connected to PC5 pin 28 ouput clock pulse generator
PC4, pin 27  input data to mega8 from the GP2D02 
Clock speed internal 1MHz
Luc De Pauw
24-7-2006
************************************************/
#define F_CPU 1000000UL
// 150 lots of noise 200 better


#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#include <avr/delay.h>

#define MA 255
#define SP 255


 uint8_t distance;

SIGNAL(SIG_OVERFLOW0)      
{
static uint8_t pulse_number=0;

switch(pulse_number)
		{
		//Wait for 70 ms before reading GP2D02
		case 0: PORTC&=~_BV(PC5);	//make output a space
				TCCR0=_BV(CS02)|_BV(CS00); //prescalar 1024
				TCNT0=186;					//delay time 70 ms
				break;
		//Start reading the bits, get bit 2^7
		case 1: PORTC|=_BV(PC5);	//make output a mark
				TCCR0=_BV(CS00);	// prescalar 1				
				TCNT0=MA;			//wait delay 100us
				break;

		case 2:	PORTC&=~_BV(PC5);	//make output a space
				if(bit_is_set(PINC,3)) distance=_BV(7); //set bit 2^7 and clear all others
									else distance=0;	
				TCCR0=_BV(CS00);	// prescalar 1				
				TCNT0=SP;			//wait delay 100us
				break;
		//get bit 2^6
		case 3: PORTC|=_BV(PC5);	//make output a mark
				TCCR0=_BV(CS00);	// prescalar 1				
				TCNT0=MA;			//wait delay 100us
				break;

		case 4:	PORTC&=~_BV(PC5);	//make output a space
				if(bit_is_set(PINC,3)) distance|=_BV(6); //set bit 2^6
				TCCR0=_BV(CS00);	// prescalar 1				
				TCNT0=SP;			//wait delay 100us
				break;
		//get bit 2^5
		case 5: PORTC|=_BV(PC5);	//make output a mark
				TCCR0=_BV(CS00);	// prescalar 1				
				TCNT0=MA;			//wait delay 100us
				break;

		case 6:	PORTC&=~_BV(PC5);	//make output a space
				if(bit_is_set(PINC,3)) distance|=_BV(5); //set bit 2^6
				TCCR0=_BV(CS00);	// prescalar 1				
				TCNT0=SP;			//wait delay 100us
				break;
		//get bit 2^4
		case 7: PORTC|=_BV(PC5);	//make output a mark
				TCCR0=_BV(CS00);	// prescalar 1				
				TCNT0=MA;			//wait delay 100us
				break;

		case 8:	PORTC&=~_BV(PC5);	//make output a space
				if(bit_is_set(PINC,3)) distance|=_BV(4); //set bit 2^6
				TCCR0=_BV(CS00);	// prescalar 1				
				TCNT0=SP;			//wait delay 100us
				break;
		//get bit 2^3
		case 9: PORTC|=_BV(PC5);	//make output a mark
				TCCR0=_BV(CS00);	// prescalar 1				
				TCNT0=MA;			//wait delay 100us
				break;

		case 10:PORTC&=~_BV(PC5);	//make output a space
				if(bit_is_set(PINC,3)) distance|=_BV(3); //set bit 2^6
				TCCR0=_BV(CS00);	// prescalar 1				
				TCNT0=SP;			//wait delay 100us
				break;
		//get bit 2^2
		case 11:PORTC|=_BV(PC5);	//make output a mark
				TCCR0=_BV(CS00);	// prescalar 1				
				TCNT0=MA;			//wait delay 100us
				break;

		case 12:PORTC&=~_BV(PC5);	//make output a space
				if(bit_is_set(PINC,3)) distance|=_BV(2); //set bit 2^6
				TCCR0=_BV(CS00);	// prescalar 1				
				TCNT0=SP;			//wait delay 100us
				break;
		//get bit 2^1
		case 13:PORTC|=_BV(PC5);	//make output a mark
				TCCR0=_BV(CS00);	// prescalar 1				
				TCNT0=MA;			//wait delay 100us
				break;

		case 14:PORTC&=~_BV(PC5);	//make output a space
				if(bit_is_set(PINC,3)) distance|=_BV(1); //set bit 2^6
				TCCR0=_BV(CS00);	// prescalar 1				
				TCNT0=SP;			//wait delay 100us
				break;
		//get bit 2^0
		case 15:PORTC|=_BV(PC5);	//make output a mark
				TCCR0=_BV(CS00);	// prescalar 1				
				TCNT0=MA;			//wait delay 100us
				break;

		case 16:PORTC&=~_BV(PC5);	//make output a space
				if(bit_is_set(PINC,3)) distance|=_BV(0); //set bit 2^6
				TCCR0=_BV(CS00);	// prescalar 1				
				TCNT0=SP;			//wait delay 100us
				break;
		// Send a 1.5ms end pulse
		case 17: PORTC|=_BV(PC5);	//make output a mark
				TCCR0=_BV(CS02);	// prescalar 256				
				TCNT0=256-6;			//wait delay 1.5ms
				break;
		};
if(pulse_number<17)pulse_number++; 
			else pulse_number =0;			
}





int main()
	{
	
	//configure pre-scalar

	TCCR0|= _BV(CS00);	   //enable prescalar
	TCNT0= 10;		//load counter timer
	TIMSK|= _BV(TOIE0); 	//unmask interrupt
	DDRC|=_BV(PC5);		//set PC5 as output	
	DDRD=0xff;		// portD as an output
	sei();
	while(1)
		{
		PORTD=distance;

		//_delay_loop_2(0x250);
		
		}
	
						
	return 0;
	}
 
Last edited:

lrat

New Member
More info and code snippet

After reading my previous posts it might be not clear. Here is what I want to achieve:
Send out sequential pulses from output pin 2
The pulses need to have a waiting time of 70msec, then 8 pulses of 0.2 msec and then a closing pulse of 1.5 msec
These series of pulses are then read by teh GP2D02 sensor and after being processed it sends out an 8-bit code to input 0. The data is then processed and is used for descision making.

I wrote a piece of code in basic as well, although it doesn't do what I want it to do, it will give you an idea of the process.
I would like classify myself as an novice with some experience.

Here is my Basic code:
Code:
symbol Vin    = 2 ' sensor control

' -----[ Variables ]--------------------------------------------------
'
symbol count1 = b0 ' result of measurement
symbol i     = b1 ' loop variable
symbol Vo    = pin3 ' sensor data
' -----[ Initialization ]---------------------------------------------
'
init: low Vin

count1 = 0

' -----[ Main Code ]--------------------------------------------------
'
start:
readadc 0,b0
debug
gosub sensor_control

pause 1000
goto start
' -----[ Subroutines ]------------------------------------------------
'
sensor_control:
'debug cls
high Vin
pause 70
count1 = 0
for i = 0 to 7
    pulsout Vin,10
    count1 = count1 * 2 + Vo
    'debug %count
    
next i
low Vin
'debug count
return
 
Last edited:

westaust55

Moderator
Might I respectfully suggest that you have a read at the Read Me First thread at the top of the active forum
http://www.picaxeforum.co.uk/showthread.php?t=7679

and then use the [code] and [/code] markers around your program listing so that they appear in the sub windows.

The easiest way to place the code markers is to select the block of code and then click on the # icon at the top of the post entry window - right most of the various icons on the lower row.

Thanks
 

westaust55

Moderator
From the datasheet, the Vin pin is an open drain input control line.

The timing diamgrams on page 3 suggsts it acts both as an ON/OFF control and a clock for sending the output data to your microcontroller.

Since many of the timing intervals are around 0.1 or <= 0.2 ms, you will need to use a command such as
PAUSEUS 10 for 100us = 0.1ms
PAUSEUS 20 for 100us = 0.2ms where 15 could be a better starting value rather than 20


EDIT: some more information here:
http://www.electronicsteacher.com/robotics/robotics-tutorial/intermediate-robotics/using-the-sharp-gp2d02.php
 
Last edited:

lrat

New Member
My aplogies

Thank you for your friendly reminder about the coding. I will edit the previous messages to stay in line with forum practices.
My apologies for the nuisance and definitely no offence taken.
Cheers,

Luc
 
Top