In this tutorial we’re going to show you how you can control a 12V lamp via SMS using an Arduino UNO, a relay module, and the SIM900 GSM shield.
![]()
Before proceeding with this tutorial we recommend the following resources:
This project uses the SIM900 GSM shield to receive and send SMS with the Arduino. This projects aims to:

In this project you need to connect the Arduino to the GSM shield and to a relay module connected to a 12V lamp. Here’s a complete list of the parts required for this project (click the links below to find the best price at Maker Advisor):
You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

To use the SIM900 GSM shield:
1) You need an active SIM card. We recommend using a SIM card with a prepaid plan for testing purposes.
2) You have to turn off the SIM card pin lock. Insert the SIM card in your smartphone and turn off the pin lock in the phone security settings.
3) The GSM shield should be powered using an external 5V power supply that can provide 2A, or 9V 1A, or 12V 1A.
4) We’re going to use software serial to communicate with the shield. So, on the serial port select, make sure the jumper cap is connected as shown in figure below to use software serial.
5) You can turn on/off the shield by pressing the power key, or you can turn it on automatically with the Arduino via the D9 pin. To automatically turn the shield on and off, you need to solder the R13 connections as shown in the figure below.

6) If this is your first time dealing with the GSM shield, we recommend you reading the Preliminary Steps in our GSM shield guide here.
7) Insert the SIM card into the SIM card holder.
8) Finally, wire the SIM900 GSM shield to the Arduino by following the schematic below.
9) You should power up the SIM900 GSM shield using the external 5V power supply. Make sure you select the external power source with the toggle switch next to the DC jack.

Note: You can test it the shield is working properly by sending AT comments from the Arduino IDE with an FTDI programmer – check Guide to SIM900 GSM GPRS Shield with Arduino
In this project we control a 12V lamp. However, this project can be easily adapted to control mains voltage or other electronics appliances. Check our Guide for Relay Module with Arduino to learn how to control mains voltage with a relay.
We are using the following 12V lamp and its corresponding lamp holder.

Add the relay module and the lamp holder to the previous circuit.
For safety purposes, we’re placing our relay inside a plastic box enclosure.

Below you can find the code for this project. Make sure you edit the code with the phone number the Arduino should reply to. We recommend you reading the code explanation before uploading the code.
/* * Complete Project Details https://randomnerdtutorials.com */ // Include Software Serial library to communicate with GSM #include <SoftwareSerial.h> // Configure software serial port SoftwareSerial SIM900(7, 8); // Variable to store text message String textMessage; // Create a variable to store Lamp state String lampState = "HIGH"; // Relay connected to pin 12 const int relay = 12; void setup() { // Automatically turn on the shield digitalWrite(9, HIGH); delay(1000); digitalWrite(9, LOW); delay(5000); // Set relay as OUTPUT pinMode(relay, OUTPUT); // By default the relay is off digitalWrite(relay, HIGH); // Initializing serial commmunication Serial.begin(19200); SIM900.begin(19200); // Give time to your GSM shield log on to network delay(20000); Serial.print("SIM900 ready..."); // AT command to set SIM900 to SMS mode SIM900.print("AT+CMGF=1\r"); delay(100); // Set module to send SMS data to serial out upon receipt SIM900.print("AT+CNMI=2,2,0,0,0\r"); delay(100); } void loop(){ if(SIM900.available()>0){ textMessage = SIM900.readString(); Serial.print(textMessage); delay(10); } if(textMessage.indexOf("ON")>=0){ // Turn on relay and save current state digitalWrite(relay, LOW); lampState = "on"; Serial.println("Relay set to ON"); textMessage = ""; } if(textMessage.indexOf("OFF")>=0){ // Turn off relay and save current state digitalWrite(relay, HIGH); lampState = "off"; Serial.println("Relay set to OFF"); textMessage = ""; } if(textMessage.indexOf("STATE")>=0){ String message = "Lamp is " + lampState; sendSMS(message); Serial.println("Lamp state resquest"); textMessage = ""; } } // Function that sends SMS void sendSMS(String message){ // AT command to set SIM900 to SMS mode SIM900.print("AT+CMGF=1\r"); delay(100); // REPLACE THE X's WITH THE RECIPIENT'S MOBILE NUMBER // USE INTERNATIONAL FORMAT CODE FOR MOBILE NUMBERS SIM900.println("AT + CMGS = \"XXXXXXXXXX\""); delay(100); // Send the SMS SIM900.println(message); delay(100); // End AT command with a ^Z, ASCII code 26 SIM900.println((char)26); delay(100); SIM900.println(); // Give module time to send SMS delay(5000); }
The code is pretty straightforward to understand. Continue reading to understand what each section of code aims to do.
The Arduino communicates with the shield using software serial communication. So, you start by including the SoftwareSerial.h library.
// Include Software Serial library to communicate with GSM
#include <SoftwareSerial.h>Then, you create a software serial port on pins 7 and 8 (pin 7 is set as RX and 8 as TX).
// Configure software serial port
SoftwareSerial SIM900(7, 8);You create a textMessage variable that will hold the text message received by the Arduino; and another variable to store the current lamp state (relay state), called lampState. The lampState variable is set to HIGH by default, because we want the relay off by default. The relay works with inverted logic, so you need to set it to HIGH to turn it off.
// Variable to store text message String textMessage; // Create a variable to store Lamp state String lampState = "HIGH";
You also assign the pin number the relay is connected to, pin 12.
// Relay connected to pin 12
const int relay = 12;In the setup() function, you start by turning on the GSM shield. The following lines are the equivalent of pressing the shield “power” button.
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
delay(5000);The relay is set as an output, and its state is off by default.
// Set relay as OUTPUT pinMode(relay, OUTPUT); // By default the relay is off digitalWrite(relay, HIGH);
Then, you initialize serial communication with the shield, and give it some time to log on to the network.
// Initializing serial commmunication Serial.begin(19200); SIM900.begin(19200); // Give time to your GSM shield log on to network delay(20000); Serial.print("SIM900 ready...");
The shield is set to SMS mode with the following:
// AT command to set SIM900 to SMS mode SIM900.print("AT+CMGF=1\r"); delay(100); // Set module to send SMS data to serial out upon receipt SIM900.print("AT+CNMI=2,2,0,0,0\r"); delay(100);
In the loop(), you read the incoming messages and compare if those messages contain the text ON, OFF or STATE. Accordingly to the message content, the Arduino will perform different tasks.
So, you start by checking whether there are messages to read. If there are, the message will be saved on the textMessage variable. For debugging purposes, the message is displayed on the serial monitor.
if(SIM900.available() >0) {
textMessage = SIM900.readString();
Serial.print(textMessage);
delay(10);
}If the message received contains the text “ON”, then the relay will be turned on, turning on the lamp. Then, you save the current lamp state on the lampState variable. Finally, you clean the textMessage variable, so that it is empty to receive new SMS.
if (textMessage.indexOf("ON") >= 0){
// Turn on relay and save current state
digitalWrite(relay, LOW);
lampState = "on";
Serial.println("Relay set to ON");
textMessage = "";On the other side, if the SMS received contains the text “OFF”, the relay will be turned off, the new state will be saved, and the textMessage is cleaned.
if (textMessage.indexOf("OFF") >= 0){
// Turn off relay and save current state
digitalWrite(relay, HIGH);
lampState = "off";
Serial.println("Relay set to OFF");
textMessage = "";Finally, if the incoming SMS contains the text “STATE”, the Arduino should send a message to a predefined phone number saying whether the lamp is currently on or off.
if (textMessage.indexOf("STATE") >= 0){
String message = "Lamp is " + lampState;
sendSMS(message);
Serial.println("Lamp state resquest");
textMessage = "";The message is sent using the sendSMS() function defined at the bottom of the code.
The sendSMS() function created at the end of the code, accepts a string as an argument. That string should be the message to be sent.
The number the Arduino answers to is set at the following line:
SIM900.println("AT + CMGS = \"XXXXXXXXXXXX\"");Replace the XXXXXXXXXXXX with the recipient’s phone number.
Note: you must add the number according to the international phone number format. For example, in Portugal the number is preceded by +351XXXXXXXXX.
After adding the phone number the Arduino should reply to, you can copy the full code to your Arduino IDE and upload it to your Arduino board.
Copyright ©2025. All Rights Reserved Emblab THE RAVE INNOVATION