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.
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.
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: