How to interface Arduino with a GSM module and delete all the read SMSes?


In this article, we will see how to interface Arduino with a GSM Module, and delete all the read SMSes. You will need the following −

  • An Arduino board

  • A GSM Module (SIM800C, SIM900A, are popular examples, but you can have any other module as well)

  • A GSM (2G) SIM Card, or a 4G SIM Card with 2G fallback option (Jio SIM Cards won't work for this project)

  • A GSM Antenna

You could also get a GSM Module development board, like the one below (the SIM Card Holder is on the other side of the board) −

A GSM Module interacts with a microcontroller via UART (see the jumper holes for the UART at the bottom left of the above board). We will use SoftwareSerial to connect to the GSM Module.

Circuit Diagram

The circuit diagram is shown below −

As you can see, we have connected the RX of the GSM Module to pin 5 of Arduino, the TX to pin 9 and GND to GND. Since the GSM Module will be powered using a separate 1A+ adapter, no need to connect Vcc to Arduino (it needs more current than what Arduino pins can provide).Since in UART connection, TX of one module is connected to RX of other and vice versa, pin 5 of Arduino will serve as TX (because it is connected to RX of GSM), and pin 9 will serve as RX.

Now, GSM Modules work on AT Commands. Going into the details of the AT Commands is beyond the scope of this article. However, you should know that each GSM Command comes with its own AT Commands manual, which you should refer to for all operations (sending SMS, making calls, connecting to internet, etc.)

The AT Commands manual for SIM900 can be found here.

For deleting SMSes, two AT Commands will be used (one for setting the mode of the module to text mode, and the other for actually deleting the SMSes) −

  • AT+CMGF=1 − This sets the SMS format to text mode. The other mode is PDU (Protocol Data Unit) mode. We are interested in only text messages, so we will set the mode to text mode.

  • The second AT Command is AT+CMGDA which is the SMS Delete command. Its syntax is −

AT+CMGDA = <stat>

The <stat> parameter accepts the following values for the text mode −

  • "DEL READ": Delete all read messages

  • "DEL UNREAD": Delete all unread messages

  • "DEL SENT": Delete all sent messages

  • "DEL UNSENT": Delete all unsent messages

  • "DEL INBOX": Delete all received messages

  • "DEL ALL": Delete all messages

In response, this function returns either OK if the messages are deleted, or else −

ERROR
+CMS ERROR: <err>

Where <err> will indicate the actual error occurred.

The Arduino code incorporating the above AT Commands is given below. If you are using a board other than Arduino Uno, all digital pins may not support SoftwareSerial.

#include <SoftwareSerial.h>
SoftwareSerial gsmSerial(9, 5); //RX, TX
void setup()
{
   gsmSerial.begin(9600); // Setting the baud rate of GSM Module
   Serial.begin(9600); // Setting the baud rate of Serial Monitor (Arduino)
   delay(1000);
   Serial.println("Preparing to delete read SMSes");
   DeleteReadMessages();
}
void loop()
{
}
void DeleteReadMessages()
{
   Serial.println("Setting the GSM in text mode");
   gsmSerial.println("AT+CMGF=1\r");
   delay(2000);
   Serial.println("Deleting Read SMSes!");
   gsmSerial.println("AT+CMGDA=\"DEL READ\"\r");
   delay(2000);
   //Print the response on the Serial Monitor
   while (gsmSerial.available() > 0) {
      Serial.write(gsmSerial.read());
   }
}

You can modify the above code to delete all the sent messages as well, along with Read SMSes.

Updated on: 24-Jul-2021

577 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements