Spooky Projects
Spooky Projects
Introduction to
Introduction to Microc
Microcontrollers with Ar
ontrollers with Arduino
duino
Class 4
Class 4
28 O
W
Wh
haatt’’s
s Fo
For
r T
To
od
daayy
••
Switches without Resistors
Switches without Resistors
••
All about piezos
All about piezos
••
Building a melody player
Building a melody player
••
Using piezos as pressure & knock sensors
Using piezos as pressure & knock sensors
••
Using
Using Processing
Processing with
with Arduino
Arduino
Recap: Programming
Recap: Programming
Reset
Reset
E
Ed
diitt
C
Co
om
mp
piillee
Upload
Upload
LikeSwitches w/o Resistors
Switches w/o Resistors
A
AVR c
VR chip ha
hip has in
s internal
ternal “pull-up”
“pull-up” resistor
resistorss
to input to input
IIn
nsstteeaad
d o
of
f tth
hiiss::
Y
Yo
ou
u ccaan
n jju
usst
t d
do
o tth
hiiss::
+5V +5V to input to input 10k 10k
But how do you turn on these internal pull-ups?
But how do you turn on these internal pull-ups?
This is sort of an aside, but it saves a
Switches w/o Resistors
Switches w/o Resistors
Arduino Arduino board board pin 7 pin 7 gnd gnd pin 6 pin 6 pin 5 pin 5 A A BB CC
Answer: use
Answer: use
digitalWrite(pin,HIGH)
digitalWrite(pin,HIGH)
on the input
on the input
Seems a little counter-intuitive,
Seems a little counter-intuitive,
think of it as setting the default value of the input
think of it as setting the default value of the input
but note, it doesn’t work the other way: you can’t set it to LOW then wire the switch to +5V. but note, it doesn’t work the other way: you can’t set it to LOW then wire the switch to +5V.
Switches w/o Resistors
Switches w/o Resistors
Can make a button box easily
Can make a button box easily
if no resistors are needed
if no resistors are needed
Plugs right into Arduino board
Plugs right into Arduino board
A
A B B CC
=
=
Piezoelectrics
Piezoelectrics
••
Big word –
Big word –
piezein
piezein
is
is greek for
greek for “squeeze”
“squeeze”
••
Some c
Some crrystals,
ystals, when sque
when squeezed,
ezed, make a spa
make a spark
rk
••
Turns out the process goes the other way too
Turns out the process goes the other way too
••
Spark a quartz crystal, and it flexes
Spark a quartz crystal, and it flexes
••
Piezo buzzers use this to make sound
Piezo buzzers use this to make sound
(flex something
(flex something back and
back and forth,
forth, it moves air)
it moves air)
Piezo buzzers don’t have quartz crystals, but instead a kind of
Piezo buzzers don’t have quartz crystals, but instead a kind of ceramic that also exhibitsceramic that also exhibits piezoelectric properties.
piezoelectric properties. I
Piezo Buzzers
Piezo Buzzers
••
Two wires, red & black.
Two wires, red & black.
Polarity matters: black=ground
Polarity matters: black=ground
••
Apply an oscillating voltage to
Apply an oscillating voltage to
make a noise
make a noise
••
The buzzer case supports the
The buzzer case supports the
piezo element and has
piezo element and has
resonant ca
resonant cavity for so
vity for sound
und
Oscillating voltage alternately squeezes and releases the piezo element. Oscillating voltage alternately squeezes and releases the piezo element. Must apply flucuating voltage, a steady HIGH or LOW won’t work.
Must apply flucuating voltage, a steady HIGH or LOW won’t work. diagrams from:
What’s in a Piezo Buzzer?
What’s in a Piezo Buzzer?
You can get at the piezo
You can get at the piezo
element pretty easily.
element pretty easily.
Be careful not to crack
Be careful not to crack
the white disc that is
the white disc that is
the actual piezo
the actual piezo
Only take it out of its
Only take it out of its
case to use it as a
case to use it as a
sensor
sensor
another $1.99 I won’t be getting back
another $1.99 I won’t be getting back from Radio Shackfrom Radio Shack
Of course, you usually destroy the enclosure to get at the element. Of course, you usually destroy the enclosure to get at the element.
And it’s the enclosure that has the proper support and resonant cavity to make a
Piezo Buzzer
Piezo Buzzer
Arduino Arduino board board pin 7 pin 7 gnd gnd + + – – piezo piezo buzzer buzzerPiezo leads are very thin. The breadboard holes grab them better than the header sockets, which is Piezo leads are very thin. The breadboard holes grab them better than the header sockets, which is why the jumper leads are used.
Pla
Play a
y a Melody
Melody
“sound_serial”
“sound_serial”
Play the piezo beeper
Play the piezo beeper
with the Serial Monitor
with the Serial Monitor
T
Type
ype multiple
multiple letters
letters
from “
from “
cdefgabC
cdefgabC
” to
” to
make melodies
make melodies
This sketch is in
This sketch is in the handout, and is the handout, and is based on “Examples/pwm_sound/based on “Examples/pwm_sound/keyboard_serial”keyboard_serial” Notice the problem with this
Notice the problem with this sketch?sketch? Di
Dif f erent notes play for dierent notes play for dif f erent amounts of time.erent amounts of time.
50 cycles of low C isn’t the same amount of time as 50 cycles of high B 50 cycles of low C isn’t the same amount of time as 50 cycles of high B
Making it Quieter
Making it Quieter
Easiest way: add a resistor
Easiest way: add a resistor
(brown, (brown, black, black, orange) orange) Arduino Arduino board board pin 7 pin 7 gnd gnd + + – – piezo piezo buzzer buzzer 10k 10k
Like most things in electronics, if you want less of
Like most things in electronics, if you want less of something, add a resistor.something, add a resistor. A better value would probably be 1k, but we
A better value would probably be 1k, but we don’t have that on hand.don’t have that on hand. This may not seem important now, but wait for the next project.
Play a Stored Melody
Play a Stored Melody
“play_melody”
“play_melody”
Plays a melody stored
Plays a melody stored
in
in the
the Arduino
Arduino
This is in
This is in the handout, but is also the handout, but is also in “Examples/pwm_sounin “Examples/pwm_sound/play_melodyd/play_melody” (pin changed)” (pin changed) Melody definition is sort of like the
Melody definition is sort of like the old cell ringtone styleold cell ringtone style Melody playing logic is hard to follow.
Make
Make a
a Theremin
Theremin
“ooo-weee-ooooo”
“ooo-weee-ooooo”
The original spooky
The original spooky
sound machine
sound machine
Works by measuring your
Works by measuring your
body’s electric field
body’s electric field
Leon Theremin
Leon Theremin
No touching needed!
No touching needed!
We’ll use light in lieu of RF
We’ll use light in lieu of RF
As heard on Star Trek, Beach Boys, horror movies, Mars
As heard on Star Trek, Beach Boys, horror movies, Mars Attacks!, and bad New Age songs.Attacks!, and bad New Age songs. Works sorta like those touch switches, but no touching here.
Works sorta like those touch switches, but no touching here. That is, your body becomes a variable capacitor.
Make
Make a
a Theremin
Theremin
Take photocell circuit from before, bolt it on
Take photocell circuit from before, bolt it on
(brown, (brown, black, black, orange) orange) Arduino Arduino board board pin 7 pin 7 gnd gnd + + – – piezo piezo buzzer buzzer analog pin 0 analog pin 0 +5V +5V 10k 10k photocell photocell
This is a light-to-sound converter, if you will. This is a light-to-sound converter, if you will.
Make
Make a
a Theremin
Theremin
Move hand over
Move hand over
photocell to
photocell to
change pitch
change pitch
Pla
Play with val
y with val processing & cycles count
processing & cycles count
to alter sensitivity, pitch and timbre
to alter sensitivity, pitch and timbre
“theremin”
“theremin”
This is
This is
frequency modulation,
frequency modulation,
since you’re changing the frequency
since you’re changing the frequency
Okay so maybe it sounds more like a
Okay so maybe it sounds more like a bad video game than a spooky moviebad video game than a spooky movie The glitchy sound is cause because of the time it takes to read the sensor The glitchy sound is cause because of the time it takes to read the sensor There are ways around such stu
There are ways around such stuf f , but requires more , but requires more complex programming using timers &complex programming using timers & interrupts
Piezo Buzzer as Sensor
Piezo Buzzer as Sensor
••
Piezo buzzers exhibit the
Piezo buzzers exhibit the
reverse
reverse
piezoelectric
piezoelectric
effect.
effect.
••
The normal piezoelectric effect is
The normal piezoelectric effect is generating
generating
electricity from sque
electricity from squeezing a cr
ezing a crystal.
ystal.
••
Can get
Can get several thousand
several thousand volts,
volts, makes a spark
makes a spark
••
You probably have seen a big example of this
You probably have seen a big example of this
already:
already:
fireplace lighter
fireplace lighter
I have a demo piezo
I have a demo piezo igniter from one of these lighters. It’s fun to igniter from one of these lighters. It’s fun to shock yourself.shock yourself. Puts out several tho
Piezo Read
Piezo Read
Arduino Arduino board board analog pin 0 analog pin 0 gnd gnd + + – – piezo piezo buzzer buzzer 5.1v 5.1v z zeenneer r 11MM (brown, (brown, black, black, green) green)••
To read a piezo you can
To read a piezo you can
just hook it into an
just hook it into an
analog
analog input,
input, but:
but:
••
You need to drain off
You need to drain off
any voltage with a
any voltage with a
resistor, or it just builds
resistor, or it just builds
up
up
••
You should have a
You should have a
protecti
protection diode
on diode to
to
limit big voltages, else
limit big voltages, else
fry your inputs
fry your inputs
piezo input schematic
piezo input schematic
Note polarity of piezo still
Note polarity of piezo still matters.matters. The protectio
The protection diode is a special kind of diode can diode is a special kind of diode called a “zener diode”. lled a “zener diode”. It acts invisible until theIt acts invisible until the voltage gets over its designed value (5.1 volts in this
Piezo Read
Piezo Read
Arduino Arduino board board analog pin 0 analog pin 0 gnd gnd + + – – piezo piezo buzzer buzzer 5.1v 5.1v z zeenneer r 11MM (brown, (brown, black, black, green) green)Create two little busses for GND
Create two little busses for GND and A0, and hook components across it.and A0, and hook components across it. Black bar on diode indicates “bar” of
Piezo Read
Piezo Read
“piezo_read”
“piezo_read”
Whack the piezo to
Whack the piezo to
generate a number
generate a number
based on force of
based on force of
whack
whack
Waits for input to go over threshold,
Waits for input to go over threshold,
then to drop below
then to drop below threshold
threshold
Number is “t”, the number of times it
How
How Doe
Does
s tha
that
t W
Wor
ork?
k?
v v o o l l t t s s time time piezo output voltage
piezo output voltage
whack! whack!
threshold threshold
tt
••
When a
When a piezo
piezo is st
is struck,
ruck, it
it “rings”
“rings” like a bell
like a bell
••
But instead of sound, it outputs voltage
But instead of sound, it outputs voltage
••
The sketch measures
The sketch measures
time
time
abov
above a cer
e a certain
tain
voltage, hoping to catch largest ring
voltage, hoping to catch largest ring
Depending on how fast you can watch the input, this technique works either really well or not that Depending on how fast you can watch the input, this technique works either really well or not that well.
well. There are mucThere are much faster ways of watchinh faster ways of watching inputs that loops with ang inputs that loops with analogRead()alogRead() But for now it works okay
Custom Piezo Sensors
Custom Piezo Sensors
Can mount the element
Can mount the element on anything
on anything
(floor mat, door, your body, etc.)
(floor mat, door, your body, etc.)
Here’
Take a Break
Take a Break
(see Craft magazine!)
(see Craft magazine!)
Processing
Processing
••
Processing makes Java programming
Processing makes Java programming
as
as fun
fun & e
& easy
asy as
as Arduino
Arduino makes
makes A
AVR
VR
programming
programming
••
Started as
Started as a tool to make generative
a tool to make generative
art
art
••
Is also often used to interface to
Is also often used to interface to
devices
devices like
like Arduino
Arduino
And it’s totally open source like Arduino. And it’s totally open source like Arduino.
Processing GUI and Arduino GUI are from the same code, which is
Using Processing
Using Processing
••
First, install
First, install
Processing
Processing
••
Load up
Load up
“Sketchbook »
“Sketchbook »
Examples »
Examples »
Motion » Bounce”
Motion » Bounce”
••
Press “Run”
Press “Run”
button
button
••
You just made a
You just made a
Java applet
Java applet
The Processing application folders are in
The Processing application folders are in the handout, no installation is the handout, no installation is needed.needed. Also try Examp
Also try Examples » Motion » les » Motion » Collision. Collision. It’s a lot of funIt’s a lot of fun..
Notice how “Run” launches a new window containing the sketch. Notice how “Run” launches a new window containing the sketch. The black area at the bottom is
About Processing
About Processing
••
Processing sketches have very similar structure
Processing sketches have very similar structure
to
to Arduino
Arduino sketches
sketches
••
setup()
setup()
– set up sketch, like size, framerate
– set up sketch, like size, framerate
••
draw()
draw()
– like
– like
loop()
loop()
, called repeatedly
, called repeatedly
Pr
Processi
ocessing
ng &
& Arduino
Arduino
••
Processing
Processing and
and Arduino bot
Arduino both t
h talk
alk to
to “serial”
“serial”
devices
devices like the
like the Arduino b
Arduino board
oard
••
Only one program per serial port
Only one program per serial port
••
So turn off Arduino’s Serial Monitor when connecting
So turn off Arduino’s Serial Monitor when connecting
via Processing and
via Processing and vice-versa.
vice-versa.
••
Proce
Processing has a
ssing has a “Serial” library
“Serial” library to talk
to talk to
to
Arduino. E.g.:
Arduino. E.g.:
serial communications
serial communications
port = new Serial(..,“my_port_name”,9600)
port = new Serial(..,“my_port_name”,9600)
port.read(), port.write(), etc.
port.read(), port.write(), etc.
serialEvent() { }
serialEvent() { }
Using the serial library adds a
Using the serial library adds a new function you can use to your sketch: serialEvent()new function you can use to your sketch: serialEvent() The serialEvent() functio
Processing Serial
Processing Serial
common Processing serial use
common Processing serial use
be sure to set to
be sure to set to
the same as
the same as
“Serial Port” in
“Serial Port” in
Arduino GUI
Arduino GUI
1.
1.
2.
2.
3.
3.
4.
4.
four steps
four steps
1.
1. lo
load l
ad lib
ibra
rary
ry
2.
2. se
set
t po
portn
rtnam
amee
3.
3. op
open
en p
por
ortt
4.
4. re
read
ad/w
/wri
rite po
te port
rt
All you need to do talk
All you need to do talk to Arduino in Processing.to Arduino in Processing. The import statement says you want to do serial stu The import statement says you want to do serial stuf f ..
The “new Serial” creates a serial port object within Processing The “new Serial” creates a serial port object within Processing Then you can that object (or used the passed in one) to read
Pr
Processi
ocessing
ng &
& Arduino
Arduino
“arduino_ball”
“arduino_ball”
Every time a number
Every time a number
is received via the
is received via the
serial port, it draws a
serial port, it draws a
ball that size.
ball that size.
Use “
Use “
piezo_read
piezo_read
””
Arduino sketch from
Arduino sketch from
before
before
This sketch is in the handout. This sketch is in the handout.
Uses “serialEvent()” and “read()” to build up a string
Spookier, Please
Spookier, Please
“arduino_
“arduino_
spookysounds”
spookysounds”
Every time the
Every time the
piezo is
piezo is knocked...
knocked...
a scar
a scary ey
y eye opens
e opens
and a spooky
and a spooky
sound plays
sound plays
piezo val is printed, but not
piezo val is printed, but not
used: just its existance is
used: just its existance is
This sketch is in the handout. This sketch is in the handout.
You can add your own sounds (must be 16-bit WAV or AIFF). You can add your own sounds (must be 16-bit WAV or AIFF). Hook a piezo up to
Hook a piezo up to your front door, and plug your computer into your stereo.your front door, and plug your computer into your stereo. Every time someone knocks on your door, a scary sound is played
Pr
Processin
ocessing
g to
to Arduino
Arduino
“http_rgb_led”
“http_rgb_led”
Fetch a web page,
Fetch a web page,
get a color value
get a color value from
from
it
it
, send the color to
, send the color to
Arduino with RGB LED
Arduino with RGB LED
real quick
real quick
This is not to build, ju
This is not to build, just quickly cover. st quickly cover. It’s not in the hanIt’s not in the handout, but,dout, but, full details at:
Fun Uses
Fun Uses
External Power
External Power
Arduino can run off USB power or external power
Arduino can run off USB power or external power
USB connector
USB connector
External power connector
External power connector
jumper switch to
External Power
External Power
Y
You
ou can
can use
use an
an A
AC
C adpa
adpater
ter
Make sure it’s
Make sure it’s
“center positive”
“center positive”
Connector is
Connector is
standard barrel
standard barrel
connector
connector
Voltage can be
Voltage can be
9-15
9-15 V
V DC
DC
Amps is > 200mA
Amps is > 200mA
center
center
positive
positive
Actually input voltage can be from like 7.5V to 35V, but don’t go over 15V so the voltage regulator Actually input voltage can be from like 7.5V to 35V, but don’t go over 15V so the voltage regulator doesn’t have to work so hard.
External Power
External Power
Or you can use a battery
Or you can use a battery
Be car
Be careful ab
eful about polar
out polarity!
ity! And shorts!
And shorts!
On the prototyping shield you plug in on top, the “9V” socket is
External Power
External Power
also solves polarity concerns
also solves polarity concerns
An easier wa
An easier way to connect a
y to connect a battery
battery
Power connector input has
Power connector input has protection diode.protection diode. Also it’s easier
External Power
External Power
Battery life
Battery life
How long does
How long does Arduino last on 9V
Arduino last on 9V battery?
battery?
••
Arduino board draws about 40 mA by itself
Arduino board draws about 40 mA by itself
••
Each LED adds about 20mA when on
Each LED adds about 20mA when on
••
Each servo maybe 100 mA when running
Each servo maybe 100 mA when running
••
Switches,
Switches, ots,
ots, etc.
etc. are
are effectiv
effectivel
el zer
zero
o
••
Battery
Battery capacity rated in milliamp-hours (mAh)
capacity rated in milliamp-hours (mAh)
••
9V batteries have about 400 mAh capacity
9V batteries have about 400 mAh capacity
Thus,
Thus, Arduino by
Arduino by itself lasts 400/40 = 10
itself lasts 400/40 = 10 hours
hours
Take all your power, add it
Take all your power, add it up, divide it into your battery capacity to get up, divide it into your battery capacity to get time in hours.time in hours.
There are techniques to make an AVR chip go into sleep mode, and draw microamps (1/1000 mA), There are techniques to make an AVR chip go into sleep mode, and draw microamps (1/1000 mA), but those techniques don’t have nice Arduino-style wrappers yet.
but those techniques don’t have nice Arduino-style wrappers yet. For more on batteries and their capacities:
Summary
Summary
Y
You’v
ou’ve lea
e learned
rned many different
many different ph
physical
ysical building
building blocks
blocks
LEDs
LEDs
switches/buttons
switches/buttons
resistive sensors
resistive sensors
servos
servos
fast
fast
prototyping
prototyping
piezos
piezos
Summary
Summary
And you’ve learned many software building blocks
And you’ve learned many software building blocks
pulse width
pulse width
modulation
modulation
serial
serial
communication
communication
digital I/O
digital I/O
analog I/O
analog I/O
data driven
data driven
code
code
modulation
modulation
frequency
frequency
multiple tasks
Summary
Summary
motors
motors
Some things we didn’t cover, like:
Some things we didn’t cover, like:
relays
relays
But they use concepts you know
But they use concepts you know
Arduino Arduino board board pin 7 pin 7 gnd gnd 1k 1k TIP120 TIP120 M M 1N40041N4004 +5V +5V DC motor DC motor Arduino Arduino board board pin 7 pin 7 gnd gnd 1k 1k TIP120 TIP120 1N4004 1N4004 +5V +5V to load to load 5V relay 5V relay
Summary
Summary
Hope you had fun and learned something
Hope you had fun and learned something
Feel fr
Tod E. Kurt
Tod E. Kurt
tod@todbot.com
tod@todbot.com
END Class 4
END Class 4
http://todbot.com/blog/spookyarduino
http://todbot.com/blog/spookyarduino
A little extra: MIDI
A little extra: MIDI
Arduino Arduino board board pin 7 pin 7 gnd gnd pin 6 pin 6 pin 5 pin 5 A A BB CC +5V +5V pin 1 pin 1 1 1 3 3 4 4 5 5 2 2 220220 MIDI jack MIDI jack analog 0 analog 0 + + – – piezo piezo buzzer buzzer 5.1v 5.1v zener zener 1M1M