Hardware Reference
In-Depth Information
Chapter 10
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.
The first issue is that the Raspberry Pi is obviously not tolerant of 110/240 V AC; putting that
sort of voltage anywhere near your GPIO pins will let the magic smoke out.
The second issue is that this voltage can kill you. The fact that it can kill you won't be much of
an issue to your Raspberry Pi but it will greatly impact your ability to finish this topic.
So you and the Raspberry Pi are not tolerant of the mains voltage. What now?
There are a few device options you can take to safely interface with the mains voltage. All of these devices provide
some form of isolation to the mains current but some offer more than others.
The device that offers the most isolation is opto-isolation and this would be my preference but
it's not often seen on less expensive devices.
Another common device you may see being used to switch AC is a power metal oxide
semiconductor field-effect transistor (MOSFET). You will know if you encounter a power
MOSFET by the size of the heatsink it uses. A power MOSFET will generate a large amount of
heat so it is not often used in small circuits or anything encased in plastic for that matter.
Lastly and most commonly you will find a relay. You will come across two types of relays:
the standard mechanical relay and the solid-state relay. A mechanical relay is easy to
spot, because it will be a little bigger than the solid-state version and, more obvious, has a
distinctive click that the device makes when activated. Both the solid-state and mechanical
relays work in a similar manner and offer the lowest level of isolation. If something bad were
to happen, there is a good chance it will fry more than just your relay.
The last thing you need to be concerned about aside from your safety is what your local laws allow you to do. Some
countries may not allow you to change any part of a mains circuit unless you have the appropriate qualifications.
So let's take a look at how the AC mains works and how it's different from direct current (DC); after that I will
show you the different ways to switch AC mains power. At the end of the chapter I will introduce you to the method
I use to switch the power. This method is easy and risk-free. Read on to find out what it is.
 
Search WWH ::




Custom Search