Introduction: Arduino Smart Home System

In this Instructable we will show you how to create your own smart home system with MATLAB's App Designer with a Sparkfun Red board. This Instructable can be used to help gain a base understanding of MATLAB's App Designer, as well as using a photoresistor, servomotor, and a PIR motion sensor.

Step 1: To Begin: Materials

This project requires the following materials:

- Arduino Uno (For this project we used a Sparkfun Red board)

- One photoresistor

- One mini-servo motor

- One continuous servo motor

- One PIR motion sensor

- One temperature sensor

- 2 LEDs

- Wires and resistors as needed

Step 2: Step 2: Approaching the Problem to Solve

The main goal of this project was to create an easy to use smart home system by coding an Arduino Uno board with MATLAB. We first thought about just working with a temperature and humidity sensor, however if we stayed with those two sensors our smart home system would not be easily marketable to a general audience. We decided that we wanted to create an overall smart home energy system that would work as a smart thermostat and security system. Finally, we wanted to work with MATLAB's AppDesigner so the user can easily alter the smart home as they please.

Step 3: Step 3: Configuring the GUI and Basic Code Flow

To start you will need to open up MATLABs AppDesigner and place the following:

Two numeric edit fields for a hot and cold threshold input

A button to unlock the door

And four indicator lamps for the fireplace, door, fan, and flood light.

Two labels to communicate with the user.

For this project we found it easier to work with global variables and the startup function within the designer. You will need these variables within the startup function:

global a
a = arduino('COM3','uno','Libraries','Servo');
global s
global p
global hotUI
global coldUI
global unlock
global temp
global curr_temp
global int_light

Right now we only have an assignment for the a variable so your computer can read the arduino. COM3 may differ depending on what port your computer may be using.

When you run the code, it will start within the startup function creating the global variables and calibrating the system. At the end of this function there will be a timer function that calls a property we named Timer. Within this Timer property we put in the code that runs the home system so the timer does not re-run the calibration code.

Note: We did not give any wiring instructions for the system. We referred to the manual that comes with the SparkFun Red board.

Step 4: Step 3: Setting Up the Thermostat System

The function for the thermostat works as following:

The user will input what temperature they consider to be too hot or too cold. Once the thermometer takes a reading, if the home is too cold then the "fireplace" (a red LED) will turn on and heat the home. If the home is too hot then a "fan" (continuous servo motor) will turn on cooling the house.

To code the Thermostat system:

We will start within the startup function to display the current temp and let the user input their cold and hot thresholds.

p = 'A0' %Photoresistor pin
volt = readVoltage(a,temp);
celc = (volt-0.5).*100;
curr_temp = celc*9/5+32; 
app.Label_4.Text = num2str(curr_temp); %Label number can change
pause(10); %May want to change!!!!!

Then we will complete the thermostat system within the Timer property.

global curr_temp
global coldUI
global a
global hotUI 
if curr_temp < coldUI
                %Fireplace
                writeDigitalPin(a,'D13',1); %D13 can change, this turns on fireplace
                app.FireplaceStateLamp.Color = [0.47 0.67 0.19]; %Turns GUI indicator lamp green
            elseif curr_temp > hotUI
                app.FanStateLamp.Color = [0.47 0.67 0.19]; %Turns GUI lamp green
                writePWMDutyCycle(a, 'D11', .9) %The next three lines of code run the servo fan
                pause (10)
                writePWMDutyCycle(a, 'D11', .0)
            else
                app.FireplaceStateLamp.Color = [0.90 0.90 0.90]; %This turns off all the GUI lamps and fireplace
                app.FanStateLamp.Color = [0.9 0.9 0.9]; 
                writeDigitalPin(a,'D13',0);
            end

Step 5: Step 4: Setting Up the Door System

The function for the door works as following:

When you first run your MATLAB code, the app will ask you to open the door so the photoresistor can take an initial light reading. Once that is completed, the timer will activate and the photoresistor will take secondary light readings. If the secondary light reading is lighter than the initial, a servo motor will lock the door. If the user wants the door unlocked, they can press a button on the app that will unlock the door.

To configure the servo motor and photoresistor:

To code the door system:

We will start within the startup function to take the initial light readings.

s = servo(a,'D9') %Pin may change based on wiring
app.Label_4.Text='Please open the door to calibrate the system';           
pause(15); %This gives time for the user to open the door
int_light = readVoltage(a,p);
app.Label_4.Text = 'You may remove your finger';

Next, we will complete the code within the Timer property

global unlock
global int_light
global s
global a
%Get a current light reading to compare
curr_light = readVoltage(a,p);
% -- Lock Door --
if int_light<curr_light
	writePosition(s,1) %Servo positions may differ per motor
	pause(0.5);
	app.DoorStateLamp.Color = [0.47 0.67 0.19];
	end
% -- Unlock Door --
if unlock == 1234
	pause(0.5);
	writePosition(s,.52)
	app.DoorStateLamp.Color = [0.85 0.33 0.10];
end

Finally we will create the unlock button callback. Once the user presses the unlock button, the global variable unlock will be assigned a number which can complete the final if statement in the Timer property.

global unlock
unlock = 1234;

Step 6: Step 6: Setting Up the Flood Light System

The function for the flood light works as following:

When you start the MATLAB code, the PIR motion sensor will start detecting motion. Once it detects some type of motion, it will cut a power signal. Once that signal is cut, a flood light will turn on outside the home.

To configure the flood light system:

To code the flood light system:

This time we can skip to the Timer property because we do not need to write any extra variables.

human_detected = readDigitalPin(a,'D2'); %Pin may change based on configuration<br>            
	    if human_detected == 0
                writeDigitalPin(a,'D7',1) %Pin may change
                app.FloodLightStateLamp.Color = [0.47 0.67 0.19];
            elseif human_detected == 1
                app.FloodLightStateLamp.Color = [0.9 0.9 0.9];
                writeDigitalPin(a,'D7',0)
            end

Step 7: Conclusion

Now that you have a draft of your GUI with the App Designer and your code for the Arduino you are ready to do your own edits or plug in your Arduino and go!