Shop OBEX P1 Docs P2 Docs Learn Events
Water Level Code Help — Parallax Forums

Water Level Code Help

jomjom Posts: 7
edited 2010-11-24 13:05 in BASIC Stamp
Hello Everyone,

Im currently working on a basic stamp project where I need to maintain water level at a certain set point. When the drain is opened the water valve needs to

I am using a water pressure sensor that sends 0 to 5v output depending on water level.

I also have an I/P connected to a water valve that works from 4 to 20mA.

Any Suggestions on which way I should direct my code to make this work.

Comments

  • stamptrolstamptrol Posts: 1,731
    edited 2010-11-12 07:23
    Welcome to the forums!

    Its always easier to get help if you tell us about what you have tried so far.

    This can be in the form of code, pictures, schematics, etc and makes it much easier for us to give meaningful assistance.

    In general terms, you will need to write three pieces of code:
    1. read the value of the sensor. Probably will use a a/d converter.
    2. send out a signal to drive the valve. Perhaps use a d/a converter.
    3. a piece of code to know when the valve should go open or closed.

    Cheers,
  • jomjom Posts: 7
    edited 2010-11-12 08:50
    This is the code I have so far

    DO
    IF IN3 = 0 THEN ' If 5 volt inputs
    HIGH 12 ' Send Signal to I/P

    ELSEIF IN3 = 1 THEN

    LOW 12

    It recognizes the input from the sensor when the water tank if full and sends 5v to the controller.


    This is the I/P Converter im currently using:

    http://www.abb.com/product/seitp330/26dbe8eacadeb6bec1256d3700451553.aspx
    900 x 1200 - 130K
  • stamptrolstamptrol Posts: 1,731
    edited 2010-11-12 11:43
    As used now, the pressure sensor may be sending a signal from 0 to 5 volts, but the Stamp only sees it as either 0 volts OR 5 volts. As it moves through a value in the 3 volt range, the input will suddenly go from 0FF to ON.
    Similarly, your output 12 is either ON or OFF and so the I/P will be telling the valve to go full on or full off.
    Now, many simple control systems will work perfectly fine with ON-OFF inputs and outputs. This is especially true of tank filling operations.
    However, you might make things more responsive by doing two things:
    1. use an analog/digital converter chip LTC1298, for example, so that you really can use the full range of the pressure sensor.
    2. Secondly, use two or more Stamp outputs to give several discrete valve outputs. Perhaps OFF, 50% open, 75% open, Full open.
    This way, the valve will slowly close as the level approaches the setpoint.

    Cheers,
  • $WMc%$WMc% Posts: 1,884
    edited 2010-11-12 19:34
    jorn wrote: »
    Hello Everyone,

    I'm currently working on a basic stamp project where I need to maintain water level at a certain set point. When the drain is opened the water valve needs to

    I am using a water pressure sensor that sends 0 to 5v output depending on water level.

    I also have an I/P connected to a water valve that works from 4 to 20mA.

    Any Suggestions on which way I should direct my code to make this work.
    '
    jorn:
    '
    I would use an ADC0831(analog to Digital converter) for the level input.
    'StampWorks experiment #28 for an example code.
    '
    Heres the link to Stamp Works
    http://www.parallax.com/Portals/0/Downloads/docs/books/sw/Web-SW-v2.1.pdf
    '
    Next I would reconfigure the level transmitter to 1 to 5 volts,Not 0 to 5volts ( looks like it is a Rosemount xmitter in the jpeg)
    '
    5 volt = Full or upper level limit
    1 volt = empty or lowest level limit.
    0 volt = Fault/loop open/loop shorted/24v Power supply dead/etc.
    '
    Use an "IF" "THEN" "GOSUB" to test the loop condition.
    " IF volt < 1 THEN GOSUB Fault
    '' IF mA < 4 THEN GOSUB Fault
    '''''''''ELSE ' Process the input
    '
    '
    1 to 5 volts is relative to 4 to 20 ma.
    '
    With a 250ohm resister in the 24volt loop,
    ''With a 4ma current flow in the loop,You should read 1 volt across the 250 ohm resister."
    ''With a 20ma current flow in the loop,You Should read 5 volt across the 250 ohm resister."
    '
    '

    Next You'll need a PID controller SUB routing
    '''Heres a link on PID control.....Its simple math,Don't let it throw Ya!
    http://en.wikipedia.org/wiki/PID_controller
    '
    '
    I can't remember what I used to go from volts to current to drive a current device. (to drive Your I/P converter)[ DAC ? ](mAmps out)
    '' I'll dig it up later,and post it.
    '
    '
    Get the input end working good and go from there.
    '''Take advantage of DEBUG .
  • jomjom Posts: 7
    edited 2010-11-16 11:13
    Ok So I set up the A to D Converter, and Used an Analog Multiplier that takes 1 to 5 volts and converts to 4mA to 20mA.

    How can I get the controller to recognize that when it reaches 5 volts as an input to stop the water valve?

    I tried using

    IF mVolts < 5 THEN
    HIGH 15

    but no luck.

    Any Suggestions?
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2010-11-17 17:45
    It is easier to use the raw data in a range than it is to use a fixed number

    You sould not use 5 volt like that because that is at the end of the range of the ADC chip

    IF result = some value THEN do something


    IF result < some value AND resut > some value THEN do something

    This is your best bet
  • ercoerco Posts: 20,255
    edited 2010-11-18 14:47
    Ain't nothing simpler than a toilet float and valve arrangement. If you can switch to some type of floating object tied via string/chain/cable to a microswitch above, you can have that switch your water valve or pump directly. Not sexy. No electronics. No Stamp, no Prop. No PID, no Kalman filter. No HiFi, WiFi, Hi-Def, Hi-Res, Bluetooth, Blackfin, Atmel, PIC,Win7. No StampPlot Lite, no Powerpoint. No Web server, no Ping sensor.

    Just bulletproof.

    Edit: Of course you could use a PING sensor looking down to measure the distance to the surface and use that data to trip your valve. Then you could add HiFi Wifi Bluetooth...
  • jomjom Posts: 7
    edited 2010-11-22 20:55
    This is what I have but it still wont send the voltage on pin 8 to turn the valve on according to the input voltage.




    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    '
    [ Program Description ]
    '
    ' 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.
    '
    [ I/O Definitions ]
    CS PIN 0 ' chip select (ADC0831.1)
    Clock PIN 1 ' clock (ADC0831.7)

    DataIn PIN 2 ' data (ADC0831.6)
    '
    [ Constants ]
    Cnts2Mv CON $139C ' x 19.6 (to millivolts)
    '
    [ Variables ]
    result VAR Byte ' result of conversion
    mVolts VAR Word ' millivolts
    v VAR Byte
    n VAR Word

    FOR n = 1 TO 199 STEP 9
    PWM 4, n, 40
    NEXT

    GOSUB reset
    GOSUB Main
    'GOSUB Valve
    GOSUB Display
    '
    [ Initialization ]
    Reset:
    DEBUG CLS, ' create report screen
    "ADC.... ", CR,
    "volts... "
    '
    [ Program Code ]
    Main:
    DO
    GOSUB Read_0831 ' read the ADC
    mVolts = result */ Cnts2Mv ' convert to millivolts
    DEBUG HOME, ' report
    CRSRXY, 9, 0, DEC result, CLREOL,
    CRSRXY, 9, 1, DEC mVolts DIG 3,
    ".", DEC3 mVolts
    PAUSE 100
    GOSUB Valve
    LOOP
    '
    [ Subroutines ]
    Read_0831:
    LOW CS ' enable ADC
    SHIFTIN DataIn, Clock, MSBPOST, [result\9] ' read ADC
    HIGH CS ' disable ADC
    RETURN

    Valve:
    v = 5 * result / 255

    IF(v = 0)THEN
    PWM 8, 100, 40
    IF (v > 0) THEN PWM 8,255,40
    ENDIF
    RETURN
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2010-11-23 06:34
    What happen when you change this line v = 5 * result / 255 to

    v = result / 255 ?

  • $WMc%$WMc% Posts: 1,884
    edited 2010-11-23 09:12
    jom wrote: »
    This is what I have but it still wont send the voltage on pin 8 to turn the valve on according to the input voltage.




    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    '
    [ Program Description ]
    '
    ' 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.
    '
    [ I/O Definitions ]
    CS PIN 0 ' chip select (ADC0831.1)
    Clock PIN 1 ' clock (ADC0831.7)

    DataIn PIN 2 ' data (ADC0831.6)
    '
    [ Constants ]
    Cnts2Mv CON $139C ' x 19.6 (to millivolts)
    '
    [ Variables ]
    result VAR Byte ' result of conversion
    mVolts VAR Word ' millivolts
    v VAR Byte
    n VAR Word

    FOR n = 1 TO 199 STEP 9
    PWM 4, n, 40
    NEXT

    GOSUB reset
    GOSUB Main
    'GOSUB Valve
    GOSUB Display
    '
    [ Initialization ]
    Reset:
    DEBUG CLS, ' create report screen
    "ADC.... ", CR,
    "volts... "
    '
    [ Program Code ]
    Main:
    DO
    GOSUB Read_0831 ' read the ADC
    mVolts = result */ Cnts2Mv ' convert to millivolts
    DEBUG HOME, ' report
    CRSRXY, 9, 0, DEC result, CLREOL,
    CRSRXY, 9, 1, DEC mVolts DIG 3,
    ".", DEC3 mVolts
    PAUSE 100
    GOSUB Valve
    LOOP
    '
    [ Subroutines ]
    Read_0831:
    LOW CS ' enable ADC
    SHIFTIN DataIn, Clock, MSBPOST, [result\9] ' read ADC
    HIGH CS ' disable ADC
    RETURN

    Valve:
    v = 5 * result / 255

    IF(v = 0)THEN
    PWM 8, 100, 40
    IF (v > 0) THEN PWM 8,255,40
    ENDIF
    RETURN
    '
    Have You check the voltage from the level sensor with the tank full at the ADC to see if it is 5volts?
    '
    If not you just need to calibrate the level senor loop.
    '
    You could increase the voltage into the senor to achieve 5volts at the ADC or just add and offset in Your software.
    '
    v = 5 * result / 255 + offset
  • jomjom Posts: 7
    edited 2010-11-23 09:13
    Changed v = result/255

    Voltage started changing but with an input of 5 volt it never reaches and output 5 volts.


    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    '
    [ Program Description ]
    '
    ' 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.
    '
    [ I/O Definitions ]
    CS PIN 0 ' chip select (ADC0831.1)
    Clock PIN 1 ' clock (ADC0831.7)

    DataIn PIN 2 ' data (ADC0831.6)
    '
    [ Constants ]
    Cnts2Mv CON $139C ' x 19.6 (to millivolts)
    '
    [ Variables ]
    result VAR Byte ' result of conversion
    mVolts VAR Word ' millivolts
    v VAR Byte
    n VAR Word



    GOSUB reset
    GOSUB Main
    'GOSUB Valve
    '
    [ Initialization ]
    Reset:
    DEBUG CLS, ' create report screen
    "ADC.... ", CR,
    "volts... "
    '
    [ Program Code ]
    Main:
    DO
    GOSUB Read_0831 ' read the ADC
    mVolts = result */ Cnts2Mv ' convert to millivolts
    DEBUG HOME, ' report
    CRSRXY, 9, 0, DEC result, CLREOL,
    CRSRXY, 9, 1, DEC mVolts DIG 3,
    ".", DEC3 mVolts
    PAUSE 100
    GOSUB Valve
    LOOP
    '
    [ Subroutines ]
    Read_0831:
    LOW CS ' enable ADC
    SHIFTIN DataIn, Clock, MSBPOST, [result\9] ' read ADC
    HIGH CS ' disable ADC
    RETURN

    Valve:
    v = result / 255

    IF(v > 0 AND v < 1) THEN PWM 8 ,51,40
    IF (v > 1 AND v< 2) THEN PWM 8, 77, 40
    IF (v > 2 AND v< 3) THEN PWM 8,102,40
    IF (v >3 AND v< 4)THEN PWM 8, 153, 40
    IF (v >4 AND v< 5) THEN PWM 8, 204, 40

    IF ( v = 5) THEN
    PWM 8 , 255, 40
    ENDIF
    RETURN
  • $WMc%$WMc% Posts: 1,884
    edited 2010-11-23 10:06
    Is Your code working now?
  • jomjom Posts: 7
    edited 2010-11-23 10:45
    Its working but the output Voltage never reaches 5 volts.

    Im using an LM358 in the output.
  • $WMc%$WMc% Posts: 1,884
    edited 2010-11-23 17:13
    jom wrote: »
    Its working but the output Voltage never reaches 5 volts.

    Im using an LM358 in the output.
    '
    Are You still using the Rosemount DP transmitter for the level senor?
    If so,Have You set the zero and span in the transmitter to match the tank levels?
  • jomjom Posts: 7
    edited 2010-11-24 13:05
    Rosemount is set to 0 at tank empty and 5 volts at tank full.
Sign In or Register to comment.