Lab 1 Introduction to Circuits and MATLAB - Penn...

17
Created by Nick Howarth (EE ’13) and Sam Wolfson (EE ’13) Last updated: September 1, 2014 by Suraj Shah (SSE ’17) University of Pennsylvania Department of Electrical and Systems Engineering ESE 111 Intro to ESE Lab 1 Introduction to Circuits and MATLAB Introduction : In this lab, you will learn how to build very basic circuits, use some of the lab equipment, and use powerful software to plot and analyze data measured from your circuits. The following concepts, components, and tools will be used throughout this lab. Ohm’s Law: As you may recall, the most basic relationship between voltage, current, and resistance is given by Ohm’s Law: V = IR, where V is voltage (in volts), I is current (in amperes, or amps), and R is resistance (in ohms). You are going to use this simple relationship to observe the difference between the ideal resistance and actual resistance of the resistors that you find in the lab. Resistors: Even though resistors are one of the most basic electrical components, they are the most ubiquitous. Nearly every circuit has at least one resistor, either as a discrete component or as a part of another component. A resistors value is labeled on it in the form of the colored bands around its body. For an explanation of how to decode a resistors value, go to http://en.wikipedia.org/wiki/Electronic_color_code . Light-emitting Diodes (LEDs): A diode is a special circuit component that only lets current pass through in one direction. As you may have guessed from the name, an LED is a diode that emits light when current flows through it. Each kind of LED takes a specific voltage to light up, usually between 0.5V and 2V. Digital Multimeter (DMM): A DMM is an instrument that measures various characteristics of an electrical circuit or component. You will be using a DMM to measure resistance, voltage, and current. Power Supply: Power supplies provide electrical power at either a set current or a set voltage level. We will be using it as a voltage supply, providing a set voltage to our circuits. MATLAB: MATLAB is an incredibly powerful piece of software used for everything from performing basic mathematics to analyzing and plotting large datasets to controlling complicated experimental setups. As such, it is a fundamental part of the modern engineers toolbox.

Transcript of Lab 1 Introduction to Circuits and MATLAB - Penn...

Page 1: Lab 1 Introduction to Circuits and MATLAB - Penn …ese111/Fall2014labs/Lab1_IntrotoCircuitsand… · Lab 1 – Introduction to Circuits and MATLAB ... ... Power supplies provide

Created by Nick Howarth (EE ’13) and Sam Wolfson (EE ’13)

Last updated: September 1, 2014 by Suraj Shah (SSE ’17)

University of Pennsylvania Department of Electrical and Systems Engineering

ESE 111 – Intro to ESE

Lab 1 – Introduction to Circuits and MATLAB Introduction:

In this lab, you will learn how to build very basic circuits, use some of the lab equipment, and

use powerful software to plot and analyze data measured from your circuits. The following

concepts, components, and tools will be used throughout this lab.

Ohm’s Law: As you may recall, the most basic relationship between voltage, current, and

resistance is given by Ohm’s Law:

V = IR,

where V is voltage (in volts), I is current (in amperes, or amps), and R is resistance (in ohms).

You are going to use this simple relationship to observe the difference between the ideal

resistance and actual resistance of the resistors that you find in the lab.

Resistors: Even though resistors are one of the most basic electrical components, they are the

most ubiquitous. Nearly every circuit has at least one resistor, either as a discrete component or

as a part of another component. A resistor’s value is labeled on it in the form of the colored

bands around its body. For an explanation of how to decode a resistor’s value, go to

http://en.wikipedia.org/wiki/Electronic_color_code.

Light-emitting Diodes (LEDs): A diode is a special circuit component that only lets current

pass through in one direction. As you may have guessed from the name, an LED is a diode that

emits light when current flows through it. Each kind of LED takes a specific voltage to light up,

usually between 0.5V and 2V.

Digital Multimeter (DMM): A DMM is an instrument that measures various characteristics of

an electrical circuit or component. You will be using a DMM to measure resistance, voltage, and

current.

Power Supply: Power supplies provide electrical power at either a set current or a set voltage

level. We will be using it as a voltage supply, providing a set voltage to our circuits.

MATLAB: MATLAB is an incredibly powerful piece of software used for everything from

performing basic mathematics to analyzing and plotting large datasets to controlling complicated

experimental setups. As such, it is a fundamental part of the modern engineer’s toolbox.

