How to connect a switch or push button to PC

As long as we’re on a serial port kick. Here’s an easy way to read an external pushbutton or switch from your PC. Remember, that with an expensive USB to serial converter, if your computer doesn’t have an RS232 port, you can read the pushbutton switch from USB just as easily.

This is a great way to interface an external switch to your software and makes applications such as a photobooth, Lean Manufacturing cycle counter, or labwork much easier.

Like the .NET lightmeter post below, we’ll use a status control input on the RS232 port to see the switch.

OK, you’ll need a pushbutton (of course), a resistor to hold the input line in the correct state when the pushbutton is off, a small capacitor for debouncing and a female D-sub 9 connector. And perhaps some kind of enclosure or box for the pushbutton switch if you don’t plan to mount it to an existing panel.

Here’s the schematic diagram:

DB9-SW

.NET source code to read the switch and display the state is over

here

Serial Port LED flasher

Need a simple software controlled indicator? A green light when there’s unread email in your inbox? Red flashing alarm showing that the last nightly software build is broken? Or perhaps you really need a “do not disturb” sign that you can control without leaving your keyboard.

serial-led

Software-driven indicator lights are one of those things that seem like they should be simple — and they are. The problem is that today’s computers make them more difficult than before. “Once upon a time” every PC had a parallel printer port and it was the doorway to thousands of I/O devices back in the day. Today, USB is dominant and it takes a bit more than a simple out() or in() command in C to get access to the outside world from the desktop computer. This is probably why we see so many hackers turning to Arduinos to do such a simple task: it’s what’s available and well known. The problem is that it’s overkill and requires that you learn yet another toolkit.

A very simple way to control an LED is from a computer’s serial RS232 port. While few new PCs are coming with RS232 ports, they all have USB ports and USB to RS232 adapters are a dime a dozen. Or at least not much more than $5. So here’s how to get an RS232 LED indicator working.

We can hang a modern high efficiency LED off one of the RS232 data flow control lines and control it by software. Microsoft .NET, the Win32 API and Linux all offer libraries to control the serial port.

OK, enough talk, here’s a schematic:

DB9-LEDFour components: one of which is more for warm fuzzies than anything else. Pin 5 on the RS232 adapter is ground and pin 4 is the Data Terminal Ready (DTR) control line. R1 is used to limit current to the LED. Depending on the serial port, the voltage at pin 4 may range from -12V to +12V. Since the reverse voltage on most LEDs tops out at around -5V, we add a signal diode to give more reverse voltage protection. For most USB-serial adapters on the market, this probably isn’t needed, since their voltage tends to not drop below -5V, but let’s be safe. R1 can be adjusted depending on the color of the LED and how bright you need it to be. In this circuit, it’s about as low as I’d consider safe: with a red LED the circuit will draw about 10mA, which is about as much as most common serial translator chips will safely provide. It works with the adapters I’ve tested, but a safer value would be around 470 ohms. The limiting factor is the drive of whatever chip is powering the RS232 lines. USB itself can provide much more current than we need here.

You can build your own from the diagram shown above if you have the parts and experience. If you just need to get to the end result right away, we provide the complete unit shown in the picture below for $14.98 (shipping included) It drives a high-brightness white LED directly from a PC serial port (or USB-serial adapter). Complete documentation on programming it with the .NET SerialPort class is included along with a sample program written in C#. If you would like to see the code, it is on GitHub at this repository.

db9-LED
Serial LED controller………………….$14.98



This product has been discontinued.

Making connections – the end of the wire

So now that we know a little about wire, what are we connecting?

Much like wire and wire gage, the type of connector is often an outcome of what needs to be connected to what. The “trick” here is to know what is commonly used in a situation similar to yours, and purchase that. For example, the signals sent from an MP3 player to headphones are at a similar level to say, a temperature sensor’s signal. So, if you need to connect the three terminals of a temperature sensor (power, ground and signal), using a standard stereo headphone jack and mating connector would be a good place to start. Easy to source, cheap to purchase and will work for most cases.

Likewise, for a small number of higher current conductors, the common PC power supply connectors are inexpensive and easily found at most parts distributors.

The type of signals most of our customers are working with are generally a good fit for the variety of DIN connectors: these are easy to find for between two to eight conductors and since they are popular in the professional audio field, are generally inexpensive. They also have the benefit of being very easy to use; just decide how many contacts you need, and go. No special tools or separate contact, housings, strain reliefs, etc. need to be purchased.

