How do I connect a micro switch to a PC's serial port and query whether it is open or closed? Which serial pin number(s) do I need to wire up?

  • \$\begingroup\$ Are you referring to RS232 Serial (DB9 connector), or some other serial like USB or lightning? \$\endgroup\$ – MarkU Jun 25 '16 at 3:07
  • \$\begingroup\$ I am referring to RS232 Serial (DB9 connector). \$\endgroup\$ – Guru Josh Jun 25 '16 at 3:12
  • \$\begingroup\$ I suppose you could connect TX and RX to the switch and see if loopback worked. \$\endgroup\$ – user2943160 Jun 25 '16 at 3:12
  • 2
    \$\begingroup\$ If RS232 serial, there are two modem control outputs: DTR(4) and RTS(7). There are four modem status inputs: DCD(1), DSR(6), CTS(8), and RI(9). Normally RTS-CTS would be used as one hardware handshake and DTR-DSR/DCD as another handshake. But you could simply connect a switch between RTS and CTS. Sensing the modem status inputs requires using appropriate system calls to open the port and poll the status. Exactly what those calls are depends on the operating system; search for source code of miniterm for example. \$\endgroup\$ – MarkU Jun 25 '16 at 3:13
  • \$\begingroup\$ I've used the RI input (ring-indicator) in the past. It can generate an interrupt and can also wake a sleeping PC. As @MarkU you would wire DTR or RTS to the other side of the switch, and you could then use that output to enable/disable it. \$\endgroup\$ – Mark Jun 25 '16 at 4:33

I used 3 second and 4 words in google to find you a circuit, that does excatly what you want.


Connector pins: 5: Ground (GND) 7: Request to send (RTS) 8: Clear to send (CTS)

  • \$\begingroup\$ I am a complete newbie at this. I don't know exactly what I am looking at in that diagram. Do I need to solder 3 wires that connect to serial pins 5, 7 & 8? The micro switches have either 3 or 6 pins? To which of these pins do I need to connect these wires? \$\endgroup\$ – Guru Josh Jun 25 '16 at 4:54
  • \$\begingroup\$ The switch is market at the circuit as S1. So you see there is 3 pins on the switch 1, 2 and 3. You must connect two of the wires to the switch. And you must connect it so it's NO (Normally Open), with other words that there is no connection between the pins, as long as you don't push the switch. \$\endgroup\$ – BufferOverflow Jun 25 '16 at 5:16
  • \$\begingroup\$ So I have 3 wires connected to serial pins 5, 7 & 8. Which of these 2 connect to the 3 (or 6) pins on the switch and which of these pins do they connect to? What happens to the third wire? \$\endgroup\$ – Guru Josh Jun 25 '16 at 5:24
  • \$\begingroup\$ Just look at the circuit drawing. DB9 is the connector, every pin is marked with a number. R2 is a resistor on 10k, C1 is a polarized capasitator on 0.1uF, S1 is the switch (Every pin is labeled with number). All you need is 1 connector, 1 resistor, 1 capasitator, 1 switch, some wires and a soldering iron. \$\endgroup\$ – BufferOverflow Jun 25 '16 at 5:32
  • \$\begingroup\$ That's all double Dutch to me. \$\endgroup\$ – Guru Josh Jun 25 '16 at 5:37

a PC serial port has 3 outputs and 5 inputs

the outputs are TXD, RTS, and DTR,

the inputs are CD, RI, RXD, CTS, and DSR

all of the inputs will respond to any voltage over about 1.2V as A high and a voltage less than 0.6V or unconnected as a low.

TXD and RXD are usually used for sending and receinving asynchronous serial data.

set one of the outputs high, and wire the switch between it and one of the inputs.

see the serial port documentation for your chosen operating system.

  • \$\begingroup\$ Jasen answered while I was commenting, so I will just repeat that the RI input can trigger an interrupt and wake a PC (among other not-as-useful things). \$\endgroup\$ – Mark Jun 25 '16 at 4:35
  • \$\begingroup\$ they can all be used to trigger interrupts. even RXD will get you either a data interrupt or a break interrupt. \$\endgroup\$ – Jasen Jun 25 '16 at 4:39

This is not an answer, it's a comment to @BufferOverflow's answer for @GuruJosh - only I need way more than the maximum number of characters for a comment!

@GuruJosh, it seems that you haven't read a schematic before ("That's all double Dutch to me"). That's OK - here's a quick tutorial.


Our problem is that you never identified the microswitch that you had - every manufacturer will make it differently, with different pins doing different functions. You say you have a 3-pin and 6-pin switch? OK, let's look at that.

The 3-pin switch probably has one pin as common - but what they call it is anyone's guess. The idea is that with the switch in one position (say "unpressed"), common is connected to one of the pins and not the other. With the switch in the other position ("pressed"), common is now connected to the other pin and not the first. Does that make sense?

  • When a switch has its pins NOT connected until the switch is closed, the switch is called "Normally Open", or "N.O.", or simply "NO".
  • When a switch has its pins connected UNTIL the switch is closed, the switch is called "Normally Closed", or "N.C.", or simply "NC".
  • You describe a 3-pin switch that (probably) has one side N.O. and the other N.C. - two switches in one!

The only way you're going to know which pin is which is to look up the part number on the web, or find out empirically. Do you have a multimeter? Hopefully one with a buzzer (continuity checker), but if not simply set it to 20 kΩ - not V or A.

Note that you can't break the switch with this test.

  1. With the two probes not touching anything, the meter should display 1.
  2. Touch the two probes together, and note the buzzer, or that the display settles down and reads 0.00 (with or without a minus). That means there's continuity.
  3. Put the two probes on any two of the pins.
    1. Either:
      1. The buzzer will go off / the display will show 0.00.
      2. The display won't change, and still show 1.
    2. Now press the switch, and note what happens again.
  4. Put the two probes on a different pair of pins. Repeat the above sub-steps.
  5. Put the two probes on the last pair of pins. Repeat the above sub-steps.

After these three steps, you should have three different results for the three pairs of pins:

  1. One pair showed 1 regardless of whether the button was pressed or not.
    This pair is the two non-common pins, that never connect to each other. That makes the third pin common!
  2. One pair showed 1 when not pressed, and showed 0.00 when pressed. This is the N.O. pair.
  3. One pair showed 0.00 when not pressed, and showed 1 when pressed. This is the N.C. pair.

With the above information, you should now know the pinout of your 3-pin switch.

The 6-pin switch is probably two 3-pin switches side-by-side, both activated by the same lever - if you've got a 2x3 arrangement of pins, the midde pair are probably common, and the outer pins are the N.O. and N.C. for each common.


The schematic is an electrical diagram of what to connect where - if you know the symbols.

  • A resistor (R2) is a zigzag line. They're rated in Ohms (Ω), so 10kΩ offers a reasonable resistance (k=kilo, of course). It can be connected either way around.
  • A capacitor (C1) is a line-with-arc-beside-it. They're rated in Farads (F), but one Farad is huge, so you'll see microFarad (µF) and even picoFarad (pF). Different capacitor types have different properties: the larger-rated ones (0.1µF is large enough!) are often polarised, in which case they must be connected the right way around. Polarised capacitors normally have a black stripe with plus (+) symbols on it, indicating one of the two legs. That's the plus terminal, that usually corresponds to the 'line' side, not the 'arc' side of the diagram. The schematic should have had a + symbol on it...
  • The switch (S1) looks like a switch. It has the three pins, and the common is the centre pin (2). You'll see that (1) is not connected - it's the N.C. side that you're not interested in. That leaves (3) as part of the circuit!
  • The DB9 connector is labelled. If you peer closely at the back of your plug you'll probably see tiny numbers numbering the pins - hopefully the same as the diagram! If not you'll have to do a mental mirror image...
  • The wires are the green lines. Unfortunately right near the DB9 pins 7 & 8 they cross. This is not an indication that they should be connected! However, elsewhere the green lines meet in a 'T' - these should be connected.

In short: @BufferOverflow has the answer. You just needed to learn how to read it!

  • \$\begingroup\$ That's very helpful. Thank you for the detailed information. One thing I'm not clear on is which wires connect to the positive and which connect to the negative on the resistor and capacitor? \$\endgroup\$ – Guru Josh Jun 25 '16 at 13:47
  • \$\begingroup\$ As I said: the resistor doesn't have a positive or negative side, it can go in either way around. And as I described, the capacitor is marked as to which leg is positive. That's the one that connects to the resistor, as per the diagram. The other leg of the capacitor connects to both pin 5 of the DB9 and pin 2 of the switch. \$\endgroup\$ – John Burger Jun 26 '16 at 4:05
  • \$\begingroup\$ Thanks so much for this. I have wired this up as instructed, written some code to query the status of the switch and it's working! \$\endgroup\$ – Guru Josh Jun 26 '16 at 7:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.