Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino Nano Based SMS-Controlled Relay Switching System

View and Edit Project In Cirkit DesignerImage of Arduino Nano Based SMS-Controlled Relay Switching System

Circuit Documentation

Summary

The circuit in question is designed to control a 4-channel relay module via an Arduino Nano, which is interfaced with a SIM800L GSM module. The SIM800L module allows the Arduino to send and receive SMS messages, enabling remote control of the relay states. An electrolytic capacitor is included in the circuit for power supply stabilization.

Component List

Arduino Nano

SIM800L GSM Module

Relay 4 Channel 5V

Electrolytic Capacitor

Wiring Details

Arduino Nano

SIM800L GSM Module

Relay 4 Channel 5V

Electrolytic Capacitor

Documented Code

#include <EEPROM.h>
#include <SoftwareSerial.h> // Create software serial object to communicate with SIM800L

SoftwareSerial GSM(2, 3);   // SIM800L Tx & Rx is connected to Arduino D2 & D3

String phone_no1 = "+919946206378"; // Change this to your first phone number
String phone_no2 = "+918606206385"; // Change this to your second phone number

String RxString = ""; // Will hold the incoming string from the GSM module
char RxChar = ' ';
int Counter = 0;
String GSM_Nr = "";
String GSM_Msg = "";

#define Relay1 4 // Load1 Pin Out (D4)
#define Relay2 5 // Load2 Pin Out (D5)
#define Relay3 6 // Load3 Pin Out (D6)
#define Relay4 7 // Load4 Pin Out (D7)

int load1, load2, load3, load4;

void setup() {
  // Set relay pins as outputs and turn them off initially
  pinMode(Relay1, OUTPUT); digitalWrite(Relay1, 1); 
  pinMode(Relay2, OUTPUT); digitalWrite(Relay2, 1); 
  pinMode(Relay3, OUTPUT); digitalWrite(Relay3, 1); 
  pinMode(Relay4, OUTPUT); digitalWrite(Relay4, 1); 

  Serial.begin(9600);  // Begin serial communication with Arduino IDE
  GSM.begin(9600);     // Begin communication with SIM800L

  Serial.println("Initializing....");
  initModule("AT", "OK", 1000);                // Check for GSM module
  initModule("AT+CPIN?", "READY", 1000);       // Check SIM card status
  initModule("AT+CMGF=1", "OK", 1000);         // Set SMS mode to ASCII
  initModule("AT+CNMI=2,2,0,0,0", "OK", 1000); // Set to read incoming SMS
  Serial.println("Initialized Successfully");

  // Load the relay states from EEPROM
  load1 = EEPROM.read(1);
  load2 = EEPROM.read(2);
  load3 = EEPROM.read(3);
  load4 = EEPROM.read(4);

  relays(); // Set relay states based on EEPROM

  delay(100);
}

void loop() {
  RxString = "";
  Counter = 0;
  
  // Read incoming data from the GSM module
  while (GSM.available()) {
    delay(1);  
    RxChar = char(GSM.read());
    if (Counter < 200) {
      RxString.concat(RxChar);
      Counter = Counter + 1;
    }
  }
  
  if (Received(F("CMT:"))) GetSMS();

  // Verify the message sender and act accordingly
  if (GSM_Nr == phone_no1 || GSM_Nr == phone_no2) {
    
    if (GSM_Msg == "load1on") { load1 = 0; sendSMS(GSM_Nr, "Ok Load 1 is On"); }
    if (GSM_Msg == "load1off") { load1 = 1; sendSMS(GSM_Nr, "Ok Load 1 is Off"); }
    
    if (GSM_Msg == "load2on") { load2 = 0; sendSMS(GSM_Nr, "Ok Load 2 is On"); }
    if (GSM_Msg == "load2off") { load2 = 1; sendSMS(GSM_Nr, "Ok Load 2 is Off"); }
    
    if (GSM_Msg == "load3on") { load3 = 0; sendSMS(GSM_Nr, "Ok Load 3 is On"); }
    if (GSM_Msg == "load3off") { load3 = 1; sendSMS(GSM_Nr, "Ok Load 3 is Off"); }
    
    if (GSM_Msg == "load4on") { load4 = 0; sendSMS(GSM_Nr, "Ok Load 4 is On"); }
    if (GSM_Msg == "load4off") { load4 = 1; sendSMS(GSM_Nr, "Ok Load 4 is Off"); }
    
    if (GSM_Msg == "allon") { load1 = 0; load2 = 0; load3 = 0; load4 = 0; sendSMS(GSM_Nr, "Ok All Load is On"); }
    if (GSM_Msg == "alloff") { load1 = 1; load2 = 1; load3 = 1; load4 = 1; sendSMS(GSM_Nr, "Ok All Load is Off"); }
    
    if (GSM_Msg == "loadstatus") {
      String loadst = "";
      loadst += (load1 == 0) ? "Load1 On\r\n" : "Load1 Off\r\n";
      loadst += (load2 == 0) ? "Load2 On\r\n" : "Load2 Off\r\n";
      loadst += (load3 == 0) ? "Load3 On\r\n" : "Load3 Off\r\n";
      loadst += (load4 == 0) ? "Load4 On" : "Load4 Off";
      sendSMS(GSM_Nr, loadst);
    }

    eeprom_write();  // Save the relay states to EEPROM
    relays();        // Apply relay states
  }

  GSM_Nr = "";
  GSM_Msg = "";
}

void eeprom_write() {
  EEPROM.write(1, load1);
  EEPROM.write(2, load2);
  EEPROM.write(3, load3);
  EEPROM.write(4, load4);
}

void relays() {
  digitalWrite(Relay1, load1);
  digitalWrite(Relay2, load2);
  digitalWrite(Relay3, load3);
  digitalWrite(Relay4, load4);
}

// Send SMS 
void sendSMS(String number, String msg) {
  GSM.print("AT+CMGS=\"");
  GSM.print(number);
  GSM.println("\"\\r\\n");
  delay(500);
  GSM.println(msg);
  delay(500);