# Imaging Lab 1: Building a Light Sensor¶

Name 1:

Name 2:

## Instructions¶

• Complete this lab by filling in all of the required sections, marked with "YOUR CODE HERE" or "YOUR COMMENTS HERE".
• When you finish notify your GSI to get get checked off for this lab. Be ready to answer a few questions to show your understanding of each section.
• Labs will be graded based on completion for teams of 2 students.

## Lab Policies¶

• YOU MUST ATTEND THE LAB SECTION YOU ARE ENROLLED IN. If you anticipate missing a section please notify your GSI in advance.

• You are required to return all parts checked out at the beginning of the lab section unless told otherwise.

• Food and drinks are not allowed in the lab.

• Clean up, turn off all equipment, and log off of computers before leaving.

## Introduction¶

Welcome to the first lab of EE16A! This is the first of a series of 3 labs in the imaging module in which you will actually build the imaging system introduced in Lecture 2. The physical components of this system include a projector, a solar cell, sensing circuit, and an Arduino microcontroller. The full imaging system is shown below:

### System Overview¶

The image below shows a brief overview of how our camera converts the photons of light bouncing off of an object and converts them into the numbers that represent images on a computer.

Digital images are composed of a grid of [pixels](http://en.wikipedia.org/wiki/Pixel), each of which represents a single color. In order to create this digital representation, optical imaging systems measure the energy, in this case light, reflected off of objects to determine the values of these pixels. Because different colors reflect different amounts of light, measurements of reflected light intensity can be used to differentiate between colors.

Commercial cameras such as those found in smartphones use chips called CMOS image sensors to measure millions of pixel values at a time (think of this as millions of devices like the solar cells that all fit on an approximately 30mm$^2$ chip). In contrast our system can only measure one light intensity value at a time, so we will instead use a controlled energy source, the projector, to illuminate specific regions of the image at a time and record the energy that reflects off the subject for one region at a time.

The solar cell converts light to induce a flow of electrons, and the circuit then outputs a voltage corresponding to light intensity. The specific configuration of the circuit sets the solar cell up so it is most sensitive to changes in light.

This changing voltage is now a set of real numbers and much closer to something we can use to represent our image on a computer. Unfortunately, our computers don't have enough memory to represent an infinite number of values so we use the Arduino to take samples of this signal and convert it to a set of numbers we can then use in IPython.

#### Review: Circuit Symbols and Concepts¶

The table below shows the circuit symbols relevant to this lab and a picture of the real component they represent.

Remember also that a voltage is defined as a difference in electric potential between two points. For example, the voltage source we will be using in this lab has a potential difference of +5V between the positive and negative terminals of the source. Because a voltage requires two points to define, it is often convenient to define a common point in a circuit as a reference. This reference voltage, commonly referred to as "ground" is defined by convention to be 0V, and therefore allows us to conveniently describe voltages at any point in the circuit with respect to 0.

In this lab we will be using a [breadboard](https://learn.sparkfun.com/tutorials/how-to-use-a-breadboard) to construct our circuit. Breadboards are very useful for prototyping circuits as they provide a convenient way to connect components.

As shown above, each of the holes in horizontal rows until the gap in the middle are electrically connected by a strip of metal on the back side of the board. Rather than just touching wires together or soldering them to a printed circuit board, you can connect two components together by pushing the wires into the same row of a breadboard. The two long vertical strips on either side of the board connect all of the holes in that vertical column together. These are very useful for components with many connections such as power and ]ground] of a voltage source.

#### Practical Tips

We highly recommend placing components on the board in some kind of organized and systematic way as shown in the image on the left.
It's both easier to translate a circuit diagram to a logical pattern on a breadboard, and easier to figure out problems after building the circuit. There are plenty of resources online if you are interested in more information about breadboards. This [video](https://www.youtube.com/watch?v=spw0IrA85ks) gives a nice introduction and [this tutorial]( https://learn.sparkfun.com/tutorials/how-to-use-a-breadboard) has a more detailed explanation including some interesting historical notes.

### Lab Equipment

Below is a brief introduction to the equipment that you will be using throughout the labs for this course.

#### Power Supply

Each lab station is equipped with an Agilent E3631A power supply. You can think of this device as a controllable battery that allows you to specify its voltage and current outputs. Below is a brief summary of the steps you should take each time you use the power supply:

1. Set a current limit This device allows you to limit the amount of current it outputs, which is very useful to prevent accidentally destroying parts when a circuit is connected incorrectly.

2. Select an output This device is capable of outputting 3 different voltages with maximum values of 6V, 25V, and -25V respectively. Make sure to push the button for the output you would like to use.

3. Set the voltage After selecting the correct output, set the voltage to the desired value.

4. Turn the output on By default the output of the device is turned off. For the device to actually output current, press the "Output On/Off" button.

#### Multimeter¶

As explained in Lecture 2, there exist devices that are capable of measuring voltages and currents. A multimeter is one such device that is capable of measuring voltage, current, resistance, and in this case even capacitance. The Agilent 34405A digital multimeter available in the lab is very useful for "debugging" a circuit.

1. Connect a black test lead to the black port on the device.

2. Connect a red test lead to one of the red ports on the device The ports are each labeled with what they should be used to measure. The top right port should be used to measure voltage, resistance, capacitance, etc. The other two ports should be used for current measurements and are capable of measuring up to a the marked maximum value.

3. Select the quanitity you wish to measure (e.g. DC voltage, resistance, etc)

4. Connect the multimeter to your circuit. Measuring voltages requires that the test leads are connected to the circuit in parallel as shown for the $V$ measurement, while current measurements require the test leads to be connected in series as shown for the $A$ measurement.

