Introduction: MATLAB Controlled Microcontroller (Arduino MKR1000)
The goal of our project was to utilize MATLAB as well as the Arduino MKR1000 to the best of our ability. Our goal was to create a script that allowed certain features of the arduino to perfom a certain output based on a specific input. We used many loops and conditional statements featured in MATLAB that made this possible. We also used MATLAB mobile by using data acquired from a mobile device's gyroscope to enhance the project as much as possible.
Step 1: Parts and Materials
-The 2018 version of MATLAB is the most preferred version, mostly because it works best with the code that connects to the mobile device. However, most of our code can be interpreted by the majority of MATLAB versions.
-This is a specific device that allows us to wire circuits up to both digital and analogous ports. It is vital that you also have a breadboard to go along with it.
-When using the MKR1000, we needed accessory parts in order to perform the necessary functions.
- Button (6)
- Interchangeable RBG LED light
- Simple Wires
- mini power switch
- temperature sensor
- 330 ohm resistor
- 10K ohm resistor
- USB-microUSB cord
- Mobile device
It is also to be noted that there are many, many more accessories that can be used with the MKR1000
Step 2: MATLAB Arduino Support Package
In order to properly use the Arduino MKR1000 through MATLAB, you must download the MATLAB Support Package for Arduino Hardware. This download gives you access to certain functions and commands directly to the arduino board.
You can download the package at the link below
Step 3: Using Data Sensors Acquired From Mobile Device
The MATLAB Mobile app allows us to use a mobile device to stream data by using its gyroscope. To acquire the data through MATLAB, we obtain the data by retrieving it from the orientation matrix from MATLAB mobile. We do this by creating a variable for each column of the orientation matrix (Azimuth, Pitch, and Roll), and indexing the constant stream of values from the mobile device to the computer. This allows us to create conditional statements that will preform an output if MATLAB picks up a specific data input from the mobile device. In order to do this, you will need MATLAB mobile on your mobile device, and the mobile device support package for MATLAB on your computer.
You can download the file at the link below
Step 4: The Code and Wiring Ports
The code begins with a startup message that asks if we want to start up our home security device. If we answer yes, and give the correct passcode, the script immediately jumps into a while loop. From there, it begins collecting data from the mobile device. There are conditionals that read this data. We can unlock and lock the system from our mobile device, and the code will turn the servo and blink the LED light depending on the given data from the mobile device.
startup = questdlg('Would you like to activate ecoTECH Smart Home Energy System?'); % Begins activation sequence of ecoTECH
waitfor(startup); if startup == "Yes" % If "Yes" is selected an activation sequence begins and enters the while loop at the end power = "on"; m1 = msgbox('Starting up ecoTECH...'); pause(2); delete(m1); m1_wait = waitbar(0,'Please wait...'); steps = 25; for i = 1:steps pause(.1); waitbar(i/steps); % Updates the waitbar end delete(m1_wait); PASSCODE = [0 0 0 0]; % Initializes passcode ii = 0; % Initializes a variable used to break out of loops m2 = msgbox('ecoTECH fully operational!'); pause(2); delete(m2); elseif startup == "No" || startup == "Cancel" % If "No" or "Cancel" is selected then the activation sequence doesn't start and doesn't enter the while loop power = "off"; m3 = msgbox('Okay! Goodbye!'); pause(2); delete(m3); end
% ecoTECH in Action Section while true while power == "on" % Mobile Key Section while true % Collects data about the Roll orientation of the mobile device KEY = m.Orientation(3); % Collects data about the buttons b2 = readDigitalPin(a,'D2'); % Button 2 (Red) b3 = readDigitalPin(a,'D3'); % Button 3 (White) if KEY >= 35 % in degrees m4 = msgbox('Welcome Home!'); writeDigitalPin(a,'D8',1); % Turns on the green light pause(.5); writePosition(s,1); % Turns the servo to unlock the door pause(2); writeDigitalPin(a,'D8',0); % Turns off the green light delete(m4); elseif KEY <= -35 % in degrees m5 = msgbox('Door Locked!'); writeDigitalPin(a,'D7',1); % Turns on the red light pause(.5); writePosition(s,0); % Turns the servo to lock the door pause(2); writeDigitalPin(a,'D7',0); % Turns off the red light delete(m5); ii = 1; break % Exits while loop containing mobile key if the door is locked using the mobile device elseif b2 == 0 && b3 == 0 % Exits while loop containing mobile key to enter passcode section break end end
After this, it can enter another while loop. This while loop controls the outcomes based on the inputs from the buttons. If the first while loop is negligible, or a manual lockdown is required, it will enter another while loop where a specific password in needed. If the password is incorrect, it will restart the loop.
if ii == 1 % Exits while loop containing passcode if the door was unlocked with the mobile device break end % Collects data about the buttons b5 = readDigitalPin(a,'D5'); % Button 5 (Blue) b1 = readDigitalPin(a,'D1'); % Button 1 (Black) b4 = readDigitalPin(a,'D4'); % Button 4 (White) if b5 == 0 % Starts passcode entering section for b = 1:5 m6 = msgbox('Please Press And Hold A Button'); pause(2); delete(m6); % Collects data about the buttons b1 = readDigitalPin(a,'D1'); % Button 1 (Black) b2 = readDigitalPin(a,'D2'); % Button 2 (Red) b3 = readDigitalPin(a,'D3'); % Button 3 (White) b4 = readDigitalPin(a,'D4'); % Button 4 (Yellow) b5 = readDigitalPin(a,'D5'); % Button 5 (Blue) % Replaces the values in the initial passcode one at a time if b1 == 0 PASSCODE(0+b) = 1; elseif b2 == 0 PASSCODE(0+b) = 2; elseif b3 == 0 PASSCODE(0+b) = 3; elseif b4 == 0 PASSCODE(0+b) = 4; elseif b5 == 0 PASSCODE = sprintf('%.0f%.0f%.0f%.0f',PASSCODE(1), PASSCODE(2), PASSCODE(3), PASSCODE(4)); % Turns the sequence of buttons pressed into numbers then converts it to a string end end % End of for loop for entering digits for passcode if PASSCODE == "2314" % If the passcode entered matches then the door unlocks for a few seconds then locks m7 = msgbox('Welcome Home!'); writeDigitalPin(a,'D8',1); % Turns on the green light pause(.5); writePosition(s,1); % Turns the servo to unlock the door pause(5); writeDigitalPin(a,'D8',0); % Turns off the green light pause(.1); writeDigitalPin(a,'D7',1); % Turns on the red light pause(.5); writePosition(s,0); % Turns the servo to lock the door pause(1); writeDigitalPin(a,'D7',0); % Turns off the red light delete(m7); ii = 1; break % Exits while loop containing passcode after entering the correct passcode elseif PASSCODE ~= "2314" writeDigitalPin(a,'D7',1); % Turns on the red light m8 = msgbox('Incorrect Passcode! Try Again!'); waitfor(m8) writeDigitalPin(a,'D7',0); % Turns off the red light continue % Allows you to re-enter a passcode end elseif b1 == 0 && b4 == 0 % Exits while loop containing passcode section if you do not want to enter a passcode ii = 1; break end end
If correct, it will immediately enter a while loop that controls the temperature sensor. If the switch is on, the loop will continue through and plot the temperature vs. time plot, allowing one to view the trend. If the button connected to the temperature sensor is pressed, it will also send an email telling you what the temperature is in your "home". If the switch is turned off, it will immediately end the code.
t = 0; % Initial time = 0 seconds
pause(5) % Gives time for the user to turn on the temperature switch SWITCH = readDigitalPin(a,'D11'); % Collects data about the switch in pin D11 while SWITCH == 0 SWITCH = readDigitalPin(a,'D11'); % Collects data about the switch in pin D11 voltage = readVoltage(a,'A1'); % Reads the voltage from the temperature sensor temp_C = (voltage.*1000 - 500)./10; % Converts the voltage into temperature in °C temp_F = (9/5).*temp_C + 32; % Converts from °C to °F plot(t,temp_C,'b.') hold on title('Room Temperature'); xlabel('Time in Seconds'); ylabel('Temperature'); axis([0,180,0,100]); plot(t,temp_F,'r.') legend('Temperature in °C','Temperature in °F') pause(1); t = t + 1; % Time counter in seconds % E-mail Section b_temp = readDigitalPin(a,'D0'); % Collects the data of the (blue) temperature button in pin D0 if b_temp == 0 setpref('Internet','SMTP_Server','smtp.gmail.com'); setpref('Internet','E_mail','ef230ecoTECH@gmail.com'); % Sender setpref('Internet','SMTP_Username','ef230ecoTECH@gmail.com'); % Sender's username setpref('Internet','SMTP_Password','Integral_ecoTECH'); % Sender's password props = java.lang.System.getProperties; props.setProperty('mail.smtp.auth','true'); props.setProperty('mail.smtp.socketFactory.class', 'javax.net.ssl.SSLSocketFactory'); props.setProperty('mail.smtp.socketFactory.port','465'); sendmail('firstname.lastname@example.org','ecoTECH Room Temperature',sprintf('The current room temperature is %.1f °C or %.1f °F.',temp_C,temp_F)); % Sends an e-mail to the recipient giving data on the current room temperature fprintf('E-mail successfully sent!\n') end if temp_F >= 75 % If the room temperature increases to 75 °F... temp_AC = 65; % Changes the temperature on the thermostat to 65 °F elseif temp_F <= 65 % If the room temperature decreses to 60 °F... temp_AC = 80; % Changes the temperature on the thermostat to 80 °F end end % End of "SWITCH == 0" while loop if power == "off" || ii == 1 % Exits "power == on" while loop break end end % End of "power == on" while loop if power == "off" || ii == 1 % Exits entire while loop break end
This was just a brief overview of the code and its functionability. We have attached the full code as a pdf if needed.
Here is the list of which port each device is wired into.
1. RGB LED: Digital Pins (7,8,9)
2. Servo: Digital Pin 6
3. Buttons: Digital Pins (1,2,3,4,5)
4. Blue Button for email: Digital Pin 0
5. Temperature Sensor: Analogue Pin 1
6. Switch: Digital Pin 11