Jannah Theme License is not validated, Go to the theme options page to validate the license, You need a single license for each domain name.
Matlab

Home Automation System using Matlab GUI and Arduino

Home automation system, Overview:

Home Automation System using Matlab GUI and Arduino- I have been making different types of Home automation and industrial automation projects using different types of controller boards including Arduino Uno, Arduino Nano, Arduino Mega, ESP8266, ESP32 WiFi + Bluetooth Module, Raspberry Pi, and PLC. Below is the list of some of my automation projects which I believe will really help you in making intermediate and advanced level Home automation projects.


In this tutorial, we will design a GUI “Graphical User Interface” application in Matlab for controlling different Home appliances like a fan, TV, motor, and lights. You can increase the number of appliances according to your requirement. This project is based on the Matlab and Arduino Uno. First you will need to do some settings before you can send the control commands from the Matlab GUI to the Arduino Uno board. So, if you are using the Arduino and Matlab for the first time then you should read my previous articles on the Matlab.

So, after reading the above articles and after installing the Hardware support package for the Arduino then you are good to go.



Amazon Links:

Arduino Uno

5v relay module

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!


Circuit diagram of Matlab home automation Project:

Home Automation system using Matlab

The circuit diagram of the Home automation project is pretty simple. As you can see the neutral wire is connected with all the devices. While the 110/220Vac phase wire is connected with the Common contact of all the relays. The relay is a mechanical switch which we will use to connect and disconnect the Live wire.

  • Connect the digital pin 1 of the Arduino with the signal pin of the relay module and connect the normally open of the relay module with the fan.
  • Connect the digital pin 2 of the Arduino with the signal pin of the relay module 2 and connect the normally open of the relay module with the light.
  • Connect the digital pin 3 of the Arduino with the signal pin of the relay module 3 and connect the normally open of the relay module with the TV.
  • Connect the vcc of all three relay module with the 5V and connect the ground of the relay module with the ground of the Arduino.

You can also use a 12V relay module for which you will need 12Vdc power supply.

Note: Be very careful while working on such high voltage system because it can be really dangerous. Don’t forget to wear protective gloves.


Matlab GUI Design for Home Automation:

In order to make the Matlab GUI application, we will write guide in the command window in the MATLAB.

Home Automation system using Matlab

When we will press enter the GUI platform will open.

First of all we will insert a panel by clicking on the panel icon:

Home Automation system using Matlab

Then we will change the color of the panel and also change the title. In order to change the color double click on the panel, a dialog box will open. Now click on the background color and select the color.

Home Automation system using Matlab

Now will need several buttons for ON and OFF for various appliances such as:

  • FAN ON and FAN OFF
  • LIGHT ON and LIGHT OFF
  • TV ON and TV OFF
  • ALL ON and ALL OFF


When we will press the FAN ON, the fan will turn ON and when we will press on the FAN OFF the fan will turn OFF and so on.

Home Automation system using Matlab

After adding these Push Buttons, the next step is to change the tag for each button and we will also need to change the title of the button. For example, for the FAN ON, we will change the title of the pushbutton to FAN ON and tag to FANON. So, you will need to repeat this for all the buttons.

Home Automation system using Matlab

Now as our project GUI is complete now we will save the project. After saving the project. the code will automatically open.

Home Automation system using Matlab



Matlab Home Automation code:

First of all in the main function we will declare a variable which will get the value of the Arduino port. We declare the “a” variable as a global which means that it is accessible to all functions and the clear all function will clear all the previous values.

function varargout = home_automation_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;
clear all;
global a;
a = arduino;

Now for each button we will write the code:

Fan button function:

When we click the fan on button the following function will be called:

function fanon_Callback(hObject, eventdata, handles)
% hObject    handle to fanon (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
writeDigitalPin(a,'D1',1);

Now in this function we will make digital pin 1 of the Arduino “1” which means that it will turn on the fan.

Now when we will click the off button then we will write 0 to the digital pin 1 which will make the fan turn off.

function fanoff_Callback(hObject, eventdata, handles)
% hObject    handle to fanoff (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
writeDigitalPin(a,'D1',0);


Light button function:

Similarly we have connected light to digital pin 2 of the Arduino. when we will click the light button on the lighton function will be called in which we have make the digital pin high due to which the light will be turn on.

function lighton_Callback(hObject, eventdata, handles)
% hObject    handle to lighton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
writeDigitalPin(a,'D2',1);

when we will click the light off button it will make the digital pin 2 low due to which the light will be turn off.

function lightoff_Callback(hObject, eventdata, handles)
% hObject    handle to lightoff (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
writeDigitalPin(a,'D2',0);

TV button function:

Similarly for the TV button on function will make the digital pin 3 high due to which the TV will be turned on.

function tvon_Callback(hObject, eventdata, handles)
% hObject    handle to tvon (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
writeDigitalPin(a,'D3',1);

When we will click the tv off button the tv will be turned off.

function tvoff_Callback(hObject, eventdata, handles)
% hObject    handle to tvoff (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
writeDigitalPin(a,'D3',0);

ALL turn on and off function:

Now if we want to turn on all the appliances together we will click on the all turn on it will make all the digital pins high and all the appliances will be turned on.

function allon_Callback(hObject, eventdata, handles)
% hObject    handle to allon (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

writeDigitalPin(a,'D1',1);
writeDigitalPin(a,'D2',1);
writeDigitalPin(a,'D3',1);

Similarly, when we will click on all off, all the digital pins will be low and all the appliances will turn off.

function alloff_Callback(hObject, eventdata, handles)
% hObject    handle to alloff (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
writeDigitalPin(a,'D1',0);
writeDigitalPin(a,'D2',0);
writeDigitalPin(a,'D3',0);


Matlab GUI Home Automation, Complete code:

function varargout = home_automation(varargin)
% HOME_AUTOMATION MATLAB code for home_automation.fig
%      HOME_AUTOMATION, by itself, creates a new HOME_AUTOMATION or raises the existing
%      singleton*.
%
%      H = HOME_AUTOMATION returns the handle to a new HOME_AUTOMATION or the handle to
%      the existing singleton*.
%
%      HOME_AUTOMATION('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in HOME_AUTOMATION.M with the given input arguments.
%
%      HOME_AUTOMATION('Property','Value',...) creates a new HOME_AUTOMATION or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before home_automation_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to home_automation_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help home_automation

% Last Modified by GUIDE v2.5 31-Jul-2021 12:38:10

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @home_automation_OpeningFcn, ...
                   'gui_OutputFcn',  @home_automation_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before home_automation is made visible.
function home_automation_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to home_automation (see VARARGIN)

% Choose default command line output for home_automation
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes home_automation wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = home_automation_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;
clear all;
global a;
a = arduino;


% --- Executes on button press in fanon.
function fanon_Callback(hObject, eventdata, handles)
% hObject    handle to fanon (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
writeDigitalPin(a,'D1',1);


% --- Executes on button press in fanoff.
function fanoff_Callback(hObject, eventdata, handles)
% hObject    handle to fanoff (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
writeDigitalPin(a,'D1',0);


% --- Executes on button press in lighton.
function lighton_Callback(hObject, eventdata, handles)
% hObject    handle to lighton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
writeDigitalPin(a,'D2',1);


% --- Executes on button press in lightoff.
function lightoff_Callback(hObject, eventdata, handles)
% hObject    handle to lightoff (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
writeDigitalPin(a,'D2',0);


% --- Executes on button press in tvon.
function tvon_Callback(hObject, eventdata, handles)
% hObject    handle to tvon (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
writeDigitalPin(a,'D3',1);


% --- Executes on button press in tvoff.
function tvoff_Callback(hObject, eventdata, handles)
% hObject    handle to tvoff (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
writeDigitalPin(a,'D3',0);


% --- Executes on button press in allon.
function allon_Callback(hObject, eventdata, handles)
% hObject    handle to allon (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

writeDigitalPin(a,'D1',1);
writeDigitalPin(a,'D2',1);
writeDigitalPin(a,'D3',1);
% --- Executes on button press in alloff.
function alloff_Callback(hObject, eventdata, handles)
% hObject    handle to alloff (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
writeDigitalPin(a,'D1',0);
writeDigitalPin(a,'D2',0);
writeDigitalPin(a,'D3',0);



Now our project is completed and now we can click on the Run button, the MATLABE GUI will automatically get connected with the Arduino.  

Home Automation system using Matlab

Home Automation system using Matlab

Now, all you need is to press the desired buttons to turn ON or turn OFF any electrical load. If you face any problems in executing the code, then I highly recommend you should read my previous articles on the Arduino and Matlab, links I have already shared above.

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

Your email address will not be published. Required fields are marked *

Back to top button