Introduction: Control Any AC Device Via PC (RS232 or USB)

About: Electronics engineer. Embedded Systems enthusiast. Tech lover. Geek.

In this simple build you will see how to turn on/off any appliance via your PC and set some rules, for example to turn coffee maker every morning at 6:55, just before you get out of bed.

The possibilities to expand this build are endless, however here I will give you just the simplest solution (because I just needed on/off feature for my PC speakers).

** Parts that you will need **
There are a few components that you will need for this build and you can get them almost anywhere

- 1x MAX232 (for serial communication)
- 4x 1uF 10V Electrolytic capacitor
- 1x ATtiny2313 (you can use even cheaper/smaller that has UART and 2 I/O pins)
- 1x AVR programmer (or programmer that can program your chip. Arduino can be used to program AVR chips)
- 1x Relay 5VDC 250VAC 10A (I had one of these laying around)
- 2x 3-terminal screws (optional, you can use anything that can keep your cables fixed and connected)
- 1x Extension cable (Best option is if you have one like mine that is 1 to 1 extension cable ( 1 cable goes to wall socket and on the another end you have just one socket ))
- 1x 2A fuse (or you can use other value, calculate one for your needs)
- cables, wires etc..
- Plastic case!!! (we are going to work with AC 110/220 lines, so YOU MUST isolate them good so no one can touch them from the outside because it can cause serious injuries or even DEATH!!! So please be careful! I'm not responsible for any damage or harm done)
- 2x LED (1 Red 1 Green)

*** Please be cautious **
- One part of our build will be directly connected to the mains via relay, it is important that you know how this device works and which side you CAN NOT EVER TOUCH while the device is plugged in since it can cause serious injuries or even death!

- Author of this build suggests that you have a basic knowledge or at least that you look trough other Instructables for more information on parts and how they work or how to make your PCB and etc.

- Also consider adding more security features to your device, some of them may include but should not be limited to:
 -> cable strain relieve (so you can't pull out the cable from the box)
 -> enclosed box that is waterproof so you can't spill anything on it
 -> optical insulation and/or separate power supply for microcontroller and relay
 -> different relay that would break both lines instead of just one

- This instructable is just an idea how something could be done, it may not be the best way and you should rather customize it to your needs (if you don't know ask someone to help you) rather than just copying it whole. However with few adjustments and changes you could get a pretty nice device that will add a feature to your appliance that you are looking for.

** Let's get started **
First we need to make a schematic to see how to connect devices to each other and how does this work.

Step 1: The Schematic

** The Schematic **

So here we go, let's design this circuit... oh wait, It's already done :) Let's comment it so you know what can you omit in your design or change.

** Part 1 **
- Here you can see 2 LEDs and 1 filter capacitor
- 1st LED that is connected between VCC and GND is POWER signal led that let us know if our device has power.
- 2nd LED is connected between MCU's SIGNAL PIN (PB4) and GND, so MCU uses it to let us know if the device is connected to mains or not.
- Resistor values should be calculated to match your LEDs. Most of the standard 20mA diodes need about 220Ohm resistor if they operate on 5V like here.

** Part 2 **
- This part is dedicated to serial communication. It uses MAX232 to do level conversion that is needed for MCU to communicate with PC over RS232 protocol. In datasheet, manufacturer suggests that you use 1uF capacitors, however I've tested this IC with 1uF, 4.7uF, 10uF and 22uF and the all work just fine.

** Part 3 **
- Here you have MCU or the microcontroler unit that does all the "heavy" lifting for you.
- We have pins that are used for programming the MCU (MISO, MOSI, SCK) and have pins that are used for communication and status indication (RXD and TXD for communication and SIGNAL to indicate relay status via LED)

** Part 4 **
- This is the header that gives the power to our device (DC power 5V). I've used old USB cable and solder RED vire (+5) to the VCC and BLACK wire to GND.
(There are ways to use power from mains but it's not covered in this instructable, you can look for Transformeless MCU power supply application note but in my build it was obsolete)

** Part 5 **
- This is the part with one 1N4148 diode that is connected to the Relay which is represented as coil and switch.
- There is also a fuse and 2 3-terminals to screw the wires that go to your device and another one that goes to AC mains.
- When MCU puts pin ONOFF to HIGH (+5V) the relay will switch from ON1 to ON2 and connect your device to AC mains.
- For safety reason I have made this device to make default state OFF so even if you connect your device to AC outlet and give power to the board, the device will remain OFF until you issue a command over serial port to turn the device ON.
- I have used a quick 2A fuse for safety reasons, if anything goes bad it will blow and if you are in luck, no other devices will be damaged.

** PART 6 **
- Here you have a 6pin header that is used to program your MCU while it's on the board... It's useful feature to have while debugging or if you think you will have to change the MCU firmware later.

** Next Step **
Ok, so that's it. We have designed our board now we have to manufacture it...
Next step shows you the finished board and some options how to make it...

Step 2: The PCB

** PCB manufacturing **
- Here you have several options. You can buy perforated PCB and solder your components onto it and connect with jumper wires. It should be really easy since all the components are trough hole and should fit in perforated board.
- Other option is to manufacture your PCB with toner transfer method or Photo PCB etching method, which ever you find the best, both will work just fine since all lines can be 30+ mils in size so it's not a problem to route your board and make a nice big lines that won't break while manufacturing.

** MAINS **
- In picture, you can see nets that are connected directly to the mains so if you make the same PCB as mine please be sure that no one can ever touch them while the device is operational since it can cause serious injury or even death! Be careful!

Step 3: Programming the Chip

** Programming the chip **
- I have used USBasp to program my ATTiny2313. You could use any other programmer (compatible with your chip) but make sure that you edit the schematic and code to make it work properly.
- The code is very simple and kept as minimal as possible so you can easily port it to another chip or make adjustments.
- There are 2 functions that we use, one is to read the character/byte that is send from PC and other is to send character/byte to PC

** How it works **
- To keep it simple, the device uses codes to communicate with PC.
- Some codes explained below
-- 0x11 (OFF CODE)
--- Tells the chip to turn the relay off, disconnecting the device from the mains
-- 0x99 (ON CODE)
--- Tells the chip to turn the relay on, connecting the device to the mains therefore turning the device ON
-- 0x22 (Status CODE)
--- This code PC can send to the chip and the chip will reply with the current status of the relay
---- Response 0x55 is the ASCII char "U" and it represents the device ON state
---- Response 0x49 is the ASCII char "I" (uppercase i) and it represents the device OFF state

** Programming the chip **
- If you want you can make modifications to the code and test the device if it's working.
- Note that you DON'T have to connect the mains to the relay/terminals at this point since you can see if the device is working just by looking at green LED and relay makes a *click* sound when it switches so you can use that to verify that relay is switching.


Step 4: Controling the Device From PC

** PC Control part **
- For our device to work we need a program that will send the commands from PC to MCU on the board to switch the relay on/off. In the debugging stage you could use a simple terminal program, however, later it would be a painful to have to send the command over terminal every time you want the device turned on/off.
- I have made a simple application that has two buttons ON and OFF. No need to explain what they do so I will just briefly explain how they work. When you click ON it opens COM port and sends the ON command (0x99) to the MCU which then turns the device ON and replays with the status.
- This application accepts/requires start-up arguments that are defined in the TARGET input box of the SHORTCUT PROPERTIES window. First parameter is your COM port on which your device is connected and the second on (optional) is TurnOn or TurnOff.
- So you can create 2 shortcuts just like I did and quickly turn the device ON/OFF or have other application turn your device ON/OFF like PC alarm application or anything that comes to your mind.
- If you get an error at start trying to run the application it could be two reasons, you haven't added a (correct) COM port number to the shortcut target or your COM port is already open/in use (some other application is using this COM port)

This application is very, very, basic but it was more than enough for me, however you can build your own app that will enable you to read the device status, timer based on/off switching and etc. or combine this application with other applications like I did. Go nuts!

Step 5: Final Words and Source Files

** Once again... the mains **
I know I have bored you with the mains bla bla bla talk, but please, I cannot emphasis enough how important is that you are extremely careful while working with mains. Make sure no one can touch them, pack your PCB in a closed plastic case for safety and put it out of reach so no one can hurt themselves.

** Design notice **
- My mains are 220V/50Hz so you should check if this design is compatible with your mains. It should be but please, better be sure than sorry. :)

** Source Files **
- Here are the source files that contain the schematic, PCB layout, microcontroler code, hex files and VB.NET application with source code.
- If you modify this build it would be nice to mention original authors name, but if you don't... I won't be mad :)

** I want to see your build and/or suggestions **
- If you have done this build please let me know, send me some pictures in comments or PM me. Also if you have questions or have suggestions/ideas for others, let us know.

** Room for improvement **

-More security features to your device, some of them may include but should not be limited to:
 -> cable strain relieve (so you can't pull out the cable from the box)
 -> enclosed box that is waterproof so you can't spill anything on it
 -> optical insulation and/or separate power supply for microcontroller and relay
 -> different relay that would break both lines instead of just one
 -> add zero crossing detection to extend the lifetime of the relay

- Like I said in the start of this build, room for improvement is endless. Here are some of the ideas that you could incorporate in your design.

- Use FTDI232 to communicate with PC over USB (since not all PCs have serial port) or you could use another MCU that has hardware USB support or use AVR software USB library (requires more programming skills).
- Add a wireless communication via 433MHz RF modules, WiFi modules or any other.
- Power the PCB from the mains with transformer, diodes and caps or use the transformerless power supply design or get a transformer and power it from the mains.
- Add more relays and power outlets to control more than one device

** Notice **
I made this device to enable my PC to turn on my Logitech x530 speakers every morning to wake me up with music. My device is located in corner of a room where it's nearly impossible to reach it, so many of the safety features are omitted. This device is safe to use (I've been using it for long time now) but you wouldn't want people stepping on it, touching it, placing it high humidity places or where someone could spill something on it etc. My Logitech X530 have zero crossing detection so I didn't add that feature but it is advisable to add it to extend the lifetime of your relay and to ad extra safety.
When you start building your own device, please consider which security features you would need, if you are not sure, add them because it's better to be sure than sorry. Also I suggest that you try to understand the principle and customize this device to your need rather than just copying what I've done here.

Thank you for all for you support, critics, comments and suggestions. Your feedback is greatly appreciated.