The Arduino is a microcontroller, which means it’s a small device made to control things. Today we will see how we can control AC devices with an Arduino.

Since most of you will probably want to control devices that use line (mains) voltage we’ll be paying particular attention to safety.

Introduction

You can control a lot of things with an Arduino. Displays, LEDs, and other devices can easily be hooked up to the I/O pins and controlled with a simple sketch.

But the output ports on an Arduino, or any microcontroller, are only designed to control logic-level devices. Interfacing to anything that requires more than a logic-level signal is going to require external components.

We have already seen how we can control DC operated devices by using BJTs (Bipolar Junction Transistors) or MOSFETs. Using these components we can switch voltages and currents that would otherwise destroy our little microcontroller.

Control AC Current with an Arduino

Controlling AC devices requires different techniques. Semiconductors that can switch DC current can only pass part of the AC waveform, so they can’t be directly used to switch alternating current.

The “different techniques” we can use to control AC range from semiconductors to electromechanical devices.

But, before we look at any of the ways we can control AC with an Arduino we need to discuss a very important consideration – safety.

Safety First!

While it is possible that you only intend to control low-voltage AC devices it is more likely that you’d like to learn how to control the equipment plugged into the outlets in your home or office. After all, it’s much more useful to switch on a desk lamp than it is to turn on an LED.

But before you start planning how you’ll control your entire home with a few Arduino’s you need to consider the dangers involved in working with high-voltage AC power.

Alternating Current 101

Alternating Current, or AC differs from Direct Current (DC) in that it constantly changes polarity, oscillating like a sine wave between positive and negative at a predetermined frequency.

That frequency can vary depending upon where on the planet you are, in North America it is 60Hz while in most other parts of the world it is 50Hz. Some industrial applications use higher voltages and frequencies as high as 400Hz, but we won’t be considering these in our experiments.

Another thing that varies worldwide is the voltage level, North American residents have voltages from 110 to 120 VAC delivered to their homes while many other countries use a higher 220 to 240 VAC.

Danger - High Voltage!

Please note that despite using a lower voltage North American AC is just as dangerous, as it’s the combination of voltage and current that needs to be considered. Also, North American homes actually do have 240 VAC delivered to them but it is split into two phases, each one carrying half the voltage. These “halves” are distributed around the home, and the full 240 VAC is made available for appliances like dryers and stoves.

Coming into contact with such high voltages can be dangerous, in fact, it can be fatal!

Here are a few guidelines for working with AC:

  • NEVER experiment directly with high-voltage AC.
  • NEVER connect high-voltage AC to a solderless breadboard.
  • NEVER connect high-voltage directly to an I/O pin (or any pin) on a microcontroller.
  • ALWAYS work on AC circuits with the power off. Confirm that the power is truly off with a multimeter.
  • ALWAYS test your wiring with a multimeter before applying power.
  • Do not leave high-voltage work unattended and powered on. Always remove power from the circuit if you are not present, to avoid the risk of injuring others.
  • Do not attempt to work with high-voltage AC if you are inexperienced.

Another thing that you might want to consider is insurance and liability. Wiring up your own equipment is fun and rewarding, but connecting home-brew equipment to your AC power line may very well void your fire insurance, as your device is not certified.

If I haven’t turned you off from working with AC then read on, and I’ll show you a SAFE method of experimenting with AC and the Arduino.

And at the end of the article, I’ll show you what is probably the safest way of controlling AC devices, using a pre-built product.

Safe Workbench Setup

As noted above, you NEVER want to experiment with 120 VAC (or 240 VAC) on your workbench. So then how do you develop circuits to control AC when you can’t test them with actual AC line voltage?

The answer is pretty simple – use AC but use a lower, and safer, voltage. And, for added protection, minimize the amount of current you can draw.

Develop your circuit using a low voltage that you can safely experiment with. Then, if you want a final design that works on actual AC line current, you can build a final prototype or product using safe construction techniques.

