Maker Pro
Maker Pro

Toggle switch keyboard emulator

falcon60

Apr 25, 2010
13
Joined
Apr 25, 2010
Messages
13
I am building a circuit intended to use a toggle switch to emulate a keystroke. I'm using a capacitor, a resistor, and a 5v 250 ohm reed relay connected to an old PS/2 keyboard card. My power source is a 6 volt 500 mA charger. I've tried numerous capacitor and resistor combinations and always get a consistent single keystroke on the discharge side but NEVER get a consistent single keystroke on the charge side; I get zero, one, two, and three keystrokes. What am I missing? How can I get consistency on the charge side? I expected it to be the same either way. Please help! Thanks.
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
Perhaps you should show us the circuit diagram. What do you want to achieve, a single keypress on each change of toggle switch position?
 

falcon60

Apr 25, 2010
13
Joined
Apr 25, 2010
Messages
13
This isn't my diagram but it's basically what I'm building (except I've added a resistor to control the caps charge/discharge time). I've tried 47uf, 100uf, and 220uF capacitors and a wide variety of resistors yielding a variety of time constants from 15ms to 1s. Nothing seems to give me a consistent single keypress on the charge side although I get a single keypress on discharge over 95% of the time. Even stranger (to me) is that there doesn't seem to be a pattern...I get zero, one, two, or three keypresses almost randomly. Shouldn't charge and discharge yield the exact same result?

Yes, I want a single keypress with each flick of the toggle.
 

Attachments

  • fig2.jpg
    fig2.jpg
    7.1 KB · Views: 424

falcon60

Apr 25, 2010
13
Joined
Apr 25, 2010
Messages
13
Correction...my shortest T is 58 ms. But using a 47uF cap and no resistor (the relay is 250 ohms), I get 58 ms and 50% reliability on charge AND discharge.
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
The problem is probably that the closing and opening of a relay produces come amount of contact bounce leading to more than one keypress.

One possibility is placing a small (0.1uF) capacitor across the relay contacts
 

55pilot

Feb 23, 2010
434
Joined
Feb 23, 2010
Messages
434
What exactly are you trying to do? Is your circuit actually how you have wired it?

The way you have it set up, when you flip the switch to the battery, the current flows through the relay and charges the cap, engaging the relay. As the cap is charged, the current falls to zero and relay disengages. When you flip the switch, the cap discharges through the relay, once again energizing the relay. As the cap discharges, the current falls to zero and the relay dis engages. Since this is a LC circuit, it may actually ring, which is where the multiple key closures are coming from.

Just take out the cap, add a diode across the relay coil (the pointy end towards the battery) and you should be fine.

---55p
 

falcon60

Apr 25, 2010
13
Joined
Apr 25, 2010
Messages
13
I'm attempting to get a single keystroke from opening and then a single keystroke from closing the switch (for a home-built flight sim cockpit). I get a single keystroke on the discharge of the cap but multiple keystrokes on the charge. I can't figure out why it's not the same on both.

I'm quite an amateur so I'm not sure what you meant by "Since this is a LC circuit, it may actually ring". How will the diode help?

Thanks for your input. This is a great site...glad I found it.
 

Laplace

Apr 4, 2010
1,252
Joined
Apr 4, 2010
Messages
1,252
You need to search on the words "contact debounce". For instance: http://www.labbookpages.co.uk/electronics/debounce.html
Interesting phenomenon that the charge and discharge cycles behave differently. But there are multiple proven methods for dealing with contact bounce. I would have thought that the keyboard interface should already have employed some such method.
 

55pilot

Feb 23, 2010
434
Joined
Feb 23, 2010
Messages
434
I'm quite an amateur so I'm not sure what you meant by "Since this is a LC circuit, it may actually ring". How will the diode help?
You are mixing two things I wrote. Let me explain them separately.

