Mechatronics class and the assignment includes MATLAB Programming.

Anonymous
timer Asked: Oct 21st, 2018
account_balance_wallet $35

Question Description

Hello, this is a writing assignment for Mechtronics class and the assignment includes MATLAB Programming. I just want a lab report for the experiment, please see the attached files.

Mechatronics class and the assignment includes MATLAB Programming.
20181017_104317.jpg
Mechatronics class and the assignment includes MATLAB Programming.
20181017_104319.jpg

Unformatted Attachment Preview

Mechatronics Fall 2018 LAB 5 INTRODUCTION TO MICROCONTROLLER UNIT OBJECTIVE • • • Introduction to the Arduino Micro-Controller Unit (MCU). Learn about how to use Digital and Analog Input / Output (I/O) on Arduino. Review of programming using MATLAB. INTRODUCTION I. Arduino Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software. Arduino is intended for engineers, artists, designers, hobbyists, and anyone who is interested in creating new mechatronics designs. An Arduino board consists of an 8-bit Atmel AVR microcontroller with complementary components to facilitate programming and incorporation into other circuits. It can be programmed using its own Applications Fig. 1. Arduino Uno Programming Interface (API) or through special interfaces to MATLAB. We will program the Arduino in MATLAB during this class. An important aspect of the Arduino is the standard way that connectors are exposed, allowing the Arduino board to be connected to a variety of interchangeable add-on modules (known as shields). For example, we will use a shield designed for driving small motors later. Digital In, Digital Out, Analog Out Analog In Fig. 2: Board Outline Showing I/O Connections Page 1 of 9 Mechatronics Fall 2018 Arduino communicates with the outside world through “ports”. The USB port is the connection through which you program an Arduino. There are also input ports to provide sensor information to the board, and output ports through which the Arduino can issue commands to LEDs, motors, and other actuators. Input and output ports are of two general types: digital and analog. The locations of the ports are shown in Figure 2. The Analog ports at the bottom of the board are analog inputs only. The connections marked “Digital” on the top side may be used either as digital inputs or as digital outputs, individually software selectable. Certain of the digital ports may also be used as analog outputs, by using a technique called Pulse-Width Modulation (PWM). Your computer communicates with the Arduino through a USB connection, which also powers the board. No external power is required for the Arduino board, unless you need to drive something that requires more power or higher voltage than can be supplied by the USB port. II. Digital Inputs/Outputs (I/O) A digital input or output is nothing more than a switch closure. In the case of an input, the switch is external to the Arduino and may be a regular switch, or it may be a sensor such as a pressure sensor, a proximity detector, a photo sensor, or any of many other types of sensors. The key is that the sensor is binary – when the pressure or temperature or light intensity gets to a certain level, a switch closes. When the measured variable drops below the threshold level, the switch opens again. Our experiment with the digital input uses the simplest case, a mechanical switch. A digital output allows you to control a voltage with the Arduino. If the software instructs a digital output pin to be high, the output will produce a voltage of +5V for our Arduinos. If the software instructs the output to be low for a digital pin, it is connected to ground and produces a 0V voltage. A digital output pin can be seeing as a computer controlled switch inside Arduino, which can be used to control other external devices, such as an LED. III. Pull-Up and Pull-Down Resistors Digital inputs have two states, which can be “on or off”, “true or false”, “1 or 0”, or “high or low”. The two choices correspond to a voltage level that may change with the hardware, but in general will be approximately this: CMOS Input, High: Vin > 3.7V CMOS Input, Low: Vin < 1.3V CMOS stands for Complementary Metal Oxide Semiconductor. You will notice that there is a gap between the “low” and “high” levels in which the state of the gate is indeterminate (neither high nor low). We cannot be sure what will be the reaction of the gate to an input in this range of voltage, so we try to avoid this voltage region. This can be achieved by using one resistor and one switch. Consider the circuit in Figure 3, a resistor is connected on one end to the ground of Fig. 3. Configuration of Pull-down Resistor the power supply and on the other end to a switch that is also connected to +5V. The signal of interest is the Page 2 of 9 Mechatronics Fall 2018 voltage at the junction of the resistor and the switch. When the switch is open, no current flows through the resistor and thus the voltage at the junction is 0V. When the switch is closed, the voltage at the junction is pulled to +5V, and some current will flow through. The resistor is called pull-down resistor. Pull-down resistors are commonly used in electronic logic circuits to ensure that inputs to logic systems settles at 0V if external switches are disconnected. A pull-down resistor may be any of a wide range of values, but 10 kΩ to 47 kΩ are common values. A pull-up resistor works in the same way, but with the opposite logic. That is, the signal is high when the switch is open. Imagine that the positions of the resistor and switch are reversed. Now, when the switch is closed, it connects the digital input directly to ground. When the switch is open, the digital input is connected directly to +5V through the pull-up resistor. Either configuration can be used, depending on the logic you wish to implement. IV. Analog Inputs/Outputs (I/O) An Analog Input or Analog to Digital Converter (ADC or A/D or A to D) converts a voltage level into a digital value that can be stored and processed in a computer. Having analog input ports on a microprocessor allows you to collect data form a multitude of sensors that convert physical quantities such as temperature, pressure, light intensity etc. into voltages. The voltages are encoded as digital signals to be processed by the computer software, or simply stored in a data file for future analysis. An Analog Output or Digital to Analog Converter (DAC or D/A) performs the opposite operations. It converts the digital signal to analog voltage output, which can be used to control the motor speed, changing the light intensity, or producing the audio signals, etc. PROCEDURE I. Initialize the Arduino Board 1. Plug the board into a USB port on your computer and check that the green LED power indicator on the board illuminates. 2. Find which COM (i.e. serial communication) port is used by Arduino in Device Manager as shown in Figure 4. Typically, the port number should be between COM3 to COM7 depending on the computer. 3. To initialize the Arduino board, the following command is required: >> a = InitArduino(‘COMX’) Depending on which port is adopted by individual Arduino board, “COMX” would need to be modified. Once the command is executed, MATLAB will start to communicate with the Fig. 4. Device Manager Page 3 of 9 Mechatronics Fall 2018 MCU board, and a message “Attempting connection ......” will be displayed. Once the communication has been established successfully, the status of the board will be reported to the MATLAB prompt window. Note that you should only have to initialize the Arduino once per session. Do NOT put the line “a = InitArduino” in your MATLAB m-files. Once the board is initialized it should remain so unless: 1. Power is lost to the Arduino board; 2. MATLAB is closed and restarted; 3. The user issues a “clear” command which erases the initialization function call; 4. Arduino is frozen or acting unpredictably. In this case, you should power down Arduino, restart MATLAB, power Arduino back up, and reissue the initialization command. II. Basic Operation of Digital and Analog I/O As noted earlier, an Arduino has both input and output pins, and in both digital and analog types. 1. Digital Output: digital outputs are either “off” or “on”, i.e., like switches. It turns out that we don’t even have to connect anything to the Arduino to test the digital output command and function. Arduino has a built-in LED that is connected to digital output pin 13. Go to MATLAB Command window and type the following command: >> DigitalOut(a,13,1) The command will turn on the LED attached to pin 13. To turn it off, simply type >> DigitalOut(a,13,0) This command requires three parameters: the name specified for the Arduino board (“a”, from your initialization command earlier), the output pin number, and the pin status you wish to invoke: (ON = 1, OFF = 0). Note that there is a socket on the digital connector labeled “13”. This is in fact port 13, and if you connected a wire to that port, you would see (with the help of a multi-meter) the voltage on the wire go to +5V when the “on” or “1” command is issued, and you would see a voltage near zero on the pin when the output command was “0”. Of course, we are more interested in controlling objects that are not on the Arduino board. To do this, we must connect them to the board using the “digital” connector shown in Figure 2. Each pin or “port” is numbered on the board. Note that ports 1 and 2 are used for serial communications and are not available for general digital I/O. However, each of the other ports can be made a digital output and can be turned on or off by the command, DigitalOut(a,x,1) for “on” or DigitalOut(a,x,0) for “off” Note that the Arduino cannot supply much power, about 5 Volts and 40 milliamps, so only fairly small devices can be controlled directly from the board. You will need to use transistors, relays, or other power amplification devices to allow the Arduino to control higher power devices. For now, let’s be content with controlling of a simple LED. In the toolbox, there should be an LED and a 330 Ω resistor. The circuit is shown in the schematic of Figure 6. The LED Page 4 of 9 Mechatronics Fall 2018 will have two leads – one longer than the other. The longer lead is the Anode, and goes to the “+” side of the circuit – in this case, to pin 9. The cathode (i.e., the shorter lead) goes to the 330 Ω resistor. The 330 Ω resistor limits the current to the LED so it does not burn out. Use your breadboard to make the circuit – do not push the LED terminals or resistor leads directly into the Arduino I/O sockets. Note that you must complete the circuit by connecting the resistor back to the Arduino “ground”, which can be any one of three pins labeled “GND” on the board, shown in Figure 5. Fig. 5: Locations of Ground Pins Repeat the same commands you used above, but using pin 9 instead of pin 13 to turn the LED on and off. Anode (long) to pin 9 330 Arduino Ground Fig. 6: Schematic Showing How to Connect an LED to the Arduino Pin 9 2. Analog Output: Some of the digital ports on the Arduino board can also be used as analog output ports (e.g. pins 3, 5, 6, 9, 10, 11). Instead of turning the port on and off, these ports can generate output voltage between 0 to 5 volts. By changing the voltage, the brightness of the LED can be controlled. You can use the following command to change the brightness of your external LED: >> AnalogOut(a,9,volt) Page 5 of 9 Mechatronics Fall 2018 You can change the variable ‘volt’ to any number between 0 and 5. From the command line, type “AnalogOut(a,9,1)” and note the brightness of the LED. Then type “AnalogOut(a,9,5)” and note that the LED glows more brightly than before. Experiment with different values of the output voltage. 3. Program 1: Fade-in Fade-out Effect By now you probably have guessed that there are four commands we will use with the Arduino: • DigitalOut(a,port,on/off) on = 1, off = 0 • AnalogOut(a,port,voltage) 0 ≤ voltage < 5 • variable = DigitalIn(a,port) • variable = AnalogIn(a,port) These cover the four I/O functions mentioned earlier. Note that output functions have three arguments while input functions have only two, and that an input is stored by assigning a variable to the value of the input when the input is checked. While all of the I/O commands may be executed from the MATLAB command line, it is also possible to execute them from inside a MATLAB program, often called a “script” or an “mfile”. To visualize the effect of changing the analog output voltage, we can write a simple MATLAB program to control the brightness of the LED. In MATLAB, go to Home/New Script; the script editor will open, allowing you to write a new m-file. Type the following: %*********************************************************************** % Your Name % Example Program 1 % This program change the brightness of an LED connected to pin 9 %*********************************************************************** for k=0:1:5 AnalogOut(a,9,k); pause(1.0); end for k=5:-1:0 AnalogOut(a,9,k); pause(1.0); end % This loop goes from dark to bright % Output the voltage k on pin 9 % Pause for 1.0 second % This loop goes from bright to dark % Note that the loop counter counts down by % making the index negative % Output the voltage k on pin 9 % Pause for 1.0 second Look at the first four lines of the program. The structure used here is the for loop. If you don’t remember how to set up a for loop, type “help for” at the MATLAB command line, and examine the examples given above. The program uses two for loops – one to increase the LED brightness from zero to full brightness, and the second one to fade the LED back to no light emission. The for loop sets up a counter k that takes on the sequential values k = 0, 1, 2, 3, 4, 5. The counter k is then used as the analog output voltage command for Pin 9 in the next line of the code. The LED will start out dark when k = 0, and will increase in brightness in steps one second apart until Page 6 of 9 Mechatronics Fall 2018 it reaches the maximum brightness, when k = 5. The second for loop takes the LED from bright to dark by making k count backwards from 5 to zero. Save this file under the desktop folder “Student Documents.” Change the directory by clicking on File/Set Path and choosing the path ending with “Student Documents”. You may now use any M-File saved in this folder simply by typing its name in the command window. Save your program in the editor, choose File/Save As: Lastname_Firstname_LEDFade.m Show a TA your result. 4. Sensors and Digital Inputs The simplest sensor is a switch, which is in your toolbox. Use a breadboard and some wires to connect the switch, a 47 kΩ pull-up resistor, and a digital input pin on the Arduino (use digital pin 7), as shown in Figure 7. Go to the MATLAB Command Window, and type in the command: 5V pin on Arduino 47k To digital input >> DigitalIn(a,port#) where the port# is the number of the digital input pin to which you wired your sensor. You will see a “1” displayed as the “answer”. Now close the switch and use the up arrow key to get back to the command for reading the channel and press [Enter]. With the switch closed you should read a “0” returned as the answer. To Arduino ground Fig. 7. Digital Input with Pull-up Resistor Now, modify your command by typing >> y = DigitalIn(a,port#); Decide whether you want the switch to be open or closed and make it so before hitting [Enter] to execute the command. With the semicolon at the end of the statement, the “answer” will not be immediately displayed; however, the “answer” has been assigned to the variable y which can be used subsequently in equations or branching statements for program control. To see this type in: y [Enter]. MATLAB will display the value of y. Note that this value will stay the same, irrespective of whether the switch is open or closed until your program reassigns a new value to y. III. Integrated Programs To integrate both input signal and output commands, a while loop will be used to control the Arduino board. Construct the circuit following the schematic as shown in Figure 8. The circuit functions as follows: Pin 2 is a digital input, and it “sees” the voltage at point “A”. If the switch is closed, the voltage there is 5V or logic “1”. If the switch is open, the voltage at A is pulled down to ground by a 1 kΩ resistor. You will use digital input 2 to control whether the external LED or the LED connected to Pin 13 is on. 1. An example program to drive the two LEDs is given in the next page. Create a new script file in MATLAB and type this program into your script. Save it on your USB drive using a Page 7 of 9 Mechatronics Fall 2018 file name that will remind you of what the program does. Study the code to make sure you understand each step of the program. Now go ahead and run it. It should alternately light the LED on Pin 13 or on Pin 9. Press the switch to stop the program. Demonstrate your circuit and your program to a T/A. Be prepared to answer questions about how the program works. A 5V 1k Gnd 9 Anode (long) 330 2 Fig. 8. Schematic for the Integrated Program % ********************************************************************** % Your Name % Example Program 2 % This program toggles two LEDs connected to pin 9 and pin 13 %*********************************************************************** toggle = 1; % Initialize the toggle variable while DigitalIn(a,2)~=1 if toggle == 1 DigitalOut(a,13,0); DigitalOut(a,9,1); toggle = 0; pause(0.3) else DigitalOut(a,13,1) DigitalOut(a,9,0) toggle = 1; pause(0.3) end end % If switch is not pressed, enter the loop % otherwise, end the program % If toggle == 1, turn off 13 and turn on 9 % Flip toggle to zero so next time through % we go to the “else” part. Pause 0.3 sec % If toggle ~= 1, do this part of the if % Switch outputs to make 13 on and 9 off % Flip the toggle back % Ends the if statement % Ends the while loop. Go back and check % input 2 again. If it is +5, stop the % program, otherwise go through again. Page 8 of 9 Mechatronics Fall 2018 2. Now modify your code so that the switch is what causes the LEDs to toggle. Since you have only one switch, you will not be able to use it to also start and stop the program. Instead you will put the program into an “infinite loop” by starting with “while 1” (more on this later). In the meantime, modify the program so that it still watches DigitalIn(a, 2) but when the switch is pressed, 9 is on and 13 is off, and when the switch is not pressed, 9 is off and 13 is on. A way to get a program to loop forever is to start with the statement “while 1”. This is equivalent to saying “while 1 == 1” and it means just do this loop forever. You will have to use “Ctrl+C” to stop the program. Show a TA your result. 3. Write a new piece of code to change the LED lighting pattern: a. After the code is running and before the switch is pressed, both LEDs on pins 9 and 13 are on; b. After the switch is pressed the first time, the LED on pin 9 stays on and the LED on pin 13 turns off; c. After the switch is pressed the second time, the LED on pin 9 turns off and the LED on pin 13 turns on; d. After the switch is pressed the third time, the two LEDs toggle every 0.5 sec.; e. After the switch is pressed the fourth time, the code ends. Show a TA your result. Please disassemble all circuits and put all components neatly into your lab toolbox. DELIVERABLE Submit a lab report to eCampus on all the experiments you made. Attach your MATLAB code as Appendix in the report. Your code should have a proper header with your name and the program name and a short description of what it does for each program. The code must also be properly formatted and commented. Page 9 of 9 % ********************************************************************** % % % %********************************************************************** * k=0 while 1 if DigitalIn(a,2)==1 k=k+1; pause(0.5) end if k==0 DigitalOut(a,13,1); DigitalOut(a,9,1); elseif k==1 DigitalOut(a,13,0) DigitalOut(a,9,1) elseif k==2 DigitalOut(a,13,1) DigitalOut(a,9,0) elseif k==3 DigitalOut(a,13 ...
Purchase answer to see full attachment

Tutor Answer

tutorangela101
School: Boston College

Hello! I've add...

flag Report DMCA
Review

Anonymous
awesome work thanks

Similar Questions
Hot Questions
Related Tags
Study Guides

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors