Chapter 13. Controlling AC

Switching AC appliances requires an understanding of the dangers and special precautions that must be used when dealing with high voltages. In this chapter, you will learn how to safely control AC devices using electromechanical and solid-state relays, as well as techniques such as zero-crossing switching.

High Voltage Can Kill

Every year, domestic AC electricity kills hundreds of people in the United States alone. Many more are badly burned and many house fires are started by faulty wiring. The domestic AC supply is high voltage and capable of supplying large currents.

When working on the practical section of this chapter, never work on a “hot” (live) line. I like to see the plug of whatever I am working on in front of me on my bench, so I know it’s not in the wall outlet.

Always use an RCD-protected AC outlet while you are working on a project, and never leave exposed wiring or PCBs on any project that controls AC without enclosing it in an insulated box and fixing it down.

Also, do not be tempted to use breadboard with AC designs. It is not designed to cope with the voltages and currents involved.

In short, unless you have had training in working with domestic AC, use a ready-made module like the PowerSwitch tail.

AC Switching in Theory

In this section, you will  learn about the theory of AC switching and various circuit designs and components suited to this area of electronics. There is a separate section later in this chapter on switching AC in practice (“AC Switching in Practice”).

What Is Alternating Current?

Whereas direct current (DC) always has current flowing in one direction, alternating current (AC) just can’t keep still. The direction of current flow reverses 120 times per second in some parts of the world (including the United States) and 100 times per second in other parts of the world. This reversing of the current flow is achieved by a reversal of the voltage across the load (Figure 13-1). There are two reversals of the flow of current in each full cycle and so the frequency of the AC is either 60 or 50Hz (cycles per second). Hertz is the unit of frequency and 1Hz is one cycle per second.

In the United States and a few other countries, the AC supply is generally 120V, and in much of the world, AC outlets are more commonly at the far more lethal 220V.

Figure 13-1. Alternating current (and voltage)

As you can see from Figure 13-1, the peak positive and negative voltages are actually considerably higher than 120V. The figure 120V AC is actually a kind of average voltage called the RMS voltage. This is the equivalent DC voltage that would be able to supply the same amount of power to a load. So, 120V DC would make an old-fashioned filament light bulb shine at the same brightness as powering it from 120V AC.

Relays

You first worked with relays back in “Controlling DC Motors with a Relay” when you used a relay to switch a DC motor. The coil of a relay is isolated from the switching part, which is essential for safe AC switching. Most relays will have their switching capabilities marked on the package. For example, a typical “sugar cube” relay might say that it can switch 10A at 250V AC and 10A at 24V DC. 

Figure 13-2 shows how a relay might be used to switch an AC load. Remember that the relay coil requires a bit too much current to be driven directly from the digital output of an Arduino or Raspberry Pi.

Figure 13-2. Switching AC with a relay

The Control, 5V, and GND lines would be connected to your Arduino or Raspberry Pi. When Control is high, the transistor Q1 will turn on, energizing the relay coil that closes the relay contact connecting the AC Hot/Live line to one end of the load (device that you want to switch on and off). The other end of the load is connected to the Neutral AC line.

Optoisolator

Relays are an old technology and solid-state relays (SSRs ), which are discussed in “Solid State Relays (SSRs)”, are replacing them in many applications that switch AC. One key component of an SSR is an optoisolator. An optoisolator acheives the essential goal of separating the low-voltage switching side of the project from the dangerous high-voltage AC side.

Figure 13-3 shows a transistor-based optoisolator.

Figure 13-3. An optoisolator

An optoisolator combines an LED and a light-sensing element (usually a phototransistor) into a single plastic package. 

The key thing is that there is no electrical connection between the LED and the phototransistor, but only an optical link. When the LED is emitting light, the phototransistor will conduct. The phototransistor is low power and will need more electronics before it can control anything AC. 

The devices are pretty sensitive and so you can drive the LED side from an Arduino or even Raspberry Pi GPIO pin using a 1kΩ resistor (limiting the current to a couple of milliamps).

Zero-Crossing Optoisolators and Triacs

Optoisolators that are designed to switch AC have a few special features. First, at the light-sensing end, they do not have a normal bipolar phototransistor, but instead use a device called a photo-TRIAC (TRIode for AC). Figure 13-4 shows the internal design of such a device (for example, the MOC3031). You can download the datasheet for this device in PDF format.

A TRIAC is a specialized type of transistor designed to switch current flowing in both directions, which is just what you need to control AC.

One feature of a TRIAC is that once conducting, it latches on. It will stay on until the current flowing through it reduces to close to nothing. This makes it pretty useless for controlling DC, but because AC swaps polarity 120 times a second, the TRIAC will have the opportunity to turn off 120 times per second.

Figure 13-4. A zero-crossing optoisolator

An advantage of this latching behavior is that because the TRIAC will only turn off when the current through it (and hence the voltage across it) is low, this reduces the large switching current that would otherwise occur. Switching the current in this way also reduces electrical interference. This gentle switching action is enhanced by the use of the zero-crossing circuit included in some optoisolators. This delays the turning on of the TRIAC until the voltage crosses zero, ensuring that the turn on and the turn off are both smooth.

A typical circuit using a zero-crossing TRAIC to control a higher-power TRIAC is shown in Figure 13-5.

Figure 13-5. Switching AC with a zero-crossing optoisolator

The TRIAC that is built into an optoisolator like the MOC3031 is low current and only intended to be used to control a more powerful TRIAC that actually switches the AC.

Controlling the load from an Arduino or Raspberry Pi becomes just a matter of supplying a milliamp or two to the LED inside the optoisolator.

R2 and R3 limit the current flowing through the low-power photo-TRIAC in the optoisolator and R4 and C1 are there to “snub” any voltage transients that arise despite the gentle switching.

AC Switching in Practice

Don’t try to build the circuits described in this chapter on breadboard—it’s not safe. If you want your Arduino or Raspberry Pi to switch AC loads, follow the guidance in this section.

Relay Modules

Back in “Relay Modules” we explored ready-made relay modules. These have the big advantage when dealing with AC that you can connect the AC device that you want to switch using the screw terminals. The relay will naturally isolate the low-voltage side of the project from the dangerous high-voltage part of the design.

Relays do contain metal parts and could potentially fail in such a way that the hot/live side of the relay becomes connected to the relay coils. This could happen if, say, the relay was bashed hard or accidentally crushed. For this reason, relays often have the additional level of safety of using an optoisolator.

Using a Relay Module Safely

Always remember that a relay module like the one shown in Figure 13-6 will have bare metal conductors and solder joints on both the top and bottom of the board that will be hot/live. Touch these and it could be the last thing you do, so always have a plastic enclosure around the relay module and any other parts of the project so that your (or anyone else’s) fingers can’t inadvertently touch something. The parts inside the box should also be securely anchored to prevent anything from moving around.

Note the use of strain relief grommets that stop the leads pulling out and possibly becoming loose from the relay module’s screw terminal and shorting to something. 

Don’t work on the relay module or connect wires to the screw terminals when it’s connected to the AC, and fit the lid onto the box as soon as you have wired it up.

Figure 13-6. A relay module in a plastic enclosure

Also watch out for low-cost relay modules that are sold as being fit for AC use, as many of these are unsafe.

Do not be fooled into thinking that if it says 10A at 25V on the relay itself then the relay module as a whole is good for that. The small screw terminals on many low-cost modules are only rated at 2A. Some of these low-cost boards also have the relay contact solder pads very close (sometimes only a mm or two) to the low-voltage side of the circuit. This is dangerous at high AC voltages, and such relays should only be used for low-voltage DC at modest currents. The best relay modules have an optoisolator and a slot cut in the PCB around the COM relay contact for maximum isolation.

The safest way to switch AC is to use a ready-made and enclosed module like the PowerSwitch Tail (see “The PowerSwitch Tail”).

You should also check that your relay module is active-low or active-high. If you have an active-low relay module, then it will activate the relay when the digital output is LOW. This means that as soon as you set the pin to be a digital output, you also need to set the output to be HIGH on the next line, otherwise the relay could briefly turn on each time the Arduino resets:

pinMode(relayPin, OUTPUT);    
digitalWrite(relayPin, HIGH);

When using an active-low relay module with a Raspberry Pi, you can make use of the optional parameter initial to set the output HIGH:

GPIO.setup(relay_pin, GPIO.OUT, initial=True)

It is common for relay modules that have an optoisolator to also have the option of removing a jumper link to allow the positive supply to the relay coil to be provided independently of the positive side of the optoisolator’s LED. This provides an additional level of isolation, but does mean that a separate power supply is needed. 

Solid State Relays (SSRs)

Figure 13-7 shows an SSR module. This is a sealed and enclosed unit that contains a circuit that is probably very similar to the one shown in Figure 13-5.

Figure 13-7. An AC SSR

These devices are readily available and make AC switching very easy. You still have the problem that there are exposed metal parts that will be hot/live, and so the whole device needs to be enclosed in an insulated box.

The device’s low-voltage side can be connected directly to a Raspberry Pi or Arduino, as they include a suitable series resistor for the LED. 

The PowerSwitch Tail

The PowerSwitch Tail (Figure 13-8) is an SSR that has an AC plug on one end and an AC outlet on the other. 

Figure 13-8. A PowerSwitch Tail

There are screw terminals that connect to the LED side of the optoisolator (series resistor built-in) and a small red LED that also lights when the SSR is activated. You will use one of these very handy devices in “Project: Raspberry Pi Timer Switch”.

Project: Raspberry Pi Timer Switch

This project uses a Raspberry Pi and a PowerSwitch Tail to control the power to a small electrical appliance. In Chapter 16 this very basic project will be improved to add a web interface, so that you can use a browser to switch the appliance on and off (see “Project: A Raspberry Pi Web Switch”). 

This project is very easy to make; the only tool you will need is a screwdriver to unscrew and then tighten the screw terminals.

Parts List

In addition to your Raspberry Pi, you will need the following parts to build this project:

Part Sources
PowerSwitch Tail Adafruit: 268
Female-to-male jumper wires Adafruit: 826
Table lamp or other small appliance  

Construction

Figure 13-9 shows the wiring diagram for the project.

Figure 13-9. Wiring diagram for the timer switch project

If you look closely at the PowerSwitch Tail’s label, it says that the input is 3-12V DC at 3-30mA. The current required for the input will vary according to the voltage, so the lower current end of the range corresponds to the 3V input. Actually, at 3.3V, the PowerSwitch Tail does draw about 6mA, which is fine for a single Raspberry Pi GPIO pin. 

You don’t need to attach anything AC to the PowerSwitch Tail while you are testing because its status LED will light when the SSR is switched on.

Software

You can find the software for this project in /python/projects/ac_timer_switch.py (for information on installing the Python code for the book, see “The Book Code” in Chapter 3):

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

control_pin = 18

GPIO.setup(control_pin, GPIO.OUT)

try:         
    while True:     1
        duration_str = input("On time in minutes: ") # 2
        duration = int(duration_str) * 60  3
        
        GPIO.output(control_pin, True)  4
        time.sleep(duration)
        GPIO.output(control_pin, False)  5          
        
finally:  
    print("Cleaning up")
    GPIO.cleanup()

The program is also very simple; it starts with the usual imports and constant definitions.

1

While True is a way of making the loop continue forever, as the condition True is never False. This is the only reason a while loop ever finishes (unless you press Ctrl-C).

2

The main loop prompts you to enter a number of minutes that you want the light to be on for.

3

Convert the string value of duration as a string into an integer number of minutes using int and then multiply by 60 to convert it into seconds.

4

The GPIO pin 18 is set to high (True) to switch on the PowerSwitch Tail, turning on whatever is plugged into its AC outlet.

5

After the appropriate delay, the GPIO pin is set LOW to turn off the SSR and the loop starts again, prompting you for a new “on time.”

Using the Project

The PowerSwitch Tail can switch up to 15A, so you can plug most things into it apart from very high-power devices like electric kettles or hair dryers. A small table lamp might be a good starting point.

Run the program and when it prompts for an “on time” enter 1 for 1 minute and press Enter. Whatever is plugged into the PowerSwitch Tail should turn on and the little status LED on the PowerSwitch Tail itself should also light. At the end of the minute, the PowerSwitch Tail should switch off.

Summary

This chapter highlighted some of the dangers of using high-voltage AC, but also showed that it’s pretty easy to switch things on and off with the right hardware.

In the next chapter, you will learn about using displays with an Arduino or Raspberry Pi. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.180.70