.st0{fill:#FFFFFF;}

Arduino with Servo Motors and GSM 

 March 15, 2019

By  Aditya Chaudhari

In this webinar episode, we focused on connecting Arduino with Servo Motors, GSM and wirelessly by covering the following topics:
1. Connecting Bluetooth HC-05 with MATLAB
2. Control LED from Android using Bluetooth
3. Control Servo Motor from Android using Bluetooth
4. Connect GSM Module with Arduino and MATLAB. Test the Connection!
5. More projects with wireless technologies and their application.

Connection Diagram (LED & Servo Motor):

Arduino Webinar 3

Connection Diagram (GSM Module):

Arduino Webinar 3

MATLAB & Arduino Codes:

// Filename: LEDSerial.ino
#include <SoftwareSerial.h>
//Initialize the Variables and ports
int TxD;
int RxD;
int data;
SoftwareSerial bluetooth(10, 9); // Connection: TX at 10; RX at 9

void setup() {
  Serial.begin(9600);
  bluetooth.begin(9600);
  Serial.println("The Bluetooth Channel is open.."); //Display when the bluetooth is ready
}

void loop()
{
  if(bluetooth.available())//Will check if bluetooth is available
  {
    data = bluetooth.read(); // Read data from serial monitor of MATLAB
    Serial.write(bluetooth.read());
    Serial.print("\n");
    
    if(data == '1') //Set LED to High and Low
      {         
        digitalWrite(11, HIGH);
        Serial.println("LED On!");

      }
    else if(data == '0') //Set LED to High and Low     
      {
        digitalWrite(11, LOW);
        Serial.println("LED OFF!");

      }
  }
 
  if(Serial.available())
  {
    bluetooth.write(Serial.read());
  }
}

% Filename:  Connection.m

%% Connect to the bluetooth with name HC-05
B=Bluetooth('HC-05',1)

%% Check the connection information
instrhwinfo('Bluetooth')

% Filename: SendCommands.m

clear all
clc
%% Connect to the bluetooth with name HC-05
B=Bluetooth('HC-05',1)

%% Open the Connection for Serial Communication
fopen(B);

%% Turn ON LED by sending High(1)
fprintf(B,'1');

%% Turn Off LED by sending Low(0)
fprintf(B,'0');

// Filename: ServoSerial.ino

#include <SoftwareSerial.h>
#include <Servo.h>
//Initialize the Variables and ports
int TxD;
int RxD;
int data;
Servo myservo;// create servo object to control a servo
int pos = 0;    // variable to store the servo position
SoftwareSerial bluetooth(10, 9); // Connection: TX at 10; RX at 9

void setup() {
myservo.attach(6);  // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
bluetooth.begin(9600);
Serial.println("The Bluetooth Channel is open.."); //Display when the bluetooth is ready
}

void loop()
{
    if(bluetooth.available())//Will check if bluetooth is available
    {
        data = bluetooth.read(); // Read data from serial monitor of MATLAB
        Serial.write(bluetooth.read());
        Serial.print("\n");
        
        if(data == '1') //Set LED to High and Low
        {
            for (pos = 0; pos <= 180; pos += 1)
              { // goes from 0 degrees to 180 degrees
                // in steps of 1 degree
                myservo.write(pos);              // tell servo to go to position in variable 'pos'
                delay(15);                       // waits 15ms for the servo to reach the position
              }
            for (pos = 180; pos >= 0; pos -= 1)
                    { // goes from 180 degrees to 0 degrees
                      myservo.write(pos);              // tell servo to go to position in variable 'pos'
                      delay(15);                       // waits 15ms for the servo to reach the position
                    }
                        
        }
        else if(data == '0') //Set LED to High and Low
        {
            int pos = 0;
            myservo.write(pos);
        }
    }
                                
    if(Serial.available())
    {
      bluetooth.write(Serial.read());
    }
                                    
    if(Serial.available())
    {
     bluetooth.write(Serial.read());
    }
}

// Filename: GSMSerial.ino

#include <SoftwareSerial.h>

SoftwareSerial mySerial(9, 10);

void setup()
{
  mySerial.begin(9600);   // Setting the baud rate of GSM Module  
  Serial.begin(9600);    // Setting the baud rate of Serial Monitor (Arduino)
  delay(100);
}


void loop()
{
  if (Serial.available()>0)
   switch(Serial.read())
  {
    case '1':
      SendMessage();
      break;
    case '0':
      RecieveMessage();
      break;
  }

 if (mySerial.available()>0)
   Serial.write(mySerial.read());
}


 void SendMessage()
{
  mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  delay(1000);  // Delay of 1000 milli seconds or 1 second
  mySerial.println("AT+CMGS=\"+91XXXXXXXXXX\"\r"); // Replace x with mobile number
  delay(1000);
  mySerial.println("I am SMS from GSM Module");// The SMS text you want to send
  delay(100);
   mySerial.println((char)26);// ASCII code of CTRL+Z
  delay(1000);
}


 void RecieveMessage()
{
  mySerial.println("AT+CNMI=2,2,0,0,0"); // AT Command to receive a live SMS
  delay(1000);
 }

Did you find some helpful content from our video or article and now looking for its code, model, or application? You can purchase the specific Title, if available, and instantly get the download link.

Thank you for reading this blog. Do share this blog if you found it helpful. If you have any queries, post them in the comments or contact us by emailing your questions to [email protected]. Follow us on LinkedIn Facebook, and Subscribe to our YouTube Channel. If you find any bug or error on this or any other page on our website, please inform us & we will correct it.

If you are looking for free help, you can post your comment below & wait for any community member to respond, which is not guaranteed. You can book Expert Help, a paid service, and get assistance in your requirement. If your timeline allows, we recommend you book the Research Assistance plan. If you want to get trained in MATLAB or Simulink, you may join one of our training modules. 

If you are ready for the paid service, share your requirement with necessary attachments & inform us about any Service preference along with the timeline. Once evaluated, we will revert to you with more details and the next suggested step.

Education is our future. MATLAB is our feature. Happy MATLABing!

About the author 

Aditya Chaudhari

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

MATLAB Helper ®

Follow: YouTube Channel, LinkedIn Company, Facebook Page, Instagram Page

Join Community of MATLAB Enthusiasts: Facebook Group, Telegram, LinkedIn Group

Use Website Chat or WhatsApp at +91-8104622179

>