#### Oscilloscope¶

The oscilloscope is another useful piece of test equipment that can be used to measure signals that vary over time. Unlike the multimeter which only shows the instantaneous voltage value, the oscilloscope shows a graph of a voltage versus time. This is particularly useful for measuring how devices like sensors respond to inputs. Although this instrument may appear complicated, half of the knobs are just to adjust the axes of the voltage plot. Below is a brief overview of how to use this oscilloscope:

1. Connect the probe to one of the 4 input channels

2. Make sure that the channel is on (indicated by a green light on the channel number). To turn on a channel, simply press one of the numbered buttons. To turn it off push the button twice.

3. Adjust the horizontal axis of the plot The knob at the top left controls the horizontal time axis and allows you to zoom in or out. The time increments represented by the tick marks on the plot are indicated at the top of the screen.
4. Adjust the vertical scale The larger of the two knobs for each channel allows the vertical scale of the voltage graph to be adjusted. As with the horizontal scale, the number of volts per tick mark on the graph is marked at the top of the screen.

5. Adjust the offset In some cases signals will appear off screen; adjusting the smaller of the two knobs corresponding to each input will shift signals up or down on the plot.

6. Add measurements such as average voltage, amplitude, etc. Measurements can be added by pushing the "Meas" button and using the buttons at the bottom of the screen to select and add measurements.

## Task 1: Measuring a Voltages¶

#### Materials¶

• Solar cell
• Oscilloscope
• Oscilloscope probes

Use the oscilloscope to measure the output of the solar cell in different lighting conditions. Compare the waveform on the oscilloscope when you cover the cell completely with the waveform measured under direct light.

How does the waveform on the oscilloscope vary with light intensity?

What is the average voltage of the cell in ambient light?

Use the oscilloscope to zoom in to 50-100 mV/division. What do you notice about the waveform? How does it differ from what you would expect an ideal device to look like?

## Task 2: Building the Sensor¶

#### Materials¶

• Wire
• 1 $\mu$F capacitor
• 100 k$\Omega$ resistor

Use the breadboard to construct the circuit shown below and use the oscilloscope to measure the output between the points marked $V_{out}$ instead of accross the solar cell.

How does the waveform differ from the one you saw in Task 1?

How is this difference useful in the context of building an imaging system?

## Task 3: Introduction to Arduinos¶

An Arduino is a popular microcontroller, a small computer on a single chip. Microcontrollers are found at the heart of all embedded devices and have enough processing power to perform simple tasks using very little power. Because of its limited processing capabilites, the Arduino doesn't run an operating system like desktop computers or servers; instead the device just executes a single function continuously in a loop as soon as it is powered on. For our application this simple program reads a single voltage value and sends it to a computer via the USB connection. We will now walk through the process of using an Arduino and uploading code to it.

#### Materials¶

• Arduino Uno
• USB A to B cable
1. Use the USB cable to connect the the Arduino to your computer. Verify that the LED on the Arduino is blinking once it is connected to the computer.

2. Open the Arduino IDE The Arduino IDE is used to compile and upload code to the microcontroller.

3. Select "Board > Arduino Uno" This software supports a wide variety of Arduino boards, some of which have different chips. In order to make sure that code is uploaded to the device we need to make sure we have specified we are using the Arduino Uno board.

4. Select serial port The Arduino uses a serial interface to communicate with computers. In order to upload code to the device we have to select the serial port that the Arduino is connected to. On the lab computers there should be two options, select the one that is not "COM1" (most likely something between "COM20" and "COM49"). If you are using your laptop, there will most likely be only one port on this menu which should correspond to the Arduino.

If at any time you are unable to upload code or communicate with the Arduino, this menu can be used to check if the port is still recognized. If not options appear in this menu try unplugging the Arduino and plugging it back in.

5. Open the file "LightSensor.ino" Note that the Arduino IDE requires that the ".ino" source file be placed in a folder with the same name. This file has code to periodically read the voltage of pin "A0" and send this data over serial to a computer.

6. Click the "Upload" button This should compile and upload code to the Arduino. Once the "Programming successful" message is displayed, the Arduino will repeatedly execute the code whenever it is powered on.

7. Connect the Arduino to the sensor circuit Our goal is to use the Arduino board to replace the oscilloscope and external power supply; the Arduino is capable of supplying the 5V necessary to power the circuit, and can also read and send voltage values to our computer.

• Disconnect and turn off the power supply.
• Disconnect the oscilloscope probe.
• Connect the 5V port on the Arduino to the positive terminal of the voltage source in the circuit diagram.
• Connect the GND port (either one) on the Arduino corresponds to the negative terminal of the voltage source
• Connect the output of the circuit (which was previously connected to the positive oscilloscope probe) with a wire to the A0 port on the Arduino.
8. Run the code block below and try covering part of the solar cell

In [ ]:
import subprocess
from light_sensor import serial_ports
def get_port():
ports = serial_ports()
if len(ports) > 1:
for p in ports:
if p != "COM1":
return p
else:
print "No serial ports detected. Connect the Arduino and try again."

port = get_port()
correct = raw_input("Port %s selected, is this correct? (y/n) "%port).lower()
if correct == 'y':
subprocess.call(["python", "light_sensor.py", "-D", port])
else:
print "Check Arduino connection"


Congratulations, you can now record sensor data on a computer! There are all kinds of exciting things you can do with this exact setup; once your sensor data is accessible in Python, you can use your sensor input as a cue to send a text message or tweet, start or stop programs, the possibilites are endless!

If you're interested applying what you've learned to your own personal projects there are a variety of spaces on campus with additional resources to support these kinds of activities: