Arduino + relay module and rock & roll: mixing AC/DC

Logos AC/DC and Arduino

After our programming tutorial and first steps in Arduino, this time we bring you a practical guide to work with Arduino and a relay module, that is, to be able to control, through Arduino’s low voltage DC circuitry, a higher voltage AC system. That is, what seemed impossible with a simple Arduino board, such as controlling 220v loads, is now made possible with the relay module.

This will allow you to control devices connected to the power grid. And in order not to be too restrictive in terms of practices, I will try to explain it in a way that can be applied to any kind of project you can think of or modified in an easy way to do what you really want, since there are many very specific projects on the Internet that use an Arduino board and a relay module .

The relay:

Let’s explain everything you need to know about relays.

What is a relay?
relé

In French relais means relay, and that gives a hint of what a relay actually does. Basically it is an electromagnetic device that works like a switch controlled by a current. By means of a mechanism with a coil and an electromagnet, one or several contacts can be operated to open or close an independent electrical circuit, since this circuit works with a different voltage and type of current than the one that controls it (at its output it handles a circuit of greater power than the input one).

It was invented by Joseph Henry in 1835 (although it is also attributed to Edward Davy in the same year) and has since evolved and changed in size to the modern relays we have today. Initially it was used for telegraphy machines, controlling a higher current signal from a weaker signal received at the input. Little by little the applications were increasing and nowadays they are used for a variety of cases.

What types are there?
diagram of relay operation

If we look inside a relay, and we analyze its operation, we see that the small input control current is what drives the electromagnet with that copper winding and moves the switch or circuit breaker that opens or closes the higher power circuit that will control its output. All this is isolated by means of an insulating protector to avoid accidents, but independently of this, I am interested in something else and it is the types that exist according to their operation.

The types of relays we have can be seen from different points. On the one hand, we must focus on the opening or closing mechanism of the breaker and according to that we have

  • N.A. or normally open: as its name indicates, these are those that without operating the electromagnet, the contacts of the output switch or switch are open, there is no electrical connection between them and therefore the circuit will be disabled or open in its normal state. When you act on the input to change that, at that moment the switch terminals will be touched and the circuit will be closed, that is, it will let current pass.
  • N.C. or normally closed: it is the opposite of the previous one, the output circuit in its normal or resting state will let the current flow. On the other hand, as soon as the input is acted upon, the circuit is opened and the current is interrupted.

This is very important to know when buying a relay depending on the project we want to create. You must think what is the most normal for your project, that the device or devices connected to the relay are always active or that you only want to activate them at certain times. Depending on that it would be better to choose one or the other.

By example, an irrigation system in which you connect a water pump to the relay so that it activates when you want, it will be better to choose a N.O. one, since only when you order from the Arduino platform should the pump be connected. On the other hand, in a safety system where it needs to be permanently connected and only disconnected at certain times, a N.C. will be more appropriate. That way you will avoid having to constantly feed the relay from the Arduino board to force a state that is not normal .

But independently of that, there are other types of relays according to other points of view, such as the mechanisms that activate them. The classics are the electromagnetic ones we have described, and they are the most popular. But there are also others that can be activated by optocoupled devices, that is, based on solid state. Another interesting type is the delayed output, that is, relays that have an additional circuit so that the effect on their output to open or close the circuit is after a certain time and not immediately.

Single relays and modules:

relay module for Arduino

You can use many types of relays for your projects, such as those sold separately, if they fit the electrical capabilities of the Arduino board at the input. However, the easiest way to avoid surprises of incompatibilities if you don’t know what you are buying is to use modules specifically designed for Arduino. There are modules with a single relay whose connection to our Arduino board is very simple, but there are also double ones like the one you can see in the picture above.

Such dual modules usually include a NO relay and an NC relay so you have everything you need for your project and can test both options with a single module mounted in a mount like the Keyes boards you will find in the market.

How do you connect and program with Arduino?

connection diagram with Arduino and the relay

Here is a simple diagram of Arduino’s connection to the relay module. The connection is very simple, as you can see. Obviously, if you have chosen a module with only one relay or a single relay that you have purchased, you will have to modify it slightly to connect it properly. By the way, if you have chosen a double relay module, you can use one or the other relay as it suits your project best as I have already mentioned.

As you can see it would be simply to put a cable from GND or ground that you will have to connect to the GND pins of your relay or module. Then the Vdc line should go to one of the 5v pins of Arduino. That will be all that is needed to power the relay, but a third “string” control line is needed to “tell” the relay to operate when we want it to or when we have programmed it in our sketch code.

Respect the safety margins of the relay, for example, do not exceed those 250VAC and 10A maximums specified by some relays. And be careful when manipulating this circuit, since you are not only “playing” with low voltages of direct current that do not affect you, but you can suffer damage if you are not careful when manipulating those 220v…

This control or signal line can be put into any of the programmable digital output pins of your Arduino and from there to the input marked IN on the relay module. Even if you used 2 in our scheme, you can use whichever one you want, but remember which one you used to modify the code properly or it won’t work if you specify a different one (very common error).

I need to comment on two other details of the scheme, one would be that where I put “here your device / s” you could connect a light bulb, a fan, AC motor or any device that works with a 220v line. Of course, you will have to power it up by plugging that device or devices into an electrical network. To do this you can modify the power cable of the device by interrupting one of its two power cables (not the earth one, if it has one), interposing the relay that opens or closes the circuit.

Programming Arduino:

You can do it with Arduino IDE, with Ardublock or Bitbloq, that is to say, as it is more suitable for you. The simple code for programming would be the following, although you can modify the code or extend it according to the needs of your project:


const int relay = 2;
/***Setup***/
void setup() {
pinMode(relay,OUTPUT);}
/***Loop***/
void loop() {
digitalWrite(relay, XXX);
}

You can change XXX to HIGH or LOW depending on what you want to do, i.e. turn it on or off respectively. But remember that you have to keep in mind if it’s a NC or an NA… Of course, you can add more code to program a timing, or to activate or deactivate it depending on an event, maybe the input or the state of another Arduino input, like adding a sensor and depending on whether or not it activates to make the relay change, etc.

You know the possibilities are endless and the limit is your imagination. You can see more possibilities and code examples in our tutorial. For example, to add times for it to turn on and off in 1 minute intervals we could use

const int pin = 2;

void setup() {

Serial.begin(9600); //initiate serial port  pin

Mode(pin, OUTPUT); //define pin as output

}

void loop(){

digitalWrite(pin, HIGH); // set pin to HIGH (activate relay)

delay(60000); // wait a min  digital

Write(pin, LOW); // set pin to LOW (disable relay)

delay(60000); // wait a min

}

I hope this tutorial was useful for you and that you get your high voltage projects going…

Leave a Comment

*

*