GSM shield interrupts work of ultrasonic sensor

I have a program where LED is turned ON by specific SMS ("ON") sent to GSM GPRS SIM900 module.
If ultrasonic sensor detects object 20cm or closer to it (while LED is turned on) it will call specific phone number.
Problem is that every time SMS is sent to GSM GPRS module, ultrasonic sensor is interrupted and would show false value (5cm or less) even tho closest object to it is 137 cm (cieling). These false values would be calculated for at least 20 seconds after SMS is sent. It looks like this:

Here is full code:

#include <SoftwareSerial.h>
#define trigPin 2
#define echoPin 3
// Configure software serial port
SoftwareSerial SIM900(7, 8);
//Variable to save incoming SMS characters
char incoming_char=0;
String message;
int led = 9;
int sirena = 5;
int button = 6;
long duration;
int distance;

void setup() {
  // For serial monitor
// AT command to set SIM900 to SMS mode
// Set module to send SMS data to serial out upon receipt 
 pinMode(led, OUTPUT);
  pinMode(sirena, OUTPUT);
 pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
void loop() {
  digitalWrite(trigPin, LOW);
  digitalWrite(trigPin, HIGH);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  // Calculate the distance:
  distance = duration * 0.034 / 2;
  Serial.print("Distance = ");  
  Serial.println(" cm");
  if(SIM900.available() >0) {
    message = SIM900.readString();
    //Get the character from the cellular serial port
  message = "";
   if(message.indexOf("+381601416081")> -1){
  if(message.indexOf("ON") > -1){
      Serial.println("LED ON");
     digitalWrite(led, HIGH);
    else if(message.indexOf("OFF") > -1){

      Serial.println("LED OFF");
 if(distance < 20  && digitalRead(led) == HIGH){
  SIM900.println("ATD + +381601416081;");

Why is GSM module interrupting distance measurments of ultrasonic sensor?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.