Send SMS using push switch

Here we are going to make a GSM + Arduino project to send SMS using an electric push button or switch.

In this device whenever the switch is pressed, a text message (text in the code) will send to the number specified in the code. When the push button is pressed, the Arduino sends the AT commands to the GSM module, for a text SMS; the GSM module works with the “AT commands”.
send message from gsm module using arduino

Connect the TX, RX and GND pins from the Arduino to the GSM (Arduino + GSM module interface) as labelled in the diagram. Then connect the push switch between the 5V and pin2. Add a pull down resistor across the pin2 and Ground. The Pin2 is configured as the input pin. When the pin2 reads an active high state, the sketch (sketch for GSM Text message) inside the if condition will be executed. And the SMS sends.

A variable called state is also added in the condition function of the program, it is to prevent multiple sending of SMS on a single switch press. The “if” condition becomes true only when the digitalRead at pin 2 is high and the value of variable state is ‘ 0 ‘. The value of variable state is initialized as ‘ 0 ‘. On pressing the switch, the “if” condition becomes true, that the SMS sends and the state value set to ‘ 1 ‘. Only when the switch is released the state value sets back to ‘ 0 ‘. Only the switch press after the setback of the state to ‘ 0 ‘ makes the if condition true. So a long press will not send multiple SMS, only press after a release sends the next SMS. That is, each consecutive press sends each SMS.

Arduino Code
int state = 0;
const int pin = 2;
void setup()
{
 //Set Exact Baud rate of the GSM/GPRS Module.
 Serial.begin(9600);
}
void loop()
{
 if (digitalRead(pin) == HIGH && state == 0) {
   Serial.print("\r");
   delay(1000);
   Serial.print("AT+CMGF=1\r");
   delay(1000);
   /*Replace XXXXXXXXXX to 10 digit mobile number & ZZ to 2 digit country code*/
   Serial.print("AT+CMGS=\"+ZZXXXXXXXXXX\"\r");
   delay(1000);
   //The text of the message to be sent.
   Serial.print("HELLO WORLD");
   delay(1000);
   Serial.write(0x1A);
   delay(1000);
   state = 1;
 }
 else {
   state = 0;
 }
}
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...

You may also like...

4 Responses

  1. baskar says:

    how to send tow types of messege with two pushbutton

    • admin says:
      //Add one more if statement.
      if (digitalRead(pin1) == HIGH && state1 == 0) {
       Serial.print("\r");
       ....
       // Same code with different message.
       ....
       delay(1000);
       state1 = 1;
      }
      else {
       state1 = 0;
      }
      
  2. Rohan says:

    Please tell us about resistor value and which switch to use

Leave a Reply

Your email address will not be published. Required fields are marked *