title: How to read an SMS from Arduino and get LED to switch on or off
body: <pre class="lang-cpp prettyprint-override"><code>#include &lt;SoftwareSerial.h&gt;
  char inchar; //Will hold the incoming character from the serial port.
  SoftwareSerial cell(2,3); //Create a 'fake' serial port. Pin 2 is the Rx pin, pin 3 is the Tx pin.
  
   int led1 = A2;
  
   void setup()
   {
       // Prepare the digital output pins
       pinMode(led1, OUTPUT);
       digitalWrite(led1, HIGH);
  
       //Initialize GSM module serial port for communication.
       cell.begin(19200);
       delay(30000); // Give time for GSM module to register on network, etc.
       cell.println("AT+CMGF=1"); // Set SMS mode to text
       delay(200);
       cell.println("AT+CNMI=3,3,0,0"); // Set module to send SMS data to serial out upon receipt 
       delay(200);
   }
  
   void loop() 
   {
       //If a character comes in from the cellular module...
       if(cell.available() &gt;0)
       {
           delay(10);
           inchar=cell.read(); 
           if (inchar=='a')
           {
               delay(10);
               inchar=cell.read();
               if (inchar=='0')
               {
                   digitalWrite(led1, LOW);
               } 
               else if (inchar=='1')
               {
                   digitalWrite(led1, HIGH);
               }
               delay(10);
               delay(10);
           }
           cell.println("AT+CMGD=1,4"); // Delete all SMS
       }
   }
  </code></pre>
  
  <p>This is the code for receiving SMSes from the cellular network. I am using the Arduino Gboard with SIM900. There is no error in the code, but the LED on the board doesn't switch on or off in response to an SMS.</p>
  
  <p>Why?</p>