Shop OBEX P1 Docs P2 Docs Learn Events
GP2D12 GPIO14 Basic Stamp — Parallax Forums

GP2D12 GPIO14 Basic Stamp

DocDoc Posts: 25
edited 2007-02-21 19:12 in BASIC Stamp
Hello everybody,

does anyone have any idea please if it is possible to program with PBASIC a GP2D12 IR Detector connected to a BS2PX through GPIO14 (ADC pin)?

Thank you
·

Comments

  • ZootZoot Posts: 2,227
    edited 2007-02-15 13:50
    I'm using GPIO14 right now with two Sharp detectors right now in a project. Great chip. The built-in ADCs offer 10bit resolution (0-1023) so you get a nice range. Quick readings too since it's I2C.

    If you check the GPIO14 documentation you will see you have a number of choices for using the ADCs. In my case, I chose to use the internal 5v reference so that I wouldn't have to give up one of the 5 ADC pins for a reference voltage. The downside for the Sharps then, is that I'm not using the full range of resolution -- the Sharps output ~0-3v so I get readings of ~ 03-510 -- but that's still an order of resolution higher than an 8-bit ADC -- and no external circuitry is required. If you want more precision, you can put a 3v voltage divider across the reference pin.

    I'm also using the dedicated PWM pin to control brightness of some LEDs, and the built-in pull-ups on Port B are nice for button input and the like.

    One comment -- since what you are really buying is the firmware that Devantech coded into the chip (a PIC) you might want to pick up a spare or two -- I don't think Devantech is going anywhere, but in 5 years if you burn the chip, it might not be so easy to replace.

    www.robot-electronics.co.uk/htm/gpio14tech.htm

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST
  • DocDoc Posts: 25
    edited 2007-02-15 14:49
    Hey Zoot,

    Thx for your·reply. But I I still would like to know, if it is possible to program with PBasic in order to read the data sent by the GP2D12.

    I have already a SRF235 connected to my BS2px and working very well. I want to use the 2 detectors together. When I have added the GPIO14, I connected the SDA-SDA, SCL-SCL between the 2. I got the +5 V from the Vdd & the Grd from the Vss both from of the BOE. The +5 V of GP2D12 is connected directly to the Vdd of the BOE, same for the Grd.The Vout to AN0 of the GPIO14.

    I hope I did not make any stupid thing with these connections!!!

    Do·you have any hints about the PBasic Commands?!!

    Tanks again

    Doc
  • ZootZoot Posts: 2,227
    edited 2007-02-15 15:07
    OK, hopefully I can answer your questions smile.gif

    Connections:

    1 -- SDA of any I2C devices (incl. GPIO14) -> pin 0 or pin 8 of Stamp
    2 -- SCL of any I2C devices (incl. GPIO14) -> pin 1 or pin 9 of Stamp
    3 -- 1.8k-4.7k pull-up resistor between SDA pin and +5v (only one pullup needed on I2C bus) - standard is 4.7k; I usually use 2k for better noise immunity
    4 -- 1.8k-4.7k pull-up resistor between SCL pin and +5v (only one pullup needed on I2C bus) - standard is 4.7k; I usually use 2k for better noise immunity
    5 -- Sharp output pin to AN(0..4) of GPIO
    6 -- +5v and gnd connections to sharp

    Programming:

    Use I2COUT command (see Pbasic Manual) to setup your ports, ADCs, etc. on the GPIO14. See the GPIO14 documentation for details. In this case, I'm presuming using internal +5v reference which will give you about 9bits of resolution on the 3v Sharp, right justified data. Then use I2COUT and I2CIN to get an ADC conversion:

    SDA PIN 0
    GPIO14 CON $40  'GPIO14 address
    cmdReg CON $00 'command register
    ADreg CON $03 'AD control register
    resultReg CON $01 'result register
    ioWord VAR Word
    
    Reset:
      I2COUT, SDA, GPIO14, ADreg, [noparse][[/noparse]%11000000]  'set AD register (3) to be right justified data, internal 5v reference
    
    Main:
      I2COUT, SDA, GPIO14, cmdReg, [noparse][[/noparse]$03 + 0 ]  'request AD conversion from AN0 by sending to command register -- change the + 0 to +1, +2 etc for the other ADC pins
      I2CIN, SDA, GPIO14, resultReg, [noparse][[/noparse]ioWord.HIGHBYTE, ioWord.LOWBYTE] 'read word sized result from result registers
      DEBUG HOME, "AN0 result: ", DEC ioWord, CLREOL
      GOTO Main
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    Post Edited (Zoot) : 2/15/2007 3:26:40 PM GMT
  • ZootZoot Posts: 2,227
    edited 2007-02-15 15:28
    Oh, one other note. You may want to put a 220ohm resistor between the output pin of the Sharp detector and the ADC pin of the GPIO14 -- your accuracy will hardly suffer, but this will prevent you from burning out the pin of the GPIO14 if you accidentally set that pin to be an OUTPUT (you'd get short circuit if the pin was an output and it was hooked up to the voltage coming out of the Sharp).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST
  • DocDoc Posts: 25
    edited 2007-02-15 16:13
    Thank you very much again, I will give it a try.
    But do not you think that a 100 nF capacitor is needed between the Vdd +5 V·supply and·Vss (Grd) of the GPIO14 or it is not essential?

    Regards,

    Doc
  • ZootZoot Posts: 2,227
    edited 2007-02-15 16:20
    I was just providing *minimum* connections that weren't wrong smile.gif

    A good rule of thumb for any integrated circuit is to put a decoupling capacitor (.01.uf-.1uf) between Vdd and Vss right at the chip. Depending on the chip(s) and the design it is not uncommon to also put a 10uf cap across the power rails at the chip also. It mostly depends on the current draw and purpose of the chip. For example, the GPIO14 can sink/source 25ma per pin (which is pretty beefy). If you are running a bunch of LEDs from the ouput pins you might want a 10uf cap also.

    That said, I've found the GPIO14 (PIC) to be pretty insensitive to noise and voltage fluctuations on the power rails.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST
  • DocDoc Posts: 25
    edited 2007-02-21 09:11
    Hello again,

    I am using this code:

    SDA···· PIN 8················ ' SDA on PIN 8
    GPIO14 CON $40············· 'GPIO14 address
    cmdReg CON $00············· 'command register
    ADreg CON $03·············· 'AD control register
    resultReg CON $01·········· 'result register
    Code VAR Word

    Reset:

    · I2COUT SDA, GPIO14, ADreg, [noparse][[/noparse]%11000000]


    DO
    ·I2COUT SDA, GPIO14, cmdReg, [noparse][[/noparse]$03 + 0]
    ·PAUSE 10
    ·I2CIN SDA, GPIO14, resultReg, [noparse][[/noparse]Code.HIGHBYTE, Code.LOWBYTE]
    ·DEBUG 1, DEC5 Code


    ·LOOP

    The problem is that the output result is not stable. The code generated with the debug command is not the same for the same distance, it keeps on changing without touching the detector.
    Any idea please?!

    Thanks

    Doc
    ·
  • ZootZoot Posts: 2,227
    edited 2007-02-21 13:49
    What kind of change are getting? Seeing change (even when you are not moving the detector) is not uncommon.

    On my detectors, I get a workable range of about 3-510 for various distances, with a "steady-state" change of 10-15. In other words, without even moving the detector I might see things jump around from 250-260.

    Remember that to make the IR detector useful you need to:

    - make sure you are reading the correct A/D pin from the GPIO14

    - take same calibrating measurements with an obstacle at 10cm, 20cm, 30cm ... 70cm

    - run the measurements through a linearizing formula to get cm or inches

    - possibly maintain a running average and/or throw away your lowest few bits on the reading to get a "stable" reading

    - I have also seen where tying unused A/D inputs on the GPIO14 to ground through a 10k resistor will help make used A/Ds a little more stable

    About halfway down on this page -- http://forums.parallax.com/showthread.php?p=617740 is an Excel spreadsheet that will let you plug in actual detector readings and obtain 3 constants for a linearizing formula to convert to centimeters. The file is called gp2d12_cal.xls

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST
  • DocDoc Posts: 25
    edited 2007-02-21 15:10
    Hello,

    For the moment, I just need to have a stable code for one fixed distance. Than I will work on calibration and linearisation with excel doc you have sent to me (Thanks a lot by the way).

    well I have made few measurments, arround 200 with 10 ms interval, I got the min code 245 and the max 311 for the same distance.

    The GP2D12 (+5V) pin and the GPIO14 VCC (+5V) pin both are connected to the Vdd (+5) of a BOE-USB. I am working also on the AN0.

    Actualy I will recover the code in a VB6 GUI and I will fixe the calibration in there.

    As·it is 10 bits data from the GPIO14, so I must have a code beween 0 - 1023. But since the GP2D12 max Vout is 3 V (2.5V) so the max code should be 614 (511), right?

    But I still find it strange this 245-311 jump for a fixed position, NO?

    By the way, are you using a voltage divider?

    Thanks
  • ZootZoot Posts: 2,227
    edited 2007-02-21 17:06
    That seems like a pretty big jump, yeah. I usually see the lowest 1-3 bits changing, but not much more than that.

    There are a number of articles that suggest putting a 33uf cap across the output pin and gnd pin of the Sharp to stabilize the output; I haven't tried that because I didn't really have the physical space to do that right at the detector.

    However, for stable readings, unless you need greater precision than 1cm, I would calibrate and linearize, THEN see what kind of jumps you get.

    Voltage divider -- I am not using one (I think we covered this above?) for two reasons:

    - I have some analog inputs that are in the 0-5v range and some that are in the 0-3v range (Sharps).

    - if you do use a voltage divider, then you sacrifice one analog pin on the GPIO14 (you need to feed the reference voltage in somewhere!)

    But if you want greatest precision, and having only 4 analog inputs, all referenced to 3v, is OK, then a voltage divider for the AREF pin would be in order. Remember to write the correct mode to the A/D control register before taking readings.

    Lastly, I guess in theory for 10bits of 3v readings against 5v reference you might get numbers as high as ~600, but that also presumes the detector is actually reaching a "perfect" 3v. The Sharps output a higher voltage the CLOSER you get, and if you get too close (<4cm or so) the voltage DROPS again...so there is really a very tiny sweet spot where you actually see max. voltage. Then add possible variations in the detector itself, actual resistance in the wiring to your GPIO14, etc., and y ou may not ever hit 3v exactly.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST
  • DocDoc Posts: 25
    edited 2007-02-21 17:59
    Well yes, huge jump.

    Actually I am looking for a 0.1 mm Precision, so if the voltage divider can help, I will sacrify one A/D.

    But how come you managed to have only 1-3 bits changing?
  • ZootZoot Posts: 2,227
    edited 2007-02-21 18:36
    That's always been about my experience with the Sharps and a given ADC in the 8-10bit range. I have pretty clean power supplies to the detectors and the chips, or maybe I'm just lucky smile.gif I personally think all IR is a bit skittish and does much better if you take a few readings and average them.

    Crunching some numbers, if you use a voltage divider for a 3v reference, in an ideal world you would get:

    3v / 1024 steps of resolution = 2millivolt resolution

    Or given the real useful range of the Sharp (10-80cm = 70cm):

    70cm / 1024 = .06cm resolution -- wouldn't that be .6mm precision at best? And you might lose a little more precision after averaging out at least a bit or two? Aaargh -- I haven't had enough coffee yet this morning for math.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST
  • DocDoc Posts: 25
    edited 2007-02-21 19:12
    Thanks a lot for your replies,

    I will try my best to use this IR at max.

    Enjoy your coffee and have a nice day/evening.

    Cheers
Sign In or Register to comment.