Send SMS to a remote person if someone triggers the ultrasonic sensor

I will use the Arduino mega as the main microcontroller. but you can use any Arduino board.

SIM900 mini use as the GSM modem. SR04 use as the Ultrasonic sensor.SIM800 also you can use as gsm modem.

SIM900 mini

SR04

GSM modem requires at least 2A when booting. so 7805 regulator nit recommend for power supply. it’s better to use the SMPS power supply for the GSM modem. I used the LM2596 buck convertor. the input voltage for the LM2596 is 12V 2A. GSM modem requires regulated 5V 2A power. so LM2596 pretty good module and cheep module for the giving power to the circuit.

LM2596

LM2596 module

Let’s connect the Arduino and GSM modem

in this case, we need to establish communication between Arduino and GSM modem. GSM modem communicates via a serial port. so we need a separate serial port. but only Arduino mega and some of the board have few serial ports. Arduino Uno, nano, pro mini have only one serial port. we need one serial port for debug print (Serial.println). so in this case I will make another serial port using code. for that, I will use the software serial library. and define RX an TX as D10 and D11 pins.

inclide <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX

if the connection between Arduino and GSM modem ok, when we send “AT” to the modem it will reply as “OK”

Arduino code for test GSM modem

#include "SoftwareSerial.h"
SoftwareSerial mySerial(10, 11); // RX, TX
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
}
void loop() { // run over and over
if (mySerial.available()) {
Serial.write(mySerial.read());
}
if (Serial.available()) {
mySerial.write(Serial.read());
}
}

Code For the send SMS using Arduino and GSM modem

we have to use standard AT command for send SMS. I will use AT command. not use any library for sending SMS via GSM modem
AT+CMGF=1
GSM mode has two modes during send SMS. those are PDU mode and Text mode. both of them, we have to use text mode. so I will send CMGF as 1. if you send CMGF as 0, modem work under PDU mode.
AT+CMGS="+94717424602"
You can send a sender number using this command. the number should start and end should be double Quotation.the c programming language we use double Quotation for representing string data. but if you put \ (backslash) in the c, the next to backslash letter will not consider while compiling the program. so you send a mobile number as below too. mobileNUmber is a string variable that we use.
AT+CMGS=\"" + mobileNUmber + "
Then you can send a message body
Finally, you can send a CTRL+Z command.
then you can send CTRL+Z as typing 0x1A or (char)26
mySerial.print((char)26);

Sending SMS to Some one

void sendSMS(String mobileNUmber, String msgBody) {
mySerial.println("AT"); // check connection
readResponse(); // read modem response
delay(1000);
mySerial.println("AT+CMGF=1");// SET MODULE TO TEXT MODE
readResponse(); // read modem response
delay(1000);
mySerial.println("AT+CMGS=\"" + mobileNUmber + "\""); // sender mob number
readResponse(); // read modem response
delay(1000);
mySerial.println(msgBody); // enter msg body
delay(1000);
mySerial.print((char)26); // send msg
delay(1000);
}


void readResponse() {
//simple function for read modem response. we will check work or not
delay(1000);
String response = mySerial.readStringUntil("OK");
Serial.println(response);
}

Let connect the Ultrasonic sensor to the Arduino

first you should connect the power pin to the 5volt rail. then connect the echo pin to the D7.
Finally trigger pin to the D8.

Now we going to the codings

digitalWrite(trig, HIGH); // triger signal as datasheet
delayMicroseconds(10);
digitalWrite(trig, LOW);

Convert time duration to the actual distance

duration = pulseIn(echo, HIGH);
dis = duration * (0.034 / 2.00); // speed of sound

Let do final coding

/*
  Software serial multple serial test
  Receives from the hardware serial, sends to software serial.
  Receives from software serial, sends to hardware serial.
  The circuit:
   RX is digital pin 10 (connect to TX of other device)
   TX is digital pin 11 (connect to RX of other device)
  Note:
  Not all pins on the Mega and Mega 2560 support change interrupts,
  so only the following can be used for RX:
  10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69
  Not all pins on the Leonardo and Micro support change interrupts,
  so only the following can be used for RX:
  8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
  created back in the mists of time
  modified 25 May 2012
  by Tom Igoe
  based on Mikal Hart's example
  This example code is in the public domain.
*/
#define echo 7 // connect echo pin to arduino D7 (ultra sonic sensor)
#define trig 8 // connect trig pin to arduino D8 (ultra sonic sensor)
#define led 13 // optional
#define thresholdDis 20
long duration; // store time duration of sound travel
int dis; // store actual distance to object
#include "SoftwareSerial.h"
SoftwareSerial mySerial(10, 11); // RX, TX
bool  flag = 0;
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  pinMode(echo, INPUT); // echo pin work as input
  pinMode(trig, OUTPUT);
  //sendSMS("0760154020", "Hi, Hello.this is me :)"); // send msg one time
}
void loop() { // run over and over
  float disInCm = checkDistance();
  Serial.println(disInCm);
  if ((disInCm > thresholdDis) && (flag == 0)) {
    flag = 1; // prevent sms sending continusly
    sendSMS("0717424602", "Max distance detected. now distance is " + String(disInCm) + "Cm"); // send msg one time
  }
  if (disInCm < thresholdDis) {
    flag = 0; // clear flag for next event
  }
  delay(100);
}
int checkDistance() {
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH); // triger signal as datasheet
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  duration = pulseIn(echo, HIGH);
  dis = duration * (0.034 / 2.00); // speed of sound
  return dis;
}
void sendSMS(String mobileNUmber, String msgBody) {
  mySerial.println("AT"); // check connection
  readResponse(); // read modem response
    delay(1000);
    mySerial.println("AT+CMGF=1");// SET MODULE TO TEXT MODE
    readResponse(); // read modem response
    delay(1000);
  mySerial.println("AT+CMGS=\"" + mobileNUmber + "\""); // sender mob number
  readResponse(); // read modem response
  delay(1000);
  mySerial.println(msgBody); // enter msg body
  delay(1000);
  mySerial.print((char)26); // send msg
  delay(1000);
}
void readResponse() {
  //simple function for read modem response. we will check work or not
  delay(1000);
  String response = mySerial.readStringUntil("OK");
  Serial.println(response);
}

Full schematic diagram of the system

All done, Happy coding