Relay control by PC, using RS232 converter CP2102

It is pretty easy to control a single relay using a PC by USB. I used a USB to RS232 (TTL) converter using CP2102 chip which has a DTR line output available and a board with relay.

IMG_20140627_191513321

The relay board has three input pins – VCC, GND and INPUT. It has two leds for VCC and INPUT signal indication. The INPUT signal is connected to a transistor which in turn controls a relay. There is also a flyback diode that prevents any voltage spikes caused by operation of the relay coil.

The usb to RS232 board has more pins brought out. In normal operation as RS232 converter one would probably connect the GND, TX and RX pins and opitionally the VCC pin (if USB is used to supply power to the host board). For this circuit only VCC (5V power output), GND and DTR pins are used. Note that not all USB to RS232 converters have a DTR pin connected / available.

In normal RS232 operation DTR pin would be used to signal the state of the device, but here we will be using it as a digital output (DTR and RTS pins can be used as digital output; CD, CTS, DSR pins can be used as digital input).

A limited number of USB to TTL converters also enables you to change the state of these pins fairly quickly so you can also use them for bit-banging certain protocols (eg. SPI, I2C, 1-wire, programming protocols for PIC or AVR).

The programming needed to change the state od DTR output on a port is pretty simple. Using .NET and C# you first need to open serial port using serialPort.Open();, the output state can then be read and changed by accessing serialPort.DtrEnable. Writing true to it by serialPort.DtrEnable = true; enables the output while serialPort.DtrEnable = false; disables it.

 

control-oncontrol-off

IMG_20140627_191629895IMG_20140627_171133345

 

The control program and its source code can be found here: Program You should choose the appropriate COM port in the drop down and then click Open button. After the program connects to the port you can control output state by On/Off button.

Please note that the DTR line may be in any state before the program connects to the port. So you should be careful to only connect devices to the relay which can’t cause any damage in case the relay accidentally switches on.

5 Comments on “Relay control by PC, using RS232 converter CP2102

  1. Hello, I am confused. Doesn’t TTL mean 0v to 5v, and typical RS232 -13v to 13v? But you write “USB-to-RS232 (TTL)”. Shouldn’t that be “USB-to-UART (TTL)”?

    Thanks for your interesting blog!

    • Thanks for your comment. Yes, maybe i should write UART instead of RS232. The CP2102 will operate on 0 to 3.3V output (I think inputs are 5V tolerant, but not sure). Connecting normal RS232 levels to it will probably damage it.

  2. Hello,
    Serait il possible de pouvoir inscrire dans la fenêtre du programme par exemple ” Relais TX” ou “Antenne Levi” ou autre.
    Salutations
    Michel Cotture
    HB9EXC

    • Yes, that would be pretty straight forward. But I wouldn’t recommend using the CP2102 dongle in environment with high levels of RF energy present (eg. near a TX/AMP). Would need to test it first to make sure that it doesn’t misbehave under such conditions.

Leave a Reply

Your email address will not be published. Required fields are marked *

*