# Control a large number of switches with RS232 via discrete logic

I am working on a device that requires controlling a large number of switches by RS-232. The designer wants to use a microcontroller. So in that scenario, a code is sent via RS-232 to the microcontroller which then operates each switch individually. Since he wants to have a one-port-to-one-switch setup, the microcontroller needs to have a lot of IO ports which means we have to use an adapter. All in all, if we add in the complexity of programming the microcontroller, it seems unnecessarily complex to me. I am wondering if there is a straightforward way we can do this using discrete logic.

So, for example, suppose we have 10 switching points and each of these switches is 4-way. Then we have 4 possible states for each switch, which requires 2 bits. So, I guess we need 2 latches per switching point, or twenty latches altogether. So, not being expert in digital logic I am not sure how it might be possible to set each latch using RS-232. Can someone point me in the right direction of how to set this up?

RS-232 has 9 pins, so if we treat 1 pin as the pin used to set logic, then we have 8 logical pins. So, if we have 20 different latches, then since 2^5 = 32, we can control these using 5 of the pins. Then the 3 remaining pins can be used to set the state. Or something like that. What is the strategy here?

An additional issue is that it would be desirable, but not necessary, to query the state of the switches. Is that easy to do in some way?

• This is very much a job for a microcontroller. Jun 10, 2021 at 16:12
• @JackCreasey For what RS-232 is, it is definitely not a protocol. It is a standard for an electrical interface. Jun 10, 2021 at 16:20
• @JackCreasey True, but RS-232 standard does not define an encoding. Jun 10, 2021 at 16:50
• @JackCreasey There is a difference between protocol and encoding. Encoding defines "how are bits transmitted" and protocol defines "what do the bits mean". For example, UART communication defines both the protocol and encoding, it uses asynchronous start-stop protocol to send data bits, and NRZ encoding is used to send each bit. Jun 10, 2021 at 17:27
• AtMega 2560 has 54 Digital I/O pins, minus two for the UART. Is that enough? Jun 10, 2021 at 17:30

Based on what you write, this design really is simpler with a MCU. It enables you to control the switches and ask the MCU how they are currently set.

Sure, GPIO expanders with serial input exist, but they would be pre-programmed microcontrollers.

A serial port does have 9 pins, but your understanding about them has some issues. One of the pins is ground. There are three outputs, and 5 inputs.

A serial port has only one data transmit pin and only one data receive pin, and those two with ground as the third pin is all you need to talk to a device like a microcontroller. The rest of the pins are handshake pins, signaling if devices are ready to function, or need delays in the data transmission. Two pins are only used with modems, for telephone ring detection and if link has formed with another modem.

You could use the RS232 pins in a very unorthodox fashion by software bit-banging the three output wires, so it could be possible to connect a SPI or I2C chips directly, but that is pretty awkward and incompatible way of doing it. Microcontroller is by far the easiest solution, and you can control any I2C or SPI chip with it for expanding small Arduino to drive dozens of relays.

You could check the FT series of chips, there are some that can be used in parallel input/output mode.

For using discrete logic, you must somehow de-serialize the data, using a shift register and/or a state machine.

You could also think of mis-using the control pins of an RS232 port as program-controlled GPIO pins.

Honestly, the idea that you could do this more simple with discrete logic than using a microconstroller sound hilarious to me.

PS why RS232? for most modern PCs that means you will need a USB-to-serial adapter. Better cut out the middle man and interface directly to USB.

• It's an audio application and RS-232 is the standard in the industry for device control. Jun 10, 2021 at 19:15

You could use a micro and a chain of e.g. 595 shift registers. Wire each output to one SPST switch.

Then clock in the entire switch array code. And then update all switches synchronously.

This needs a minimum of 3 logic pins: serial clock, serial data and the output latch.

Using a micro for this is honestly a bad choice as it doesn't even allow to update more than 32 switches synchronously for a 32 bit micro if I am not mistaken.