I use two different types of transformers to safely work with AC:

  • Step-down Transformer – This device steps the AC voltage down to a lower level. I chose a 28-volt transformer as it is a low enough voltage to be safe yet is high enough to be used with solid state switches (more on those in a few moments).
  • Isolation Transformer – This device outputs the same voltage as its input, so in my case it outputs 120VAC. While this is still a dangerous voltage it is current-limited by the transformer. It is also a good device to use to test your final, constructed project, as if you do have a wiring error you won’t be affecting your entire electrical supply.

After choosing a step-down transformer you’ll need a load to test with as well. I used some 28-volt incandescent light bulbs, but even a power resistor will do the trick. You can often salvage a transformer out of an older appliance and save a few dollars, although a new transformer is not exactly expensive.

Low-Voltage AC Workbench Setup

The isolation transformer is optional, but I highly recommend it if you plan on doing a lot of development using AC voltages. It will limit the amount of current you can draw, and can also be used to test your final product.

Controlling Alternating Current Devices

So now that we know how to SAFELY work with AC voltages we’ll need to learn how to control these voltages with an Arduino or other logic-level device.

There is more than one way of doing this, we’ll look at a few of them.

Silicon Controller Rectifier (SCR)

The SCR is a component that has been around for a long time, and it has found use in devices like light dimmers and motor speed controls. Some people also refer to it as a “Thyristor”, while others say it is part of the Thyristor family.

Silicon Controlled Rectifier

In many ways, you can think of an SCR as a “switchable diode”. DC voltage applied to the Gate will allow current to flow through the Anode and Cathode.

But, of course, since the device behaves a lot like a diode it has the same characteristics – it only allows current to flow in one direction.

SCR AC Input

SCR - AC Output, half-wave

So really an SCR would be better at conducting DC than AC. So why use them?

The answer is you need to use two of them, with Anode and Cathode reversed and the Gates tied together. This will allow the current to flow in both directions.

Dual SCR - Full Wave Output

Triode AC Switch (TRIAC)

Connecting two SCRs back-to-back is one way of controlling AC, but a better way is a TRIAC.

TRIAC

Essentially a TRIAC is exactly the same as two reverse-connected SCRs in a single package. Making it the ideal semiconductor for switching and controlling AC.

You’ll find TRIACs in a number of devices ranging from lamp dimmers to heater controls. In fact, they are one of the key components in the next AC controller device on our list.

Solid State Switch (SSR)

A Solid State Switch can also be referred to as a Solid State Relay, or SSR.

Internally a Solid State Switch has a TRIAC on the output and an optoisolator on the input.

Solid State Switch

An optoisolator is a simple device with an infrared LED and a phototransistor in an enclosed package. Lighting the LED will switch on the phototransistor, essentially transferring a digital signal with an optical connection. With no electrical connection, an optoisolator is a great way of keeping high-voltages and low-voltages apart.

This configuration makes the Solid State Switch an ideal component for safely controlling AC current with a microcontroller like an Arduino. It’s one of the two components I would recommend.

Solid State Switches

However, Solid State Switches are not perfect. A few characteristics you need to be aware of:

  • They are more expensive than just buying a TRIAC and optoisolator. I think the extra expense is justified by having a prewired circuit in a sealed enclosure.
  • Many of them won’t work on lower voltages, so switching low-voltage AC will require a different technique. The one I used in my demo requires a minimum of 24 VAC, which is why I’m experimenting with a 28 VAC transformer.
  • They slightly distort the AC sine wave. Note the use of the word “slightly”, most devices won’t be affected.

For a semiconductor solution for switching AC the Solid State Switch (Solid State Relay) is a great choice.

Relays

A relay is an electromechanical component, not an electronic one. It has been around longer than you have, with the first relays used in telegraph systems as far back as 1835.

Relay

The principle of relay operation is very simple. A relay has a coil, which acts as an electromagnet. When current is applied to the coil the magnetic field it produces attracts a metal arm, known as an “armature”. The armature acts as the arm in a switch, connected to one pole when the relay is activated, and another one when it is not activated. A spring within the relay pulls it back into its resting position when the current is removed from the coil.

As a relay is essentially an electromagnet and a switch its specification are twofold:

  • The coil specification, which includes the voltage required and the current consumed.
  • The switch specifications, which include configuration and current/voltage capacity.

SPDT (Single Pole Double Throw) and DPDT (Double Pole Double Throw) switch configurations are the most common, but there are specialized relays with four or more poles.

As they are essentially mechanical switches relays are perfect for switching both AC and DC. And, despite the age of this technology, they are very much in use today, and will be for a very long time.

Relays have a number of advantages over other solutions:

  • They are very easy to use, as they are essentially an electromagnet and a switch.
  • They are inexpensive.
  • They are available in a wide variety of coil voltages and current ratings.
  • They can be used for low-voltage signals, both AC and DC.
  • They do not distort, amplify, or modify the switched signal in any way.

Of course, relays also have a few disadvantages.

First, a relay coil usually consumes more current than the digital I/O ports on the Arduino can supply. So instead of directly interfacing a relay to your Arduino, you will need to use a transistor to drive it. Depending upon the coil specification you might also need a separate power supply.

Second, when a relay is activated the action of the armature being pulled towards the coil creates an electrical pulse in the opposite polarity to that applied to the coil. This pulse, referred to as the “back EMF” or “back EMP”, can damage the transistor you are using to drive the relay.

Relay with Transistor and Diode

The solution to this second problem is to install a diode across the relay coil to absorb the back EMF.

A few other relay issues:

  • Being electromechanical devices relays are sensitive to vibrations and shock.
  • The relay contacts can wear or arc after a while, degrading the connection or even destroying the relay.
  • Relay coils consume more current than semiconductor solutions, not really suitable for battery-powered designs. Remember, as long as the relay is active its coil is consuming current.

So, while they are not perfect, relays are still a great solution for controlling AC.

And in order to make them even safer and easier to use we will move on to the next selection – relay modules.

Relay Modules

A Relay Module is simply a relay with the transistor driver and back EMF suppression diode already installed. Many relay modules also incorporate an optoisolator on the input, adding an extra layer of isolation.

Another thing relay modules usually have are screw terminals to connect the switched device to. This can greatly simplify the safe wiring of high-voltage AC on the output.

Relay Module Schematic

Relay modules come in many styles, some with only one relay and some with up to 32 relays. They can be strapped to use the Arduino power supply or an independent one.

Relays and Relay Modules

In short, relay modules make interfacing a relay with a microcontroller like the Arduino a simple task. And for that reason, they, along with solid state switches, are the two methods I recommend for interfacing an Arduino with high-voltage AC devices.

Using Relay Modules with Arduino

We will begin our Arduino Interfacing experiments using a relay module. You may use any module, with one or more relays. In fact, the module I used in the experiment is a dual-relay model, I’m only using one of the relays.

When driving a relay module you should be aware that they are usually “active low”, meaning that you need to send them a LOW (ground) signal to activate the relay. If you happen to come across a module that is “active-high” you can change the code to use HIGH logic instead of LOW logic. Another, even simpler, solution would be to just wire the output load to the opposite set of contacts (NC instead of NO).

In order to make the experiment more interesting I’m using a Light Dependent Resistor (LDR) , sometimes called a “photocell” or “photoresistor”, to sense light and activate the relay when it drops below a specific level.

This actually turns this experiment into a practical project, where you could turn something (a lamp, for example) on or off when the light gets below a certain level.

Relay Module Hookup

The hookup for the relay module experiment is illustrated here.

Arduino Light-Controlled Relay Hookup

Just about any LDR can be used, you could also substitute another resistive device such as a thermistor (a resistive temperature sensor) for it. The resistor I used was 10k, but you can experiment with different values to “fine-tune” your LDR.

On the relay module, you will see a place for a jumper between the “VCC” and “JD-VCC” pins. You’ll need to insert a jumper here to power the relays from the Arduino power supply. We can do this as we are only using one relay in the module, if you wanted to use two or more then you would be better off providing your own power supply and removing the jumper.

My output load is my 28VAC transformer (which in actual life measured more like 35 VAC) and a 28-volt incandescent light bulb (Mouser & Digikey have these). For this experiment, any load and any low voltage will work.

After you wire it up you can load some code.

Relay Module Sketch

The sketch for the light-sensitive relay is shown here:

This is a very simple sketch, all it does is to measure the level of voltage on the analog input and trigger the relay once it drops below a specified level.

