Shop OBEX P1 Docs P2 Docs Learn Events
Voltage to Distance using a BS2 — Parallax Forums

Voltage to Distance using a BS2

BicyclerBicycler Posts: 4
edited 2009-08-23 01:12 in BASIC Stamp
I am using a BS2 and an ADC 0831 to read te voltage of an analog sensor. The sensor gives a 4-20ma output proportional to the range it is set at. I currently have it set to a minimum measurement of 1 ft (4ma) and maximum measurement of 6 ft. (20ma) The ADC is reading the values across a 250ohm resistor to give a·1-5 v·measurment into the 0831.

I am using a program code from Parallax Stamps Works and it works just fine·for voltage measurement, but I am trying to figure out how to convert the reading to a distance of inches or feet ad show it using DEBUG. Any help would be greatly appreciated. My program is listed below. I'm not sure how to add to it to show distance.

·'{$STAMP BS2}
'{$PBASIC 2.5}
'
[noparse][[/noparse]Program Desrcription]
'
'This program demonstrates reading a variable voltage with an ADC0831
'analog-to-digital converter chip. This program uses a Vref input of
'5.000 volts (Vdd) for a bit resolution of 19.6 millivolts.
'
[noparse][[/noparse]I/O definitions]
cs········ PIN· 0·································· 'chip select (ADC0831.1)
Clock····· PIN· 1·································· 'clock (ADC0831.1)
DataIn···· PIN· 2·································· 'clock (ADC0831.1)
'
[noparse][[/noparse]Constants]
Cnts2Mv··· CON· $139C······························ ' x 19.6 (to millivolts)
'
[noparse][[/noparse]Variables]
result···· VAR· Byte······························· 'result of conversion
mVolts···· VAR· Word······························· 'millivolts
'
[noparse][[/noparse]Initialization]
Reset:
· DEBUG CLS,······································· 'create report screen
······· "ADC....· ", CR,
······· "volts... "
'
[noparse][[/noparse]Program Code]
Main:
· DO
··· GOSUB Read_0831································ 'read the ADC
··· mVolts = result */ Cnts2Mv····················· 'convert to millivolts
··· DEBUG HOME,
········· CRSRXY, 9, 0, DEC result, CLREOL,
········· CRSRXY, 9, 1, DEC mVolts DIG 3,
······················· ".", DEC3 mVolts
··· PAUSE 100
·· LOOP
'
[noparse][[/noparse]Subroutines]
Read_0831:
· LOW CS··········································· 'enable ADC
· SHIFTIN DATAin,Clock,MSBPOST,[noparse][[/noparse]result\9]·········· 'read ADC
· HIGH CS·········································· 'disable ADC
RETURN

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-08-17 03:34
    Well, according to the data you provided, 1000mV = 1ft and 5000mV = 6ft. You basically want to come up with a linear equation that fits these points of the form DIST = MV * A + B.

    We know the slope of the line here is A = (6ft - 1ft) / (5000mV - 1000mV) = 5ft / 4000mV = 1 / 800 ft per mV

    Our equation is now DIST = MV / 800 + B

    If we plug in the data you provided, we get B = -0.25, so the formula is DIST = mV/800 - 0.25

    If we use inches instead of feet, we get DIST = 12*mV/800 - 12*0.25 = (3*mV)/200 - 3

    This is the formula you can use with mVolts to get the distance in inches
  • BicyclerBicycler Posts: 4
    edited 2009-08-17 16:02
    Thanks for the solution! I'm anxious to plug it into the program and run it.
  • BicyclerBicycler Posts: 4
    edited 2009-08-23 01:12
    The code worked perfectly!
Sign In or Register to comment.