• Now, if you would explain how on earth would you drive a 595 with RS232 directly? Jun 10, 2021 at 16:09
• I wasn't suggesting to omit the micro. The shift registers are merely the stated "adapter" between micro and switches Jun 10, 2021 at 16:13
• OK but the question specifically was how to drive relays without a microcontroller, but discrete logic, so I assumed your answer did not include using a microcontroller. Besides you are definitely mistaken about microconrollers, you can update any arbitrarily large amount of relay outputs synchronously even if you have a 8-bit micro and a chain of 595 shift registers. Jun 10, 2021 at 16:23
• With 595, yes, but not with the micro itself ? Jun 10, 2021 at 16:30
• Well a 32-bit MCU might not have 32-bit IO ports either. And the relays take so much time to switch so the MCU will be fast enough that there is no noticeable delay. Jun 10, 2021 at 16:45

RS-232 has 9 pins, so if we treat 1 pin as the pin used to set logic, then we have 8 logical pins.

Ummm ... No.

A standard RS-232 connector has 25 pins. The stripped-down version created by IBM for the original PC has 9 pins, but all of them are used by the RS-232 protocol. A serial port cannot be used as a general purpose I/O port. The IBM PC printer port can, but it no longer is a standard part of most PC or microcontroller designs.

A normal RS-232 communications link moves one 8-bit byte at a time. This is what falls out of, or is fed into, a 40-pin UART chip, so an all-discrete design can address 255 individual bits, or points, or switches, or whatever.

If you define one bit as the set/reset or open/close bit, and have a latch at each switch or 4-switch group, then you can open or close or alter any one of 127 individual switches with each byte transferred. The logic chip for this is called an addressable latch.

Since you don't need very many addressable points, you can do things like define one bit as a broadcast open-all-switches or close-the-first-switch-in-each-4-switch-group, or whatever.

Bottom line - yes, this is completely doable as an all-discrete, zero microcontroller design. However, the cost, pc board size, etc. will be significant.

• Hmm, ok, is there a half-way solution. Have the serial port talk to something on the board that does discrete logic? In other words, still use the serial port normally, but not have to program a microcontroller? In other words, is there a device that receives serial codes and can turn them into latch settings, or high-lows that can be used to drive digital logic? Jun 10, 2021 at 17:26
• @TylerDurden the device that converts serial data from RS232 into parallel is the UART. Jun 10, 2021 at 17:31

A microcontroller is the sensible way to do this.

Consider using a microcontroller with a UART and I2C or SPI interface. Then you could design packets to be sent to/from the microcontroller to control the various outputs as you desire (for example, you could set/reset individual bits or in small groups, or you could re-write the entire lot by sending a few bytes together). You could design a response packet that shows the current state of all the outputs or whatever you want. You can include checksums or CRCs. You could have the settings expire and put the outputs into a safe or neutral state if communication is lost. Many options depending on your requirements.

SPI would be convenient for dealing with shift registers such as 74HC595. I2C could use expansion chips such as the PCF8574A, easily expanded to 8 x 8 = 64 I/O on a single two-wire I2C bus.

• That sounds sensible. Are there microcontrollers that are specifically designed to do switching? What I want to avoid is my designer wasting a lot of time writing software which is not his skillset. It would be ideal if there was a microcontroller that could just be sent codes to turn switches on and off, instead of having to program a generic microcontroller. Does such a thing exist? Jun 10, 2021 at 19:01
• Pretty much any microcontroller will do this. As I said the things to look for are a hardware UART and a hardware SPI and/or SPI (and an external crystal or resonator clock, which most have as an option). For just one piece, an Arduino or similar board would do the trick. It is preferable to pick hardware that a) will do the job and b) suits the experience and tools of the user. I might use a Microchip PIC or AVR 8-bit part for this. I could also use a 32-bit ARM processor or an MSP430 or an STM8 but I would likely not. Jun 10, 2021 at 19:19
• Are you saying these microcontrollers are designed to specifically do switching without programming? The chips you are describing are incredibly complicated computer like chips. That's exactly what I don't want. I want a chip that is simple and designed specifically for switching and takes specific RS-232 codes to operate switches. Let me repeat: I DO NOT WANT MY DESIGNER PROGRAMMING COMPUTERS. Jun 10, 2021 at 19:43
• All microcontrollers switch bits. If you don't want your designer learning to program an MCU, farm it out to someone who does know how. We're talking about a few pages of code. Jun 10, 2021 at 19:45
• Yeah, thats what they all say, oh its only a few hundred lines of code. And 1000 lines of bugs later they are explaining why they need another week to program the computer. This is why I want discrete logic. It's simple, direct and I don't need a computer scientist. Jun 10, 2021 at 19:47