Help me with an Sharp GP2D12 IR sensor

Here is a different thread with information and sample code.

lynxmotion.net/viewtopic.php?t=570

If you are asking how to convert the value to centimeters, there’s an example I cam across at the Basic Micro website. At the bottom of the page there’s example code for the GP2D12. the code uses a byte table, which is an array of constants.

basicmicro.com/downloads/sam … omird.html

[code]
’ ----- Title ]-----------------------------------------------------------

’ File… Range.bas

’ Purpose… Atom Module -> GP2D12 IRD

’ Author… Nathan Scherdin and Chuck Hellebuyck

’ Started… March 21, 2002

’ Updated… April 6, 2002

’ ----- Program Description ]---------------------------------------------

’ This program demonstrates how to read the Sharp GP2D12 infrared analog output

’ detector with an Atom module and display the results in a terminal window.

’ Hardware Connections:

’ Atom Module Sharp Sensor

’ ------ --------------------------------

’ AX0 Pin 1

'----- Variables ]------------------------------------------------------------

scanrange var word ’ A/D result variable

floating var long ’ Floating point math result storage

val var byte ’ Table conversion result storage

'---- Table Setup ] ----------------------------------------------------------

scantable bytetable 80,80,80,80,80,80,80,80,80,78, |

                                76,74,72,70,68,66,64,62,60,59, |

                                58,57,55,53,52,51,50,49,48,47, |

                                45,43,42,41,40,39,38,37,35,33, |

                                32,31,30,30,29,29,28,28,27,27, |

                                26,26,26,25,25,25,24,24,24,23, |

                                23,22,22,21,21,20,20,20,19,19, |

                                18,18,18,17,17,16,16,16,15,15, |

                                15,14,14,13,13,13,12,12,11,11, |

                                11,10,10,10,10,10,10,10,10,10

'---- Main Loop ] -----------------------------------------------------------

main

        adin AX0,2,AD_RON,scanrange                   ' Read sensor value

        floating = float scanrange fdiv 5.12                 ' Limit value to <200 values

        val = scantable(int floating)                            ' Convert A/D to measurement

        if scanrange > 512 then                                   ' Test for obstacle to close to detector

        serout s_out,I9600,"Too Close To Measure", 13]   ' Send "Too Close" message

        else

’ debug [REAL floating," ",DEC val,13] ’ Output result to debug window

        serout S_OUT,i9600,[REAL floating\2," - ",DEC val," cm",13]      

        endif                                                                ' End If-Then-Else command

       

        goto main                                            ' Loop back and get another reading[/code]

Because the sensors response is NOT linear Nathan has set up a lookup table, to get accurate distance measurements. Unfortunately his example is for an atom not the pro.

What? :open_mouth:

What do you mean, What? :open_mouth:

The Atom code is a lot like the Atom Pro. are you sure it won’t work with the pro? maybe the code can be edited for the pro…

I am sure the program will require editing before it will run on the pro. I’m not able to test it at the moment. Sorry…

I did a quick and dirty conversion to Pro… Did not try running it but it did compile. Note: I made a simply logic change as I don’t like accessing random memory if I am outside of the array…

[code]’ ----- Program Description ]---------------------------------------------

’ This program demonstrates how to read the Sharp GP2D12 infrared analog output
’ detector with an Atom module and display the results in a terminal window.
’ Hardware Connections:

’ Atom Module Sharp Sensor
’ ------ --------------------------------
’ P16 Pin 1

'----- Variables ]------------------------------------------------------------

scanrange var word ’ A/D result variable
floating var float ’ Floating point math result storage
val var byte ’ Table conversion result storage

'---- Table Setup ] ----------------------------------------------------------

scantable bytetable 80,80,80,80,80,80,80,80,80,78, |
76,74,72,70,68,66,64,62,60,59, |
58,57,55,53,52,51,50,49,48,47, |
45,43,42,41,40,39,38,37,35,33, |
32,31,30,30,29,29,28,28,27,27, |
26,26,26,25,25,25,24,24,24,23, |
23,22,22,21,21,20,20,20,19,19, |
18,18,18,17,17,16,16,16,15,15, |
15,14,14,13,13,13,12,12,11,11, |
11,10,10,10,10,10,10,10,10,10

'---- Main Loop ] -----------------------------------------------------------

main

adin P16,scanrange                   ' Read sensor value 
if scanrange > 512 then                               ' Test for obstacle to close to detector 
	serout s_out,I9600,"Too Close To Measure", 13]   ' Send "Too Close" message 
else 
	floating = tofloat( scanrange) / 5.12                 ' Limit value to <200 values 
	val = scantable(toint floating)                        ' Convert A/D to measurement 

’ debug [REAL floating," “,DEC val,13] ’ Output result to debug window
serout S_OUT,i9600,[REAL floating\2,” - “,DEC val,” cm",13]
endif ’ End If-Then-Else command
goto main ’ Loop back and get another reading[/code]

Thanks Kurte, you rock. 8)

I have confifdence it will work without issues.

Thank you very much for all of you . Kurte it work very well your solution. Thank you !

You are welcome!

Kurt

iv seen that a few are adding these sensors and so i feel i want to join in. :wink:

does the sensor connect to P16 or AX0?
botboard or ssc?

The sensor can be added to the Bot Board II on pins 0,1,2,3,16,17,18,19 when using an atom pro. It can also be used on the 4 inputs on the SSC-32.

ok great. i will be using AX2 on the ABB.
question: what jumpers would i need to install?
iv read though loads of docs and some of them contradict each other so im now left confused.

im running a 9v though VL!

I’m not sure anything conflicts so much as there are many acceptable but different ways to do things with the boards. AX0,1,2,3 are Atom pins. On the pro they are 16,17,18,19. I/O pins 0,1,2,3 are also analog capable. As far as jumpers, you only need one to apply 5vdc power to the I/O buss (group of four) that you will connect the GP2D12 to. Hope this helps.

ok thanks dude.

thanks for posting this. really helps. :slight_smile:

Hi Jim,
I use a SSC32 with a BBII and BATOMpro 28 and I’ve create a simple program to move my robot ARM.
I’ve added a sensor on the input pin on the SSC32 (A) and I wont use it with the basic code:
Do you have a simple example code?
THX

Please just start a thread on the subject. There is no basic code for an arm that supports reading sensors from the SSC-32 that I am aware of.

Excuse me!!!
Ok I undestand… I will try to connect the sensor directly on a BBII!
Thx