Difference between revisions of "7745 Sensing Lab 2"

From CCRMA Wiki
Jump to: navigation, search
Line 23: Line 23:
  
  
== Analog Circuits ==
+
== More Analog Circuits ==
  
 
Now we will work with the continuous input values provided by analog sensors - potentiometers, accelerometers, distance rangers, etc
 
Now we will work with the continuous input values provided by analog sensors - potentiometers, accelerometers, distance rangers, etc
Line 56: Line 56:
  
  
== Digital Sensing ==
+
== First Digital Circuits ==
 +
=== Build the Button and LED Circuit ===
 +
We'll start our digital tutorial with three simple light circuits.
 +
 
 +
* In the first one, the LED is on all the time.
 +
* In the second, the LED only lights up when a button is pressed and a circuit is completed.
 +
* In the third example, we'll replace the manual switch with an Arduino pin (set to output mode), so we can control the LED from our program.
 +
 
 +
[[Image:1a_schem.png|200px]] [[Image:1b_schem.png|200px]] [[Image:1c_schem.png|400px]]
 +
 
 +
 
 +
==== Power an LED all the time ====
 +
[[Image:1a.png|500px]]
 +
 
 +
Build the following circuit on your breadboard. Use a 220 Ohm resistor (red red brown gold -- see [http://wiki.xtronics.com/index.php/Resistor_Codes this page] about resistor color codes).
 +
 
 +
Because the LED is a diode, it has a set voltage drop across the leads; exceeding this causes heat to build up and the LED to fail prematurely. So! It is generally important to have a resistor in series with the LED.
 +
 
 +
Also, another consequence of the LED being a diode is that it has directionality. The longer lead, the anode, should be connected towards power (THE LONG ARM REACHES FOR POWER!!!); the shorter, cathode, should be connected towards ground. (In the diagram, the longer lead has a bent "knee.")
 +
 
 +
 
 +
==== Make a light switch ====
 +
Next, we'll insert a switch into the circuit. The momentary switches in your kit are "normal open", meaning that the circuit is interrupted in the idle state, when the switch is not pressed. Pressing the switch closes the circuit until you let go again.
 +
 
 +
[[Image:1b.png|500px]]
 +
 
 +
Use a multimeter to see what happens to the voltage on either side of the LED when you press the switch.
 +
 
 +
 
 +
==== Toggling LED with Pd ====
 +
In the third example, we'll replace the manual switch with an Arduino pin (set to output mode), so we can control the LED from our program. The safe way to do this is to let the Arduino pin sink current - if we toggle the pin low, it acts as ground and current flows through the resistor and the LED as it did in the previous examples. When we take the pin high, to 5V, there is no potential difference and no current flows - the LED stays off.
 +
 
 +
* Open the lab patch ''~/pd/labs-Music-250a-2012/lab2/arduinoLab.pd''
 +
 
 +
* To get pd to connect properly to the Arduino Nano, you need to correctly select the serial port # (see the upper left-hand portion of the patch). This functionality allows you to address multiple Arduino Nano's connected at the same time. Right now we only have one. Try selecting different serial port #'s until you see a message like "[comport] opened serial line device 4 (/dev/ttyUSB0)."
 +
 
 +
* Verify now that communication with the Arduino Nano is working by using digital pin 13, which is wired directly to an LED on the top of the Arduino. First, set the ''mode'' of pin ''13'' to ''output''. Then open up the subpatch for sending digital outs by clicking on ''pd sending digital outs''. Then using the checkbox ''13'' inside that subpatch, you should be able to turn the LED on pin 13 on and off.
 +
 
 +
* Go back to the main patch and set pin ''2'' to ''output''.
 +
 
 +
* Then go back to the "digital outputs" pane. When you click on the checkbox for digital pin 2, the state of your added LED should change (on vs. off).
 +
 
 +
<!--
 +
* There are a lot of options in the patch. We have prepared some exercise presets (see the upper right-hand corner) to try to make it easier for you.
 +
* Press the "toggling LED with software" button in the upper right to preset the outputs properly. The patch expects you to connect the LED to digital pin 2 (D2). (Hint: You can look inside the preset by double clicking on "pd toggling_LED_with_software.")    -->
 +
 
 +
Optional: Try changing your patch so the light stays on when you press the mouse button, and stays off when you press it again. After that, change your patch so the light blinks on/off. Then, have your patch button switch the light between on and blinking.
 +
 
 +
[[Image:1c.png|500px]]
 +
 
 +
 
 +
=== Sensing buttons in software  ===
 +
We've used code to trigger output - what about the other direction, sensing physical input in code? Just as easy. Here is a simple switch circuit:
 +
 
 +
[[Image: Button1_schem.png|200px]]
 +
[[Image:Button1.png|600px]]
 +
<!-- Nicer image but with longer breadboard and different row numbers  [[Image: Lab2-6.jpg]] -->
 +
 +
When the switch is open, the Arduino pin (set to input mode) is pulled to 5V - in software, we'll read ''1''. When the switch is closed, the voltage at the Arduino pin falls to 0V - in software, we'll read ''0''. The pull-up resistor is used to limit the current going through the circuit. In software, we can check the value of the pin and switch between graphics accordingly.
 +
 
 +
* Setup the circuit and connect it to digital pin 4 (D4).
 +
* Set the ''mode'' of pin ''4'' to ''input''.
 +
* Then look at the "digital input readings" pane while you press the button. <!--But you will notice that the pin 4 reading does not change!
 +
* This is because the corresponding digital input port needs to be enabled. In other words, even if you set a pin to input, you need to also enable the inputs for that ''port''. Each port is mapped to eight pins. Because pin D4 is part of the first eight pins, you should enable port zero in the "enable digital input ports" pane.
 +
* Now -->
 +
When you press the button, you should see the digital pin 4 reading change in the "digital input readings" pane.
 +
<!--  * In the ArduinoLab patch, press the "sensing buttons in software" button to preset the outputs properly. The patch expects you to connect the switch to digital pin 4 (D4).  -->
 +
 
 +
 
 +
=== Fading LED (Optional) ===
 +
What about those "breathing" LEDs on Mac Powerbooks? The fading from bright to dim and back is done using pulse-width modulation (PWM). In essence, the LED is toggled on and off rapidly, say 1000 times a second, faster than your eye can follow. The percentage of time the LED is on (the duty) controls the perceived brightness. To control an LED using PWM, you'll have to connect it to one of the pins that support PWM output - 3,5,6, 9, 10 or 11 on the Arduino. Then write a patch that cycles the PWM values.
 +
 
 +
For instance, for a single LED connected to pin D9:
 +
* Connect the LED to pin D9 of the Arduino using the usual circuit.
 +
* In ''ArduinoLab.pd'', set the mode of digital pin ''9'' to ''pwm.''
 +
* In the pane for ''pwm/servo outputs,'' choose pin 9.
 +
* Pull the ''analog_output'' horizontal slider back and forth and watch the LED get brighter and dimmer.
 +
* Fun extension: Get an RGB (red/green/blue) LED, connect it to digital pins 9, 10, and 11, and adjust the brightness of the three color components to cycle the LED through a rainbow of colors '''(see very end of this page)'''!
 +
 
 +
<!--
 +
* In the ArduinoLab patch, press the "Fading LEDs" button to preset the outputs properly. The patch expects you to connect the LED to digital pins 9-11 (D9-11).
 +
* In your Arduino Kit, you have a RGB LED which has four leads (it's white when not lit); it's basically like 3 LEDs sharing the same ground. Use PWM and this [[http://blog.ncode.ca/?p=38 pin out information]] to make the LED cycle through a rainbow of colors.    -->
 +
[[Image: Fading led.png|500px]]
 +
 
 +
 
 +
 
  
  

Revision as of 19:37, 26 February 2014

This lab most recently revamped by Edgar Berdahl and Wendy Ju. Chris Carlson, possibly Bill Verplank, and others have likely contributed.

For this lab you need your Satellite CCRMA kit, a laptop computer with Ethernet adaptor to program it, and some headphones with a mini 1/8" (2.54mm) stereo jack.

You are also invited to bring the following optional items, but they are by no means required:

  • Some of your favorite breadboardable sensors and LEDs.
  • Your powered loudspeaker.



Power Up and Prepare Arduino

  • Use the same procedure as described before to power up Satellite CCRMA and login as the user ccrma with the password temppwd.
  • As always start by running the following command to stop the default pd patch from running

stop-default

  • Then start pd by executing

pd &



More Analog Circuits

Now we will work with the continuous input values provided by analog sensors - potentiometers, accelerometers, distance rangers, etc

Voltage Divider Circuit

  • Open the pd patch ~/pd/labs-Music-250a-2012/lab2/
  • Read the messages in the pd window and make sure that it was able to open the device on /dev/ttyUSB0 (this is the Arduino -- it's showing up as the 0th serial device).
  • Enable the input for analog in 0 by checking the appropriate checkbox.
  • Experiment further by connecting some additional analog sensors to pins such as a0, a1, a2, a3, a4, etc. The sensors you choose should be completely new to you. Start making an entirely new musical interaction using the sensors. Some sensing circuits are shown below. For more ideas, see the sensors lecture or ask.
  • potentiometer:

Pot.png

  • force-sensitive resistor (FSR):

FSR.png

Try both circuits. Test the resistance range of your sensor. If you want 2.5 volts to be the middle, make the comparison resistor (33k in the diagram) the "average" value of the FSR's resistance. Test this with a multimeter.

  • Bend Sensor

Bend sensor.png




First Digital Circuits

Build the Button and LED Circuit

We'll start our digital tutorial with three simple light circuits.

  • In the first one, the LED is on all the time.
  • In the second, the LED only lights up when a button is pressed and a circuit is completed.
  • In the third example, we'll replace the manual switch with an Arduino pin (set to output mode), so we can control the LED from our program.

1a schem.png 1b schem.png 1c schem.png


Power an LED all the time

1a.png

Build the following circuit on your breadboard. Use a 220 Ohm resistor (red red brown gold -- see this page about resistor color codes).

Because the LED is a diode, it has a set voltage drop across the leads; exceeding this causes heat to build up and the LED to fail prematurely. So! It is generally important to have a resistor in series with the LED.

Also, another consequence of the LED being a diode is that it has directionality. The longer lead, the anode, should be connected towards power (THE LONG ARM REACHES FOR POWER!!!); the shorter, cathode, should be connected towards ground. (In the diagram, the longer lead has a bent "knee.")


Make a light switch

Next, we'll insert a switch into the circuit. The momentary switches in your kit are "normal open", meaning that the circuit is interrupted in the idle state, when the switch is not pressed. Pressing the switch closes the circuit until you let go again.

1b.png

Use a multimeter to see what happens to the voltage on either side of the LED when you press the switch.


Toggling LED with Pd

In the third example, we'll replace the manual switch with an Arduino pin (set to output mode), so we can control the LED from our program. The safe way to do this is to let the Arduino pin sink current - if we toggle the pin low, it acts as ground and current flows through the resistor and the LED as it did in the previous examples. When we take the pin high, to 5V, there is no potential difference and no current flows - the LED stays off.

  • Open the lab patch ~/pd/labs-Music-250a-2012/lab2/arduinoLab.pd
  • To get pd to connect properly to the Arduino Nano, you need to correctly select the serial port # (see the upper left-hand portion of the patch). This functionality allows you to address multiple Arduino Nano's connected at the same time. Right now we only have one. Try selecting different serial port #'s until you see a message like "[comport] opened serial line device 4 (/dev/ttyUSB0)."
  • Verify now that communication with the Arduino Nano is working by using digital pin 13, which is wired directly to an LED on the top of the Arduino. First, set the mode of pin 13 to output. Then open up the subpatch for sending digital outs by clicking on pd sending digital outs. Then using the checkbox 13 inside that subpatch, you should be able to turn the LED on pin 13 on and off.
  • Go back to the main patch and set pin 2 to output.
  • Then go back to the "digital outputs" pane. When you click on the checkbox for digital pin 2, the state of your added LED should change (on vs. off).


Optional: Try changing your patch so the light stays on when you press the mouse button, and stays off when you press it again. After that, change your patch so the light blinks on/off. Then, have your patch button switch the light between on and blinking.

1c.png


Sensing buttons in software

We've used code to trigger output - what about the other direction, sensing physical input in code? Just as easy. Here is a simple switch circuit:

Button1 schem.png Button1.png

When the switch is open, the Arduino pin (set to input mode) is pulled to 5V - in software, we'll read 1. When the switch is closed, the voltage at the Arduino pin falls to 0V - in software, we'll read 0. The pull-up resistor is used to limit the current going through the circuit. In software, we can check the value of the pin and switch between graphics accordingly.

  • Setup the circuit and connect it to digital pin 4 (D4).
  • Set the mode of pin 4 to input.
  • Then look at the "digital input readings" pane while you press the button.

When you press the button, you should see the digital pin 4 reading change in the "digital input readings" pane.


Fading LED (Optional)

What about those "breathing" LEDs on Mac Powerbooks? The fading from bright to dim and back is done using pulse-width modulation (PWM). In essence, the LED is toggled on and off rapidly, say 1000 times a second, faster than your eye can follow. The percentage of time the LED is on (the duty) controls the perceived brightness. To control an LED using PWM, you'll have to connect it to one of the pins that support PWM output - 3,5,6, 9, 10 or 11 on the Arduino. Then write a patch that cycles the PWM values.

For instance, for a single LED connected to pin D9:

  • Connect the LED to pin D9 of the Arduino using the usual circuit.
  • In ArduinoLab.pd, set the mode of digital pin 9 to pwm.
  • In the pane for pwm/servo outputs, choose pin 9.
  • Pull the analog_output horizontal slider back and forth and watch the LED get brighter and dimmer.
  • Fun extension: Get an RGB (red/green/blue) LED, connect it to digital pins 9, 10, and 11, and adjust the brightness of the three color components to cycle the LED through a rainbow of colors (see very end of this page)!

Fading led.png






Putting it all Together

  • Create a patch to make sounds based on button and sensor values from the Arduino. You can try to adapt your patches from Lab 1, or come up with a new patch. Try to make the sound good!
  • Make a simple musical interaction. Think about music -
    • does it have dynamics?
    • can you turn the sound off?
    • can it be expressive?



NMC 2013