Shop OBEX P1 Docs P2 Docs Learn Events
Using BASIC stamp with electric solenoid valve, need help — Parallax Forums

Using BASIC stamp with electric solenoid valve, need help

peavlerkpeavlerk Posts: 4
edited 2012-11-27 07:25 in BASIC Stamp
Hi, my group and i are trying to build a water cooler that dispenses water using a motion sensor. The whole device has to be run using a Parallax BASIC Stamp 2, and we're having trouble figuring out how to program our water valve using the stamp. The valve is a EHCOTECH electric solenoid valve, 12V with 500mA. The BASIC stamp 2 can have a maximum of 5V running through it, so we're thinking we need a transistor.
Any ideas on how to program the water valve using the stamp, circuit models, or any general advice is welcome!
Also i can be reached by email at peavlerk123@gmail.com
thanks in advance!

Comments

  • Matt GillilandMatt Gilliland Posts: 1,406
    edited 2012-11-13 13:21
    PM
    Hi peavlerk, Welcome to the Forums :thumb:

    I'll let some of our Amazing Forumistas speak up on the how and the specifics, but you'll need to connect some sort of transistor to supply that much energy to your solenoid. I'm partial to a type of transistor called a MOSFET - I have piles of IRF511's in my junk drawer. The transistor will take the little signal (from the Stamp) and turn on the high-powered solenoid by connecting its three pins in a particular way....

    But there's lots of other methods too. Let's see what some of these are...

    -MattG
  • xanatosxanatos Posts: 1,120
    edited 2012-11-13 13:54
    Hi Peavlerk,

    You'll need a transistor and a relay to drive the 12v 500mA solenoid valve. There are actually a lot of ways, but here's one of the easiest. You can use any general purpose NPN transistor for the schematic I am using below. A 2N2222 would work perfectly. Be sure to include the diode across the relay coil since relay coils, when de-energized, have a big reverse-polarity spike that can kill your driver transistor otherwise.

    Connect your solenoid to the 12v supply through the contacts of the relay, and connect the base of the transistor to the Basic Stamp via a small resistor, like 220 ohms. You'll want to have the pin you connect to the transistor be LOW when you want the solenoid off, and HIGH when you want the solenoid on.

    Let me know if this was helpful and welcome to the forum! There's a lot of very helpful folks here who love this kind of stuff.

    Dave

    PS., Matt's MOSFET is a great suggestion as well, I'm just guessing that you're more likely to have a general purpose NPN transistor laying around than a MOSFET :-)
    500 x 300 - 35K
  • peavlerkpeavlerk Posts: 4
    edited 2012-11-14 16:51
    xanatos wrote: »
    Hi Peavlerk,

    You'll need a transistor and a relay to drive the 12v 500mA solenoid valve. There are actually a lot of ways, but here's one of the easiest. You can use any general purpose NPN transistor for the schematic I am using below. A 2N2222 would work perfectly. Be sure to include the diode across the relay coil since relay coils, when de-energized, have a big reverse-polarity spike that can kill your driver transistor otherwise.

    Connect your solenoid to the 12v supply through the contacts of the relay, and connect the base of the transistor to the Basic Stamp via a small resistor, like 220 ohms. You'll want to have the pin you connect to the transistor be LOW when you want the solenoid off, and HIGH when you want the solenoid on.

    Let me know if this was helpful and welcome to the forum! There's a lot of very helpful folks here who love this kind of stuff.

    Dave
    thanks for all the advice and the shematic of the circuit! However, we still are not sure of how to actually program the valve using the BASIC stamp 2. Any advice on how to do this would be greatly appreciated.
    Thanks again!
  • xanatosxanatos Posts: 1,120
    edited 2012-11-14 17:02
    Ah, ok... well, to start, do you have any experience at all with programming basic stamps? Do you have the Basic Stamp Editor? Do you have a programming cable? I'm not sure exactly how new you are to this, so if these are really dumb questions, I apologize.

    Assuming you have the above mentioned items and know how to use them, you'll want to set up the pin that you have connected to the driver for the solenoid in your program. Let's say you have the driver connected to pin 1, and you want to call it by the name "Valve" in your program. Your code would contain the following:
    ' -----[ I/O Definitions ]-------------------------------------------------
    
      Valve           PIN     1               ' output pin to valve driver circuit (2N2222)
    
    

    Now later in the program, you'd set your Valve to LOW when you want tthe valve to be off, and HIGH when you want it to be on. Here's a paracode example:
    IF (some parameter has been met for turning on the valve) THEN
         HIGH Valve
    ELSE
         LOW Valve
    END IF
    
    

    Let us know how familiar you are with PBasic, or if you're starting COMPLETELY from scratch, and we can go from there.

    Dave
  • peavlerkpeavlerk Posts: 4
    edited 2012-11-14 17:18
    xanatos wrote: »
    Ah, ok... well, to start, do you have any experience at all with programming basic stamps? Do you have the Basic Stamp Editor? Do you have a programming cable? I'm not sure exactly how new you are to this, so if these are really dumb questions, I apologize.

    Assuming you have the above mentioned items and know how to use them, you'll want to set up the pin that you have connected to the driver for the solenoid in your program. Let's say you have the driver connected to pin 1, and you want to call it by the name "Valve" in your program. Your code would contain the following:
    ' -----[ I/O Definitions ]-------------------------------------------------
    
      Valve           PIN     1               ' output pin to valve driver circuit (2N2222)
    
    

    Now later in the program, you'd set your Valve to LOW when you want tthe valve to be off, and HIGH when you want it to be on. Here's a paracode example:
    IF (some parameter has been met for turning on the valve) THEN
         HIGH Valve
    ELSE
         LOW Valve
    END IF
    
    

    Let us know how familiar you are with PBasic, or if you're starting COMPLETELY from scratch, and we can go from there.

    Dave
    Dave,
    We do have the cable to program the stamp, and we have used the Editor before, but only in a class, and even then it wasn't very extensive. Just to make sure that we understand completely, just assume that we have never used the editor, and I apologize for the inconvenience this may cause! So yes we have the required program and cable to do it, we just dont have the know-how.
    Thanks again for all the help you've already given, and for any future advice!
  • xanatosxanatos Posts: 1,120
    edited 2012-11-14 17:44
    ok... wow... let's begin.

    First, you'll need to start the Basic Stamp Editor. Once it's up, you'll have a blank screen that has a tab at the top called "Untitled1".

    So to get you started, copy and paste this code into that window:
    ' =========================================================================
    '
    '   File...... ValveControl.bs2
    '   Purpose...
    '   Author....
    '   E-mail....
    '   Started... 11-14-2012
    '   Updated... 11-14-2012
    '
    '   {$STAMP BS2}
    '   {$PBASIC 2.5}
    '
    ' =========================================================================
    '
    ' -----[ I/O Definitions ]-------------------------------------------------
    
      Valve             PIN     1               ' Connection to Valve Driver 2N2222
      Sense             PIN     2               ' Switch goes to 1 (HIGH) when pushed
    ' -----[ Variables ]-------------------------------------------------------
    
    ' -----[ Program Code ]----------------------------------------------------
      LOW Valve
    
      Main:
    
        do
          IF Sense = 1 then
            HIGH Valve
          else
            LOW Valve
          ENDif
        loop
    
        STOP
    
    

    Save that file as ValveControl.bs2, and load in (Control R) into the stamp (make sure your programming cable is hooked up and the stamp is powered on, etc).

    Now power down your stamp. Make and hook up the valve control circuit I showed you in the previous post. Be sure to hook up the base of the 2N2222, through the 220 or 330 ohm resistor, to IO pin 1 (actual pin 5 I believe, but check the manual. If you don't have the manual, in the editor, look at the top toolbar (File, Edit, Directive...), and click Help, then Basic Stamp Manual (PDF). There you'll find the whole BS2 universe opened up to yo ufor the asking.)

    Then, take a momentary pushbutton switch, hook up a 330 ohm resistor to IO pin 2 (probably pin 6 on the chip), one leg of the momentary pushbutton to the other end of the resistor, and the remaining leg of the pushbutton to your +5v supply line (the same +5v that supplies the stamp. *** Be careful - if you're using a board that it powered by a 9v battery, or an external power supply, use the Vdd line, not the Vin line!!! If in doubt, measure it with a meter!)

    You may also want to put a 10k resistor between the Sense pin (IO2) and Ground, to prevent a false high reading.

    Now once you've hooked up those connections, and you've checked it several times, carefully, slowly, and preferably by multiple people, then power your Stamp board back on.

    If you've hooked it all up correctly (and I haven't made any mistakes in my directions), the valve should activate whenever you push the button, and release whenever you let the button go.

    Let me know if that worked or not.

    Tomorrow, we'll either troubleshoot what went wrong (by the way, do all this AT YOUR OWN RISK. I'm not responsible if you cook your BS2, or yourself, or your neighbor's toaster...) -OR - if it works, we'll go over how to change it from a manual switch being pushed, to it reading whatever you're using for motion/proximity/distance sensors.

    This stuff - once you get the basics - is immensely fun. There is almost no limit to what you can get microcontrollers to do, and you'd be surprised at how fast you can come up to speed on these things, and the levels of complexity you can achieve in a short time.

    So take your time, be careful, pay attention to details.... and have fun. You're in for a fun ride!

    Let me know how you make out with the above instructions.

    Dave
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2012-11-15 10:09
    Based on the current draw of the solenoid valve and the gain of the 2N2222 @ 500mA I would recommend a Darlington Power Transistor such as TIP120 or even better yet a FET as Matt G had mentioned.
  • xanatosxanatos Posts: 1,120
    edited 2012-11-15 15:41
    Based on the current draw of the solenoid valve and the gain of the 2N2222 @ 500mA I would recommend a Darlington Power Transistor such as TIP120 or even better yet a FET as Matt G had mentioned.

    Actually, yes, that is kind of a stiff load for a 2N2222 now that I think about it!
  • ercoerco Posts: 20,256
    edited 2012-11-15 19:08
    I'm not EVEN going to mention a r.... rrr...

    ... but I will provide a link. :)

    http://www.radioshack.com/product/index.jsp?productId=2062478
  • xanatosxanatos Posts: 1,120
    edited 2012-11-15 19:49
    I actually did have a relay in that original 2N2222 circuit! Nothing wrong with relays! :-)
  • willembadwillembad Posts: 47
    edited 2012-11-16 17:01
    I'm a little late to this thread but I used an Echotech valve in this project http://forums.parallax.com/showthread.php?140526-Nitrox-Stick-Controller&p=1106810#post1106810
    It includes code and a schematic. Pin 7 is labelled Solenoid and all the code that reference that label either opens or closes the valve.
  • KyrkKyrk Posts: 1
    edited 2012-11-26 21:16
    Sorry for being late to the thread, but I'm his partner for the project. We've been having difficulties on how to program our Parallax PING))) sensor to where it opens the solenoid valve whenever it detects something from a certain distance while using the same solenoid valve code you posted. I saw multiple sample codes for the sensor, but wasn't sure which one to use or how to use it properly.
  • xanatosxanatos Posts: 1,120
    edited 2012-11-27 07:25
    Hi Kyrk,

    Not sure about the PING))) as I've never had need to use it. But... curiosity got the better of me, so... here's the first example code from the parallax store page for the PING:
    ' =========================================================================
    '
    '   File....... Ping_Demo.BS1
    '   Purpose.... Demo Code for Parallax Ping Sonar Sensor
    '   Author..... Jon Williams -- Parallax, Inc.
    '   E-mail..... jwilliams@parallax.com
    '   Started....
    '   Updated.... 11 JAN 2005
    '
    '   {$STAMP BS1}
    '   {$PBASIC 1.0}
    '
    ' =========================================================================
    
    
    ' -----[ Program Description ]---------------------------------------------
    '
    ' This program demonstrates the use of the Parallax Ping Sonar sensor and
    ' converting the raw measurement to English (inches) and Metric (cm) units.
    '
    ' Sonar Math:
    '
    ' At sea level sound travels through air at 1130 feet per second.  This
    ' equates to 1 inch in 73.746 uS, or 1 cm in 29.034 uS.
    '
    ' Since the Ping sensor measures the time required for the sound wave to
    ' travel from the sensor and back.  The result -- after conversion to
    ' microseconds for the BASIC Stamp module in use -- is divided by two to
    ' remove the return portion of the echo pulse.  The final raw result is
    ' the duration from the front of the sensor to the target in microseconds.
    
    
    ' -----[ Revision History ]------------------------------------------------
    
    
    ' -----[ I/O Definitions ]-------------------------------------------------
    
    SYMBOL  Ping            = 7
    
    
    ' -----[ Constants ]-------------------------------------------------------
    
    SYMBOL  Trigger         = 1                     ' 10 uS trigger pulse
    SYMBOL  Scale           = 10                    ' raw x 10.00 = uS
    
    SYMBOL  RawToIn         = 889                   ' 1 / 73.746 (with **)
    SYMBOL  RawToCm         = 2257                  ' 1 / 29.034 (with **)
    
    SYMBOL  IsHigh          = 1                     ' for PULSOUT
    SYMBOL  IsLow           = 0
    
    
    ' -----[ Variables ]-------------------------------------------------------
    
    SYMBOL  rawDist         = W1                    ' raw measurement
    SYMBOL  inches          = W2
    SYMBOL  cm              = W3
    
    
    ' -----[ EEPROM Data ]-----------------------------------------------------
    
    
    ' -----[ Initialization ]--------------------------------------------------
    
    Reset:
    
    
    ' -----[ Program Code ]----------------------------------------------------
    
    Main:
      GOSUB Get_Sonar                               ' get sensor value
      inches = rawDist ** RawToIn                   ' convert to inches
      cm = rawDist ** RawToCm                       ' convert to centimeters
    
      DEBUG CLS                                     ' report
      DEBUG "Time (uS)..... ", #rawDist, CR
      DEBUG "Inches........ ", #inches, CR
      DEBUG "Centimeters... ", #cm
    
      PAUSE 500
      GOTO Main
    
      END
    
    
    ' -----[ Subroutines ]-----------------------------------------------------
    
    ' This subroutine triggers the Ping sonar sensor and measures
    ' the echo pulse.  The raw value from the sensor is converted to
    ' microseconds based on the Stamp module in use.  This value is
    ' divided by two to remove the return trip -- the result value is
    ' the distance from the sensor to the target in microseconds.
    
    Get_Sonar:
      LOW Ping                                      ' make trigger 0-1-0
      PULSOUT Ping, Trigger                         ' activate sensor
      PULSIN  Ping, IsHigh, rawDist                 ' measure echo pulse
      rawDist = rawDist * Scale                     ' convert to uS
      rawDist = rawDist / 2                         ' remove return trip
      RETURN
    
    

    Now if it were ME, the first thing I'd probably do is just run the above code to see it work. If it did... I'd then add an IF-THEN statement, using inches as the reference, and say something like if inches <= 6, then HIGH pin-that-controls-relay-for-solenoid, ENDIF

    I'd also probably add a time-on hold function, so that if the relay gets triggered, it stays triggered for at least a second or something.

    I might also add a counter routine so that the PING has to report at least two polling cycles where inches is <= 6 before trigerring the relay, to help avoid false detections, and I might drop th ePAUSE from 500 to maybe 250.

    But there are a LOT of PING users on here with ACTUAL experience using these items, so they may have different and better advice... keep us posted!

    Dave
Sign In or Register to comment.