Shop OBEX P1 Docs P2 Docs Learn Events
Pin comparision — Parallax Forums

Pin comparision

NosePickerNosePicker Posts: 54
edited 2008-08-13 00:04 in BASIC Stamp
I have a small project where I want to know anytime one of my input pins changes from high to low or low to high.
Which pins I used is not important so if it will be easier to use different pins I can.
However if I understand this right it looks like it will be best to use pins 0 - 7.
Can someone look at the following code and tell me if that will work?
The assumption is the line (LastValue = INL) will assign a value not a pointer.

' {$STAMP BS2}
' {$PBASIC 2.5}
' Sets pins 0 - 7 as input
DIRL = 0
LastValue VAR Byte
' INL = the byte for the combined pins 0 - 7
LastValue = INL
MainLoop:
· IF (INL <> LastValue) THEN
··· LastValue = INL
··· DEBUG DEC LastValue
· ENDIF
· PAUSE 5
· GOTO MainLoop

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Thanks

NosePicker

Comments

  • FranklinFranklin Posts: 4,747
    edited 2008-08-04 21:13
    I'd load it and see. That's the only way you are going to learn.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • NosePickerNosePicker Posts: 54
    edited 2008-08-04 23:04
    I will but I can't complete my circuit until I can find a website that sells a comparator like the MCP6044.
    Does anyone know where I can get those from?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Thanks

    NosePicker
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2008-08-05 03:43
    http://search.digikey.com/scripts/DkSearch/dksus.dll?lang=en&site=US&keywords=MCP6044&x=16&y=20

    It's an op-amp, not a comparator specifically.· But, anyway.
  • ercoerco Posts: 20,256
    edited 2008-08-05 17:51
    NP: You may not need a comparator if you just want low/high reference. The Stamp does that very well; its threshold between low and high is ~1.4 volts. Per my email, you probably only need a few resistor voltage dividers to condition your signal to this level. Remember the KISS principle: Keep It Simple, Simon!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"If you build it, they will come."
  • NosePickerNosePicker Posts: 54
    edited 2008-08-06 00:57
    Well I am trying to detect a signal that when it is "on" ranges between 4v and 1v when it is off it will be 0v. Someone on here recommended I use the MCP6044 which hopefully will be able to help me detect the voltage when it is below the 1.4v threshold of the stamp.

    I searched digikey and they have so many different models. Do I just need to read the datasheet for all of them to figure out which will do what I need?
    I will need 7 separate (? detections) so it looks like 2 quads should do the trick.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Thanks

    NosePicker
  • metron9metron9 Posts: 1,100
    edited 2008-08-06 01:18
    There is i think a 25 dollar minimum at digikey. Mouser or 4 of the lm358's from the parallax store will work as well.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think Inside the box first and if that doesn't work..
    Re-arrange what's inside the box then...
    Think outside the BOX!
  • ercoerco Posts: 20,256
    edited 2008-08-06 02:31
    There's always the trusty LM339 quad comparator, even Radio Shack stocks those...
    http://www.radioshack.com/product/index.jsp?productId=2062593&cp=

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"If you build it, they will come."
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2008-08-06 02:47
    Nosepicker,
    Any chance this range of voltages is actually the result of PWM and different duty rates?
  • metron9metron9 Posts: 1,100
    edited 2008-08-06 13:22
    Another idea i had and just tested to monitor a voltage lower than 1.4V down to perhaps 1/4 volt without using an op amp or comparator.
    Using a small inductor and a 220 ohm resistor I am able to create a voltage spike by taking the input pin LOW-Output and then changing it back to input. A voltage spike (attached scope output) shows a 2.6V spike when fed 950mV.· The stamp however (attached program) is not fast enough to catch the spike as it is only above the 1.4V threshold for 200ns.· A microchip with an interrupt on pin change for example could detect this short of a pulse. Then again, perhaps the stamp could detect it using pulsin?
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    ' {$PORT COM1}
    

    spike VAR Byte
    i VAR Byte
    INPUT IN0
    

    main:
    IF IN0 = 1 THEN
     LOW 10
    ELSE
    

     LOW 0
     PAUSE 10
     INPUT IN0
    

     spike=0
    

     FOR i=1 TO 100
      IF IN0=1 THEN
       spike=1
       EXIT
      ENDIF
     NEXT
    

     IF spike=1 THEN
      LOW 10
     ELSE
      HIGH 10
     ENDIF
    

    ENDIF
    

    GOTO main
    

    
    
    
    
    
    
    
    
    
    
    
    
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think Inside the box first and if that doesn't work..
    Re-arrange what's inside the box then...
    Think outside the BOX!
    511 x 466 - 8K
  • NosePickerNosePicker Posts: 54
    edited 2008-08-06 21:57
    Hey erco,

    I also thought the LM339 would do it but I purchased some and after reading the datasheet and testing with them on my breadboard, it appears when the comparator detects a difference in the voltage for the + and - input, it simply connects the output pin to ground. So to try this, I connected an LED and 220ohm res from my +5v supply to the output pin of the comparator. I then connected a 12v battery + and - to the input pins. And the LED would turn off and on as I would connect and disconnect the + side of the battery.

    So would I have to basically replace the LED with the stamp pin?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Thanks

    NosePicker
  • ercoerco Posts: 20,256
    edited 2008-08-08 01:53
    NP: Check the datasheet on the 339 (probably the same as all comparators) to see if the 339 supply voltage must be as high or higher than the input signal voltage, I believe that's the case. If so, your whole comparator circuit will have to be powered by 12 volts, and then you'll take the 12-volt output (0-12 volts) and step it down to 5 volts (for Stamp input) with a voltage divider. That comparator is active low, so the output is at 12 volts then gets pulled down to ground when the comparator turns on. A bit funky. Do you have a circuit showing what you're trying to do?

    IMHO your best option is to use a voltage divider to reduce your input signal from 12 volts down to less than 5. Then your comparator and all your circuitry will operate at stamp voltage and everything is easier. Can you do that?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"If you build it, they will come."
  • NosePickerNosePicker Posts: 54
    edited 2008-08-09 03:43
    >>>>>>>> IMHO your best option is to use a voltage divider to reduce your input signal from 12 volts down to less than 5. Then your comparator and all your circuitry will operate at stamp voltage and everything is easier. Can you do that? <<<<<<<<<<<

    Yes I can but we are losing sight of what I am really trying to accomplish. I was just using the 12v battery to test something. My real situation is connecting a device that will produce anywhere from 4v down to about 1v which is below the threshold of the stamp to detect. (Read my 3rd post from the top) Because the voltage drops below the threshold is why I am trying to use either a comparator or the Op Amp to detect the signal.

    But for my "test" you are right I need the input to be = or < the supply voltage.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Thanks

    NosePicker
  • ercoerco Posts: 20,256
    edited 2008-08-10 18:12
    OK, as long as your circuit is powered by 5 volts, not 12, then you can still use the LM339 quite easily. Have a look at http://www.elecfree.com/electronic/low-voltage-battery-monitor-by-lm339/ to see a circuit that uses one of the four comparators. Power·your·circuit from 5 volts. Delete R1 and the zener diode, and feed your varying voltage signal into pin 4. R2 is an adjustable voltage divider used to set your reference voltage. You can delete the buzzer, but leave the LED & resistor (but change from 1K to 220 ohms), you need those. Just attach the comparator output (pin 2 shown here)·to your Stamp's pin input. When the LED is glowing, the comparator output is low, and vice versa. BTW, you should ground the pins of all unused comparators (four seperate comparators in the 339).


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"If you build it, they will come."
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-08-10 18:34
    A simple transistor switch will do what you're trying to accomplish (schematic attached). The transistor will conduct on any input voltage above about 0.8V, pulling its output low (i.e. acting as an inverter).

    -Phil
    380 x 227 - 2K
  • ercoerco Posts: 20,256
    edited 2008-08-11 01:21
    Sheer genious, PhiPi! You win the "simplest is best" award. NP, this is pure gold: verify that your 'on' signal is at least 0.7-0.8 volt and you're golden.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"If you build it, they will come."
  • NosePickerNosePicker Posts: 54
    edited 2008-08-13 00:04
    I was wonder if the 2 resistors that connect to the device are really needed.

    It looks like you are creating a voltage divider.

    Considering the input voltage from the device is guaranteed to be between say 4v and 0.8v when on and 0v when off would I need those resistors still?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Thanks

    NosePicker
Sign In or Register to comment.