Reading the sharp GP2D12 IR Sensor with Basic ATOM PRO

I’ve wired up the GP2D12 to pin 31 of my ATOM, and run this code:

sensor_read var word

main:
Adin 31,sensor_read
debug [DEC sensor_read,13]
pause (300)
goto main

its supposed to report distances between 10 and 80 cm as a value between 0 and 1024. But all I get is values between 600-800, and the values tend to wander all over the place, even when the sensor is pointing at the an immobile object.

Can anyone offer some advice with using these things? Is my sensor fubar?

Well, I realized I was reading a non-existent pin, so the new code is:

sdata var word

main:
Adin 0,sdata;
pause (2000)
debug DEC sdata,13]
goto main

but its still reading funny. When I turned it on, this was its output, it started out reading 496. With every read the value climbed till it stayed steady at 811. Moving the object its pointing at closer and farther didn’t change the value.

I restarted it and it repears the climb from a number near 500 to a number in the 800’s. Very strange!

After talking to Basic Micro tech support, they told me that on a ATOM PRO AX0 - AX3 is P19- P16!! So now I am using this code:

main:
Adin P19,sensor_read
debug [DEC sensor_read,13]
pause (300)
goto main

I am basing my info on this page (see step 14 at the bottom of the page):

lynxmotion.com/images/html/build074.htm

This sensor’s docs do not say anything about triggering it (its not the lynxmotion IRPD, this is the Sharp IR Distance Sensor).

I started to wonder if the sensor was even turned on, so I measured the + and GND pins on AX0 of my BotBoard, and they do not appear to be carrying any current!!!

So, how do I turn on the power so my sensor can work in the first place? Anyone? The instructions on that page make is sound “Plug and Play” easy, but it sure isn’t working out that way!

Got it working. Forgot to put a jumper across the 5V supply for the A/D pins.

Just so this thread wasn’t a complete waste of time, I’ll go through what stumped me so others can read this later when they do a search…
**
#1** Where the heck is my A/D pin???

ABB Pin Label<-> ATOM PRO Pin name
AX0 <-> P19
AX1 <-> P18
AX2 <-> P16
AX3 <-> P17

#2 Yep, they are out of sequence!!!

#3 There is no code example for the Pro anywhere on BasicMicro’s site. This code works:

my_input var Word
loop:
    ADIN P19, my_input
    debug [DEC my_input,13]
    pause 20
goto loop

Run this in DEBUG mode and it’ll print values based on the sensor readings.

#4 Do I need to pause after reading the sensor?

Doesn’t look like it. Speed tests on the PRO show that the ADIN command takes less than 1 mS to execute, at least with this sensor anyway…

#5 Is this thing ON!?!

If your sensor seems to completely ignore things (just seems to spit out random #'s, or gradually climbs to 1023 or drops to 0, its probably off. Make sure you jump the 5V supply for the A/D bank.

I sure hope this helps some future roboteer avoid the 7 hour hair pulling session that my day-off became. Time for a beer! :wink:

Glad you got it working!

Thanks for the helpful tips.
That’s one more post that I can bookmark and add to my arsenal of helping links.

:smiley:

hi,

how i can change the analog input into inch or cm?

or how i can write a reaction when the input is 4 inch?
the i want so start a program or so…

how to do that?

The output of the sensor is analog, but it’s not linear. You would get the best results just measuring the value returned with the obstacle at the distance you want.