cmsc838 week 01 | lecture 02 | jan 29, 2015 arduino ... · today’s learning goals experimenting...
TRANSCRIPT
Human Computer Interaction Laboratory
@jonfroehlich Assistant Professor Computer Science
CMSC838 Tangible Interactive Computing
Week 01 | Lecture 02 | Jan 29, 2015
Arduino, Sensing, and Processing
TODAY’S LEARNING GOALS
1. An overview of the course, course logistics, ‘Maker’ culture, the
Hackerspace, and assignments (especially IA2 & MP1)
2. Arduino basics such as digitalWrite, digitalRead, analogWrite, and
analogRead
3. Light-emitting diode (LED) basics including: LEDs only allow current flow
in one direction and that they require current limiting resistors.
4. How to read an LED datasheet and calculate the value of the resistor
5. How to use basic input components like trimpots and basic output
components like a speaker
6. How to use the Serial println functionality in Arduino to communicate
with your computer and build a simple interactive application in
Processing
You’ll get a taste of a lot of things today, some of which we will go over again ourselves; however, I
encourage you to grab components from the HCIL Hackerspace and begin playing and
experimenting with concepts, tools, and components to help you learn and reinforce your learning.
Making the invisible, visible.
First, some inspiration: you can do a lot of creative
things with simple circuits, a microcontroller, and light
Timo Arnall, Jørn Knutsen, & Einar Martinussen, Immaterials: Light Painting WiFi, https://vimeo.com/20412632
Timo Arnall, Jørn Knutsen, & Einar Martinussen, Immaterials: Light Painting WiFi
Timo Arnall, Jørn Knutsen, & Einar Martinussen, Immaterials: Light Painting WiFi
Timo Arnall, Jørn Knutsen, & Einar Martinussen, Immaterials: Light Painting WiFi
Kuznetsov et al., Red Balloon, Green Balloon, Sensors in the Sky, UbiComp2011
Kuznetsov et al., http://www.instructables.com/id/Air-quality-balloons/
air quality sensor + microcontroller + RGB LED + battery
Kuznetsov et al., Red Balloon, Green Balloon, Sensors in the Sky, UbiComp2011
Helen Evans and Heiko Hansen, Nuage Vert, http://hehe.org2.free.fr
Helen Evans and Heiko Hansen, Nuage Vert, https://vimeo.com/17350218#
power-aware cord
cord light pulsates & varies in intensity based on power draw
[Gustafsson and Gyllenswärd, CHI2005]
Arroyo et al., Waterbot: exploring feedback and persuasive techniques at the sink, CHI’05
HEATSINK thermistor senses water temperature , which feeds into a microcontroller that controls LEDs mounted around faucet
hello world intro arduino example
OPEN THE ARDUINO IDE
SELECT THE BOARD YOU ARE USING
SELECT THE COM PORT You will program your Arduino via the serial port over USB.
NOW, WE’RE READY: LET’S BLINK AN LED Arduinos typically have a single LED—often called the ‘L’ LED as this is how it’s labeled
on your board--that you can control from your sketches. The location of the ‘L’ LED is
circled below on the UNO and Leonardo. On the UNO and Leonardo, we can control
this LED via D13 (digital pin 13).
[source: https://learn.adafruit.com/adafruit-arduino-lesson-1-blink/the-l-led]
WRITING THE BLINK PROGRAM Goal: blink pin 13 (hooked up to the ‘L’ LED on the board) once every 1 second.
WRITING THE BLINK PROGRAM Goal: blink pin 13 (hooked up to the ‘L’ LED on the board) once every 1 second.
The ‘checkmark’ compiles and verifies the program
WRITING THE BLINK PROGRAM Goal: blink pin 13 (hooked up to the ‘L’ LED on the board) once every 1 second.
The ‘checkmark’ compiles and verifies the program
The ‘right arrow’ sends the program to your Arduino
THE ENTIRE BLINK PROGRAM
SOME MODIFICATIONS I paramaterized the code a bit and added my own special prefix to track global
variables.
[source: https://cdn.sparkfun.com/assets/f/4/9/2/2/Arduino_Cheat_Sheet-11-12-13.pdf]
hello world 2 blinky with external led
NOW, LET’S BLINK
OUR OWN LED
NOW, LET’S BLINK
OUR OWN LED
To do this, we need to
learn about two things:
breadboards and LEDs.
breadboards
BREADBOARDS: LOTS OF VARIETY
[source: sparkfun.com]
BREADBOARDS: HOW THEY WORK
[source: https://learn.sparkfun.com/tutorials/how-to-use-a-breadboard,]
[source: https://learn.sparkfun.com/tutorials/how-to-use-a-breadboard,]
wire1
wire2
wire3 wire4
BREADBOARDS: HOW THEY WORK
[source: https://learn.sparkfun.com/tutorials/how-to-use-a-breadboard,]
wire1
wire2
wire3 wire4
wire5
wire6
wire7
wire8
wire9
wire10
wire15
wire16
wire17
wire18
wire19
wire20
wire11
wire12
wire13
wire14
wire25
wire26
wire27
wire28
wire29
wire30
wire21
wire22
wire23
wire24
wire31
wire32
wire33
wire34
BREADBOARDS: HOW THEY WORK
[source: https://learn.sparkfun.com/tutorials/how-to-use-a-breadboard,]
wire1
wire2
wire3 wire4
wire5
wire6
wire7
wire8
wire9
wire10
wire15
wire16
wire17
wire18
wire19
wire20
wire11
wire12
wire13
wire14
wire25
wire26
wire27
wire28
wire29
wire30
wire21
wire22
wire23
wire24
wire31
wire32
wire33
wire34
wir
e64
wir
e63
wir
e62
wir
e61
wir
e60
wir
e59
wir
e54
wir
e53
wir
e52
wir
e51
wir
e50
wir
e49
wir
e58
wir
e57
wir
e56
wir
e55
wir
e44
wir
e43
wir
e42
wir
e41
wir
e40
wir
e39
wir
e48
wir
e47
wir
e46
wir
e45
wir
e38
wir
e37
wir
e36
wir
e35
BREADBOARDS: HOW THEY WORK
[source: https://learn.adafruit.com/assets/17394, https://learn.sparkfun.com/tutorials/how-to-use-a-breadboard,]
Breadboard now flipped over (you’re looking at the back with the insulated backing peeled away)
BREADBOARDS: A LOOK INSIDE
[source: https://learn.adafruit.com/assets/17394, https://learn.sparkfun.com/tutorials/how-to-use-a-breadboard,]
A single strip of conductive metal from breadboard column
BREADBOARDS: A LOOK INSIDE
BREADBOARDS: A QUICK TIP
[source: https://learn.adafruit.com/assets/17394, https://learn.sparkfun.com/tutorials/how-to-use-a-breadboard,]
Two jumper wires used to connect the power rails on both sides. Always attach the ‘+’ to ‘+’ and the ‘-’ to ‘-’. I also like to use ‘black’ wire for ‘-’ and ‘red’ wire for ‘+’.
The power rails on either side are not connected, so if you want the same power source on both
sides, you will need to connect the two sides with some jumper wires. Keep in mind that the
markings are there just as a reference (you need not follow this convention if you have good
reason not to).
DEFENSIVE BREADBOARDING
[source: https://learn.adafruit.com/assets/17394, https://learn.sparkfun.com/tutorials/how-to-use-a-breadboard,]
Notice how the 5V and GND rails are only connected on opposite sides of the breadboard. This is to limit stupid mistakes!
Some ‘Makers’ choose to only connect power on one side of the breadboard and
ground on the other to reduce the chance of accidentally plugging a wire into the
wrong socket and shorting out a circuit.
BREADBOARDS: LIKE LEGOS!
[source: sparkfun.com]
Many breadboards have little nubbins and slots on the sides, and some even have
them on the tops and bottoms. These allow you to connect multiple breadboards
together to form the ultimate prototyping surface.
BREADBOARDS VS. PERFBOARDS
!=
Sometimes novice ‘Makers’ mistake breadboards for perfboards—but they are
completely different (though both allow you to hook up circuits in a semi-organized
fashion).
Breadboard Perfboard
Breadboards are great for experimenting, and testing a circuit - but once you’re happy with how it works, you’ll probably want to transfer it to something that makes it a bit more permanent. In that case you can transfer your circuit to a perfboard. Perfboards require soldering.
[source: https://learn.adafruit.com/collins-lab-breadboards-and-perfboards/transcript,]
BREADBOARDS VS. PERFBOARDS Some perfboards have the same connections and hole spacings as a breadboard
[source: https://learn.adafruit.com/collins-lab-breadboards-and-perfboards/transcript,]
PERFBOARD USE
[source: https://learn.adafruit.com/collins-lab-breadboards-and-perfboards/transcript,]
NOW, LET’S BLINK
OUR OWN LED
So, that’s a quick intro
to breadboards. Now
we need to learn
about LEDs.
Anode
Cathode
light-emitting diodes (LEDs)
LEDS
LEDs have polarity: the long end is the ‘positive’ lead
[https://learn.adafruit.com/adafruit-arduino-lesson-2-leds/leds]
Anode
Cathode
LEDS
LEDs have polarity: the long end is the ‘positive’ lead
[https://learn.adafruit.com/adafruit-arduino-lesson-2-leds/leds; Platt, Encyclopedia of Electronic Components]
Anode
Cathode
Helpful mnemonic: the long side is ‘+’ and the negative side is ‘-’, so consider that the plus sign would be twice as long as the minus sign if its horizontal and vertical stroke were dissembled and placed end to end.
LEDS
LEDs have polarity: the long end is the ‘positive’ lead
[https://learn.adafruit.com/adafruit-arduino-lesson-2-leds/leds]
Anode
Cathode
You cannot connect an LED directly to a battery or voltage source. You must include a resistor to limit the current flowing through the LED, otherwise the LED could burn out!
LEDS
LEDs have polarity: the long end is the ‘positive’ lead
[https://learn.adafruit.com/adafruit-arduino-lesson-2-leds/leds]
Anode
Cathode + -
Resistor
Long end of LED (anode) connected towards positive supply voltage
Current limiting resistor to protect LED
Current limiting resistor. An absolute must!
+ -
Resistor
But how do we know what resistor value to select?
TRIAL AND ERROR!
[source: https://learn.sparkfun.com/tutorials/light-emitting-diodes-leds]
Makers know that their LEDs require
current limiting resistors but they are often
not so exact in selecting the theoretically
optimal resistor. Instead, they pick a likely
resistor value (e.g., 330Ω) and check the
resulting brightness of their LEDs and
iterate. See flow chart.
Honestly, the easiest way to do this is via a
variable resistor like a trimpot where you
can adjust the resistance, find a good
value, and then replace the trimpot with a
permanent resistor of that value.
This works fairly well in practice but it’s
important that you understand how to
select an appropriate resistor as it involves
key circuit skills like reading a datasheet
and applying Ohm’s law.
[source: https://learn.sparkfun.com/tutorials/light-emitting-diodes-leds; https://www.sparkfun.com/quiz/60]
SELECTING A CURRENT LIMITING RESISTOR
+
-
Resi
sto
r
“Pseudo” schematic “Real” schematic
Though we’ve been using the schematic representation on the left,
the “real” electronics schematic representation is on the right. Both
diagrams represent the same circuit.
[source: https://learn.sparkfun.com/tutorials/light-emitting-diodes-leds; https://www.sparkfun.com/quiz/60]
SELECTING A CURRENT LIMITING RESISTOR
Using the circuit to the left, we need to
know three values in order to determine
the current limiting resistor value:
1. i : LED forward current in Amps (found
in the LED datasheet)
2. Vf : LED forward voltage drop in Volts
(found in the LED datasheet)
3. Vs : supply voltage
Once you have obtained these three values, you can use this equation
derived from Ohm’s Law to find the value, in Ω, of the resistor:
𝑅 =𝑉𝑠 − 𝑉𝑓
𝑖
Let’s try an example…
All we really need to know has been highlighted here by Sparkfun, but let’s view the datasheet anyway as a learning exercise
LED
DATASHEET
Forward Current: how much current your LED will be able to handle continuously. In this case, 20mA.
Peak Forward Current: how much current the LED can sustain in short bursts. In this case, 30mA.
Suggestion Using Current: not all data sheets provide this, but this company recommends operating the LED at 16-18mA for best performance
Reverse Voltage: The maximum reverse voltage that the LED can sustain without damage (pay attention to this value if you often accidentally connect your LED anode to GND and cathode to VSS)
Power Dissipation: The amount of power in milliWatts that the LED can use before taking damage
[source: https://learn.sparkfun.com/tutorials/light-emitting-diodes-leds]
READING AN LED DATASHEET https://www.sparkfun.com/datasheets/Components/LED/COM-09590-YSL-R531R3D-D2.pdf
Forward Voltage: The forward voltage is the voltage drop across the LED. You must reach this threshold to turn ‘on’ the LED. In this case, 1.8V to 2.2V
Wavelength: A precise way of explaining the color of the LED, in this case its 620-625nm
LED Brightness: A measure of how bright the LED can get. The unit, millicandela (mcd), is standard for measuring the intensity of a light source. This LED has a luminous intensity of 150-200, which would make for a good indicator light (super bright LEDs can have 160,000-180,000mcd).
Viewing Angle: Different style LEDs will incorporate different types of lenses, reflectors, or diffusers to concentrate light or spread it widely.
[source: https://learn.sparkfun.com/tutorials/light-emitting-diodes-leds; https://www.sparkfun.com/quiz/60]
READING AN LED DATASHEET https://www.sparkfun.com/datasheets/Components/LED/COM-09590-YSL-R531R3D-D2.pdf
[source: https://learn.sparkfun.com/tutorials/light-emitting-diodes-leds; https://www.sparkfun.com/quiz/60]
Using the circuit to the left, we need to
know three values in order to determine
the current limiting resistor value:
1. i : LED forward current in Amps. In this
case, it’s 20mA.
2. Vf : LED forward voltage drop in Volts
(found in the LED datasheet). In this
case, it’s 1.8-2.2V, so let’s say 2V.
3. Vs : supply voltage. In this case, let’s
say 5V (which is a common Arduino
value)
Once you have obtained these three values, you can use this equation
derived from Ohm’s Law to find the value, in Ω, of the resistor:
EXAMPLE LED https://www.sparkfun.com/datasheets/Components/LED/COM-09590-YSL-R531R3D-D2.pdf
𝑅 =𝑉𝑠−𝑉𝑓
𝑖 𝑅 =
5 −2
0.02 = 150 Ω
EASY WAY TO TEST AN LED Just use a coin cell battery (e.g., a 3V) CR2025 or similar
SOME ADDITIONAL LED RESOURCES AddOhms #8 Current Limiting Resistors http://youtu.be/81zNcctopBI
LED Current Limiting Resistors https://www.sparkfun.com/quiz/60
Electric Power https://learn.sparkfun.com/tutorials/electric-power
NOW, LET’S BLINK
OUR OWN LED
So, that’s a quick intro
to breadboards and
LEDs, let’s get back to
our example.
LET’S HOOKUP OUR CIRCUIT The circuit is on the left and code is on the right
hello world 3 analogWrite and fading the LED
ARDUINO CODE: FADE THE LED ON/OFF
hello world 4 analogRead to control the fade of the LED
ARDUINO CODE: FADE THE LED BASED ON ANALOGREAD Here, we will fade the LED based on the value of the analog input pin A0
HOW TO USE YOUR TRIMPOTS! You all have breadboard friendly trimpots, let’s quickly learn a bit about them and
how to use them in our circuits!
POTENTIOMETERS ARE EVERYWHERE! Used to adjust sensitivity, balance, input, output
(especially in audio equipment)
HUGE VARIETY OF POTENTIOMETERS
SLIDE POTENTIOMETERS!
[source: https://www.sparkfun.com/products/11620]
SCHEMATIC SYMBOLS
[source: Platt, Encyclopedia of Electronic Components: Volume 1, 2012]
American Symbol European Symbol
THIS IS A BREADBOARD FRIENDLY TRIMPOT
[source: https://www.sparkfun.com/products/9806?_ga=1.88313947.1111827570.1420822056]
POTENTIOMETERS: HOW IT WORKS Inside the potentiometer is a single resistor and a wiper, which slides along the
resisitive material. Externally, there are three pins: two pins connect to each end of the
resistor while the third connects to the “wiper.”
POTENTIOMETERS: HOW IT WORKS
[source: https://learn.sparkfun.com/tutorials/voltage-dividers/applications]
Inside the potentiometer is a single resistor and a wiper, which slides along the
resisitive material. Externally, there are three pins: two pins connect to each end of the
resistor while the third connects to the “wiper.”
A
W
B
POTENTIOMETERS: HOW IT WORKS
[source: https://learn.sparkfun.com/tutorials/voltage-dividers/applications]
Inside the potentiometer is a single resistor and a wiper, which slides along the
resisitive material. Externally, there are three pins: two pins connect to each end of the
resistor while the third connects to the “wiper.”
A
W
B
If the resistive material in a potentiometer is of constant width and thickness, the electrical potential at the wiper will change in ratio with the rotation of the wiper and shaft (or with movement of a slider).
POTENTIOMETERS: HOW IT WORKS
A
W
B
[source: Platt, Encyclopedia of Electronic Components: Volume 1, 2012]
FADE LED WITH A POTENTIOMETER Let’s hookup the circuit first.
FADE LED WITH A POTENTIOMETER Let’s hookup the circuit first.
How would you write code to read the potentiometer value and then use this value to control the brightness of the LED?
FADE LED WITH A POTENTIOMETER The circuit is on the left and code is on the right
We can use Serial.println and the serial monitor to display our values
Super useful for debugging!
ARDUINO SERIAL MONITOR Go to Tools -> Serial Monitor
hello world 5 Fading an LED and playing a tone
ARDUINO TONE() FUNCTION Generates a square wave of the specified frequency (and 50% duty cycle) on a pin. A
duration can be specified, otherwise the wave continues until a call to noTone(). The
pin can be connected to a piezo buzzer or other speaker to play tones.
Only one tone can be generated at a time. If a tone is already playing on a different
pin, the call to tone() will have no effect. If the tone is playing on the same pin, the call
will set its frequency.
Use of the tone() function will interfere with PWM output on pins 3 and 11 (on boards
other than the Mega).
It is not possible to generate tones lower than 31Hz. For technical details, see Brett
Hagman's notes.
[source http://arduino.cc/en/reference/tone]
FADE LED AND PLAY TONE The circuit is on the left and code is on the right
hello world 6 interacting with processing
Open Processing and copy/paste the code from here:
http://arduino.cc/en/Tutorial/Graph
PROCESSING ARDUINO GRAPH
PROCESSING ARDUINO GRAPH The sketch should run right away with one exception, the highlighted part. Note: the
code below is a slightly modified version of the Arduino Graph with my own variable
names and code formatting
You must change the index in Serial.list()[4] to whatever port your Arduino is plugged into. How do you know? See next slide.
SELECTING THE RIGHT SERIAL PORT You can open up the Arduino IDE and go to Tools -> Port and simply count (starting
from 0) until you get to the COM port that is connected to your Arduino.
index 0 index 1
So, my UNO is on COM6, which will be index 1 in the Serial.list() array
SELECTING THE RIGHT SERIAL PORT The sketch should run right away with one exception, the highlighted part. Note: the
code below is a slightly modified version of the Arduino Graph with my own variable
names and code formatting
Changed to index 1
PROCESSING ARDUINO GRAPH From Serial Monitor to fanciful visualization. Note: you cannot have them both open
at the same time!
If you have the Arduino Serial Monitor open, you will not be able
to read the serial port with your Processing program!
Can you modify the code so that the graph color is based on the analog value?
PROCESSING ARDUINO GRAPH Let’s modify this graph so that the color is based on the input
SCROLLING, RESIZABLE ARDUINO GRAPH You will need to make your graph resizable for IA2
Can you draw and move a box across the screen based on the analog value?
MY SILLY DRAW RECTANGLE EXAMPLE
Photocell; $1.50 Touch Membrane
Potentiometer; $12.95
Trimpot 10k; $0.95 Flexiforce Pressure Sensor
(1lb); $19.95
MY IA2 ARDUINO GRAPH DEMO
Playtime