Random Nerd Tutorials

Control a 12V Lamp via SMS with Arduino


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:

First, watch the video demonstration

Project overview

This project uses the SIM900 GSM shield to receive and send SMS with the Arduino. This projects aims to:

  • turn a 12V lamp on when you send an SMS to the Arduino with the text “ON”
  • turn a 12V lamp off when you send an SMS to the Arduino with the text “OFF”
  • you can request the lamp state by sending an SMS to the Arduino with the text “STATE”, the Arduino should reply back with the text “lamp is on” or “lamp is off”

Parts required

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!

Preparing the GSM Shield

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

Wiring the Relay Module

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.

The code is pretty straightforward to understand. Continue reading to understand what each section of code aims to do.

Import library

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>

Software serial port

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);

Creating variables

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);
digitalWrite(9, LOW);

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

// Give time to your GSM shield log on to network
Serial.print("SIM900 ready...");

The shield is set to SMS mode with the following:

// AT command to set SIM900 to SMS mode
// Set module to send SMS data to serial out upon receipt 


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();

Turn the lamp on

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 = "";

Turn the lamp off

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 = "";

Returning the lamp state

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;
  Serial.println("Lamp state resquest");
  textMessage = "";

The message is sent using the sendSMS() function defined at the bottom of the code.

Sending an SMS

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.

Wrapping up

In this project we’ve shown you how you can use the SIM900 GSM shield to control a 12V lamp and to request data (the lamp state) to the Arduino.

The concepts learned in this project can be applied to control any electronics appliances you like. You can also use what you’ve learned here to request the Arduino about sensor data – for example, request the latest readings of your weather station.

You can also build a surveillance system using a PIR motion sensor and a GSM shield that sends you an SMS when unexpected motion is detected.

We hope you’ve found this project useful. If you like this project, make sure you check our Arduino course: Arduino Step-by-step Projects with 25 Projects.

Thanks for reading.

Leave a Comment:

Add Your Reply