It states by defining integers to represent the analog input pin, the resulting analog input value, and the pin used for the relay output. None of these pins are critical, so you can use other ones if you wish.

Another integer is defined to represent the threshold, the point at which the relay will trigger. You may want to experiment with the value to achieve the proper trigger point for your situation or to compensate for a different LDR than the one I used.

In the Setup, the pin used for the relay is defined as an output. The serial monitor is also initialized, you can use this to monitor the reading obtained from the LDR and use that value to determine the optimum threshold value.

In the Loop, the sensor value is read and assigned to the ldrValue variable. That value is then used within an if-else statement to decide what action to take.

If the value is below the established threshold then the relay is activated. Note that the relay will be activated when the relay module input goes LOW, which may seem counterintuitive but that is how most relay modules work. If you happen to run across one that uses active-HIGH inputs then you’ll need to change the sketch accordingly.

LDR Arduino Relay On

Otherwise, the relay output is set HIGH to turn off the relay.

LDR Arduino Relay Off

Finally, we apply a short time delay, to prevent relay “chattering” when the light level is close to the threshold. This also prevents the system from being triggered by a very short absence of light (ie. someone walks in front of the sensor).

Load the sketch to the Arduino and observe the operation. When the LDR is exposed to light the relay module will be inactive, and the incandescent AC load will be off. If the light falls below a specific level (you can put your finger on the LDR to block it) then the relay module should trigger.

A simple circuit, but it has a lot of potential applications.

Using Solid State Switches with Arduino

For our next experiment, we will be using a module with four solid state switches (SSRs). We will be using this to build a marquis-style light chaser.

While I’ll be using my 28-volt bulbs on the workbench for safety, this circuit could also be used to drive full-sized light bulbs, to create a real marquis display. You can also easily modify it to have more than four segments.

Solid State Switch Hookup

The hookup for the light chaser is illustrated here:

Arduino SSR Chaser Hookup

If you don’t have a 4-SSR module you could substitute four individual solid state switches, the module simply makes the wiring easier.

Also, although I only show four incandescent light bulbs on the output I actually used eight in my test. I wired the additional four bulbs in parallel with the others.

The potentiometer was just a 10K linear-taper pot, but pretty well any linear taper pot over 5K would work properly.

Once you have your chaser hooked up you can focus on the sketch.

Solid State Switch Sketch

Once again we are using a simple sketch for our project, however, this sketch does illustrate a technique for reducing code that I don’t believe I’ve shown you before. So it’s valuable as a learning experience as well!

The first integer we define is outputPins, which defines the number of pins we are controlling. If you want to use more (or less) than four outputs you can adjust the value of this variable accordingly.

The “code reducing” technique starts with the second definition. Instead of defining discrete variables to represent the four output pins I’ve placed them into an array instead. Note the order of the pins within the array represents the order they will be sequenced in the chaser. You may add additional output pins or change the order if you wish.

The next two integers define the analog input for the speed-control potentiometer, as well as the value of that analog input voltage.

In the Setup, you can really see how the “code reduction” works.

We need to define all of our four pins as outputs. We also need to set them so that they initialize in a HIGH state, which will keep the solid state relays off when the project is first started.

Normally we would need eight lines of code to do this. If we expanded the number of outputs we would need more, with 10 outputs we would need 20 lines of code to accomplish this.

But, as our output pins are defined within an array, we can use a lot less code. We use a for-loop to cycle through the array elements and for each one we assign it as an output and set it as HIGH.

In the Loop, we get the value of the analog input and map it to a range that produces a reasonable time delay, which results in our chaser moving at different speeds in response to the potentiometer.

There are a few changes you might want to make here to customize the project for your personal needs:

  • You can change the range (40-400) to one more suitable. The lower number is the fastest speed, the higher the slowest. Remember. though. that incandescent bulbs can’t respond as quickly as LEDs.
  • You can reverse the numbers if you want the potentiometer to operate in the opposite direction, which many might feel is more intuitive.

After getting the speed (time delay) we go into another for loop, just as we did in setup. This time we cycle through the helmets and set them LOW (to activate the solid state switch). We then keep the SSRs activated for the delay period determined by the potentiometer position, and then set the output HIGH to turn off the SSR.

