cmsc838 week 01 | lecture 02 | jan 29, 2015 arduino ... · today’s learning goals experimenting...

106
Human Computer Interaction Laboratory @jonfroehlich Assistant Professor Computer Science CMSC838 Tangible Interactive Computing Week 01 | Lecture 02 | Jan 29, 2015 Arduino, Sensing, and Processing

Upload: others

Post on 21-May-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

Human Computer Interaction Laboratory

@jonfroehlich Assistant Professor Computer Science

CMSC838 Tangible Interactive Computing

Week 01 | Lecture 02 | Jan 29, 2015

Arduino, Sensing, and Processing

Page 2: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

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.

Page 3: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

Making the invisible, visible.

Page 4: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

First, some inspiration: you can do a lot of creative

things with simple circuits, a microcontroller, and light

Page 5: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

Timo Arnall, Jørn Knutsen, & Einar Martinussen, Immaterials: Light Painting WiFi, https://vimeo.com/20412632

Page 6: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

Timo Arnall, Jørn Knutsen, & Einar Martinussen, Immaterials: Light Painting WiFi

Page 7: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

Timo Arnall, Jørn Knutsen, & Einar Martinussen, Immaterials: Light Painting WiFi

Page 8: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

Timo Arnall, Jørn Knutsen, & Einar Martinussen, Immaterials: Light Painting WiFi

Page 9: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

Kuznetsov et al., Red Balloon, Green Balloon, Sensors in the Sky, UbiComp2011

Page 10: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

Kuznetsov et al., http://www.instructables.com/id/Air-quality-balloons/

air quality sensor + microcontroller + RGB LED + battery

Page 11: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

Kuznetsov et al., Red Balloon, Green Balloon, Sensors in the Sky, UbiComp2011

Page 12: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

Helen Evans and Heiko Hansen, Nuage Vert, http://hehe.org2.free.fr

Page 13: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

Helen Evans and Heiko Hansen, Nuage Vert, https://vimeo.com/17350218#

Page 14: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

power-aware cord

cord light pulsates & varies in intensity based on power draw

[Gustafsson and Gyllenswärd, CHI2005]

Page 15: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

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

Page 16: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

hello world intro arduino example

Page 17: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

OPEN THE ARDUINO IDE

Page 18: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

SELECT THE BOARD YOU ARE USING

Page 19: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

SELECT THE COM PORT You will program your Arduino via the serial port over USB.

Page 20: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

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]

Page 21: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

WRITING THE BLINK PROGRAM Goal: blink pin 13 (hooked up to the ‘L’ LED on the board) once every 1 second.

Page 22: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

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

Page 23: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

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

Page 24: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

THE ENTIRE BLINK PROGRAM

Page 25: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

SOME MODIFICATIONS I paramaterized the code a bit and added my own special prefix to track global

variables.

Page 26: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

