0% found this document useful (0 votes)
4 views

Lab 08 - Smart Light Lab - Procedure

The document provides a lab manual for ENGR 1181, focusing on the use of the Arduino Mega2560 microcontroller and MATLAB support package. It outlines the objectives, equipment, and procedures for setting up a smart-light system that interfaces with various components such as LEDs and photoresistors. Additionally, it includes instructions for programming the Arduino using MATLAB to create a responsive lighting system based on ambient light conditions.

Uploaded by

Tyler Moss
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lab 08 - Smart Light Lab - Procedure

The document provides a lab manual for ENGR 1181, focusing on the use of the Arduino Mega2560 microcontroller and MATLAB support package. It outlines the objectives, equipment, and procedures for setting up a smart-light system that interfaces with various components such as LEDs and photoresistors. Additionally, it includes instructions for programming the Arduino using MATLAB to create a responsive lighting system based on ambient light conditions.

Uploaded by

Tyler Moss
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

ENGR 1181 | Smart Light Lab

- Part 1 – Arduino & MATLAB support package


- Part 2 - Lab Procedure
ENGR 1181: S m a rt Li g h t
Preparation Material

Part
ORS 1 – Arduino & MATLAB support package

INSTRUCTOR
The MANUAL
Arduino Mega2560
A microcontroller is a small computer contained in compact integrated circuit, which can be
used to control embedded systems in products and devices such as vehicles, home appliances,
smart phones, medical devices, power tools, and more. Microcontrollers are often embedded
in Printed Circuit Boards (PCBs) in order to make it easier for engineers to use or to add
functionality to the controller. There are a wide range of available microcontroller boards
depending on the specifics needs of the user. Arduino is a common brand of microcontroller
boards designed to be easy to use for engineers of all experience levels while also providing
moderately high functionality. This lab manual will introduce you to the Arduino Mega2560,
shown below in Figure 1.

Figure 1: The Arduino Mega2560

The Arduino Mega2560 can be used to operate and interface with a variety of components
including LEDs, digital and analog inputs such as buttons and sensors, outputs such as motors
and buzzers, and more. A complete pinout diagram can be seen in Figure 2 on the following
page, and additional information can be found here.

2
ENGR 1181: S m a rt Li g h t
Preparation Material

ORS

INSTRUCTOR MANUAL

Figure 2: Arduino Mega2560 Pinout Diagram

MATLAB Support Package


A major benefit of the Arduino Mega2560 is that it can be programmed using MATLAB by
installing the MATLAB Support Package for Arduino Hardware. This package should already be
installed on all ENGR 1181 classroom computers. If this lab is being performed on a computer
without it, however, the package can also be installed on any device with MATLAB using the
following steps:
1. Open MATLAB and navigate to the “Home” tab
2. Find the “Add-Ons” icon, click on the down-pointing arrow, and then click on the “Get
Hardware Support Packages” option as shown in Figure 3 below. When you click on this,
a pop-up should appear showing all the available support packages for MATLAB.

Figure 3: Selecting Get Hardware Support Packages in MATLAB

3
ENGR 1181: S m a rt Li g h t
Preparation Material

3.
ORS Navigate to the MATLAB Support Package for Arduino Hardware option shown in Figure
4 and click on it.
INSTRUCTOR MANUAL

Figure 4: Navigating to the MATLAB Support Package for Arduino Hardware

4. Then select the “Install” option shown in Figure 5 (don’t select “Download only”) and
follow the on-screen instructions. After installation, you will see a pop-up asking if you
want to set up your hardware now or set up later. Choose to set up later.

Figure 5: Installing the MATLAB Support Package for Arduino Hardware

4
ENGR 1181: S m a rt Li g h t
Lab Procedure

Part
ORS 2 – Lab Procedure

Smart Light Lab


INSTRUCTOR MANUAL
INSTRUCTOR MANUAL

Table of Contents
The Arduino Mega2560 ...................................................................................................... 2
MATLAB Support Package ................................................................................................ 3
Objectives ........................................................................................................................... 6
Lab Equipment (as arranged below in Figure 1) ............................................................... 6
Background ......................................................................................................................... 7
Digital and Analog I/O ................................................................................................................. 7
Breadboard .................................................................................................................................. 7
Resistor ........................................................................................................................................ 8
LED ............................................................................................................................................... 8
Photoresistor ............................................................................................................................... 9
Lab Activity.......................................................................................................................... 9
Circuit Setup ................................................................................................................................ 9
MATLAB Setup ........................................................................................................................... 10
Required Arduino Functions ..................................................................................................... 11
Programming Exercise ............................................................................................................... 12
Troubleshooting ................................................................................................................ 13
Deliverables ...................................................................................................................... 13

5
ENGR 1181: S m a rt Li g h t
Lab Procedure

Objectives
ORS
1. Become familiar with the Arduino Mega2560 and hardware components.
2. Setup the
INSTRUCTOR Arduino software in MATLAB.
MANUAL
3. Program the basic function calls to read and write to digital and analog inputs/outputs.
INSTRUCTOR MANUAL
4. Upload and run programs on the Arduino Mega2560.
5. Become familiar with troubleshooting techniques.
6. Understand how electronics can be used to sense and react to real world phenomena.

Lab Equipment
(as arranged below in Figure 6)

1. Jumper Wires (across top)


2. Arduino Mega2560 (far left)
3. USB Cable
4. 330ꭥ resistor
5. 10 kꭥ resistor
6. LED (light emitting diode)
7. Photoresistor (LDR)
8. Breadboard (far right)

Figure 6: Lab equipment

6
ENGR 1181: S m a rt Li g h t
Lab Procedure

Background
ORS
Digital and Analog I/O
INSTRUCTOR
The MANUALcan be used to receive real world input from a variety of sensors
Arduino Mega2650
(buttons, light sensors,
INSTRUCTOR MANUALpressure sensors, etc.) and to control the state of various outputs (LEDs,
motors, buzzers, graphical displays etc.):

• Digital I/O uses only binary values (0 or 1 / True or False / ON or OFF). For example,
reading the state of a simple pushbutton (input) or switching an LED on/off (output).
• Analog I/O uses a spectrum of values (0-5V for Arduinos in MATLAB). For example,
reading the state of a pressure sensor (input) or varying the brightness of an LED (output).
There are a wide variety of digital and analog components available that are compatible with
the Arduino.

Breadboard
Breadboards (shown in 7a) are used to quickly build and test prototype electronic circuits
without having to solder permanent connections. Circuit components are inserted into the
small holes on the board to make electrical connections to other components. The breadboard
has metal strips running underneath it which connects different holes. Figure 7b shows which
groups of holes are connected by these metal strips, allowing circuit connections to be made
easily. Holes within each colored outline are connected to each other, but not to any other
holes on the breadboard. The holes in the vertical columns designated with +/- signs are
connected. The horizontal rows designated with letters A-E are similarly connected, as are holes
in rows F-J. Note that the holes in the same row (A-E and F-J) are separated by the middle
divider and are NOT connected.

(a) (b)
Figure 7: Breadboard (a) and its connections (b)

7
ENGR 1181: S m a rt Li g h t
Lab Procedure

Resistor
ORS
Resistors resist the flow of current. Resistance is measured in Ohms, and a higher value of
Ohms corresponds
INSTRUCTOR MANUAL to more resistance. Resistor values can be identified by their colored stripes
as shown in Figure 8. Resistors are necessary for almost all circuits to control the flow of current
INSTRUCTORand
appropriately MANUAL
safely. Even when you don’t use an external resistor that you plug into a
breadboard yourself, there is usually at least one resistor on-board a device you are using.

Figure 8: Resistor color band key.

LED
The LED (Light-Emitting Diode) is a semiconductor light source, which emits light when current
flows through it. The LED only allows current to flow in one direction, and it has a positive lead
and a negative lead, as shown in Figure 9. The positive lead is longer than the negative lead,
and there is also a flat edge on the negative side of the LED. A resistor must be wired in series
with the LED to limit the amount of current flowing into the positive end of the LED, or the light
will burn out. A typical resistor value of 220 Ohms is used. Higher resistances will make the LED
dimmer.

Figure 9: 5mm red LED

8
ENGR 1181: S m a rt Li g h t
Lab Procedure