The result is the solid state relays will activate in sequence, thus creating the “chaser” effect.

Load the sketch and observe the outputs. If your SSR module is similar to the one that I used then you should see the LED indicators on the module “chase” as well.

Arduino SSR Light Chaser

You can make adjustments to fine-tune the performance, as noted earlier.

This is just a cute little demo, but it could be used as a practical circuit to build a real, full-sized, chaser light assembly.

IoT Control Relay

After reading all of this you might still be hesitant about working with AC. And there are many valid reasons to feel that way.

Experimenting with low-voltage AC in order to design circuits to control line voltage is a great method of development. But sometimes you just need to get something built, without all the experimenting.

Another consideration when putting together a high-voltage controller is certification. There are a number of certification boards around the world that certify electrical equipment, and in many countries, all equipment attached to your home electrical circuits must have this certification. Many insurance policies won’t pay out if your home is damaged due to an uncertified device.

Certification is an expensive and complicated process, so most (if not all) experimenter designs can’t obtain it.

And another reason for avoiding working with high-voltage AC is, of course, safety. If you have never worked with high-voltages then you should proceed with caution.

If you feel overwhelmed by some, or all, of the above, then I have a solution for you. And it’s as easy (and safe) as using a power bar.

There are many devices that already have the high-voltage wiring done for you. They are certified for use, properly constructed and easy to use.

The device I made use of is the IoT Relay. It’s essentially a mechanical relay in a box, with isolated control circuitry.

This device has four outlets. Two of them are Normally Open, so they are switched on when a control signal is applied. One is Normally Closed, so it is switched off when the signal is received. And the final one is always on, convenient for powering the Arduino itself.

The control input on the IoT Relay can be activated with a range of voltages, both DC and AC. On the DC side, it can activate with voltages as low as 3.3-volts, making it suitable for control by both 3.3-volt and 5-volt logic devices.

Operation is simple – apply the control voltage and the relay is activated.

In the video, I demonstrated the use of the IoT relay with an Arduino. I connected the relay input positive to the Arduino output pin 13, and the negative to the Arduino ground. Sending a HIGH signal to pin 13 activated the relay, as expected.

IoT Switch with Light Bulb

For a sketch, I simply used the infamous Blink sketch! And the result was a flashing light.

If you want to control AC with an Arduino, or any microcontroller or microcomputer, this is a very good and extremely safe method.

Note that I live in Canada, where we use 120VAC line current (as they do in the United States). You can find similar devices to the IoT Relay for other countries as well.

Conclusion

Being able to control AC devices can really expand the number of projects you can build with an Arduino, but you have to do it safely. Experimenting with an isolated source of low-voltage AC is a way of staying safe during development.

In a future article and video, I’ll show you how to go beyond turning AC devices on and off, we’ll see how you can regulate AC to control brightness on light bulbs or the output of a space heater.

Until then stay safe!

Resources

Code for this Article – The sketches used in the article and video, in a handy ZIP file.

IoT Relay – The IoT Relay, a simple and safe method of controlling AC devices.

Electricity around the World – A Wikipedia reference to line (mains) voltages around the world.

Explosion Protection Basics – Understanding some of the safety  the ratings on electrical equipment (submitted by Pugwash)

 

Controlling AC Devices with Arduino
Summary
Controlling AC Devices with Arduino
Article Name
Controlling AC Devices with Arduino
Description
Learn how to safely control Alternating Current devices with an Arduino microcontroller. I'll show you how to safely experiment with AC on your workbench.
Author
Publisher Name
DroneBot Workshop
Publisher Logo

If you have a question...

Comments about this article are encouraged and appreciated. However, due to the large volume of comments that I receive, it may not be possible for me to answer you directly here on the website.

You are much more likely to get answers to technical questions by making a post on the DroneBot Workshop Forum. Your post will be seen not only by myself, but by a large group of tech enthusiasts who can quickly answer your question. You may also add code samples, images and videos to your forum posts.

Having said that, please feel free to leave constructive comments here. Your input is always welcome. Please note that all comments may be held for moderation.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x