Page 2: Lab 1 Introduction to Circuits and MATLAB - Penn …ese111/Fall2014labs/Lab1_IntrotoCircuitsand… · Lab 1 – Introduction to Circuits and MATLAB ... ... Power supplies provide

Goals of this Lab:

- Learn how to prototype simple circuits on a breadboard

- Learn how to use the power supply and digital multimeter (DMM)

- Understand Ohm’s Law and how to apply it

- Learn how to use MATLAB to plot and analyze data

Materials Needed for this Lab:

- A light-emitting diode (LED)

- A 1-kΩ resistor (Brown-Black-Red-Gold bands)

- Digital Multimeter and Triple Output DC Power Supply (located at each lab station)

- Breadboard

- Two pairs of Banana Plug/Mini Grabber cables (i.e. two red/two black)

- Four pieces of wire, two black, two red, each stripped at both ends

- One photoresistor

- Another resistor of your choosing

Page 3: Lab 1 Introduction to Circuits and MATLAB - Penn …ese111/Fall2014labs/Lab1_IntrotoCircuitsand… · Lab 1 – Introduction to Circuits and MATLAB ... ... Power supplies provide

Procedure:

1. Build a simple LED circuit

- Take out the light-emitting diode (LED) and 1-k resistor (Brn-Blk-Red-Gld).

- Measure the resistance of the resistor using the Digital Multimeter (DMM). Take the

two pairs of the banana/mini grabber cables and connect them to the resistor and

multimeter as shown in Figure 1. Turn on the HP 34401A DMM at your station. To

measure resistance, press the button with the omega symbol (third button from the left

in the top row).

Figure 1: Measuring resistance with the HP 34401A DMM

You will now build a very basic circuit on a breadboard, also called a protoboard. The

breadboard that we are using, shown in Figure 2, features rows of holes (1 – 63) arranged in sets

of five (a – e, f – j) on each side of the partitions. Each of the five holes in a row is electrically

connected by a conducting plate running underneath. Therefore, if you were to connect a wire

from 5V to a hole, anything connected to the other four holes in that row would be at a potential

of 5V. The set of holes to the right of a red line are connected within that column. Likewise the

set of holes to the left of a blue line are connected within that column.

Page 4: Lab 1 Introduction to Circuits and MATLAB - Penn …ese111/Fall2014labs/Lab1_IntrotoCircuitsand… · Lab 1 – Introduction to Circuits and MATLAB ... ... Power supplies provide

Figure 2: Breadboard

The circuit that you will build will light up an LED. Current will pass from a 5-V source through

a 1-k resistor and the LED to ground, or 0V. Two equivalent schematic representations of this

circuit are shown in Figure 3. Figure 4 shows this circuit built on a breadboard, where the red

wire goes to 5V and the black wire goes to ground. (By convention, red signifies connection to

the positive power source and black signifies connection to ground.)

Figure 3: Equivalent schematic representations of the simple LED circuit

Page 5: Lab 1 Introduction to Circuits and MATLAB - Penn …ese111/Fall2014labs/Lab1_IntrotoCircuitsand… · Lab 1 – Introduction to Circuits and MATLAB ... ... Power supplies provide

Figure 4: Simple LED circuit represented by Figure 3

- Build the circuit shown in Figure 3 using your LED, 1-kΩ resistor, Banana Clip/Mini Grabber cables, and two pieces of wire. Consult Figure 4 for clarification.

- The longer leg of the LED is the positive end (Figure 5), so make sure that end is

connected to the voltage source.

