Example Code for Arduino-Serial Controlled Relays
This article offers a detailed guide on using Arduino to control relays, including hardware preparation, software setup, wiring diagrams, and sample code. It explains how to safely connect and control lamps or appliances through relays using serial inputs, emphasizing the importance of careful handling of live circuits.
Hardware Preparation
- Arduino UNO
- Relay Shield For Arduino V2.1
- Power Supply:7~ 12V
Software Preparation
Arduino IDE
Library installation
Wiring Diagram
In this sample connection diagram it shows how LED can be controlled via relays, while this is a bit of overkill for controlling LEDs its only meant to be an example. These relays can be used to control lamps, or some other mid-voltage range appliances.

Other Preparation Work
Set the serial monitor baud rate to 57600.
Ensure the PROG SWITCH is set to RGN after programming.
We will use "NO" for our example, using "NC" will simply reverse the logic, as explained above.
We recommend using a swappable cable to do this with, as using a relay requires you to perform some minor surgery on the appliance's cable. To plug in an appliance such as a lamp: Cut and strip a portion of the positive wire so that you end up with two ends of the wire as shown in Figure 2.
The relay should have the positive wire of the device being used connected to "COM" and to "NO" as shown in figure 2, and any digital signal pin on the arduino end (For example pin 13). Sending a digital high or a "1" will trigger the relay. Sending a digital low or "0" will disable the relay.
It needs an external power supply to drive the xbee module and relays. Suggested: 7-12V.

Sample Code
/*
# This Sample code is for testing the Relay shield V2.1 for Arduino.
# Editor : Phoebe
# Date : 2013.2.28
# Ver : 0.1
# Product: Relay shield for Arduino
# SKU : DRI0144
# Hardwares:
1. Arduino UNO
2. Relay Shield For Arduino V2.1
3 Power Supply:7~ 12V
*/
byte relayPin[4] = {
2,7,8,10};
//D2 -> RELAY1
//D7 -> RELAY2
//D8 -> RELAY3
//D10 -> RELAY
char input=0;
int val;
void setup() {
for(int i = 0; i < 4; i++) pinMode(relayPin[i],OUTPUT);
Serial.begin(57600);
delay(100);
Serial.println("Press 1-4 to control the state of the relay");
Serial.println("waiting for input:");
for(int j = 0; j < 4; j++) digitalWrite(relayPin[j],LOW);
}
void loop() {
if (Serial.available())
{
char input= Serial.read();
if(input != -1)
{
switch(input)
{
case '1':
Serial.println("Relay1");
val=digitalRead(relayPin[0]);
val=!val;
digitalWrite(relayPin[0],val);
break;
case '2':
Serial.println("Relay2");
val=digitalRead(relayPin[1]);
val=!val;
digitalWrite(relayPin[1],val);
break;
case '3':
Serial.println("Relay3");
val=digitalRead(relayPin[2]);
val=!val;
digitalWrite(relayPin[2],val);
break;
case '4':
Serial.println("Relay4");
val=digitalRead(relayPin[3]);
val=!val;
digitalWrite(relayPin[3],val);
break;
default:
if(input != '\r' && input != '\n')
Serial.println("invalid entry");
break;
}
}
// else unablerelay();
}
}
Result
Open the serial monitor, press 1-4 to toggle the corresponding relay. The serial monitor will display "Relay1" to "Relay4" when a valid input is received, or "invalid entry" for incorrect inputs. The relays will toggle their state each time the corresponding number is pressed.
Additional Information
Please be vary carful not to play with live circuits! 120V or 220V should not be taken lightly. Make sure the appliance to be tinckered with is unplugged from mains. DO NOT CONNECT IT TO THE WALL WHILE MESSING WITH THE CABLE!
Was this article helpful?