Photoresistor
ORS
The photoresistor is a light dependent resistor (LDR) which changes in resistance value
depending on the
INSTRUCTOR amount of light falling on it (~500 ohms in bright light, ~50k ohms in dark).
MANUAL
INSTRUCTOR
This MANUALcan be measured using a voltage divider circuit and reading the voltage
variable resistance
value across the photoresistor through an analog input pin on the Arduino. A voltage divider
splits the supplied voltage across two resistors in proportion to the resistance of the resistors. If
the photoresistor has a lower resistance than the fixed resistor, then less voltage will drop
across the photoresistor and more voltage will drop across the fixed resistor, and vice versa.
The Arduino analog input pins allow the variable voltage across the fixed resistor to be read,
which tells you the intensity of the light measured by the photoresistor. Shown in Figure 10
below, is the photoresistor and a wiring schematic to connect this sensor to the Arduino.
GND

10 kꭥ 5V

Figure 10: Image and wiring diagram of photoresistor

Lab Activity
Circuit Setup
Based on the diagram for the smart-light system shown in Figure 11 below, create this circuit
using the provided circuit components, breadboard, and Arduino Mega2560.
1. Insert the 330ꭥ resistor into the breadboard, with each of its leads into a different,
horizontal row.
2. Add the LED to the breadboard. As shown in Figure 11 below, connect the anode to the
330ꭥ resistor. Accomplish this by inserting the anode side lead of the LED into the same
horizontal row as one of the two leads from the resistor. Insert the cathode side lead of
the LED into the breadboard’s negative (-) column.
3. Connect the LED circuit to the Arduino board. Run a jumper wire from the unconnected
side of the resistor to D13 – the port on the Digital IO connector (see Figure 1) labeled
“13”. Run a second jumper wire from the breadboard’s negative column to GND – either
the one on the Digital IO connector, or one of the ports on the Power Pins connector (see
Figure 1) labeled “GND”.

9
ENGR 1181: S m a rt Li g h t
Lab Procedure

4. Add the LDR to the breadboard. One lead should connect to GND – insert it into an open
ORS
port on same negative column used above. The other can go into an empty horizontal
INSTRUCTOR
row. MANUAL
5. Add the 10k resistor to the photoresistor circuit. One lead should connect to the
INSTRUCTOR MANUAL
photoresistor; the other can go into any unused row or column. Note: neither lead should
be connected to GND.
6. Connect the photoresistor circuit to the Arduino board. The GND connection is the same
as was used above, so no new connection is needed for this. Run a jumper wire from the
row that connects the 10k resistor to the LRD to port A0 – so labeled on the Analog Input
Pins connector (see Figure 1). Run the last jumper wire from the far side of the 10k resistor
to 5V – the port labeled “5V” on the Power Pins connector (see Figure 1).
7. Double check that all connections match those shown in Figure 11, double checking the
polarity of the LED.

Figure 11: Circuit schematic of the smart-light sensor system and LED polarity

MATLAB Setup
1. Navigate to the Command Window in MATLAB. Then enter the command “arduinosetup”
and press enter. Running this command will generate a new window shown in Figure 12
below, which will walk you through the hardware setup.
2. The first page will ask how you want to connect your board to the host computer. Ensure
that the Arduino is connected to the computer via USB. Then, select USB and click
“Next”.
3. On the next page, the Arduino board (Mega, Uno, Nano, Due, etc.), the serial port which
your board is connected to, and the specific libraries which you want downloaded on your
board must be selected (see Figure 12 below).
10
ENGR 1181: S m a rt Li g h t
Lab Procedure

a. Set the Board to Arduino Mega2560.


ORS
b. Set the Serial port to the highest COM port available in the list.
INSTRUCTOR MANUAL
i. If there are none available, check that your USB cable is connected to both
your Arduino board and your computer.
INSTRUCTOR MANUAL
c. Click the check box to “Select All” libraries, then uncheck the box for MotorCarrier.
NOTE: Not all libraries will be available on all Arduino boards. If you receive an
error in the next step, uncheck the box for the listed unavailable library.

Figure 12: Selecting the board, port, and libraries

4. Once you have selected the above options, click “Program”, and when complete, click
Next.
a. If this results in a red error message appearing below the “Program” button, swap
the USB cable to a different USB slot on your computer tower and try again.
5. On the next page, click “Test Connection”. This ensures that your board is properly
connected to your host computer. Click Next when the test is complete. If the test fails,
see the troubleshooting section at the end of this lesson.
6. The Arduino set up is now complete. Deselect the “Show Examples” option in this final
window and click “Finish” to exit the window.
Required Arduino Functions
Below is a list of useful Arduino specific functions available in MATLAB for programming, which
you will need to complete this exercise. NOTE: All existing MATLAB syntax and functions can
also be used when programming Arduinos.

11
ENGR 1181: S m a rt Li g h t
Lab Procedure
arduino(port,board)
Creates an object to establish the connection of the board to the specified port.
ORS
Valid ports: ‘COM1’, ‘COM2’, ‘COM3’, … ‘COMX’
INSTRUCTOR MANUAL
Valid boards: ‘Nano’, ‘Uno’, ‘Duo’, ‘Mega2560’
INSTRUCTOR MANUAL
readDigitalPin(a,pin)
Reads data from the specified pin on the Arduino hardware in connection a. Digital pins should be
entered as a string, example: readDigitalPin(a,’D2’)

readVoltage(a,pin)
Reads the voltage on the specified analog pin on the Arduino hardware in connection a. Analog pins
should be entered as a string, example: readVoltage(a,’A1’)

writeDigitalPin(a,pin,value)
Writes the specified value to the specified digital pin on the Arduino hardware in connection a.

Programming Exercise
In this section, you will write and run a MATLAB script file that will interface with the Arduino
and associated circuit to create a smart-lighting system that will turn the LED on automatically
when the ambient light in the room diminishes.
1. Create a new script file called SmartLight.m, in which you will write code to turn on and
turn off the LED based on input from the photoresistor.
2. At the top of your script file, make six lines of comments which you will later fill with a
description of your logic and the variables used in your script. You do not need to write
the comments now, just reserve space for them.
3. After your documentation comments space, begin your script with basic functions such
as clc, clear, and close.
4. Create and initialize the variables you will use in your script by doing the following, either
running your script or using the Evaluation Selection option after each bullet point:
a. Create a variable called a and assign to it an arduino object using the arduino
function. Using this function will establish the connection to your Arduino. The
port will be the same port name as the one you used in your hardware setup (e.g.
‘COM4’), and the board will be ‘Mega2560’.
b. Create to a new variable called LDR and assign to it the name of the analog pin
connected to the photoresistor. NOTE: LDR will be a character array (i.e. ‘A0’).
c. Making sure that the LDR is unobstructed, call the function readVoltage in the
Command Window, using the variables created in 4a and 4b as inputs. Hard code
the output in a new variable called LDR_amb.
d. Covering the photoresistor with your hand, repeat step 4c, hard coding the output
in a new variable called LDR_covered.

12
ENGR 1181: S m a rt Li g h t
Lab Procedure

e. In your script, use fprintf to print your observations to the command window in
ORS
the format shown below:
INSTRUCTOR MANUAL The voltage output of the photoresistor in ambient light and
when covered is X.XX volts and Y.YY volts, respectively.
INSTRUCTOR MANUAL
f. Based on these observations, create a variable called Threshold to hold the analog
voltage reading at which your LED should turn on or off.
g. Create to a new variable called LED and assign to it the name of the digital pin
connected to the LED. NOTE: LED will be a character array (i.e. ‘D13’).
5. End your script with a loop of code that will program the Arduino to turn on the LED if
there is little or no light hitting the photoresistor and should have the following features:
a. Run forever (infinite loop) until the user uses ctrl + c to break out of the loop.
Accomplish this by beginning this part with “while 1” and ending with “end”
at the very end of the script.
b. A conditional statement to determine whether the LED should be turned on
or not based on the threshold voltage value and the current voltage value.
c. Use the pause function to ensure that there is a short pause of 0.1 seconds
between each run of the infinite loop. NOTE: Using the pause function without
giving it an input will pause the script until the user presses a key.
6. Click ‘Run’ in MATLAB to run your script file and demonstrate that your code works as
expected to a member of the instructional team.

Troubleshooting
• Error connecting to Arduino:
o Verify USB cable plugged into computer tower (NOT monitor)
o Restart arduinosetup and verify correct board and port
• Error compiling/running MATLAB code:
o May have a syntax error
o Use clear command in MATLAB and re-establish connection to arduino
• I/O components not responding as expected?
o Usually due to wiring issue, verify the circuit is correct
o May also have a broken component, consult TA if you believe this is the case

Deliverables
With your paired lab partner, submit a picture of your final circuit and a pdf of your script file.
Your script should be fully documented by including the following in the commented space at
the top of your file:
A full description of the flow of your logic in paragraph form – what happens when and why.

A list of variables used with descriptions of what they are used for – names, data types, uses.

13

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy