Controlling a Mains Device

So far you have been able to do a lot of interesting projects with your Raspberry Pi. There is, however, one topic that always comes up when you talk to anyone about practical uses for a microcontroller or, in your case, for the Raspberry Pi. That question goes something like “how can I turn on some random device in my house?” For example, you may want to turn on a lamp at night or your air conditioner before you get home. Nothing like a cron job to turn on your air conditioner! Now if you think about this quickly, getting the Raspberry Pi to turn on a random device is an easy task. In theory, turning a mains-powered device on or off is no different. It’s just interrupting the flow of electricity to that device; after all, this is what your common wall switch does. Unfortunately, here are two big issues that you will face and they both have the same source. That source is alternating current, or AC.