In this project we are going to learn how to control a relay with an SMS. This relay could be used in many applications to Switch ON or OFF devices such as an electric bulb, or opening a security boom gate like in this example. A security access control can be implemented at apartments or housing complexes, when a visitor arrives at the gate, he/she can phone the visited person who can send an SMS: “OPEN” to the gate cellphone number, upon receiving this message from the GSM module, the PIC will trigger the relay to open the gate and then close automatically the gate after a short delay or after the car has entered the into the housing complex (Infrared beam detectors could be used to detect if a car has passed the gate).

In this project we are going to use a GSM modem, a PIC microcontroller and a relay.

A GSM modem is a wireless modem that works with a GSM wireless network. GSM stands for Global System for Mobile communications, this architecture is used for mobile communication in most of the countries in the world.

A wireless modem acts basically like the traditional dial-up modem, the main difference is that a dial-up modem sends and receives data through a fixed telephone line while a wireless modem sends and receives data through radio waves. Besides the dial-up connection, GSM modem can also be used for sending and receiving SMS which is also one of the key features of GSM modem.

To learn more about interfacing GSM modem with PIC microcontrollers, please read more the article:

If you would like to build this project as a prototype for your soon to be improved project, we would advise to create a PCB for this project as you won’t be able to work well with relays on a breadboard, you don’t have to worry about the cost even if you are a student or with limited budget, these days it’s really cheaper to get quality professional PCB or PCB SMD component assembly relatively cheap.

There are many PCB companies in the world that can make you good quality PCBs, One of those companies we can recommend is PCBWay a china based PCB manufacturer specializing in PCB prototyping, small volume production and PCB assembly service all under one roof with more than a decade of experience.

They are now running an unbelievable SMD component assembly for only $30 USD this is for 1-20pcs plus free shipping worldwide!!!

You won’t get this offer anywhere with great SMT quality, so try them and verify their SMT services quality, maybe they could become your future partner with your prototyping. They produce also PCBs of great quality from only $5 USD for 10 pcs 1-2 layers.

To find out more about this great offer or to learn more about their services, please click on the image below:

MikroC Code

In this project, we are going to receive an SMS by using the Polled Response. In this mode, an AT command is sent t the modem to list any new Unread messages instead of getting a notification from the modem:

1

2

AT+CGMF=1//Set SMS text mode

AT+CMGL="REC UNREAD"// List UNREAD messages

If we assume that there is only one unread message, the modem may respond:

1

+CMGL:1,"REC UNREAD","0123456789",,"15/07/19,00:01:00+35"

1 is the index number. After receiving, we could delete all messages from the memory to be ready to receive new messages.

We are going to enable to UART interrupts so that when a character is received from the modem the program jumps straight to the interrupt service routine.

// conencted to the Relay is thus controlled remotely from a mobile phone

while(1)

{

Send_To_Modem(Get_Message);// Check if any SMS arrived

//

// Wait until modem is ready

//

while(1)

{

Send_To_Modem(AT);

Delay_Ms(1000);

if(Modem_Response()==OK)break;

}

//

// Check if a message has arrived

//

if(SMS_Arrived==1)// A new SMS arrived

{

SMS_Arrived=0;// Clear SMS arrived flag

if(Relay==ON)

{

PORTB.RB0=1;// Activate the Relay

}

elseif(Relay==OFF)

PORTB.RB0=0;// Deactivate the relay

//

// Delete all messages

//

Send_To_Modem(Delete_All);// Delete all existing messages

Wait_Modem_response(OK);// Wait until modem is ready

}

Delay_Ms(10000);

}

}

This code just demonstrate the basics of receiving an SMS then control a relay, there are many ways we could improve this code:

We could improve this by instead of sending a new SMS to close the gate, we could simply use a short delay like 10 seconds to close the gate automatically or even use infrared beam detectors to detect the presence of a car and only close the gate automatically once the car enters the area.

Add a send function to send to the user the status of the gate. gate open or closed to make sure the command was executed

Check the telephone number of the sender to make sure only numbers from authorized users can open the gate.