The relay coil is an inductor. When you run current through it, the inductor stores energy. When you stop forcing current through it, it releases the energy by continuing the current flow. A capacitor also stores energy. In a LC circuit (L=inductance, C=Capacitance) you can have a situation where the capacitor transfers its energy to the inductor, which transfers it back to the capacitor, which transfers.... When this happens in resonance, you get an oscillator. Otherwise, the back and forth may happen a few times at decreasing amplitude before its stops. That is called ringing. Like hitting a bell and it keeps producing a sound for some time after you hit it. It is the same phenomenon in the mechanical world.

The diode is needed if you have just a battery and relay. When you disconnect the switch, the current in the relay coil wants to keep flowing (inductor releasing energy). This creates a condition called "flyback" which can damage the relay or the switch. To suppress the flyback voltage and provide a path for the current, you need the diode.

The reason you are getting double bounce is due to ringing. Another issue that you need to consider is that when you discharge the cap, the inductor is most probably making its output negative. If you have a polarized cap, it is not liking it one bit. The cap is going to fail a lot sooner than you are expecting.

The right way to do it is with a micro. You can either use a PicAxe or a straight PIC or one of about a dozen other micros and use it to control the relay. Or you can get a PIC or some other micro that has a USB port and make it look like a keyboard. I know that Microchip has reference design and reference code to do that for the PIC. I suspect so do all other micro manufacturers. You plug your device into the PC and the PC will think that you have installed a second keyboard and will accept keypresses from that keyboard. The down side is that you are looking at A LOT more learning on your part. The up side is that once you have that figured out, there is A LOT that you can do to enhance you cockpit (notice the greater emphasis on the second "a lot").

---55p
 

55pilot

Feb 23, 2010
434
Joined
Feb 23, 2010
Messages
434
I would have thought that the keyboard interface should already have employed some such method.
The "standard" for PC keyboards is to consider about 4 mS of continuous contact as a closure and 2 mS continuous no-contact as release. He is dealing with much longer time-frames.

---55p
 

falcon60

Apr 25, 2010
13
Joined
Apr 25, 2010
Messages
13
Okay..the fog is beginning to clear. I'll shorten the time constant and read up on ways to eliminate the ring and/or bounce. Will a diode between the cap and the relay accomplish this? You all are very generous with your knowledge...thank you so much.

By the way, I'd actually much prefer to accomplish all this with a microcontroller but I want to make this circuit work too...so I can say I did it (and learn from it as I am now). I realize this will require additional learning but I'm up for it. Any direction you can give me toward this end is appreciated. Is there a particular programming language you would recommend that I start with? Is there a particular type of micro (or keyword) I should search for?

Thanks again!
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
55p has provided you with many useful tips already.

I will add just one more. The relay would not be my preferred choice of device. I would look at using an optocoupler. They are smaller, make no noise, and require less power.

I also recall that someone was after a circuit that would trigger one twice, once when a button was pushed, and again as it was released (very similar to you) -- let's see if I can find it...

ok, here it is:

I didn't have a CAD program installed so I just jotted a diagram on paper and scanned it.

Note firstly that there is an error in the circuit as presented in this post -- read along further to see the correction.

That is set to switch different buttons as the power is applied, then removed. You can either connect both phototransistors in parallel of use a package that has bi-directional diodes.

If you plan on making several of these things, less clattering relays would be a good thing. You may be able to get the power usage down to a point where the whole thing could be powered from the batteries in your wireless keyboard (should you even wish to).

You will have far less to worry about with contact bounce and ringing since there is no huge inductor in this circuit.
 

55pilot

Feb 23, 2010
434
Joined
Feb 23, 2010
Messages
434
Okay..the fog is beginning to clear. I'll shorten the time constant and read up on ways to eliminate the ring and/or bounce. Will a diode between the cap and the relay accomplish this?
Maybe the fog is not really beginning to clear! Neither shortening the time constant nor a diode is going to help. The circuit is inherently unstable. You may be able to get it to work but it will not stay working as soon as the temperature or the environment changes even a bit.

Besides software, Steve's idea of using an optocoupler (also called optoisolator) is the only one that has a hope of working. You can use your original setup but replace the relay coil with 4 diodes arranged in a bridge, driving an optocoupler's LED as the load. You will need a current limiting resistor in series with the LED.

