Altium
Matlab

Ultrasonic sensor, Servo motor, and Arduino interfacing with MATLAB

Description:

Ultrasonic sensor, servo motor, and Arduino interfacing with MATLAB– In this tutorial we are using ultrasonic sensor HC-SR40T, Servo motor, and Arduino with MATLAB.  The servo motor will be used for the rotation of the ultrasonic sensor and the data will be send to the Arduino and from the Arduino data will be send to the MATLAB. Checkout some of my intermediate and advanced level projects on Arduino and Matlab.


Components Required:

We will require the following components for this project:

  • Ultrasonic sensor HC-SR40T
  • Arduino UNO
  • Servo motor

Amazon Links:

Ultrasonic sensor HC-SR40T

Arduino UNO

Servo motor

Other Tools and Components:

Top Arduino Sensors:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Arduino Matlab Project Circuit Diagram:

Ultrasonic sensor servo motor Matlab

So let’s see the connection used in our project as we know that the ultrasonic sensor consists of four pins which are:

  • VCC
  • Ground
  • Echo
  • Trigger

So in order to connect the ultrasonic sensor with the Arduino we will perform the following steps:

  • Connect the ground of the ultrasonic sensor with the ground of the Arduino
  • Connect the VCC of the ultrasonic sensor with the 5V of the Arduino
  • Connect the Echo pin of the ultrasonic sensor with the digital pin 11 of the Arduino
  • Connect the trigger pin of the ultrasonic sensor with the digital pin 12 of the Arduino

Now in order to connect the servo motor to the Arduino:

  • Connect the VCC of the servo motor with the 5V of Arduino
  • Connect the ground of the servo motor with the ground of the Arduino
  • Connect the signal wire with the digital pin 9 of the Arduino

For the VCC we can also use the external power supply for the servo motor because the servo motors is consume huge amount of current while starting search which may damage our Arduino board.



Arduino code for the Ultrasonic sensor and servo motor:

First of all we will includethe servo motor library

Then we have created servo object with the name myservo

Then we have declared some variables which are:

CM for the storing distance, duration for storing the time and pos for storing the position of the servo motor. Duration will store the time of the generated ultrasonic pulse which will go and return back to the sensor and CM will store the distance of the obstacle.


As we have connected the ultrasonic sensor with the digital pin 11 and 12 so we will declare it as:

In the setup we have defined the pin 9 to which a servo motor will be attached then we have started the serial monitor and declare the trigPin as OUTPUT and echoPin as input.

Now in the loop function we have started a servo motor to rotate from 0 to 180 degree with a gap of four degrees then we have generated an ultrasonic pulse and then we have calculated the duration and the distance of the obstacle and we have stored this in the variable duration and cm. Then we have print these variables onto the serial monitor of the Arduino.


Arduino Complete code:


Interfacing Arduino with MATLAB:

First of all we will write the following command in order to clear all the variables and figures in the MATLAB.

Then we established serial communication with the Arduino board using the command in which we will define the port number , baud rate and rate of the serial communication which we have define in the Arduino code and write the fopen command to initiate the Arduino communication

We have also prepared two variables and initialized each of them to zero the CM and the POS.

Then we have written our while loop which works until our servo motor reached the angle of 180 degree and inside this loop we have given the position of the servo motor as well as the reading of the ultrasonic sensor from the serial monitor of the  Arduino.Also we have made a table in which we have stores each of these variables that is the position of the servo motor as well as the reading of the ultrasonic sensor.



After this while loop ends we have created a polar plot of our surroundings we have written polar to plot the data on the graph also given the title to a map switch on the grid and at last we have ended the communication with our Arduino by writing fclosecommad.


Complete MATLAB code:

Now our project is completed first of all we will upload the code to the Arduino then we will open the MATLAB and now in the MATLAB window run your MATLAB code to start the demonstration of the project. The servo motor will start rotation it is going from zero degree to 180 degree with gap of four degree each it is taking the reading of the obstacle which will come in front of it and the data will be plotted in the MATLAB.

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

Back to top button
%d bloggers like this: