Shop OBEX P1 Docs P2 Docs Learn Events
Simple A to D — Parallax Forums

Simple A to D

TechnoRobboTechnoRobbo Posts: 323
edited 2008-10-10 14:48 in Robotics
This is a real simple A to D made from a Radio Shack Quad Comparator compatible to the LM339 and a BS2.

It uses an PWM command and RC circuit to create the reference voltage and a short and very fast successive approximation routine to read the input voltage. The result is very linear yielding a value from 0 (0v)·to 255 (5V).


the schematics and code are attached below.


' {$STAMP BS2}
' {$PBASIC 2.5}
IDX······ VAR Nib
X········ VAR Byte
LOW 7
INPUT 15
Main:···· 'Successive Aproximation Routine
DO
··· x=128
··· FOR idx=7 TO 0
····· X=X | DCD idx
····· PWM 7, X, 5
····· IF IN15=0 THEN
········ X=X ^ DCD idx
····· ENDIF
··· NEXT
··· DEBUG HOME,DEC3 X
LOOP
END
·····

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Have Fun


TR

Post Edited (TechnoRobbo) : 12/30/2006 4:29:45 PM GMT
1540 x 2040 - 188K

Comments

  • TechnoRobboTechnoRobbo Posts: 323
    edited 2006-12-30 16:31
    Found this article in nuts and volts - code is not as tight but the circuit draws less amps.

    http://www.parallax.com/dl/docs/cols/nv/vol1/col/nv25.pdf

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Have Fun


    TR
  • TechnoRobboTechnoRobbo Posts: 323
    edited 2007-01-02 03:14
    Increasing the Resolution

    Utilizing the other comparators on the chip you can increase the A/D resolution by using pull up and pulldown resistors in voltage dividing combinations - In the example below on comparator handles 0v to 2.5v and the other handles 2.5v to 5v.· I sum them together to double the resolution. This allows the stamp to read 1/100th of a volt (9 bit resolution). Sometimes the range of the sensor your using is to large and impractical -Using this technique to·adjust the your A/D reference voltage may yield even higher resolutions without the need for more pins or components.

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    IDX······ VAR Nib
    X········ VAR Byte
    stdout··· VAR Word
    INPUT 15
    INPUT 14
    Main:···· 'Successive Aproximation Routine
    DO
    ··· x=0
    ··· FOR idx=7 TO 0
    ····· X=X | DCD idx
    ····· PWM 7, X, 5
    ····· IF IN15=0 THEN
    ········ X=X ^ DCD idx
    ····· ENDIF
    ··· NEXT
    ··· stdout=x
    ··· 'double it
    ··· x=0
    ··· FOR idx=7 TO 0
    ····· X=X | DCD idx
    ····· PWM 6, X, 5
    ····· IF IN14=0 THEN
    ········ X=X ^ DCD idx
    ····· ENDIF
    ··· NEXT
    ··· stdout=stdout + x
    ··· DEBUG HOME,DEC3 stdout
    LOOP
    EN


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Have Fun


    TR

    Post Edited (TechnoRobbo) : 1/2/2007 3:51:59 AM GMT
    1540 x 2040 - 228K
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2007-01-03 04:21
    TechnoRobbo


    Utilizing the other comparators on the chip you can increase the A/D resolution by using pull up and pulldown resistors in voltage dividing combinations


    Can you give some·more details on how to do this what is in red

    What i would like to do is have the following

    comparator 1······· ·0 to 5 volts
    comparator 2·········5 to 10 volts···· Any· idea.gif··s

    comparator 3········ 10 to 15 volts


    Please tell me if this can be done and how you go about doing this

    ·Have tried this part of what i am talking about before

    Thank You for any ·idea.gif·that you have on how to do this

    Sam





  • TechnoRobboTechnoRobbo Posts: 323
    edited 2007-01-05 02:59
    Divide your input voltage with 3· resistor·10k like this - rest of cicuit is attached

    use PWM 7, X, 10· instead of PWM 7, X, 5 (replace 7 with the pin your using)

    ·················attachment.php?attachmentid=44871






    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Have Fun


    TR

    Post Edited (TechnoRobbo) : 1/5/2007 2:14:37 PM GMT
    720 x 538 - 31K
    1540 x 2040 - 228K
  • TechnoRobboTechnoRobbo Posts: 323
    edited 2007-01-05 14:13
    sorry I had to repost the schematic I forgot the pullup on pin 13

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Have Fun


    TR
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2007-01-05 20:47
    TechnoRobbo

    Thanks for your post and your ·idea.gif
    ·If I have time this weeken I will· built this and try it and let you how it works

    I just want to make sure of one thing and that is that all "vdd"'s are 5 volts

    Please let know if I sm right about this

    Thank again for help and you time






    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them





    Sam
  • TechnoRobboTechnoRobbo Posts: 323
    edited 2007-01-05 22:39
    Yes Vdd's are 5volts but the Vin (pin 3 on comparator) must be more than 5 volts to resolve 5 volts.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Have Fun


    TR
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2007-01-06 00:13
    TechnoRobbo

    Thanks for your reply

    Thanks for telling about pin 3 on the LM339 and all of the VDD"s




    ·I will try to have some fun········hop.gif··· smile.gif·· jumpin.gif·But these Guys are having way to much fun



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them





    Sam
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2007-01-10 02:29
    TechnoRobbo

    How would you write the code for pins 13 and pin 5

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    IDX VAR Nib
    X VAR Byte
    stdout VAR Word
    INPUT 15
    INPUT 14
    INPUT 13
    Main: 'Successive Aproximation Routine
    DO
    x=0
    FOR idx=7 TO 0
    X=X | DCD idx
    PWM 7, X, 5
    IF IN15=0 THEN
    X=X ^ DCD idx
    ENDIF
    NEXT
    stdout=x··········· · '........................................
    'double it
    x=0

    FOR idx=7 TO 0
    X=X | DCD idx
    PWM 6, X, 5
    IF IN14=0 THEN
    X=X ^ DCD idx
    ENDIF
    NEXT

    ' I am not sure what·is suppost to be here·...........>>>>>>>>

    This

    'stdout=stdout + x·········· ·'.......................................

    ·Or this

    My thinking is that this is what go here

    'stdout=x
    'double it
    'x=0

    ·
    FOR idx=7 TO 0
    X=X | DCD idx
    PWM 5, X, 5
    IF IN13=0 THEN
    X=X ^ DCD idx
    ENDIF
    NEXT
    stdout=stdout + x ......................................' Is this right
    DEBUG HOME,DEC3 stdout
    LOOP

    LOOP
    END

    Thanks for all of the help and that·you given to me in this matter



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them





    Sam

    Post Edited (sam_sam_sam) : 1/10/2007 3:03:45 AM GMT
  • TechnoRobboTechnoRobbo Posts: 323
    edited 2007-01-11 13:31
    Add low then middle then high , use a duration of 10 due to highest resistance in voltage path. The stages will max out at 255 before the next stage rises from 0. The output is·cumulative therefore the range will be 0 to about 765 (0V to 15V). All 4 comparators together·will give you a 10 bit resolution but it uses up 8 Stamp IO pins.
    ·


    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    IDX VAR Nib
    X VAR Byte
    stdout VAR Word
    INPUT 15
    INPUT 14
    INPUT 13
    Main: 'Successive Aproximation Routine
    DO
    x=0
    FOR idx=7 TO 0
    X=X | DCD idx
    PWM 7, X,10
    IF IN15=0 THEN
    X=X ^ DCD idx
    ENDIF
    NEXT

    stdout=x·············

    x=0
    ·
    FOR idx=7 TO 0
    X=X | DCD idx
    PWM 5, X, 10

    IF IN13=0 THEN
    X=X ^ DCD idx
    ENDIF
    NEXT
    stdout=stdout + x

    x=0

    FOR idx=7 TO 0
    X=X | DCD idx
    PWM 6, X, 10
    IF IN14=0 THEN
    X=X ^ DCD idx
    ENDIF
    NEXT

    stdout=stdout + x··········



    DEBUG HOME,DEC5 stdout
    LOOP

    END






    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Have Fun


    TR

    Post Edited (TechnoRobbo) : 1/27/2007 4:44:53 AM GMT
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2007-01-12 12:30
    TechnoRobbo
    Thanks so very much reply this will help alot

    ···························

    ·························· smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them

    ·
    ·
    ·
    ·
    Sam
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2007-01-16 03:52
    TechnoRobbo

    Here is the code for the Battery Tester

    For some reason the Attachment Manager is not working very good to night





    I put this together today the Simple A to D·this works very good thanks for posting it··I have not built the other one yet· but i will when i have some more time

    ·this what i came up with a battery tester and this is the little routine that i wrote for it

    I will need use this routine when i built a project that i have in mind builting

    · 'This Routine Displays The type of battery that is hook up

    I add this to your routine



    ··· IF x > 50 AND x < 80 THEN
    ··· DEBUG "1.2 TO 1.5 Volts", CR
    ··· PAUSE 500···················· 'This is to clear the screen·· I am going to put leds instead of the debug statements this just to test it
    ··· EXIT

    ··· ELSEIF x > 120 AND x < 160 THEN
    ··· DEBUG " 2.5 TO 3.0 Volts", CR
    ··· PAUSE 500····················· 'This is to clear the screen
    ··· EXIT

    ··· ELSEIF x = 238 THEN
    ··· DEBUG "4.5 Volts", CR······· ' The range is N/A at this time will come latter
    ··· PAUSE 500························ ·'This is to clear the screen
    ··· EXIT

    ··· ENDIF


    LOOP

    DEBUG CLS, CR····················· 'This is the routine to clear screen
    GOTO main




    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 1/16/2007 3:59:49 AM GMT
  • TechnoRobboTechnoRobbo Posts: 323
    edited 2007-01-16 04:35
    Glad I could help.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Have Fun


    TR
  • darshanrtdarshanrt Posts: 4
    edited 2007-10-17 21:26
    Hey Guys,


    I am going thorugh your conversations and I'm really amazed by your work... I have some similar question to continue if you guys can help me too... well suppose I have a battery outside and if I want to measure its voltage with the use of basic stamp, how can I go with it...??

    again appriciated,

    Regards,
    Darshan
  • TechnoRobboTechnoRobbo Posts: 323
    edited 2007-10-19 00:15
    from Private message:

    a 12 bit A to D will give you 0 to 5 volt with 4096 step resolution
    multiply your input x and multiply by */3125
    volt=x*/3125
    5 volts will read out as 49987
    use the DIG command to format the output and add the decimal point
    DEBUG DEC1 x DIG 4,".",DEC1 x DIG 3,DEC1 x DIG 2,DEC1 x DIG 1,DEC1 x DIG 0

    http://www.parallax.com/detail.asp?product_id=604-00026

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Have Fun


    TR
  • Luiz mauricio mionLuiz mauricio mion Posts: 77
    edited 2008-08-20 05:29
    The ne566 vco , works like a time lapsed A/D conversion,count the number of cycles square wave,and pre determine the amount of cycles to count.
    I do not remenber how much speed this VCO work,but its good to obtain voltage meter or low sample rate audio,sampling.
  • po2lepo2le Posts: 76
    edited 2008-10-10 06:30
    Techno,

    I've built and used the simple A to D converter using an LM 339, it works.· Thanks for the information.

    Can I store the results of the A to D output in EEprom?· The results are shown as numbers up to 255, which indicates 5v, on the debug screen.· Can these results be stored as the same numbers in EEPROM?

    Thanks

    Patrick
  • Mike GreenMike Green Posts: 23,101
    edited 2008-10-10 14:48
    po2le,
    Read the Stamp Basic Manual sections on the WRITE, READ, and DATA statements to learn about the use of EEPROM for data storage.
Sign In or Register to comment.