You are not logged in.

Music for this Lab

## Goals:

Two Mini-Projects Today: We'll add an analog-to-digital converter chip to our circuit oeuvre so that our Pi can now start to sensing not just digital (Yes/No) signals but also analog signals (mentioned in homeworks...the yes/no/various shades of grey signals). With this we'll first build a light-level controlled LED using a phototransistor (A sorta-dumb smart lamp), and then we'll add hand controls to our game form last time.

In the first six-ish labs we've made decisions based off of inputs that are either ON or OFF. The Switches are either pressed or not pressed, for example. We call signals like these "digital". Lots of signals in nature and the universe do not fall into this category and we call these types of signal analog. We can already sort of produce analog signals via the audio amplifier from Lab03 that you did. If we'd like to be able to read-in analog signals, we'll need a special chip which converts the analog signal into a digital signal (for the purposes of the Raspberry Pi to read) For that we're going to use an MCP3008 Analog to Digital Converter. The MCP3008 is a 16 Pin DIP so very similar to the LM386 (audio amplifier), except with more pins:

In a blank area of your circuit, build the following with your MCP3008 Note the dangling lines ending with labels like AI0, AI1, AI2, etc.. are pins which you can use to measure analog values.

Your circuit should look approximately like the following when completed:

### 1.1) Hardware Check

To make sure you built this circuit up correctly run the following code on your Pi. If everything is good, it should print a True

try:
import spidev

spi = spidev.SpiDev()
spi.open(0,0)
spi.max_speed_hz = 2000000

return data
data_sets = [[readChannel(x) for x in range(8)] for y in range(10)]
num_zeros = 0
for x in range(8):
for y in range(10):
if data_sets[y][x] == 0:
num_zeros +=1
print(num_zeros)
if num_zeros ==80:
print('Too many quiet measurements. Double Check that 13, 12, and 11 are going to SCLK, MISO, and MOSI, respectively.  See diagram and hint')
print (False)
elif num_zeros in [76,77,78,79]:
print('Make sure Pin 10 on the MCP3008 is hooked up to CE0. See diagram and Hint!!!')
print(False)
else:
print(True)
except:
print(False)


### 1.2) Phototransistor

We're first going to build a variation on the voltage divider format that you've been getting some experience with in the homeworks. The new part is a phototransistor, which is shown below. It is a part which changes its conductivity based on the amount of light it is exposed to (they use these in remote-control receivers and many other things)

When we add this part into a standard voltage-divider like circuit, as shown below, we generate a circuit who's output voltage will vary with light!

Connect the output voltage V_{out} to the AI0 pin on your MCP3008 chip (pin 1 in case you forgot).

## 2) The Output

When ready run the following code:

import time
import spidev

spi = spidev.SpiDev()
spi.open(0,0)
spi.max_speed_hz = 2000000

#don't change the function below
return 3.3*data/1024

while True:
#feel free to change what is in this while loop!
print(readChannel(0)) #just printing the voltage being measured


You should see values fly by and their values change significantly as your hand covers and uncovers the phototransistor. If plotted out, something like the following is the result:

The phototransistor is a complicated device so I can't say honestly that its "resistance" changes with light. Instead you can think that as more light shines on it, more current flows through it. This will result in more current through the top resistor, and then more of a voltage drop across that resistor, meaning less voltage at the output where we measure.

So this is great. Let's do something with it.

## 3) Auto-Brightness

Build an auto-brightness system based around the schematic below (don't destroy your other circuits, btw!!!!). Pick an unused GPIO pin and connect an LED to it. Develop some code that will turn on the LED when the phototransistor gets too dark and will turn off the LED when the phototransistor gets too light. The result will be a behavior similar to one of those crappy garden lights that never work.

When complete show a staff member. Have the phototransistor point at the LED. See if you can generate a negative feedback loop where the LED turns the phototransistor on, which turns the LED off, which turns the phototransistor off, which turns the LED on, which turns the phototransistor on, ad nauseum.

## 4) Game-Time

Let's now build a second phototransistor circuit. Connect this one to AI7 on your MCP3008 like shown below:

Make a copy of your final game code (or the original starter code) from Lab 05 and integrate these two light sensors into it so that your hand position controls the left-right movement of your player's character. This will involve finding where in the code the left-right inputs are processed, adding in a scheme where the two phototransistor readings are compared and processed, and then using that output to determine how to drive the character.

When it is working, show a staff member.

In Lab 07 we'll add in a microphone to our system and ultimately use this to control the shooting of missiles...so you then get a full immersive game experience.

## 5) Keep Going

Are you all done? Silly, child...No you aren't. Consider doing the following additional tasks, and by consider I mean do them:

• Make the speed at which the character moves be based on the proximity of your hands (so it is no longer a on-off-on type of movement controller)
• Make a security alarm system where a proximity sensor causes that creepy voice thing to say, "Back off", or something similar.
• Using the two phototransistors can you come up with a motion detector...something that can discern a left-to-right motion vs. a right-to-left motion? (implied answer is yes you can).