Shop OBEX P1 Docs P2 Docs Learn Events
Basic Stamp 2's IO Pin Keeps Going Low From High Randomly / How Can I Fix ??? — Parallax Forums

Basic Stamp 2's IO Pin Keeps Going Low From High Randomly / How Can I Fix ???

BCLITKEIBCLITKEI Posts: 11
edited 2015-01-11 13:46 in BASIC Stamp
Hello Basic Stamper's, I have a Basic Stamp 2's and my IO pin's keeps going from high to low randomly / how can I fix this if possible ??? The stamp basically has a real time clock connected to it and two relay control boards with 4 push button to set the time. the whole thing controls an AC and a Heater. The 4 four push buttons set the real time clock and after the time is set I reload the the code with the buttons being used to control the AC and heater on/off. Everything works perfectly the: auto on/off, the buttons, even a water sensor for AC, the problem is randomly, maybe 2 to 3 hours of being on it will just randomly shut off or turn on. I'am at work at the moment or i would upload the code with this post, I have tried all sorts of code for the buttons and i'am at the point of considering another method for my push button all together. This has also been affecting another project of mine as well in the same fashion but has not been a problem till this project. If anyone has any thoughts or similar experiences any help would be greatly appreciated. I have been trying to figure this particular problem over a year with no success, thanks in advance and help or comments would be greatly appreciated.

Comments

  • SapphireSapphire Posts: 496
    edited 2014-12-13 10:29
    Do you have a pull-down or pull-up resistor on each pin? Just connecting it to a pushbutton will cause this to happen, when the switch is open.
  • BCLITKEIBCLITKEI Posts: 11
    edited 2014-12-13 10:39
    I have it set up active low for all four push button ( IO pin to 220r + 10 k to 5v vdd with button connecting to vss / ) pin is high till button pressed bringing it low.
  • SapphireSapphire Posts: 496
    edited 2014-12-13 10:44
    Okay, so you do have pull-up resistors, the 10Ks to Vdd.

    What about that relay board, how is it connected? Do you have a flyback diode across each coil? How are you powering your project and the relays? Relays can be noisy, and introduce spikes on the power and ground lines, which "might" cause inputs to change.

    Do you have a schematic?
  • BCLITKEIBCLITKEI Posts: 11
    edited 2014-12-13 11:02
    I don't have a circuit at the moment I have been adding to the circuit at I go, I can attempt to write one this evening and attach some pictures.The relay boards are connected as such (IO pin to 2N4401 switching transistor with non switched pin to vvs on stamp and switched pin to ground on coil, + of coil on relay is connected to 9v on stamp with diode connected across coil). Side note before adding push buttons I had the realtime clock turn AC/Heater on for several months without this issue and buttons were still in the circuit. It wasn't till adding the code for the button to turn off the heater/AC that this problem began. The stamp is connected to power supply 12v not sure on the amps of top of my head. On the other project I mentioned having this issue with, I added a voltage regulator to help correct the voltage but this did not fix the problem.
  • BCLITKEIBCLITKEI Posts: 11
    edited 2014-12-13 17:40
    Here is a quick diagram that I drew up really quick, I will add code as soon as a I get home.
    968 x 542 - 34K
  • SapphireSapphire Posts: 496
    edited 2014-12-13 18:07
    The only thing I see so far is you should have a resistor between the I/O pin and base of the 2N4401 transistor, maybe 1k.

    We'll see what the code reveals.
  • BCLITKEIBCLITKEI Posts: 11
    edited 2014-12-13 23:09
    Thanks, I will try what you recommend hopefully to it will remedy my issue. I have attached a copy of my code as well and tried my best to add understand able comments.
  • SapphireSapphire Posts: 496
    edited 2014-12-14 00:03
    At first glance, I think the problem is the BUTTON command. You use the same BTNWORK variable for multiple buttons. This won't work. BUTTON was designed to run in a loop (constantly being called) and the work variable must be unique for each BUTTON (i.e. pin) that you monitor. By re-using the same BTNWORK variable between different buttons, it is getting confused.

    In reality, the BUTTON command is not very useful. It doesn't debounce (as claimed in the stamp manual) and takes a lot of program space and variables. Instead, it is easier to write one subroutine to check all your buttons, and put them in a single variable (one bit per button). If all your button pins are sequential, and in one of the four pin groups 0-3, 4-7, 8-11, 12-15 then all you need are a nib to hold the current states. Calling the subroutine will read the pins, and update the states. Then anywhere in the program you can get the current button state by reading a variable.
    btn VAR Nib
    
    read_buttons:
     btn = INB  ' get current state for pins 4-7
     return
    

    You can use btn.bit0, btn.bit1, btn.bit2, etc to check just one button.
  • BCLITKEIBCLITKEI Posts: 11
    edited 2014-12-14 23:34
    Thank you very much for all of your help so far, I have not had a chance to add resistors to the I/O pins connected to the transistors but I will as soon as I get a chance. I did happen to update my code with the new code you provided and have everything back to operational status, I just need to wait several hours to see if the glitch occurs lol. I did have one question about the code its self for future reference, if I wanted to use say pins 0-3 would I put btn= INA and if I wanted to used pin 8-11 would I put btn=INC and save it to a different variable or would they all be INB just a different variable ? And another question, does it have to be four pins or could it just be pins 1 and 2 as long as the pin were sequential ? I will also attach a copy of my new code and will let you know if this did or didn't correct my problem. Thanks again for all the help.
  • SapphireSapphire Posts: 496
    edited 2014-12-15 13:44
    You would use INA for pins 0-3, INB for pins 4-7, INC for pins 8-11, and IND for pins 12-15. Four works out nice because of the Nib variable, if you have less, still use Nib, but some bits go unused. If more than four, then use INL for pins 0-7 or INH for pins 8-15 and a Byte variable. If only one, then a Bit variable would work, and the input would be IN# where # is the pin (0-15).
  • BCLITKEIBCLITKEI Posts: 11
    edited 2014-12-22 13:27
    Thank, I really appreciate the advice I will use this code in future programs. Unfortunately the new code or adding the 1k resistor from IO to transistor did not correct my issue, sorry It took me a couple days to write back, I wanted to run a few more test to rule out the fact it might me browning out and resetting but this does not appear to be the case either. It would seem that after about 2 - 3 hours ( sometimes way longer sometimes way shorter ) IO PIN0 goes from high to low. As you could probably see in the program, after pressing button on PIN6 , IO PIN0 is told to go HIGH and the programs continues on ( heater is now on). Now that PIN0 is HIGH it will enter another subroutine for a couple MS to check to see if the time matches the shut off time for the heater (on PIN0) and if it matches will shut off, or will shut off if button (on PIN6) is pressed. If IN0 is LOW it will skip the heater off subroutine all together (because heater is off ). Its when the heater is on and entering the heater off sub that the error seems to be occurring. The only commands to turn off heater (PIN0 LOW ) is the button command on PIN6 and the timed heater off command. I need to do a few more test but I can not tell if PIN0 is just changing state and was not due to a command, or if it is was changing state due to the command following the press of button on PIN6, meaning the state of PIN6 is randomly changing. Either way one is randomly going LOW since both would have been in a HIGH state beforehand. I cant find any reference to random state changes or if the basic stamp can forget the state a PIN is in after so much time without a command ( Example if I wrote a program that brings PIN0 HIGH and then entered a DO - LOOP with no commands how long would PIN0 stay HIGH ? ). Thanks again, any help is appreciated.
  • SapphireSapphire Posts: 496
    edited 2014-12-22 13:41
    A pin will stay in its programmed state until another command changes it. So if you bring PIN0 high and never again set it low, it will stay high. I notice there are 3 places in your routine to set PIN0 low, and one is just after testing "result" which is the result of an RCTIME statement. Are you sure this isn't the problem? RCTIME may be returning non-zero. You might want to add a DEBUG just before each LOW 0 command so you know which one turned it off.
  • BCLITKEIBCLITKEI Posts: 11
    edited 2014-12-22 15:20
    Thanks that is good to know. I suppose that is possible the "result" variable is used to tell if the AC is on or off. The AC is its own unit so I soldered wire form the power led to the stamp ( + to IO8 and GtoG ) and hoped it would register in the debug as a 1 ( DEBUG ? IN8 ) but instead I would get (0001011011) or random combination. I remembered RCTIME measured the time between state change and returned a value from 1 to 700 or so depending on setup and a 0 if no state change was detected. So basically if the AC was turned on the "result" would be greater than > 1 ,and if off the "result" would be 0. So when the AC is turned on ( even form the ac remote ) it will switch into the AC off subroutine and also if the heater had been on ( HIGH 0 )it would then shut it off ( LOW 0), its seems to be working as expected. When I revised the code to fix the buttons I and added some debugs, including ones for "result". From what I have observed the "result" stays a 0 till the AC is activated ( which is not in use due to the fact its winter), I tried to watch for as long as possible and hopefully observe the glitch but it does not happen for hours. I would like to mention that I'm having this exact same problem on another project, a door entry system it has 2 buttons 2 lights and a relay to deactivated door, there is a lot less code and no external devices minus the maglock and I'm having the same issue. The relays and the buttons are setup in the same fashion, and the unit is at another location one town over so I don't believe its the building 120v. So if the "result" variable was the issue it would not help figure out this issue on the door entry, which I believe the issue are one in the same but I could be incorrect in my assumption. When I get home from work I will upload the code for the entry system and the updated code for the AC/Heater and run a few more test and return my finding. If you have any thought or suggestions they would be greatly appreciated, thanks again
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-12-23 09:07
    I'm not clear on exactly what you're trying to do, however one method I have employed for AC is to use an optocoupler. Back in the late 80s I used an opto on the transformer to pick off the 60 Hz signal to drive the NMI line on my Z80 for an RTC. On the BASIC Stamp you could use the COUNT command for a very short duration to determine if there are any pulses on the line to detect the presence of AC. This has the advantage of keeping the AC isolated from the BASIC Stamp Module and since the output is pulled high, you shouldn't get erroneous readings when there is no AC signal.
  • BCLITKEIBCLITKEI Posts: 11
    edited 2014-12-23 10:57
    Thank for all your help, the LED light I tapped into is off the button panel of the AC and the panel is 12v with the LED light measuring less then 1 volt. I believe this is why it does not completely register on the basic stamp, but this has not been giving me an issue it is actually work as designed. After running some test last night, I determined that PIN0 powering the circuit for the relay is chaining state from HIGH to LOW. I originally thought that it was related to my button circuit or code but after adding END commands after certain sub routines I determined it is not that. Then as sapphire had recommended I checked to see if the "RESULT" variable was causing the issue. The "result" variable tells the basic stamp that the AC is on, and if the AC were to be turned on while the heater was on it would turn off the heater. So that got me thinking maybe this was the issue but after removing the LOW0 command the problem still persists. So basically I removed all the LOW0 command and the PIN0 still goes LOW after so many hours with no command. The only thing I can think is has something to do with the transistor (but I'm just assuming at this point) I've had this same problem, with 3 different projects. 1 I never resolved and went another route , and 2 I'm currently trying to fix. The first project just used the transistor ( no relay ) and the other 2 have transistors and relays. I even thought maybe the stamp was browning out but I added a button command to not start main loop till button press, just in case it reset I would be able to see, but this was also not the case. At this point I'm baffled ?
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-12-23 11:04
    On the BASIC Stamp Modules a high is registered as being above 1.4V, so if you have a 1V signal that isn't going to be high enough to be considered a logic high.
  • BCLITKEIBCLITKEI Posts: 11
    edited 2014-12-23 11:17
    No i have not, I'am unfamiliar with a logic high do you have a reference so can familiarize myself. I don't believe this is related to my other issue though, this part of the circuit seems to working fine minus some extra code. My main problem is the PIN 0 changing state without command on this project and my other project. The other project is just 2 buttons 2 led and 1 relay control circuit connected to a maglock. It is a door entry system and is not connected to any other circuits and it also has the same issue.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-12-23 11:41
    Glancing back through I wasn't clear what P0 is connected to. Do you have a schematic diagram?
  • BCLITKEIBCLITKEI Posts: 11
    edited 2014-12-23 12:34
    Yes, sorry its not the best. also here is a copy of my code, and I have since added a 1k resistor to PIN 0 and PIN 3 to the transistors.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-12-23 17:13
    Okay, so in the diagram it seems P0 is a yellow connection shown going from the BASIC Stamp to another device. But it doesn't indicate what the device is or how it connects. Then it trails off to a black dot. I am unsure what the black dot on the device is. I think others may have mentioned that if the pin is floating then it will randomly toggle based on RFI/EMI radiation in the air. If the pin is connected to a device that it only active in one state, then in the inactive state it may be floating. I'm afraid this doesn't really help to explain what P0 is doing. I was expecting a more traditional schematic. =)
  • MrBi11MrBi11 Posts: 117
    edited 2015-01-11 13:46
    Quick scan of this thread and I see you're getting the 'AC' power on status from another devices control panel 'LED' is that correct?

    If so, are you sure that LED is just 'on' or 'off' and not multiplexed.
Sign In or Register to comment.