.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);
 }

Get instant access to the code, model, or application of the video or article you found helpful! Simply purchase the specific title, if available, and receive the download link right away! #MATLABHelper #CodeMadeEasy

Ready to take your MATLAB skills to the next level? Look no further! At MATLAB Helper, we've got you covered. From free community support to expert help and training, we've got all the resources you need to become a pro in no time. If you have any questions or queries, don't hesitate to reach out to us. Simply post a comment below or send us an email at [email protected].

And don't forget to connect with us on LinkedIn, Facebook, and Subscribe to our YouTube Channel! We're always sharing helpful tips and updates, so you can stay up-to-date on everything related to MATLAB. Plus, if you spot any bugs or errors on our website, just let us know and we'll make sure to fix it ASAP.

Ready to get started? Book your expert help with Research Assistance plan today and get personalized assistance tailored to your needs. Or, if you're looking for more comprehensive training, join one of our training modules and get hands-on experience with the latest techniques and technologies. The choice is yours – start learning and growing with MATLAB Helper today!

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

>