Projects / SMS-controlled relay

SMS-controlled relay

Beginner1.5 hGuided buildReviewed

Build it step by step

0 of 6 done
  1. NANOLM2596

    Set the supply

    Trim the LM2596 to exactly 4.0 V and check with a multimeter.

  2. NANO

    Wire the module

    Connect power, the capacitor and the serial lines with the voltage divider.

  3. NANO

    Test with AT

    Open a serial terminal and send AT to see OK, and AT+CSQ for signal strength.

  4. NANOON

    Wire the relay

    Connect the relay module and a 12 V test lamp.

  5. NANO

    Upload

    Put your phone number in ALLOWED and upload.

  6. NANO

    Send an SMS

    Text ON and then OFF and read the replies.

In short

How do you build an SMS-controlled relay?

Send ON or OFF by SMS from a known number to switch a 12 V pump or light, and get a reply confirming it. You need Arduino Nano, SIM800L GSM module, Relay Module 5V, LM2596 Buck Module, 12 V 2 A DC adapter, Electrolytic capacitor 1000 µF and Resistor 1 kΩ × 3. It takes 6 steps and about 90 minutes.

Level
Beginner
Time
About 90 min
Parts
7

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
12 V adapter +LM2596 IN+ and relay load supplySupply
12 V adapter -LM2596 IN-, relay load return, Nano GNDCommon ground
LM2596 OUT+ (set to 4.0 V first)SIM800L VCC and 1000 µF capacitor +Module supply
LM2596 OUT-SIM800L GND and capacitor -Common ground
SIM800L TXDNano D10Data from the module
Nano D111 kΩ resistor then SIM800L RXDData to the module
Two 1 kΩ resistors in seriesSIM800L RXD and GNDDivides 5 V down to about 3.3 V
Relay module VCC, GND, INNano 5V, GND, D6Relay control
Relay COM12 V adapter +Feeds the 12 V load
Relay NO12 V load + (pump or light)Switched 12 V
12 V load -12 V adapter -Return
Nano powerUSB cablePower

Code

Arduino IDE, board "Arduino Nano" (processor ATmega328P, or ATmega328P (Old Bootloader) for clones). No extra libraries are needed. Set the LM2596 to 4.0 V on a multimeter BEFORE connecting the SIM800L: the module is destroyed above about 4.4 V, and it needs up to 2 A for short bursts, so also fit the 1000 µF capacitor close to its supply pins. The module's own serial output is about 2.8 V, which the Nano usually reads as high. Put your own numbers in ALLOWED in international format and only they can switch the relay. The relay here switches a 12 V load; any mains load must be wired by a qualified electrician. Check that your SIM works for SMS and the module supports the local network band.

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

#include <SoftwareSerial.h>

SoftwareSerial sim(10, 11);                // RX from the module, TX to the module
const byte RELAY = 6;
const char* ALLOWED[] = { "+91XXXXXXXXXX", "+91YYYYYYYYYY" };   // placeholders: put your own numbers in international format
const byte NUMBERS = 2;
bool on = false;

String readLine(unsigned long ms) {
  String s; unsigned long t = millis();
  while (millis() - t < ms) { while (sim.available()) s += (char)sim.read(); }
  return s;
}

void send(const String& cmd, unsigned long wait = 500) { sim.println(cmd); readLine(wait); }

void reply(const String& number, const String& text) {
  sim.print("AT+CMGS=\""); sim.print(number); sim.println("\"");
  delay(500);
  sim.print(text);
  sim.write(26);                            // Ctrl+Z ends the message
  readLine(3000);
}

bool allowed(const String& number) {
  for (byte i = 0; i < NUMBERS; i++) if (number == ALLOWED[i]) return true;
  return false;
}

void setup() {
  pinMode(RELAY, OUTPUT); digitalWrite(RELAY, HIGH);     // most relay modules are active low, so HIGH is off
  sim.begin(9600);
  delay(5000);                              // wait for the module to find the network
  send("AT"); send("AT+CMGF=1"); send("AT+CNMI=2,1,0,0,0"); send("AT+CMGD=1,4");
}

void loop() {
  String s = readLine(1000);
  int p = s.indexOf("+CMTI:");               // a new message arrived
  if (p >= 0) {
    int comma = s.indexOf(',', p);
    String index = s.substring(comma + 1, comma + 3); index.trim();
    sim.print("AT+CMGR="); sim.println(index);
    String m = readLine(1500);
    int q1 = m.indexOf("\"", m.indexOf("REC")); q1 = m.indexOf("\"", q1 + 1);       // first quote after the status
    int q2 = m.indexOf("\"", q1 + 1);
    String number = m.substring(q1 + 1, q2);
    String body = m.substring(m.indexOf("\n", q2) + 1); body.trim(); body.toUpperCase();
    if (allowed(number)) {
      if (body.startsWith("ON"))  { on = true;  digitalWrite(RELAY, LOW); }
      if (body.startsWith("OFF")) { on = false; digitalWrite(RELAY, HIGH); }
      reply(number, on ? "Relay is ON" : "Relay is OFF");
    }
    send("AT+CMGD=1,4");                    // clear stored messages
  }
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Text ON from an allowed numberRelay switches on and a reply says Relay is ON
Text OFFRelay off and a reply says Relay is OFF
Text from another numberIgnored
Text with lower caseStill works

If it does not work

Module does not answer AT

Check the supply voltage, the capacitor and the TX/RX wires.

Resets when sending

Supply cannot give the burst current. Use a larger capacitor and a better buck module.

No network

Check the SIM, antenna and signal with AT+CSQ.

Relay is inverted

Swap the HIGH and LOW writes for your relay type.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the SMS-controlled relay?

Arduino Nano, SIM800L GSM module, Relay Module 5V, LM2596 Buck Module, 12 V 2 A DC adapter, Electrolytic capacitor 1000 µF and Resistor 1 kΩ × 3.

How long does the SMS-controlled relay take to build?

About 90 minutes for a beginner maker, in 6 steps.

How do I know the SMS-controlled relay works?

Text ON from an allowed number: Relay switches on and a reply says Relay is ON. Text OFF: Relay off and a reply says Relay is OFF. Text from another number: Ignored.

Module does not answer AT: what should I check?

Check the supply voltage, the capacitor and the TX/RX wires.

Resets when sending: what should I check?

Supply cannot give the burst current. Use a larger capacitor and a better buck module.

No network: what should I check?

Check the SIM, antenna and signal with AT+CSQ.

Relay is inverted: what should I check?

Swap the HIGH and LOW writes for your relay type.