For maximum versatility, we like Circular connectors.

CIR_Connector

These are often referred to as “Cannon” connectors from the company who popularized them for industrial and military use. Their use spans the range from very low level millivolt signals, to high voltage, high current applications.

Circular Plastic Connectors are less expensive than the metal versions, but retain the versatility. They can be complex since you need to match the connector shell with the appropriate pin size, perhaps add a backshell, cable clamp or seal, and decide on crimp or solder type connectors. But if you need to securely connect a few dozen signals in a harsh environment, they are definitely worth a look.

Making connections

So you’ve got a piece of electronic hardware. Perhaps it’s a motor controller, or one of our serial relay controllers or serial pulser readers, or even an Arduino.

Now you want to connect it to a sensor, or drive a motor. or even just get power to it. This is where the issue of interconnections raises its head. It may seem simple to do a task like connecting a 12 volt DC power supply to an electronic board, and often it is. But it can become complicated very quickly. Some of the questions that must be considered when connecting two pieces of electronics together are:

  • How many connections are there?
  • How far apart are the items to be connected?
  • Do you need to conduct low level analog sensor signals or digital controls?
  • How much current does the connection carry?
  • Is the connection permanent or temporary? Will it be connected and disconnected frequently?
  • Is the environment likely to cause connections to become loose? Is there a lot of vibration or motion present?

Let’s stop here before the number of things to consider gets out of hand. The first thing to consider is the type of signal and how the wiring will be terminated.

Selecting wire

Low level signals such as those from temperature or pressure sensors should use shielded cable. This is because those signals are typically very small and they can be affected by electrical noise. Electrical noise is the interference generated by electric motors, or other nearby magnetic fields. Other signals like digital control lines, or power connections, tend to not need shielding except in unusual cases.A shielded cable has at least one insulated center wire that carries the signal and a wound, solid or braided conductive shield around the signal wire. The shield is connected to ground to protect the signal wire from interference.

Most signals can be carried moderate distances by fairly small wire. We normally recommend stranded 24-26AWG (American Wire Gauge) size wire for most sensor and control signals. Keeping the wire thin has several advantages:

  • It uses less copper and so is less expensive
  • It is more flexible, which is important if you are bundling large numbers of conductors or need to negotiate sharp bends around equipment
  • Less copper also means less weight

Stranded wire is preferable in most cases because it is less likely to break when flexed. It can also be more easily crimped onto connector pins. Stranded wire is specified by the overall size, the number of conductors, and the gauge of each conductor. So a 24 AWG wire may be stated as 24 AWG 7/30 meaning it is made from seven strands of 30 gauge wire.

For wires that carry power, a larger gauge is used. For specific amounts of current and length of wire, you can refer to tables found online, but in general wire in the range of 18 to 22 gauge will meet the most common requirements of moderate distances (up to 20 feet) and current (up to 1A).

In the next post, we’ll discuss what options there are for interconnections between wires and from wire to board.

 

.NET Light Meter via serial port

This is a quick implementation of an idea I had. Someone online asked for the cheapest way to read light levels into a Windows application.

The lowest cost way I could think of was to avoid a microcontroller or other logic component and use whatever was already on a modern PC. My idea was to use a Cadmium Sulphide (CdS) photocell. CdS cells are used in many camera light meters and work by changing resistance in response to ambient light. By using the current through a photocell to charge a capacitor, we can tell the light level by measuring the time taken to charge. In days of old we could have used a joystick port to do this, but those are long gone, now replaced by USB-connected joysticks.

On PCs without joystick ports, we could have done the same thing with the printer port. However, those are also a thing of the past.

Many PCs still come with serial ports, so we’ll try those. If not, a USB to serial converter will work. The Clear To Send (CTS) and Data Terminal Ready (DTR) lines can be controlled from a Win32 or .NET application. There is some concern about the responsiveness of Windows, but as long as we avoid very short time constants, we can get usable data.

The concept is to build an RC circuit from a photocell and a 200uF capacitor. The photocell is driven from the DTR line and the junction of capacitor and photocell is read by the CTS input. Here’s the schematic: it’s pretty simple.

The results are surprisingly useful: discharging the circuit for two seconds and then charging it while polling the CTS line every 5 milliseconds shows a clear difference as the photocell is pointed at various areas of a lighted room.

Code to use this circuit is on GitHub.