Zonnestroompanelen in Nederland

duurzaamheid achter de meter

(21) Arduino combined with a switching relay

by Floris Wouterlood – The Netherlands – October 12, 2020

Summary

If one wants to use an Arduino to control electric home appliances it is necessary to have a switch at hand that can handle voltages and currents higher than the tiny Arduino can deliver. An Arduino-controlled switching relay may be handy or even necessary under these circumstances. This paper describes how to implement a 5V switching relay.

Introduction and a word of caution

Arduinos operate with 3.3V and 5V DC. These voltages and the associated currents (max 40 mA per pin; total no more than 150 mA, or 0.7 W in total) are sufficient to power chips and LEDs. In home automation however, one wants to switch or dim room illumination or control home appliances that typically work with high voltages, in Western Europe 230V AC. While the DC voltages used in the Arduino environment are unharmful and in accidents do not produce more than some smoke and smelling from fried microprocessors, grid-connected 230V AC can be very harmful, may cause sparks and fire, and is potentially lethal. So, if you feel that you are unqualified or inexperienced, stay away from interfacing Arduinos and grid operated home appliances. People who start tinkering with 230V AC grid appliances should know what they are up to, and continuously be aware that one is experimenting with potentially lethal currents and voltages. Take appropriate precautions.    

Figure 1. High voltage wiring diagram: The switching relay is connected on its low voltage side with 5V, GND and a switch control wire (green) in this case to pin D2 of the Nano. The two contacts (‘NO’ and ‘COM’) on high voltage side acts as the poles of a switch in the 230V power supply of the appliance.
Figure 2. On the workbench. The Arduino Nano controls via its pin D2 the relay that, in turn, switches the lamp on and off. Note that the Arduino and the relay need their own 5V DC power supply. In this setup a green LED attached to pin D5 of the Nano indicates whether pin D2 is HIGH or LOW

Arduino relays

Several manufacturers offer miniboards like the one shown in figure 1 that contain a switching relay that supports home appliances with a power consumption of up to 2,500 Watts. This amount of power is sufficient to run a home coffee maker, a score of lamps or a small pump. The switch contains an optocoupler which guarantees complete isolation of the low-voltage DC side from the high voltage 230V AC side. Safety first here because an Arduino does not live long when exposed to 230V AC. Nor does a USB port on your computer AND your computer.Connecting a switching relay First identify the low and high voltage sides.

  • The low voltage side has a three-pin female pin header labeled VCC, GND and IN. The high-voltage side has a three-screw connector that is often labeled NO, COM and NC (in figure 1 indicated with Chinese characters).Low voltage side: On the low voltage side, VCC is connected to the 5V pin of the Arduino, GND to GND, and the pin IN can be connected with any free digital pin of the Arduino. Because the Arduino has in principle 18 of such pins, 18 switching relays can be supported simultaneously. Here we use pin D2.
  • The high voltage side: the middle pin (COM) and pin NO (NO stands for Normally Open) form the two poles that together from the high-voltage switch. Note that these relays work equally well with lower ‘high’ voltages, e.g. 125V AC in the USA, or 20V DC and even 5V DC (figure 3). The pin NC (Normally Closed) can be used in conjunction with a second switch, e.g. in a staircase or in a long corridor with on both ends a light switch (‘hotel’ switch). NO is connected with the appliance, COM is connected to a 230V wall plug (figure 1 – you can also connect COM with the appliance and NO with the wall plug).

Electronic components needed

1x Arduino Nano microcontroller board, prototyping breadboard, 1x switching relay, home appliance – here a 230V AC 5W LED lamp in an E27 socket.>Figure 3. You can use a relay switch to switch all kinds of voltages and currents – as long as you stay within the specifications printed on top of the relay – In this configuration 5V DC power is switched. An ‘event LED’ is connected with the high voltage/current side of the relay and is controlled by the Arduino.

 

Figure 3. You can use a relay switch to switch all kinds of voltages and currents – as long as you stay within the specifications printed on top of the relay. An ‘event LED’ is introduced here to report the switching state.

Sketch The sketch, ‘relay_switch_nano_blink.ino’ is quite straightforward. An output pin is defined and this pin is set HIGH for 4 seconds alternating with 1 second LOW A HIGH state at pin D2 causes the relay to open its switch and keep it that way as long as the HIGH state lasts. The secondary circuit is interrupted and any appliance here will be OFF. A LOW state at pin D2 causes the relay to close its switch and keep it that way as long as the LOW state lasts. The secondary circuit is closed and any appliance here will be ON.

/ relay_switch_nano_blink
//
// instructs a 5V relay switch to alternate between OFF for 4 seconds and ON for 1 second
//
// October 11, 2020
// Floris Wouterlood
// public domain

#define relayPin 2 // we are using pin D2 here to control the relay

void setup() {
Serial.begin (9600); // initialize serial communication
Serial.print (“let’s start”);
Serial.println ();
pinMode (relayPin, OUTPUT);
}


void loop() {

digitalWrite (relayPin, HIGH);
Serial.println (“relay HIGH – switch open”);
Serial.println (“HIGH = no current can pass in secondary circuit”);
Serial.println ();

delay(4000);

digitalWrite (relayPin, LOW);
Serial.println (“relay LOW – switch closed”);
Serial.println (“LOW = current flows in secondary circuit”);
Serial.println ();

delay (1000;

}

Downloadable sketch:

relay_switch_nano_blink.ino