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.
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.
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.