Project tutorial
Control Two Relays with an SMS

Control Two Relays with an SMS © GPL3+

Drive the relays of the MKR Relay Shield connected to a MKR GSM 1400 with an SMS.

  • 1 comment
  • 14 respects

Components and supplies

About this project

This project shows how to control a MKR Relay Shield using a MKR GSM 1400 and the Short Message Service (SMS); this solution works with a plain SIM with no data plan and it is suitable for applications where the GSM network is with poor coverage.

What You Need

The project uses a MKR GSM 1400, the antenna, a battery pack, a mobile phone, one SIM card, two LEDs, two 220 ohm resistors, a breadboard, cables, and a MKR Relay Shield.

  • The MKR GSM 1400 executes the sketch and manages the incoming and outgoing SMSs;
  • Antenna and battery pack are respectively used tor allow the connection to the cellular network with a good signal and to power the device when other power supplies are not available;
  • The MKR Relay Proto Shield is a board that includes two relays and is made for MKR format boards. It is used to switch loads and voltages usually not manageable with solid state solutions (MOSFET). In this project it just switches LEDs;
  • The mobile phone is required to send and receive the text messages;
  • The SIM card is required to access the GSM network and allow network operation;
  • LEDs, resistors, breadboard and cables, are used in this project to show the opening and closing of the relay contacts.

Hardware Setup

The antenna, the SIM and the battery pack should be connected to their own connector on head and tail of the MKR GSM 1400.

The circuit on the breadboard is simple: bring 5V and GND to the rails on the breadboard (5V to the red rail and GND to the black one). Put on the breadboard each LED so that the short leg (-) goes to the black rail with a wire jumper. Each long leg (+) goes to the 220 ohm resistor that is connected with a wire jumper to one of the NO contact on the relay connector. The common of each relay goes to the red rail of the breadboard (5V).

When the relay is excited, it closes the NO contact bringing 5V to the LED that will light up.

How It Works

This project uses the SMS capabilities of the GSM network to receive some text that is parsed and used to toggle the two relays. For security reasons, the sketch checks the number of the sender and this information must be stored in the arduino_secrets.h file. We don't know the status of the relays and therefore the sketch uses a "toggle" approach, where each SMS received with 1 or 2 as text toggles the status of the corresponding relays. To let you know the result of the operation, an SMS is sent back with the message "Relay <nunber>, state: <0 or 1>". Looking at the history of the messages you should be able to keep track of the relays status.

The Sketch

The first code section is used to includes the libraries required by the application; MKRGSM include all the GSM connection functionalities, these are available through the object GSMClient, GPRS and GSM, the header GSM_SMS import the APIs through which the sketch is able manage the SMS sended or received by the MKRGSM:

#include <MKRGSM.h>
GSM gsmAccess;
GSM_SMS sms;

After the include section, we provide the two lines of code that refer to your secret pin number and incoming number. With this syntax the web editor generates a new Tab called Secrets where you input the two constants:

String sender = SECRET_YOUR_NUMBER;

The setup section initializes all the objects used by the sketch; the GSM connection sequence that allow to register to the network occupies most of this section.

void setup()
// initialize serial communications and wait for port to open:
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
Serial.println("SMS Messages Receiver");
// connection state
bool connected = false;
// Start GSM connection
while (!connected) {
if (gsmAccess.begin(PINNUMBER) == GSM_READY) {
connected = true;
} else {
Serial.println("Not connected");
Serial.println("GSM initialized");
Serial.println("Waiting for messages");

Last code section is the loop function where the basic control logic is executed. In our caseit simply checks if an SMS is available, if true, gets the SMS from the SIM and checks the text. If it is from the right sender it decodes the first character and switches the relay specified by the user. To confirm the operation it sends an SMS with the status of the Relay switched. It is important to delete the received SMS from the SIM card to avoid that it fills up and stops receiving SMSs. The sketch uses the ASCII code of the numbers 1 and 2 in the swtch ... case structure.

void loop()
 int c;
 String texmsg = "";
 // If there are any SMSs available()
 if (sms.available()) {
Serial.println("Message received from:");
// Get remote number
sms.remoteNumber(senderNumber, 20);
if (String(senderNumber) == sender) {
// An example of message disposal
// Any messages starting with # should be discarded
if (sms.peek() == '#') {
Serial.println("Discarded SMS");
c =;
switch (c) {
case 49:
digitalWrite(1, !digitalRead(1));
texmsg = "Relay 1, state: " +  String(digitalRead(1));
case 50:
digitalWrite(2, !digitalRead(2));
texmsg = "Relay 2, state: " +  String(digitalRead(2));
Serial.println("\nEND OF MESSAGE");
// Delete message from modem memory
Serial.println("MESSAGE DELETED");
} else {
Serial.println("MESSAGE DELETED");

How to Use It

Double check that your phone number is properly stored in the Secrets Tab, then upload the sketch and open the Serial Monitor to verify that the module properly attaches to the network.

From a simple mobile phone, send an SMS with just "1" or "2" to the mobile number associated with the SIM inserted in the MKR GSM 1400 board. You should see in a few seconds the LED turning on or off depending on its state before the SMS was sent. Together with the change of state the sketch sends back a message containing the current state of the toggled LED.


Actuation with SMS


Similar projects you might like

Using SMS messages to control LED color

Project tutorial by SteiniBrodda

  • 6 respects

Control an LED with the Remote Control

Project showcase by Nicholas_N

  • 9 respects

SMART CUBE: A New Way to Control Your Home

Project tutorial by Alberto Sartori

  • 30 respects

Servo Control with TV Remote Control

Project showcase by eldo85

  • 17 respects

Control Arduino Uno Using ESP8266 WiFi Module and Blynk App

Project tutorial by Adithya TG

  • 25 respects
Add projectSign up / Login