Professional Documents
Culture Documents
Winter 2021
y(t) = C1 − C2 e−t/τ
C1
f(t)
C 1 -C2
For this lab, you will use a pulse-width-modulated (PWM) signal on the Arduino. A PWM signal is
a square wave with a fixed frequency. The duty cycle is the percentage of time that the signal is high.
The Arduino PWM period is approximately 2 ms. If the PWM signal is high (5V) for 0.5 ms and low
(0V) for 1.5 ms, this is a 25% duty cycle. For this lab, the duty cycle will be fixed at 80%, but in Lab 2
this will be a variable.
page 1 of 6
Control Systems Winter 2021
2 Lab Safety
This lab has potential safety concerns due to electric current and elevated temperatures. When the power
adapter is connected, make sure to not touch any wires or circuit components as there is a risk of electric
shock. Before touching circuit components, make sure to disconnect the power adapter. When the power
adapter is connected, be careful when handling the thermoelectric Peltier heater, the metal block, and
the temperature sensor as they may be hot. Do not leave any part of the circuit or temperature control
system running when you are not present in the room. If you must leave the room, you can unplug the
power adapter which eliminates most risks. Finally, be careful when handling parts with sharp edges
including the metal blocks and the pins on integrated circuits.
1. Build the circuit in Figure 2 with the motor driver, four 1N4007 diodes, Peltier cooler (indicated
with a P in a circle), and 5V power adapter. Note that the voltage at pins 2 and 7 will be controlled
by the PWM signals coming from the Arduino, but the current through the Peltier (pins 3 and 6)
is actually supplied by the 5V power adapter.
2. Connect Vcc1 to the 5V supply on the Arduino. Connect Vcc2 to the 5V supply from the 5V
power adapter using the barrel jack to bare wire adapter. The two sources must have their grounds
connected together.
3. Make sure to connect all pins as indicated. There are four pins that must be grounded.
4. Connect pin 1 to the 5V source coming from the Arduino. This is an enable input that must be
high to enable the motor driver.
5. With this circuit, the maximum current in the circuit will near 500 mA. It is important to never
touch the circuit components or wires when the power is connected.
6. Attach jumper cables to the TMP36 as extensions to the pins. The power supply for the TMP36
should be connected to 5V source coming from the Arduino.
https://learn.adafruit.com/tmp36-temperature-sensor
7. Use electrical tape to tape the sensor to the block and the block to the Peltier cooler.
page 2 of 6
Control Systems Winter 2021
Figure 2: The motor driver can be connected in this way to power the thermoelectric heater.
1. If you havent already, you need to add a package to MATLAB. On the “Home” menu click “Add-
ons”. You need to add the specific package “MATLAB Support Package for Arduino Hardware”.
https://www.mathworks.com/help/supportpkg/arduino/ug/install-support-for-arduino-
html
2. Connect your Arduino to your computer using the USB cable. Run the line a = arduino. MATLAB
should connect to the Arduino and display the device name and COM port.
4. We will use the function configurePin() to set a digital pin to be a PWM signal. You will need to
specify the variable for the Arduino device (a), the pin to be the PWM signal (e.g. ‘D8’ in single
quotes), and the third parameter is ‘PWM’ in single quotes.
5. You will use the function writePWMDutyCycle() to set the PWM cycle. You will need to specify
the Arduino device, the PWM pin, and the duty cycle between 0 and 1 (representing 0% to 100%).
Note that the PWM signal switches from 0V to 5V with a frequency of 490 Hz.
6. Test these functions to be sure that you can set a PWM signal and read temperature sensor values.
You should be able to heat the block by setting one PWM pin high (1) and the other low (0). You
can identify which pin heats the block when it is high (“heating pin”) and which pin cools the block
when it is high (“cooling pin”). If you wish to switch the two pins, you can swap their connections
to the motor driver at pins 2 and 7.
page 3 of 6
Control Systems Winter 2021
1. Review your work on the Arduino Tutorial using the TMP36 temperature sensor. Connect the
temperature sensor to your Arduino.
2. Initially, keep the power adapter disconnected so that you can measure the temperature of the metal
block at room temperature.
3. Write a MATLAB script that collects temperature data points from the Arduino for 10-20 seconds.
Run this script and average across all values collected.
4. At the same time, measure the current temperature in Fahrenheit using the thermometer you have
available. If the temperature changes during the time, average the min and max temperatures to
get an approximate average. This voltage and temperature pair is your first calibration point.
5. Connect the power adapter. Configure the two PWM pins so that the heater heats the metal block.
◦ ◦
6. Allow the metal block to heat to a temperature in the range of 80 F to 90 F. Record the voltage
and temperature. This is your second calibration point.
7. Using these two points, find a linear equation that maps voltage measurements to an accurate
temperature value. Only the line slope and vertical intercept are needed. You will use this line as
a conversion to temperature.
8. The calibration may not be accurate over time so I recommend repeating the calibration if you leave
the lab and come back.
Option 2
For this option, you will use a set of sensor voltage readings and corresponding temperature values to
create an equation for temperature calibration. While these values were collected using a TMP36 and a
trusted thermometer, they may or may not be accurate for your specific sensor. The voltage readings are
[0.707, 0.774, 0.841, 0.883, 0.946, 0.961], and the temperature values are [72, 79, 85, 90, 95, 99].
1. Type the voltage readings and temperature values into Matlab as two vectors.
2. Under “Apps” open the “Curve Fitting Tool”. Use the voltage readings as the x-data and the
temperature values as the y-data.
3. Set the fit to linear and find an equation that maps voltage measurements to an accurate temperature
value. Only the line slope and vertical intercept are needed. You will use this line as a conversion
from voltage readings to temperature.
page 4 of 6
Control Systems Winter 2021
1. Disconnect the power adapter and allow the system to cool at least 5 minutes before proceeding
with data collection.
2. Select two digital output pins to be used to send signals to the motor driver.
3. For the cooling pin, set the Arduino PWM to 0% duty cycle.
4. For the heating pin, set the Arduino PWM to 80% duty cycle.
6. In the loop, you need to record the time of the measurement. This is best done by assuming that
the initial measurement is time zero and each measurement after is some number of seconds since
the starting time. This can be done using the “tic” and “toc” keywords.
7. It is recommended to preallocate the variables for 100000 measurements. This is likely many more
points than needed. However many points you collect over 15 minutes will be sufficient to identify
the system.
8. Run your MATLAB script to collect a vector of sensor measurements and a vector of time values.
9. Use the “save” function to save the sensor readings and time vector as a .mat file.
10. Use your calibration equation to convert these voltage measurements to a temperature in degrees
Fahrenheit.
11. Plot the data over time and save the figure as a .fig file. Make sure to include this in your report.
12. Make sure to set both Arduino PWM signals to 0% duty cycle.
1. Under “Apps” open the “Curve Fitting Tool”. Use your time vector as the x-data and your tem-
perature data for the y-data. Make sure to remove the zeros at the end of each vector before fitting
this curve. Set the fit type to “Exponential” with 2 terms.
page 5 of 6
Control Systems Winter 2021
2. The result of the curve fitting should be two exponential terms with coefficients and exponential
decay rates. One of the decay rates should be much smaller and can be approximated as zero to
give a constant term. The other time constant will be larger and can be used for the exponential
term in the system response below.
y(t) = C1 − C2 e−t/τ
3. You may run into the issue where one of the decay rates isn’t much smaller than the other. In this
case, you will have to manually set the b parameter to zero by clicking “Fit Options”. For this row,
set the max and min values for b both to zero. Look at the remaining variables (a, c, and d) in the
equation and fit them to the equation above for y(t).
4. Finally, to calculate the system gain KG , you can take the maximum difference in temperature and
divide by the voltage supplied. In our case, we are supplying 5V at an 80% duty cycle. We can
approximate this as supplying 4V. With the gain, you can write the transfer function for this first
order system.
8 Lab Report
This lab is the first part of a two part lab. You do not need to write and turn in a lab report for this
lab. Instead, you should document your observations and save your data. You will write a section in your
next lab report that summarizes your work on this lab. You should especially focus on what you did for
sensor calibration and collecting data to fit the first order model. Make sure to save all data, code, and
figures that you generated in this lab. Your lab report will summarize what you did for Lab 1 and Lab 2.
page 6 of 6