When the switch connects the battery to the cap, the bridge will cause the current to flow through the optocoupler's LED while the cap is charging. This will activate the optocoupler and the keyboard will see a switch closure. As the cap gets charged, the current will decay and optocoupler will turn off and the keyboard will see a release.

When you flip the switch, the bridge will allow the cap to discharge through the optocoupler's LED, once again activating the optocoupler. The keyboard controller will once again see the switch closure that will last till the current in the LED decays.

By paying with the LED resistor and capacitor values, you should be able to get the switch to do what you want it to do. I have not recommended any values because the values will be specific to the particular optocoupler you are using.

---55p
 

Attachments

  • Clipboard01.png
    Clipboard01.png
    2.3 KB · Views: 313
Last edited:

falcon60

Apr 25, 2010
13
Joined
Apr 25, 2010
Messages
13
Okay...the fog did clear...only to reveal a mountain in my path. But that's okay, I'm determined to learn this. I've read everything I could find on-line but have obviously underestimated my little project...so back to the drawing board...and the library.

I initially considered using an optocoupler but (foolishly) decided a relay would be easier...plus they were available at Radio Shack! I'll try the circuit you recommended (thank you) as soon as I can get an optocoupler. I've seen the H11AA2 used in a similar application...would that be a good choice? Any recommendations? Can you suggest a parts supplier that doesn't require a minimum order of 1,000?!

ps would an NPN switching transistor work as well?
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
There are a whole stack of optocouplers you could use. I would recommend one that has an AC or DC input. This allows you to avoid the diode bridge (i.e. 4 less components).

There are many of these available. This one is the first of about 6 pages of them. You can purchase in lots of 1.

I would advise you to place a resistor across the optocoupler's input as well as in series with it. This will sharpen the "off" transition.

I would start off with the 47uF capacitor, a 1K resistor across the optocoupler, and a 220 ohm resistor in series with it.

Pins 1 and 2 are the input of the optocoupler, and pins 3 and 4 are the output. The output should go across the keyboard switch, with pin 3 to the side that is most positive when the switch is open.

Would an NPN switching transistor work?

That's not a simple question. The answer depends a lot on how many of these you want to make, and whether the switches in the keyboard have a common connection.

If the keys are connected in a matrix (which is highly likely) then there is no common connection, and to use a transistor which would be tied to one of your supply rails would probably work for 1 switch, but not for more than 1 unless they were on the same row or column of the matrix.
 

falcon60

Apr 25, 2010
13
Joined
Apr 25, 2010
Messages
13
I just ordered two types of optocouplers, FOD814A-ND and H11AA!M-ND. I'll rebuild my circuit as advised. In the meantime, I'll be reading up on transistors...and microcontrollers. I hope my next post is about how well it works...I'll be sure to include a diagram or a pic for future posters. Thanks again.
 

falcon60

Apr 25, 2010
13
Joined
Apr 25, 2010
Messages
13
No go. I rebuilt the circuit as suggested using a FOD814A-ND optocoupler in place of the relay and got the same, if not worse, results. I still get multiple keypresses on the charge, and now get inconsistent keypresses on the discharge. I must have a basic flaw in my circuit or am still getting bounce. Could I be getting bounce on the keyboard card side of the optocoupler? I hope you're not tired of me yet...any suggestions? As always, thanks...
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
Show us the *exact* circuit diagram.
 

falcon60

Apr 25, 2010
13
Joined
Apr 25, 2010
Messages
13
Forgive the drawing...just learning to use a freeware circuit drawing program. This is basically it: I've used a relay, a reed relay, and now an optocoupler and get the same results...always on the charge side. I've also tried a dozen different values for the cap and resistors. I'm obviously missing something obvious. What I don't get though is that I've seen this circuit (on-line) used by others without a hitch.
 

Attachments

  • TSKPE2.png
    TSKPE2.png
    10.1 KB · Views: 438
Top