Figure 5: LED Schematic (Source: http://www.societyofrobots.com/electronics_led_tutorial.shtm)

- You should always put a resistor in series with an LED in order to limit the current

flowing through the LED (and thereby prevent it from burning out).

- Always make sure no bare wires are touching!- Connect the banana plugs at the ends of the wire clips (from Figure 4) to the HP E3631A

Triple Output DC Power Supply (located under the HP DMM you just used) as shown in

Figure 6.

Page 6: Lab 1 Introduction to Circuits and MATLAB - Penn …ese111/Fall2014labs/Lab1_IntrotoCircuitsand… · Lab 1 – Introduction to Circuits and MATLAB ... ... Power supplies provide

Figure 6: HP E3631A Triple Output DC Power Supply

- Turn on the power supply by pressing the Power button.

- To set the voltage across the terminals on the left with the “6V” label above them, press

the “Display Limit” button. Then press the “+6V” button and use the knob and arrow

buttons on the right to set the voltage to 5.000V. - Generally, it is also good practice to set a current limit to minimize the risk of

overpowering your circuit and blowing something up (which means you’ve done something wrong). To set the current limit, press the “Voltage | Current” button on the

right, and then use the knob and arrow keys to set the current to 0.500A.

- Once your voltage and current limit are set, press the “Output On/Off” button to turn the

output on. Your LED should now be lit!

Now that you have a completed circuit, you can use the DMM to measure the voltage drop across each of the components and the amount of current running through the circuit.

- Press the button labeled “DC V” (for Direct Current Voltage) on the DMM, as shown in

Figure 7.

- To measure the voltage drop over a component, the DMM leads must be placed in

parallel with that component (see Figure 8). Take the second pair of Banana Clip/Mini

Grabber cables and measure the voltage drop over the resistor by clipping the leads to

the legs of the resistor, as shown in Figure 9. Record the value shown on the DMM.

- Now measure the voltage drop over the LED and record the value. Do these two

measurements add up to 5V?

Page 7: Lab 1 Introduction to Circuits and MATLAB - Penn …ese111/Fall2014labs/Lab1_IntrotoCircuitsand… · Lab 1 – Introduction to Circuits and MATLAB ... ... Power supplies provide

Figure 7: Measuring DC voltage using the HP 34401A DMM

Figure 8: Schematic showing voltage drop measurement over resistor

Figure 9: Measurement of voltage drop over resistor

Page 8: Lab 1 Introduction to Circuits and MATLAB - Penn …ese111/Fall2014labs/Lab1_IntrotoCircuitsand… · Lab 1 – Introduction to Circuits and MATLAB ... ... Power supplies provide

- To measure current, move the red banana plug from the top-right hole to the bottom-right

hole, as shown in Figure 10. Then, press the blue button labeled “Shift,” followed by the

“DC V” button to select “DC I” (‘I’ represents current).

- The DMM must be placed in series with the component(s) through which you want to

measure current (see Figure 11). Connect the DMM leads to two new jumper wires and

connect them in series with the resistor and LED, as shown in Figure 12. (If you want,

you can avoid using extra wires by disconnecting the right lead of the resistor and the left

lead of the LED from the breadboard and clipping the DMM leads to these leads to

complete the circuit, ensuring that the current passes through the DMM.)

- Based on your measured voltage drop over the 1-k resistor, does your

measured current make sense? Use Ohm’s Law (V = IR) to verify your findings.

Figure 10: Measuring current using the HP34401A DMM

Figure 11: Schematic showing measurement of current through the simple LED circuit

Page 9: Lab 1 Introduction to Circuits and MATLAB - Penn …ese111/Fall2014labs/Lab1_IntrotoCircuitsand… · Lab 1 – Introduction to Circuits and MATLAB ... ... Power supplies provide

Figure 12: Current measurement through simple LED circuit

2. Adjust the brightness of the LED with a photoresistor

A photoresistor, sometimes called a photocell, is a form of variable resistor whose resistance decreases with exposure to light. In effect, a photoresistor is a light sensor.

Figure 13: Photoresistor

- Obtain a photoresistor, like the one shown in Figure 13. Using the DMM, measure the

photocell’s resistance as you vary its exposure to light, by placing and removing your

hand from the photoresistor, placing the photoresistor near a light source, etc. Remember

to use the DMM setup from Figure 1 to measure resistance!

- Approximate the maximum and minimum resistances the photoresistor can achieve.

- Press the “Output On/Off” button to turn the output off. You should always make sure

that the output of the power supply is OFF before modifying a circuit.

- Replace the 1-k resistor in your circuit from Figure 3/4 with the photoresistor.

- Press the “Output On/Off” button to turn on the supply power to the circuit. - Use your hand to adjust the amount of light exposed to the photoresistor, and observe the

effect that it has on the LED.

Page 10: Lab 1 Introduction to Circuits and MATLAB - Penn …ese111/Fall2014labs/Lab1_IntrotoCircuitsand… · Lab 1 – Introduction to Circuits and MATLAB ... ... Power supplies provide

3. Use MATLAB to plot voltage vs. current characteristics of a resistor

MATLAB is an incredibly powerful piece of software used for everything from performing basic

mathematics to analyzing and plotting large datasets to controlling complicated experimental

setups. As such, it is a fundamental component of the modern engineer’s toolbox. In this

section, you will measure the current through a resistor at different voltages and plot this data

using MATLAB. You will then compare your experimental data to the ideal resistor model.

Recording voltage and current measurements from circuit

- Choose a random resistor from the component bins. Measure and record its actual value.

- Build the circuit represented in Figure 17. Remember that the DMM must be connected

in series with the resistor to measure current, and the red banana plug must be plugged

into the bottom-right hole on the DMM. Consult Figure 10 for clarification.

Figure 17: Current measurement circuit schematic

- Vary the voltage from the power supply by one-volt increments, recording the current

through the resistor at each increment. Start at 0V and stop at 6V. Record the data in a

table like Table 1.

Table 1: Supply Voltage vs. Measured Current through a - Resistor

Voltage (V) Current (mA)

0

1

2

3

4

5

6

Page 11: Lab 1 Introduction to Circuits and MATLAB - Penn …ese111/Fall2014labs/Lab1_IntrotoCircuitsand… · Lab 1 – Introduction to Circuits and MATLAB ... ... Power supplies provide

Prelab Exercises:

1. Calculate the resistance (in ohms) of a resistor that is part of a circuit with a voltage of 5V

and a current of 940 mA.

2. Given the following resistor, decode its resistance value using the Electronic Color Code

link given on the previous page:

3. (Optional):

Go to http://www.mathworks.com/academia/student_center/tutorials/launchpad.html and

browse through some MATLAB tutorials. MATLAB is an important tool that you will

undoubtedly use throughout your Engineering career. Getting familiar with the basics of it

wouldn’t be a bad idea.

Page 12: Lab 1 Introduction to Circuits and MATLAB - Penn …ese111/Fall2014labs/Lab1_IntrotoCircuitsand… · Lab 1 – Introduction to Circuits and MATLAB ... ... Power supplies provide

Opening and using MATLAB

Like any good engineer in possession of experimental data, you must now be itching to plot it

and figure out what it means. It’s time to use MATLAB to scratch that itch.

- To open MATLAB: Start menu All Programs Mathematics MATLAB R2014a The MATLAB window by default contains several sections. For now, you will be working in

the section labeled “Command Window” (the big window in the center of the screen). In this

window, you can enter commands one line at a time.

MATLAB stores data in vectors, represented as a list of numbers enclosed by brackets. For

example, [1 2 3] is a vector with components 1, 2, and 3. There are two basic types of vectors:

row vectors and column vectors.

- Type [1 2 3] into the command window in MATLAB and hit enter. This is an example

of a row vector.

- Now type in [1;2;3] and hit enter. This is an example of a column vector. Another way

to enter a column vector into MATLAB is to add an apostrophe (‘) to the end of a row

vector.

Figure 18 shows the Command Window output after these row and column initializations were

entered. As you can see, the components in a row vector are arranged horizontally, and the

components in a column vector are arranged vertically.

Figure 18: Output of MATLAB Command Window showing vector initialization

Page 13: Lab 1 Introduction to Circuits and MATLAB - Penn …ese111/Fall2014labs/Lab1_IntrotoCircuitsand… · Lab 1 – Introduction to Circuits and MATLAB ... ... Power supplies provide

Creating row vectors to store measured data

- Create a vector named “V” to store your voltage data by entering “V = [0 .5 1 1.5 2...]”,

leaving spaces between each of the entries in the vector.

- Create a vector named “I_exp” to store your experimental current data in the same way

that you created the “V” vector.

Figure 19: Example of vector initialization to store experimental data

Plotting experimental data

- Enter “plot(V,I_exp)”. This will make a line graph of the points with the x-values given

by the “V” vector and the y-values given by the “I_exp” vector.

- Enter “title(‘Voltage vs Current through a -kOhm resistor’)”. This will add a title to

your plot. You should include the value of the resistor you used in the title. Don’t forget

the single quotes around your title, as these let MATLAB know that you are entering a

string of letters and not a variable.

- Enter “xlabel(‘Voltage (V)’)” and “ylabel(‘Current (A)’)”. This will create labels for your

x- and y-axes, allowing other people to interpret your graph. Figure 20 shows the commands used to create a plot of the voltage vs. current data. Figure 21

shows the plot created using data from an ideal 2-ohm resistor model.

Figure 20: Commands to make plot of data

Page 14: Lab 1 Introduction to Circuits and MATLAB - Penn …ese111/Fall2014labs/Lab1_IntrotoCircuitsand… · Lab 1 – Introduction to Circuits and MATLAB ... ... Power supplies provide

Figure 21: Example plot of voltage vs. current for ideal 2-ohm resistor

Calculating resistance from data

Since (by Ohm’s Law), V = IR, finding R given V and I is fairly straightforward, as you did in

Prelab Exercise 1:

R =

You will now use MATLAB to calculate the resistance at each voltage level, and then find the

average value for resistance.

- Enter “R = V ./ I_exp”. This creates a vector called “R” of the resistance at each voltage

level. Don’t forget the period in front of the slash—this is important because it tells

MATLAB to divide the entries of the vectors (first entry of V/first entry of I_exp, second

entry of V/second entry of I_exp, etc) instead of just dividing the vectors.

- To find the average value of R, enter “mean(R)”. Note: if any of the entries of I_exp is

0, then you will have an entry in R that looks like “NaN”. This is MATLAB being

upset about dividing by 0—to find the mean, you are going to have to ignore this

entry.

- Compare the average experimental resistance to the resistance you measured using the

DMM, and to the nominal (labeled) value of the resistor. Which is larger? Which do you

think is most accurate?

Page 15: Lab 1 Introduction to Circuits and MATLAB - Penn …ese111/Fall2014labs/Lab1_IntrotoCircuitsand… · Lab 1 – Introduction to Circuits and MATLAB ... ... Power supplies provide

Now you are going to add lines to the plot to compare the voltage vs. current characteristics

using the labeled resistance and the measured resistance to the experimentally determined line.

Adding Additional Lines to a Plot

- Enter “hold all”. This tells MATLAB to add more plot lines to the same figure.

- Enter “plot(V, measured resistance*V)”. Make sure that you replace measured

resistance in the command with the actual value that you measured.

- Enter “plot(V, labeled resistance*V)”. Make sure that you replace labeled resistance

in the command with the actual value with which the resistor is labeled.

- Enter “legend(‘experimental’,’measured resistance’,’labeled resistance’)”. This will

create a legend on the plot so that you can tell which line is which.

The commands to add example data lines to the plot are shown in sequence in Figure 22. The

resulting plot should have three differently colored lines and a legend, as shown in the example

plot in Figure 23.

Figure 22: Commands to add lines to a plot

Figure 23: Example voltage vs. current plot with multiple data lines

Page 16: Lab 1 Introduction to Circuits and MATLAB - Penn …ese111/Fall2014labs/Lab1_IntrotoCircuitsand… · Lab 1 – Introduction to Circuits and MATLAB ... ... Power supplies provide

Calculating percent difference

- Calculate the percent difference between the labeled value and the measured value (from

the DMM), the labeled value and the experimentally determined (calculated) value, and

the measured value (DMM) and the experimentally determined value (calculated). The

formula to find percent difference between two values A and B is:

Percent Difference

In your postlab assignment, include a copy of your MATLAB code (similar to

Figures 19, 20, and 22) and the final labeled graph (Figure 23), but all with your

own data.

Page 17: Lab 1 Introduction to Circuits and MATLAB - Penn …ese111/Fall2014labs/Lab1_IntrotoCircuitsand… · Lab 1 – Introduction to Circuits and MATLAB ... ... Power supplies provide

Postlab Exercises:

Answer these either in a separate Word document or handwrite them and scan your work. Then

upload the answers to Canvas.

1. Attach a screenshot of your MATLAB code and a screenshot of your fully labeled figure

that graphs the experimental, labeled, and measured resistances.

2. Calculate the percent difference between the labeled value and measured value, between

the labeled and experimental value, and between the measured and experimental value as

indicated on the previous page. Show your calculations.

3. What are the consequences of a large discrepancy in experimental and labeled resistance

values in a real-world situation? (For example, what if the electrical engineer designing a

certain pacemaker errs in the value of a resistor within the pacemaker? What ramifications

could this have on the patient, doctor, clinic, medical device company, and industry?)

4. List three real-world applications of a photoresistor (light sensor).