[source: https://cdn.sparkfun.com/assets/f/4/9/2/2/Arduino_Cheat_Sheet-11-12-13.pdf]

Page 27: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

hello world 2 blinky with external led

Page 28: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

NOW, LET’S BLINK

OUR OWN LED

Page 29: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

NOW, LET’S BLINK

OUR OWN LED

To do this, we need to

learn about two things:

breadboards and LEDs.

Page 30: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

breadboards

Page 31: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning
Page 32: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning
Page 33: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

BREADBOARDS: LOTS OF VARIETY

[source: sparkfun.com]

Page 34: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

BREADBOARDS: HOW THEY WORK

[source: https://learn.sparkfun.com/tutorials/how-to-use-a-breadboard,]

Page 35: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

[source: https://learn.sparkfun.com/tutorials/how-to-use-a-breadboard,]

wire1

wire2

wire3 wire4

BREADBOARDS: HOW THEY WORK

Page 36: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

[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

Page 37: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

[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

Page 38: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

[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

Page 39: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

[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

Page 40: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

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).

Page 41: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

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.

Page 42: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

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.

Page 43: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

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,]

Page 44: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

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,]

Page 45: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

PERFBOARD USE

[source: https://learn.adafruit.com/collins-lab-breadboards-and-perfboards/transcript,]

Page 46: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

NOW, LET’S BLINK

OUR OWN LED

So, that’s a quick intro

to breadboards. Now

we need to learn

about LEDs.

Page 47: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

Anode

Cathode

light-emitting diodes (LEDs)

Page 48: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

LEDS

LEDs have polarity: the long end is the ‘positive’ lead

[https://learn.adafruit.com/adafruit-arduino-lesson-2-leds/leds]

Anode

Cathode

Page 49: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

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.

Page 50: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

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!

Page 51: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

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

Page 52: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

Current limiting resistor. An absolute must!

+ -

Resistor

Page 53: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

But how do we know what resistor value to select?

Page 54: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

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.

Page 55: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

[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.

Page 56: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

[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:

𝑅 =𝑉𝑠 − 𝑉𝑓

𝑖

Page 57: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

Let’s try an example…

Page 58: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning
Page 59: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning
Page 60: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

All we really need to know has been highlighted here by Sparkfun, but let’s view the datasheet anyway as a learning exercise

Page 61: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

LED

DATASHEET

Page 62: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

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

Page 63: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

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

Page 64: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

[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 Ω

Page 65: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

EASY WAY TO TEST AN LED Just use a coin cell battery (e.g., a 3V) CR2025 or similar

Page 67: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

NOW, LET’S BLINK

OUR OWN LED

So, that’s a quick intro

to breadboards and

LEDs, let’s get back to

our example.

Page 68: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

LET’S HOOKUP OUR CIRCUIT The circuit is on the left and code is on the right

Page 69: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

hello world 3 analogWrite and fading the LED

Page 70: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

ARDUINO CODE: FADE THE LED ON/OFF

Page 71: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

hello world 4 analogRead to control the fade of the LED

Page 72: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

ARDUINO CODE: FADE THE LED BASED ON ANALOGREAD Here, we will fade the LED based on the value of the analog input pin A0

Page 73: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

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!

Page 74: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

POTENTIOMETERS ARE EVERYWHERE! Used to adjust sensitivity, balance, input, output

(especially in audio equipment)

Page 75: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

HUGE VARIETY OF POTENTIOMETERS

Page 76: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

SLIDE POTENTIOMETERS!

[source: https://www.sparkfun.com/products/11620]

Page 77: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

SCHEMATIC SYMBOLS

[source: Platt, Encyclopedia of Electronic Components: Volume 1, 2012]

American Symbol European Symbol

Page 78: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

THIS IS A BREADBOARD FRIENDLY TRIMPOT

[source: https://www.sparkfun.com/products/9806?_ga=1.88313947.1111827570.1420822056]

Page 79: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

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.”

Page 80: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

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

Page 81: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

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).

Page 82: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

POTENTIOMETERS: HOW IT WORKS

A

W

B

[source: Platt, Encyclopedia of Electronic Components: Volume 1, 2012]

Page 83: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

FADE LED WITH A POTENTIOMETER Let’s hookup the circuit first.

Page 84: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

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?

Page 85: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

FADE LED WITH A POTENTIOMETER The circuit is on the left and code is on the right

Page 86: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

We can use Serial.println and the serial monitor to display our values

Super useful for debugging!

Page 87: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

ARDUINO SERIAL MONITOR Go to Tools -> Serial Monitor

Page 88: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

hello world 5 Fading an LED and playing a tone

Page 89: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

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]

Page 90: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

FADE LED AND PLAY TONE The circuit is on the left and code is on the right

Page 91: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

hello world 6 interacting with processing

Page 92: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

Open Processing and copy/paste the code from here:

http://arduino.cc/en/Tutorial/Graph

Page 93: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning
Page 94: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

PROCESSING ARDUINO GRAPH

Page 95: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

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.

Page 96: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

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

Page 97: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

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

Page 98: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

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!

Page 99: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

Can you modify the code so that the graph color is based on the analog value?

Page 100: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

PROCESSING ARDUINO GRAPH Let’s modify this graph so that the color is based on the input

Page 101: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

SCROLLING, RESIZABLE ARDUINO GRAPH You will need to make your graph resizable for IA2

Page 102: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

Can you draw and move a box across the screen based on the analog value?

Page 103: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

MY SILLY DRAW RECTANGLE EXAMPLE

Page 104: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

Photocell; $1.50 Touch Membrane

Potentiometer; $12.95

Trimpot 10k; $0.95 Flexiforce Pressure Sensor

(1lb); $19.95

MY IA2 ARDUINO GRAPH DEMO

Page 105: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning

Playtime

Page 106: CMSC838 Week 01 | Lecture 02 | Jan 29, 2015 Arduino ... · TODAY’S LEARNING GOALS experimenting with concepts, tools, and components to help you learn and reinforce your learning