Classes/2010/VIS147A/Labs

From Robert-Depot
Jump to: navigation, search

Contents

Lab 1 - Power Supply, Multimeter, Breadboard

Topics

  • connectivity on the breadboard
  • polarity of the power supply, positive and negative voltages.
  • proper practice for cutting wires and stripping the ends of wires. how to use your wire clippers and your wire strippers.
  • how to use your multimeter to measure continuity, voltage.

Supplies

  • wire clippers
  • wire strippers
  • 12V DC 500 mA power supply/AC Adapter. (do you have any of these at home? In case you burn one out, it may come in handy. check your old scanner, phone charger, etc., they are all labelled with voltage, current, polarity).
  • solderless breadboard
  • black and red hookup wire.
  • black and red alligator cables.

Activities

Distribute the kits. Please keep them in the lab, with the understanding that once you have paid for them with Judi you can take them home. (We will have the final price in next week, please bring a check addressed to the UC Regents).

Breadboard Setup and Voltage Supply:

  • Connect the positive and negative rails running along the top and bottom of the breadboard. The positive rails have a red bar running next to them and are labeled with a "+", the negative rails have a blue bar running next to them and are labelled with a "-". Use a short red wire to connect the positive rails and short black wire to connect the negative rails. Make your wires long enough to avoid blocking any of the holes on the breadboard, but not so long that they are trailing off into space. See the picture below.

Breadboard w supply.jpg

  • Set up your multimeter: attach the red and black probes and remove the covers over the tips.
  • Test the continuity setting of the multimeter. Turn the dial to the position in the photo below, and make sure the black probe is connected to the middle socket and the red probe is connected to the far right socket. Keeping the probes apart, the screen should display "1" on the far right. When you touch the two probes, it should beep. This setting lets you know if two points in a circuit are connected without power running through the circuit.

Continuity setting.jpg

  • Use the continuity setting on your multimeter to check that you successfully connected the positive and negative rails in step 1.
    • Are your red supply holes connected at every point on the board?
    • How about the black ones?
    • You are making sure that your power is distributed properly for later use.

Power continuity check.jpg

  • Clip the end of the power adapter off, saving enough wire with the plug to reconnect the two later in the semester (do not clip the wires too close to the plug).
    • Cut the two loose ends to different lengths, to prevent accidentally touching them together.
    • Separate the two wires and strip the loose ends of each.
    • Twist the end of each wire to join the loose strands together.

Clipped plug.jpg Assymetrically clipped.jpg

  • With the DC voltage setting on the multimeter in the appropriate range, identify which is the positive and which is the negative wire coming from the adapter. The voltage setting on the multimeter dial is shown below. The number at top will display the current voltage. It is helpful to use a red and black alligator cable to connect from the multimeter probes to the power supply. Attach them as shown. WARNING: You need to plug the adapter into the wall. Be sure that you do not touch the two power supply wires together, this will short out your power supply and you will be out $5.00. This is your first live voltage!

Voltage setting.jpg Pos neg alligator clips.jpg Testing powersupply voltage.jpg

    • What is the voltage you record?
    • Which is the positive wire and which is the negative wire?
    • Is the positive wire labeled in any way?
    • Attach the red alligator cable to the positive wire, and the black alligator cable to the negative wire. This is the standard color code for positive and negative, red for "+" and black for "-". We will use this throughout the course.
  • Unplug the power supply.
  • Using one red and one black alligator cable and a short length of red and black hookup wire, connect the positive and negative lines from the power supply to the appropriate power buses on the breadboard.

Power connected.jpg

  • Plug the power supply back in.
  • Measure the voltage across the positive and negative power bus. Measure it at a number of different points on the board.
    • What is the voltage that you see?
    • Is it the same at every point on the board?

Measuring power bus.jpg

  • Unplug your power adapter from the wall, and look at the label on the back.
    • What is the labeled output value?
    • Is this different from the voltage you measured before?

Lab 2 - Voltage, Current, Resistance

Topics

  • Electricity flow.
  • Voltage, Current, Resistance.
  • Ohm's law. V = I * R
  • SI units. (Volts, Amperes, Ohms)

Supplies

  • 4 6V bulbs datasheet "MSCP" stands for "Mean Spherical Candlepower" [1] p. 507 Scherz.
  • 4 1K resistors. (Brown-Black-Red. for reference see the inside of the second page in Scherz)
  • 12 V power supply
  • LM7805 5V regulator datasheet
  • wire clippers, wire strippers, pliers.
  • multimeter
  • red and black hookup wire

Activities

Part 1: Set up your 5V power supply

Set up the voltage regulator on your breadboard. This chip, the LM7805 is a "5 volt regulator", meaning it takes an INPUT voltage ("+12V" from your adapter), and turns it into an OUTPUT voltage, +5V. It also needs to be connected to GROUND. This is a diagram of the regulator:

7805 schematic.png

Notice the regulator has three legs. Each of these legs has to be connected to the right thing. From page 2 of the datasheet for the regulator [2], under "Pin Assignment" we see what each of the legs are. Looking at the front, they are numbered 1, 2, and 3, from left to right, and are labeled "Input", "GND", and "Output":

7805 pinout.png

TIP: use your pliers to insert the 7805 into the breadboard. Its legs are thicker than your typical wire/resistor/LED, and sometimes are hard to get into the board. On the breadboard the circuit will look like this:

7805 on breadboard.jpg

Set up a light bulb to be a power indicator, as shown in the images below:

7805 schematic w bulb.png

7805 with power light.jpg

Connect your power supply. If your board has power, the bulb should turn on.

Be careful with the regulator when the power is on, it may get hot!!!

QUESTIONS. With your multimeter, answer the following:

  • what is the voltage across the bulb?
  • what is the voltage across the input to the voltage regulator?
  • what is the voltage across the output from the voltage regulator?
  • how much current is flowing through the bulb? According to the datasheet for the bulb [3], how much current should the bulb draw? REMEMBER: measuring current with a multimeter is different than measuring voltage. You need to "break the circuit" and re-connect it through the multimeter.

Part 2: Series and Parallel (with bulbs)

For each of the following diagrams, rate the relative brightness of the bulbs. Use a scale of 1 to 5, with 1 being brightest and 5 being most dim. You can sketch them by hand and do this on your own paper, or you can print out the images from this pdf [4].

To get you started, the bulb in diagram 1 will have a brightness of "1"--the brightest a bulb will be in any of the circuits.

Series parallel1.png

Series parallel2.png

For help, see Scherz p 49, "2.11 Electric Circuits".

Part 3: Series and Parallel (with resistors)

Working in pairs, wire up the circuits and verify the voltages and currents you calculated for circuits 1-5 in last week's homework. Get help from your TA in determining which resistors are which using the resistor color code. The color code is described on the back side of the first page in the Scherz book.

Don't build circuit #6!! I will show it to you in class.

...but here is the rationale if you want to think about it:

  • We have a 5 Volt supply and a 10 Ohm Resistor.
  • The resistor is rated at 1/4 of a Watt, or 0.25 Watts.
  • P=I*V. P=I*5V.
  • this amounts to a brief (and cautionary) demo of what a power rating is, and why it is important.

Lab 3 - Switches, Logic, Loops

Topics

  • Learn about the variety of switch types:
    • Push Button switches: Normally Open (NO) and Normally Closed (NC).
    • Throw Switches: incl. SPST, SPDT, DPST, DPDT
  • Boolean Logic: True, False, AND, OR, NOT, NAND.
  • Relays.
  • A Latch: one bit of information. Simple Set/Reset latch

Supplies

  • 1 SPST Normally Open (N.O.) switch
  • 1 SPST Normally Closed (N.C.) switch
  • 5V, 40mA relay PB370-ND [5]
  • 1 light bulb.
  • Red and black hookup wire.
  • Wire strippers, clippers, and pliers.
  • Ridiculous Knife Switches (DPDT, available in class).

Activities

  • Using the continuity setting on your multimeter, determine which is your Normally Open (N.O.) and which is your Normally Closed (N.C.) pushbutton switch. Building a simple switch and light bulb circuit, test out how each one behaves:

SPST circuit.png

  • With the ridiculous knife switches available in class, build a simple "selector switch" that turns on one of two bulbs:

SPDT bulbs.png

  • Build an AND gate and verify its behavior (by filling in the truth table):

AND circuit.png

  • Build an OR gate and verify its behavior:

OR circuit.png

  • Familiarize yourself with the 5V relay.
    • Which pins go to the coil?
    • Which pins are the contacts that get switched?
    • Is the relay Normally Open(NO) or Normally Closed (NC)?
    • Using the multimeter for current sensing, how much current does the relay coil draw?
  • With the relay, build a simple S-R latch:

One bit memory.png

  • Now, turn the latch into a pixel:

One pixel.png

  • Congratulations You have built yourself one bit of digital memory and simple digital display!
  • Now, returning to the idea of logic, label one side of a knife switch "TRUE" and the other side "FALSE".
  • Working in small groups, use multiple knife switches (say five or more) and build a circuit that responds to a complex input. For instance, my circuit has five switches ("A" through "E") and one Light Bulb ("O"). The lighbulb, O, only turns on if switches A, C, and D are True, and switches B and E are false.
    • Build your circuit and show that it works.
    • Represent that circuit as a logical statement. O = (A + C + D) + (!B + !E)
    • Draw that circuit as a diagram.

Lab 4 - Sensors, Semiconductors

Topics

  • Potentiometers, photoresistors, variable resistance.
  • Transistors

Supplies

  • TIP-120 transistor.
  • Bulb.
  • 10K potentiometer.
  • Pliers, strippers, clippers.
  • Hookup wire.
  • Jumper kit (short pre-cut wires).
  • Breadboard.
  • Power Supply.
  • Multimeter.

Activities

  • Explore the behavior of your potentiometer. Is the relationship between rotation + resistance linear? ("linear" vs "exponential" taper)
    • What is the measure between "A" and "B"? does it change as your turn the knob?
    • Record and plot the rotation of the knob and resistance at a few points. Is the relationship between them linear? ("linear" or "expontential" taper)

Potentiometer labelled.jpgVariable resistance.png

  • Wire up a simple voltage divider using your potentiometer and a 10K resistor, conected to the 5V supply.
    • How does the output voltage vary as you turn the knob? (measure at the point between the potentiometer and the resistor)
  • Photoresistor as variable resistance
    • measure the "light" resistance of your photoresistor
    • measure the "dark" resistance. (cover the resistor with your hand, or try shutting it in one of the lab cabinets)
    • setup up a voltage divider with the photoresistor and a 10K resistor. What range of output voltages can you get by covering or uncovering the photoelement?
  • Connect a transistor to control the brightness of a bulb by turning the knob of a potentiometer.
  • Substitute a photoresistor in place of the potentiometer.
  • Light "phobic" and light "philic" circuits with a transistor

Lab 5 - Motors, Coils, Inductors

Lab 6 - Microcontrollers

Topics

  • soldering
  • assembling a breadboard
  • basics of compiling a program and uploading to your microcontroller.

Supplies

  • Soldering Iron, stand, solder, solder wick.
  • Pliers, clippers, strippers.
  • Freeduino Kit.

Activities

  • Introduction
  • Assemble your Freeduino:
  • Download and install most recent Arduino software: http://www.arduino.cc/en/Main/Software
  • NOTE: We have a newer, beefier processor than the original Arduino. You need to tell the Arduino software this. (if you had problems uploading, this may have been why)
    • In the Arduino software, under Tools->Board, select the Duemilanove w/ Atmega 328.

Assignment

  • For Next Week: Assembled, functioning Freeduino, communicating with computer, able to upload test sketches.

Lab 7 - Digital input/output

From last week

  • You should have the basic Blink example running (File->Sketchbook->Examples->Blink)
    • This means you have soldered the board correctly.
    • You can compile and upload files.
  • We are going to extend from there.

Set up Breadboard

  1. Power from arduino to breadboard:
    1. connect positive and negative power buses on proto board
  2. run power, "5V" and "GND" from arduino to breadboard.

Power hookup.png

  1. verify that this worked (with multimeter, or LED test).

Verify power.jpg

Simple Digital Out

  1. Set up LED on external board, check that it works
Led on board.png
  1. Connect the LED to one of your Digital Out pins.(Pins 0-13 on the "Digital" side of your board, by the power light.)

D out 2.png

Basic LED Driver

  1. change Blink program (from Sketchbook->Examples->Digital) to output to that pin you connected.
  2. Compile, upload, and run it.... Does it work?
  3. try changing the timing (HINT: the delay(1000) statement in Blink example)

Simple Digital In

Switch.jpg

D in.png

Active High, Normally Open (N.O.) switch

  1. Set up this circuit on your breadboard, with a switch, +5V, and current limiting resistor to ground. This is a basic digital input.
  2. Open the button example (File->Sketchbook->Examples->Digital->Button)
  3. Connect your switch circuit to on if the digital I/O pins, and change inputPin in the button program to this number. (Mine is Pin 7)

Some Programming

  • In the simple blink exampe, this code here:

 digitalWrite(ledPin, HIGH);   // sets the LED on
 delay(1000);                  // waits for a second
 digitalWrite(ledPin, LOW);    // sets the LED off
 delay(1000);                  // waits for a second

  • is what creates the behavior: half the time on, half the time off.
  • The digitalWrite(ledPin, HIGH) turns on the LED
  • digitalWrite(ledPin, LOW) turns off the LED.
  • What is this actually doing? The HIGH is setting that output pin to High, 1, True, +5V.
  • similarity, the LOW is setting that output pin to Low, 0, False, 0V or GND.
  • This is where your programmable, internal world of the microcontroller is interfacing with the hardware, LED world of intro to electronics.
  • We needed to do some setup first pinMode(ledPin, OUTPUT)
  • and then it is a matter of digitalWrite calls.
  • Similarly, in the digital_in example above, we do some setup
 pinMode(ledPin, OUTPUT);      // declare LED as output
 pinMode(inputPin, INPUT);     // declare pushbutton as input

  • And then we read inputs and make outputs with digitalRead and digitalWrite calls.
  • If you ever seen anything in a program, and you want to learn more about what it does, for example digital read, hilight the term, and select Help->Find in reference.
    • This will pull up the arduino reference files. There is a lot to be found here. Very useful.

So what can we change?

  • You could insert your own code, for instance

 digitalWrite(ledPin, HIGH);   // sets the LED on
 delay(1000);                  // waits for one second
 digitalWrite(ledPin, LOW);    // sets the LED off
 delay(1000);                  // waits for one second
 digitalWrite(ledPin, HIGH);   // sets the LED on
 delay(500);                  // waits for half a second second
 digitalWrite(ledPin, LOW);    // sets the LED off
 delay(1000);                  // waits for one second

  • to get a "long-short-long-short" blinking.
    • Try this out.
  • As you add more complicated behavior, things are going to get ridiculous very soon, you will have tons of special cases, lines and lines of code. There are more efficient ways to do this...
  • What are more efficient ways to do this, use For loops or arrays.

Loop (Optional)

  • Specifically, looping across multiple outputs.
  • File->Sketchbook->Examples->Digital->Loop
  • Same as single input above, connect multiple LED outputs.
  • Compile, upload, and run the loop example.
  • Change the behavior of the loop: how about instead of equal on/off timing, change it in some other way.
  • Can you make the loop run through a series of meaningful states, rather repeating a simple on/off behavior.
  • Differently, with only one output, how could you make a program that changes over time using a loop.

Thoughts

  • The idea of encoding information.
  • Idea of input and response. Triggering behavior.
  • Framing: how does the meaning of a thing change through context. LEDs (or other lights) surround us, everywhere, to indicate all kinds of things.
    • "Record" light on a camera. Warning light.
    • think about contextualization... if the light is in an empty fuel gauge... warning light... how does its meaning change
    • how could you change the meaning through contextualization
    • what novel package or "frame" could change the meaning of a thing.

Assignment

  • A quick thought/lab experiment for next week: Extend the example in some way, particularly focusing on the idea of framing.
    • This could be through:
      • the addition of multiple different inputs.
      • ...multiple different outputs.
      • ...create a time-varying signal. (code?)
      • change through packaging, framing, context.
      • getting off of the breadboard (or tucking the breadboard inside of something) how could you change what the LED indicates.
    • Some of these are technical challenges, some or more concept based.
    • For this assignment, I am interested in the more concept based... having an idea you want to make.

Lab 8 - Analog Input/Output, PWM

Analog Input

  • File->Sketchbook->Analog->AnalogInput

The value of the input signal changes the timing of the light: the lower the signal the lower the on-off frequency, the higher the signal, the higher the frequency. We can return to this at the end of the lab.

First we will use a potentiometer as our input.

Potentiometer

Pot in.png

A_IN can be any of the Analog In pins (0-5) on your Freeduino.

Pot photo.JPG

  • Q1.1: What will the voltage at A_IN be? What are the upper and lower values? (HINT: it is a voltage divider. How do you calculate the voltage in a voltage divider?)

photoresistor

Photo resistor.png

Photoresistor.JPG

Take the photoresistor from your 147A kit, which has a resistance between 140k - 800 depending on your lighting conditions.

  • Q2.1: With the same R2 resistor (10k) from above, replacing R1 with the photoresistor, what range of voltages (and corresponding data values) would you expect to see at your A_IN pin?
  • Q2.2: The important part of this sketch is the line where the microprocessor reads the analog input: val = analogRead(potPin); Looking at the Arduino help for that function (analogRead()), what do you think the value val will be for those highest and lowest voltages at your A_IN pin?
  • Q2.3: How can you verify your guess for the previous question? Come up with a plausible method, and try it out. Anything goes! (even using functions next weeks material, like Sketchbook->Communication->Graph) If you have a good idea, share it with the class....
  • Q2.4: How could you make this more sensitive? There are least three ways to try... think in terms of the physical enclosure around the sensor, the hardware setup (a voltage divider), and the software (look at File->Sketchbook->Analog->Calibration..., specifically the map() function)
  • Q2.5: Does changing the code make this more sensitive?

FYI other sensors

I have some of them here if you want to try them.

flex sensor

spectra symbol flex sensor [6]

home made strain gauge

A poor man's flex sensor. See page 32 in Forest Mims "Electronic Sensor Circuits & Projects" to make your own.

force / pressure sensor

Force Sensing Resistor (FSR)

FSR.JPG

datasheet purchase

Voltage returned is proportional to force applied to surface:

Flexi-response.gif

Application circuit ([7]):

flexi-circuit-new.jpg

Good for substantial physical interaction (this project: http://roberttwomey.com/hitme/)

more force sensors

FlexiForce Sensors: http://www.tekscan.com/flexiforce/flexiforce.html

IR rangefinders

Sharp GP2D12 INFRA-RED RANGER

IR RANGER.JPG

"This sensor takes a continuous distance reading and returns a corresponding analog voltage with a range of 10cm (4") to 80cm (30"). The sensor package includes a JST 3-pin connector, three pre-crimped wires, and a booklet with detailed information and examples." [8]

datasheet

One of many Sharp Rangers, with different effective ranges comparison guide

Ultrasonic rangefinders

http://www.acroname.com/robotics/parts/R271-SRF05.html range of four meters

And more...

See the Homework section.

can we sample sound?

  • That is a good question.
  • We can do definitely do threshold detection: is a sound louder than a certain point.
  • Can we make WAV files? I Don't Know...

Analog Output

Continually varying signals, rather than discrete "steps". How does a digital microprocessor create an analog output?

PWM, duty cycle, and effective voltage.

LED Dimming

  • File->Sketchbook->Examples->Analog->Fading

The crux of this sketch is the analogWrite() function.

NOTE: The function analogWrite() only works on some digital output pins. Look up the help for the function, it will tell you which pins work: Digital pins 3,5,6,9,10, and 11. <youtube v="62gWVWCyw_w" loop="true"/>

LED FADER.JPG

  • It looks like a MacBook napping...
  • Get this working. Then...
  • Q: Drive this dimming example with the light sensor from before.
    • use analogRead() to get the value from the sensor, like you did before, then use analogWrite() to output the value to the LED. NOTE: analogRead() will return values from 0-1023, and analogWrite() writes values from 0 to 255, so you will need to divide your read value by 4 to get the output value. Or you could use map()
  • Q3.1: Does yours turn on when it is light, or does it turn off? Switch this to give it the opposite behavior, in either hardware or software. You could do either!

Lightbulb

Same as the LED, but with a higher current load: your microprocessor can not source enough current to power the bulb. (Or maybe it can, but you probably don't want to test it!) We need to use a power transistor: TIP120. This allows you to control a larger current (the bulb) with a smaller current (digital out from the chip).

Lamp fader.png

BULB.JPG

Motor

  • Basic motor speed control (for a DC motor).
  • Same circuit as above, swapping the motor for the lamp.

MOTOR SPEED.JPG

Q: Drive it with the input from the potentiometer.

Sound/Tone Generation! For Sound People (optional)

An analog output, different from the light dimmer. To make a varying sound, you would want to change the frequency of the output signal, not the amplitude.

  • This is more like the light blinking from part one, where changing the input voltage will change the rate of the light. But now you will do it with a Piezo, adjusting the time delay in microseconds.
  • If you like sound, try this: http://www.arduino.cc/en/Tutorial/PlayMelody, I have a piezo you can use. (Or you can get one at RadioShack).
  • This example adjusts the frequency of the output (tone) not the magnitude.
  • the light fading above (analogWrite()) adjusts the brightness of the light (magnitude) with PWM.

Homework

NEW ASSIGNMENT: Acquire a sensor of some sort... not one of the ones from your kit. Could be a pressure, temperature, VOC, range-finder, accelerometer (!!) ... there are lots of options. Start with some of the links below. If you need more ideas, look through the web. Be sure to have your new sensor by next week!! We are going to use them in class, for Serial Communications.

Don't worry if you don't know how to make it work: if you are concerned whether your choice is viable, send me an e-mail with a link to your sensor of interest before you get it.

MORE SENSORS:

A Cornucopia of sensors

I gave some examples of sensors in the first part of today's lab.

Actuators, for future reference

SERVO.JPG

Servo

SOLENOID.JPG

Solenoid

Stepper Motor

Cellphone buzzer (I have one here)

Buzzer.JPG

AC appliances with a relay (!!!!CAUTION CAUTION CAUTION!!! get input from me before you try anything VERY DANGEROUS!!!!) (think dancing pixar light)

Anything that moves, blinks, has activity, and runs off electricity... ?

BONUS

Cadsoft EaglePCB - the program I have been using to make these schematics. http://www.cadsoft.de/download.htm

If you want to get a headstart on the future, download this software. It is free. You can use it to design circuits, from schematic to layout to fabrication. (FYI It is also installed on the lab computers)

Lab 9 - Interfacing with the computer, higher level sensors

Interfacing

This is the program we are going to use for our in class work, and for your homework this week. To get a sense of what people do with processing, you can look at their exhibition space http://processing.org/exhibition/, or do some google searching. We are going to use Processing because it is easy to get started with, quick to make experiments, and it has a similar layout and feel to our Arduino software.

Serial Basics

Basic text output from the microcontroller is accomplished with

Serial.print()

and

Serial.println()


You can use this to verify values and behavior inside of a program, for debugging, for example:

Displaying values of sensors:

val=analogRead(input_pin);
Serial.print(val);

(assuming you've done the necessary setup).

Checking program flow, communicating the status of things to the user or debugger (you!).

Inside the Arduino software, we will use the "Serial Monitor" to observe what is going on with the serial port. Here is where you will see data printed with the Serial.print() command, and you can also send some very simple messages back to the processor with the "Send" button.

  • Open the Sketchbook->Examples->Communication->ASCIITable, compile it, and run it on your processor.
  • This program does basic text communication from the processor to your PC, running through all of the characters in the alphabet.
  • Click on the "Serial Monitor" button (to the right of the "Upload to Board" button) in the arduino software. verify that your processor is communicating with the PC (do you see the letters streaming by?) This is your processor sending data to the computer. We will use this later.

Sending Data from your Arduino to the Computer

For this section you are going to use three potentiometers (or light sensor, or whatever you would like) to drive an application on the computer. Examples->Communication->VirtualColorMixer

  • Wire up three potentiometers as voltage dividers (like last week), and connect them to Analog_IN pins 0, 1, and 2.

Pot in.png Color mixer pot in.jpg

  • Compile the Virtual Color Mixer, and upload it to your arduino.
  • In the Serial Monitor, verify that you see R, G, and B values, and they change as you move the potentiometers.

Color mix serialout.png

  • Create the processing Sketch "arduino_color_mixer"
  • Change the Serial port in the initialization in Processing. (port = new Serial(this, Serial.list()[2], 9600); on my computer)
  • Change the Integer.parseInt(buff); statements to say rval = Integer.parseInt(buff)/4;. This relates to the issue last week with the analogRead() and analogWrite(): our Arduino is transmitting a 10bit (0-1023) value captured from the analogRead(), but the background(rval, gval, bval); command in Processing wants 8bit (0-255) values for Red, Green, and Blue. Thus, we need to divide the 10 bit values by 4.

Arduino color mixer.png

  • Run this program (same as on the Arduino, click the little "Play" button at the upper left). NOTE: Hold down the reset button on your Arduino while you start the Processing applet. Release the button once the new window has appeared. If you do not, the Processing applet will start reading mid-data stream, which will confuse it and crash. You should see something like the window below:

Color mix.png

  • You should be able to change the color of the on-screen square by dialing the knobs back and forth. This behavior could be anything: you could move images around on the screen according to the knobs, you could trigger media clips, you could fire off e-mails. Anything you can program on the computer could be triggered by input from your Arduino. This is the first half of an interactive microprocessor / computer sculpture.

Sending Data from the Computer to Control your Arduino

Controlling a physical LED with the computer.

  • Compile the Examples->Communication->Dimmer example, and upload it to your board. This example sets up communication on the serial port (in the void setup() function, and then reads values from the Serial input, and writes them to the ledPin output.
  • Hook up a simple resistor and LED to the pin 9, to test the dimer example.

D out 2.png

  • Connect up the TIP120 driver circuit and motor from last week to the ledPin (pin 9). You should now be able to control the motor speed via the serial port on your computer... interesting...
  • Click on the Serial Monitor. Try sending different values to the board. Type in numbers and press Enter (or click on the "Send" button, either will work).
    • What behavior do you observe?
    • Can you get the motor to speed up or slow down?

modifying for rudimentary keyboard control

As-is, the Dimmer code is not very useful for controlling through the serial terminal like this. Now we are going to modify it to so that it works a little better. Our goal is to be able to set the speed of the motor typing the numbers 0-9 on the keyboard. 9 will go fastest (nearly full speed), and 0 will be a dead stop.

  • Make a new copy of the Dimmer example, and replace the original loop() code with this new one:
void loop()
{
 char val;
 byte out_speed;
 // check if data has been sent from the computer
 if (Serial.available()) {
   // read the most recent byte (which will be from 0 to 255)
   val = Serial.read();
   
   if((val>='0')&&(val<='9')) {
     val=val-'0'; // set value between 0 and 9. 
     out_speed=val*28; // set out_speed between 0 and 252.
     analogWrite(ledPin, out_speed);
   } 
 }
}
  • What I have done is change the Serial.read() and analogWrite() from before. Now we are reading a char rather than a byte from the Serial port, and we are checking whether this character it reads is one of the characters between '0' and '9': a letter on the keyboard was pressed, it is one of the numbers? If it is (if((val>='0')&&(val<='9'))) then we will set the speed of the motor proportionally.
  • Compile and upload this sketch to the board, and try it out. Start the Serial Monitor again, and try sending the numbers 0-9 on the keyboard, the motor should speed up and slow down appropriately.
  • EXPLANATION: What did we just do? The problem in our original Dimmer code was that the microcontroller was looking just at the binary value of the data it was receiving, but did not know what key was pressed. So, when we typed the number 0, which happens to have a binary value of ___ in the ASCII table, it will set the speed to that value, not to 0. We changed the program so the microcontroller is looking for the specific characters, '0' to '9', and then doing the appropriate math to set the output to the right speed.

controlling your arduino from processing app

Save your modified Dimmer example, and revert back to the original dimmer code. We are now going to learn how to control the processor from the processing App. See the commented Processing code hidden at the end of the Dimmer program.

  • In Processing, open a new sketch, and copy this code into it. Save this sketch as "arduino_test" or something like that. It should look like this:

Arduino serialtest processing.png

  • Try running this sketch. When you do, it may give you an error about "COM1" or something of that sort. You need to tell the program what serial port your arduino is connected to. When the program runs, you will see a list of Serial ports, you can see mine in the picture above. On my comnputer, I see that my serial port is the "tty.usbserial-A900acnt", which is listed as device number 2 ([2]), so I go back into the processing code and change the line that says port = new Serial... to read port = new Serial(this, Serial.list()[2], 9600);
  • Now if I run the sketch again, I know that it will be properly communicating with the Arduino. You will need to modify that line to reflect whichever serial port your arduino is connected to.
  • Upload the original (unmodified) Dimmer code to your arduino in the arduino software. Your processor should now be running that code... waiting for input!
  • Run your newly created arduino_test sketch in processing. You will see a small graphic window (like the one below), as you drag your mouse forwards and backwards across that window, your motor should speed up and slow down. Allright!! Now you have a simple graphical interface!

Arduino serialtest.png

Send data from your sensor to the computer

  • Assuming you have a sensor which produces an analog output, hook it up to one of the Analog Inputs on your arduino. If you do not have a sensor, use the light sensor from last week. If you are not sure how your sensor works, come over and talk to me and we can try to figure it out.
  • Run a short program to read data from that sensor and send it to the Serial port.
    • You will need a setup() block, which initializes the Serial communication Serial.begin(9600);
    • And in the main loop of the program (loop()), you will need to do the analogRead() and Serial.print() commands from above.
  • See what the output from your Sensor is. How does it change as you manipulate your sensor? This is what we were getting at last week figuring out the voltage range for our sensors: when we want to use a sensor to make a project sensitive and responsive to the environment, we need to know what voltages correspond to the conditions of interest. When you start with a new project, this sort of Serial communication is a very valuable tool.

Homework

With your sensor, build a simple computer/microprocessor project. Establish communication between the computer and processor, either controlling the device with commands from your computer program, or driving the computer program with information from the processor. You may use Processing, and adapt one of the simple examples we have seen in class, or you can use any other program you are comfortable with (Flash, MaxMSP, PD), so long as you can demonstrate it to us in class next week. We will take the first part of class to look at your these mini-projects.

The little program from the first part of the lesson, where your Arduino program outputs the data from your sensor Serially will be useful here. Now you just need to make a program on the computer to take that data and do something with it. If you are looking for inspiration, pick a couple of the sketchbook examples in Processing and run a few of them. You will need to intergrate the Serial data input into one of those examples, or come up with something new. We can work on this in class today.

Interesting Side Note: Pachube

http://www.pachube.com/ a service that enables you to connect, tag and share real time sensor data from objects, devices, buildings and environments around the world. The key aim is to facilitate interaction between remote environments, both physical and virtual."

Extra Info: Interfacing with Other Devices

Connecting to various external devices and components: http://www.arduino.cc/playground/Main/ComponentLib

Lab 10 - Summary, discussion, wind-down / Final Project Work