Read GP2D02 into digital input on Picaxe 28x1

I just had a read through of your article "How to make your first robot" on This is a fantastic article, however, I've got one slight problem; I am working with a Sharp GP2D02 instead of GP2D12 and the code needed is somewhat different.
I am renovating my robot which I made up at uni in 2006. We had to use Atmel AVR Mega8 processors and do the coding in C.
Now, to be honest, I've forgotten most of the C-coding and I am re-building my robot using a Picaxe 28X1 processor and a Sharp IR sensor GP2D02. It would be much easier to use the GP2D12, I realize that, but I've got one slight problem: I am located at Willis Island in the Coral Sea (North Queensland, Australia) more then 450 km from the mainland for the next 6 months. There is no way I can order parts in at all. So, I have to stick with what I've got. I took all my parts with me to the island to be able to work on some projects.
I have problems coding up the Vin input of the GP2D02. I contacted the Picaxe forum but to no avail ( ). I really want to get this thing going but I'm scared I will destroy the IR sensor in the setup (Apparantly they are very fragile).
Now what I want to do is simple. It is similar to the article of Fritzl: How to make your first robot, but he is using a GP2D12. ( ).
Would you guys be able to give me a bit of help coding up the Vin and Vout part of it (In BASIC)?
Output 2 needs to generate the pulses. Wait 70 ms, and then 8 pulses (for eight bits) for 15 ms, then wait 100 ms and then start all over again.
At the same time Input 1 needs to read the 8 bit code and convert it to a number which can then be used for if-then situation. Say for example: If value>28 then MotorRight, if value<28 then MotorStraight, etc.
The other thing is, being on the island, we have a very slow internet connection. I would have mailed you a picture of my robot but it would take ages to get it through.
Many thanks for your help in advance.

Poor internet makes for poor documentation

I understand your problem with your Internet link. Do you have all manuals that you need for understanding Picaxe Basic? What about the datasheets for the sensor?

I for one, do not know the exact Sharp model you are talking about. Googling the datasheet is trivial for me. Do you need a copy? How much data can you handle, say, per hour?

I found this link at [184 kB] describing the GP2D02: it is not a datasheet but describes what they found using their own experiments on the thing. Seems like you are familiar with the info already.

Wow, I just glanced over that document. This sensor is not for beginners! The distance value comes out via some sort of serial signal that needs to be clocked by your picaxe. Seems to me you need to read up on the Basic commands “pulsin” and “pulsout”. Do you have access to the Picaxe basic manuals?

Thanks Rik

Thanks Rik,

Before I came to the island I downloaded whatever I could put my hands on. I have all datasheets and manuals for Picaxe and GP2D02 available on the net, no worries. I have my code written in C but seem to have problems translating it into BASIC as the Picaxe has limited capacities compared to the Atmel AVR Mega 8.

I received some limited help and suggestions from other members on the Picaxe Forum. This is the link.

I was told to run the Picaxe at 8 MHZ and use other commands then pauseus as the read-in time is very fast.

My C-code can be found here.

Thanks for your prompt reaction.

My code so far is:

Symbol dangerlevel = 70 ’ how far away should thing be, before we react?
symbol turn = 300 ’ this sets how much should be turned
symbol servo_turn = 700 ’ This sets for how long time we should wait for the servo to turn (depending on it´s speed) before we measure distance

main: ’ the main loop
gosub sensorcontrol
spiin 3,0,MSBPre_L,(b1/8) 'Clock 8 bits into b1
debug b1
pause 500
if b1 < dangerlevel then
gosub nodanger ’ if nothing ahead, drive forward
gosub whichway ’ if obstacle ahead then decide which way is better
end if
goto main ’ this ends the loop, the rest are only sub-routines

setfreq m8 'Change to 8 MHz clock speed
low 3
pause 140 'Pause for 70 ms (7000 us)(Pause for 2 msec with 8 MHZ clock speed (70,000 us)
high 3 'Pulse 1 (Will take approx. 0.1ms)
low 3
high 3 'Pulse 2
low 3
high 3 'Pulse 3
low 3
high 3 'Pulse 4
low 3
high 3 'Pulse 5
low 3
high 3 'Pulse 6
low 3
high 3 'Pulse 7
low 3
high 3 'Pulse 8
low 3
high 3 'Now do the 2ms high min duration
pause 4 'Pause for 2 msec with 8MHz clock speed

nodanger:'Drive forward
’for w0 = 1 to 50
’random w1 'generate next random number
’b4 = w1 / 595 + 16 'calculate a pseudo-random sound frequency
’sound 1,(b4,5)
'next w0

high 5 : high 6 : low 4 : low 7

gosub totalhalt ’ first stop!

'Look one way:
gosub lturn ’ look to one side
pause servo_turn ’ wait for the servo to be finished turning
spiin 3,0,MSBPre_L,(b1/8) 'Clock 8 bits into b1
gosub totalhalt

’Look the other way:
gosub rturn ’ look to another side
pause servo_turn ’ wait for the servo to be finished turning
spiin 3,0,MSBPre_L,(b2/8) ‘Clock 8 bits into b1
gosub totalhalt

’ Decide which is the better way:
if b1<b2 then
gosub body_lturn
gosub body_rturn
end if

high 6 : low 5 : low 7 : high 4 ’ this should be your combination that turns the robot one way
pause turn : gosub totalhalt

high 5 : low 6 : low 4 : high 7 ’ this should be your combination that turns the robot the other way
pause turn : gosub totalhalt

servo 0, 100 ’ look to one side

servo 0, 200 ’ look to the other side

low 4 : low 5 : low 6 : low 7 ’ low on all 4 halts the robot!
Servo 0,150 ’ face forward
wait 1 ’ freeze all for one second

Feels alright to me

Did you test this code yet, or are you too apprehensive? Fearful of yor precious sensor hardware?

Consider (extra) series resistors to protect the poor GP against too much current.

Tested but not working

Hi Rik,

Yes  I have tested this software on the robot but there is no reading whatsoever. On the debug command I can see a value of 255 or 254 is created. Moving objects in front of it doesn’t alter the reading at all. That’s why I’m puzzled. I haven’t got an oscilloscope to check out the output signal of the GP2D02. The only thing I can think of is that something is wrong in my code.

If I run the program in the simulator and put in values between 0 and 255 on the input then the program responds correctly. I am pretty convinced the clock signal going to the sensor should be working fine. However something goes wrong when a digital value needs to be produced.

I will be back on the mainland in June and I will order a GP2D12 sensor, but in the meantime it would be nice to have my project going as I have lots of free time.

Thanks for your interest and help anyway.


I hope I don’t confuse the issue.

I have never used a PICAXE, but, I have paid close attention to PICs. There is another free language called JAL that is based on PASCAL. I thought I would post a link to the code they used to control a GP2D02 as well as some suggestions for protecting it. FYI to get the full understanding of what they are coding you would need at a minimum the ir_ranger_gp2d02.jal file that is linked to in the article.