Shop OBEX P1 Docs P2 Docs Learn Events
Newbie IO Question on BS2 — Parallax Forums

Newbie IO Question on BS2

savagesavage Posts: 3
edited 2007-08-05 23:19 in BASIC Stamp
Hello fellow members, total newbie here trying to get better understanding of how the stamp chip's IO works, specifically the input.
(I have BS2 on a Board of Education)

If I drive +5VDC from the VDD on the board of education through a 10K resistor and into PIN0, it will set the IN0 to a value of "1".· However, when I remove the +5VDC from PIN0, the IN0 remains with a value of "1" and does not reset to "0" unless I ground out PIN0.·

Is this the expected behavior?· If so, how do I write my program to properly monitor inputs?· My sample program is below...

' {$STAMP BS2}
' {$PBASIC 2.5}
 
' -----[noparse][[/noparse] (I/O) Output Definitions ]------------------------------------------
 
SENSOR1     PIN     0    ' (I/O) Sensor

' -----[noparse][[/noparse] (Program Variables Definitions ]------------------------------------
 
SENSOR1_LastKnownState VAR     Byte

' -----[noparse][[/noparse] (Program Initialization ]-------------------------------------------
 
INPUT SENSOR1
 
' -----[noparse][[/noparse] (Program Mainline ]-------------------------------------------------
 
Mainline:
 
  ' check for changes from last known state
  IF (SENSOR1 <> SENSOR1_LastKnownState) THEN
 
    ' display new status
    IF (SENSOR1 = 0) THEN DEBUG "SENSOR1 IS <OFF>",CR
    IF (SENSOR1 = 1) THEN DEBUG "SENSOR1 IS <ON>",CR
 
    ' debug
    DEBUG "SENSOR1 STATE: ",BIN1 SENSOR1,CR
 
    ' syncronize
    SENSOR1_LastKnownState = SENSOR1

  ENDIF
 
  ' slow things down a bit
  PAUSE 100
 
  ' infinite mainline program loop
  GOTO Mainline
 
EndProgram:
  END

Thank You,
· ...Savage...

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-05 16:52
    An input pin is a high impedance and is voltage sensitive. Typically, any voltage over roughly 1.6V will be seen as high and anything under that will be seen as low. If the input is unconnected, the voltage it sees is determined by static electricity, by moisture on the surface of the board, by the nearby electrical fields, etc. The general engineering practice for unused inputs on a CMOS device (which most ICs are these days) is to connect them through a high value resistor (maybe 10K or more) to either ground or the supply voltage (+5V here).

    The reason for using the resistor is that these pins can also be used as outputs and you might accidentally program them as such. If so and the output state is opposite from what the pin is connected to ... you'll burn out the I/O pin circuitry on the chip. The resistor prevents the damage by limiting the current.
  • savagesavage Posts: 3
    edited 2007-08-05 17:55
    Mike, thanks for the response. I have the 10K resistor inline on the input pin to help protect the stamp chip. Is there something else I need to add?

    Maybe I would be better off further explaining what I am trying to do and perhaps there is a problem with my circuit. I would like to use a magnetic contact closure sensor such as are found in alarm systems to control in input on the stamp (PIN0). Then I can perform whatever programming logic when the switch is closed/opened.

    [noparse][[/noparse]+5VDC]
    >[noparse][[/noparse]Contact Closure Switch]
    >[noparse][[/noparse]10K Resistor]
    >[noparse][[/noparse]Stamp Input Pin (IN0)]

    Can I simply control the IN0's HI/LO state by sending the +5VDC like this?
    The issue I am seeing is that PIN0 will go HI, but will not return to LOW when the voltage is removed.

    So you said that when the PIN is open, it can pickup ambient voltage from moisture, etc, what do I need to add to prevent this and force the PIN LOW when the contact closure switch is open?

    Thanks,
    ...Savage...
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-08-05 18:09
    Hi savage, an input that is not connected is considered to be "floating" and it's logic level undetermined. Typically you would have a pull up or pull down resistor to hold your input at +5v (pull up) or 0v (pull down). In your instance you need to hold pin 0 at 0v (pull down) while your contact is open. Its a simple procedure of adding an additional 10k resistor from pin 0 to ground. When your contact closes the voltage at the juction of the two resistors will be a logic1 while it is open the pin will be at logic0.

    Jeff T
  • savagesavage Posts: 3
    edited 2007-08-05 23:19
    Jeff,

    Thanks! That did the trick. I have a background in software, not hardware, so I'm still learning basic circuit logic smile.gif

    Thanks again... and also thank you Mike for helping shed some light,
    ...Savage....
Sign In or Register to comment.