100% found this document useful (7 votes)
183 views46 pages

Arduino Projects

The document discusses using piezoelectric buzzers and sensors with Arduino microcontrollers. It covers connecting piezo buzzers to play tones and melodies, and using piezos as sensors by reading the voltage they output when struck or pressed. The document provides code examples for playing tones, reading piezo sensors, and building a basic theremin musical instrument using a piezo and light sensor.

Uploaded by

tecnojorge
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (7 votes)
183 views46 pages

Arduino Projects

The document discusses using piezoelectric buzzers and sensors with Arduino microcontrollers. It covers connecting piezo buzzers to play tones and melodies, and using piezos as sensors by reading the voltage they output when struck or pressed. The document provides code examples for playing tones, reading piezo sensors, and building a basic theremin musical instrument using a piezo and light sensor.

Uploaded by

tecnojorge
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
  • Introduction
  • Agenda
  • Recap: Programming
  • Switches without Resistors
  • Piezoelectrics
  • Play a Melody
  • Make a Theremin
  • Piezo Buzzer as Sensor
  • Processing
  • External Power
  • Summary
  • A Little Extra: MIDI

Spooky Projects

Introduction to Microcontrollers with Arduino

Class 4

28 Oct 2006 - machineproject - Tod E. Kurt


What’s For Today
• Switches without Resistors
• All about piezos
• Building a melody player
• Using piezos as pressure & knock sensors
• Using Processing with Arduino
• Stand-alone Arduino
Recap: Programming
Edit Compile

Reset Upload

Like always, just make sure. Make “led_blink” come alive again. Do it. Trust me.
Switches w/o Resistors
AVR chip has internal “pull-up” resistors

Instead of this: You can just do this:


+5V

10k
to input to input

But how do you turn on these internal pull-ups?


This is sort of an aside, but it saves a lot of wiring.
Switches w/o Resistors
Answer: use digitalWrite(pin,HIGH) on the input

Arduino
board

pin 7
pin 6
pin 5
gnd

A B C

Seems a little counter-intuitive,


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.
Switches w/o Resistors
Can make a button box easily
if no resistors are needed

Plugs right into Arduino board

=
A B C
Piezoelectrics

• Big word – piezein is greek for “squeeze”


• Some crystals, when squeezed, make a spark
• Turns out the process goes the other way too
• Spark a quartz crystal, and it flexes
• Piezo buzzers use this to make sound
(flex something back and forth, it moves air)

Piezo buzzers don’t have quartz crystals, but instead a kind of ceramic that also exhibits
piezoelectric properties.
I pronounce it “pie-zoh”. Or sometimes “pee-ay-zoh”.
Piezo Buzzers

• Two wires, red & black.


Polarity matters: black=ground
• Apply an oscillating voltage to
make a noise
• The buzzer case supports the
piezo element and has
resonant cavity for sound

Oscillating voltage alternately squeezes and releases the piezo element.


Must apply flucuating voltage, a steady HIGH or LOW won’t work.

diagrams from: [Link]


What’s in a Piezo Buzzer?

You can get at the piezo


element pretty easily.

Be careful not to crack


the white disc that is
the actual piezo

Only take it out of its


case to use it as a
sensor

another $1.99 I won’t be getting back from Radio Shack


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 loud sound
Piezo Buzzer

piezo
Arduino
buzzer
board
+
pin 7

gnd

Piezo leads are very thin. The breadboard holes grab them better than the header sockets, which is
why the jumper leads are used.
Play a Melody
“sound_serial”

Play the piezo beeper


with the Serial Monitor

Type multiple letters


from “cdefgabC” to
make melodies
This sketch is in the handout, and is based on “Examples/pwm_sound/keyboard_serial”
Notice the problem with this sketch?
Different notes play for different amounts of time.
50 cycles of low C isn’t the same amount of time as 50 cycles of high B
Making it Quieter
Easiest way: add a resistor

Arduino
board
10k
+
pin 7 piezo
(brown, buzzer

black,
orange)
gnd

Like most things in electronics, if you want less of something, add a resistor.
A better value would probably be 1k, but we don’t have that on hand.
This may not seem important now, but wait for the next project.
Play a Stored Melody
“play_melody”

Plays a melody stored


in the Arduino

This is in the handout, but is also in “Examples/pwm_sound/play_melody” (pin changed)


Melody definition is sort of like the old cell ringtone style
Melody playing logic is hard to follow.
Make a Theremin
“ooo-weee-ooooo”

The original spooky


sound machine

Works by measuring your


body’s electric field

No touching needed!
We’ll use light in lieu of RF Leon Theremin
As heard on Star Trek, Beach Boys, horror movies, Mars Attacks!, and bad New Age songs.
Works sorta like those touch switches, but no touching here.
That is, your body becomes a variable capacitor.
Make a Theremin
Take photocell circuit from before, bolt it on

piezo
+5V Arduino
buzzer
board
+
pin 7
photocell
analog pin 0 –
10k gnd
(brown,
black,
orange)

This is a light-to-sound converter, if you will.


Make a Theremin
“theremin”

Move hand over


photocell to
change pitch

Play with val processing & cycles count


to alter sensitivity, pitch and timbre

This is frequency modulation,


since you’re changing the frequency

Okay so maybe it sounds more like a bad video game than a spooky movie
The glitchy sound is cause because of the time it takes to read the sensor
There are ways around such stuff, but requires more complex programming using timers &
interrupts
The sound can get annoying quick
Piezo Buzzer as Sensor
• Piezo buzzers exhibit the reverse piezoelectric
effect.
• The normal piezoelectric effect is generating
electricity from squeezing a crystal.
• Can get several thousand volts, makes a spark
• You probably have seen a big example of this
already:
fireplace lighter

I have a demo piezo igniter from one of these lighters. It’s fun to shock yourself.
Puts out several thousand volts. (ionization voltage of air =~ 30kV/cm)
Piezo Read
• To read a piezo you can
just hook it into an
analog input, but:
Arduino

• You need to drain off


piezo
buzzer
board

any voltage with a +


analog pin 0
resistor, or it just builds – 5.1v
zener 1M
gnd

up (brown,
black,


green)
You should have a
protection diode to
limit big voltages, else piezo input schematic
fry your inputs

Note polarity of piezo still matters.


The protection diode is a special kind of diode called a “zener diode”. It acts invisible until the
voltage gets over its designed value (5.1 volts in this case), then it acts like a short circuit.
Piezo Read

Arduino
piezo board
buzzer

+
analog pin 0
– 5.1v
gnd
zener 1M
(brown,
black,
green)

Create two little busses for GND and A0, and hook components across it.
Black bar on diode indicates “bar” of diode.
Piezo Read
“piezo_read”

Whack the piezo to


generate a number
based on force of
whack

Waits for input to go over threshold,


then to drop below threshold

Number is “t”, the number of times it looped waiting for the value to drop below THRESHOLD/2.
How Does that Work?
• When a piezo is struck, it “rings” like a bell
• But instead of sound, it outputs voltage
• The sketch measures time above a certain
voltage, hoping to catch largest ring
volts

piezo output voltage


threshold

time
t
whack!

Depending on how fast you can watch the input, this technique works either really well or not that
well. There are much faster ways of watching inputs that loops with analogRead()
But for now it works okay
Custom Piezo Sensors
Can mount the element on anything
(floor mat, door, your body, etc.)

Here’s one glued to a larger brass disc for a drum trigger


Take a Break
(see Craft magazine!)
Processing

• Processing makes Java programming


as fun & easy as Arduino makes AVR
programming

• Started as a tool to make generative


art

• Is also often used to interface to


devices like Arduino

And it’s totally open source like Arduino.


Processing GUI and Arduino GUI are from the same code, which is why it looks & acts similar.
Using Processing
• First, install
Processing
• Load up
“Sketchbook »
Examples »
Motion » Bounce”

• Press “Run”
button

• You just made a


Java applet

The Processing application folders are in the handout, no installation is needed.


Also try Examples » Motion » Collision. It’s a lot of fun.
Notice how “Run” launches a new window containing the sketch.
The black area at the bottom is a status window, just like in Arduino.
About Processing

• Processing sketches have very similar structure


to Arduino sketches
• setup() – set up sketch, like size, framerate
• draw() – like loop(), called repeatedly
• Other functions can exist when using libraries
Processing & Arduino
serial communications

• Processing and Arduino both talk to “serial”


devices like the Arduino board
• Only one program per serial port
• So turn off Arduino’s Serial Monitor when connecting
via Processing and vice-versa.

• Processing has a “Serial” library to talk to


Arduino. E.g.:
port = new Serial(..,“my_port_name”,9600)
[Link](), [Link](), etc.
serialEvent() { }
Using the serial library adds a new function you can use to your sketch: serialEvent()
The serialEvent() function will get called whenever serial data is available.
Processing Serial
common Processing serial use

four steps
1.
1. load library
2. set portname be sure to set to
2. the same as
3. open port
4. read/write port “Serial Port” in
3. Arduino GUI

4.

All you need to do talk to Arduino in Processing.


The import statement says you want to do serial stuff.
The “new Serial” creates a serial port object within Processing
Then you can that object (or used the passed in one) to read from in the “serialEvent()” function
Processing & Arduino
“arduino_ball”

Every time a number


is received via the
serial port, it draws a
ball that size.

Use “piezo_read”
Arduino sketch from
before

This sketch is in the handout.


Uses “serialEvent()” and “read()” to build up a string and then parse it into a number with “int()”
Spookier, Please
“arduino_
spookysounds”

Every time the


piezo is knocked...
a scary eye opens
and a spooky
sound plays

piezo val is printed, but not


used: just its existance is

This sketch is in the handout.


You can add your own sounds (must be 16-bit WAV or AIFF).
Hook a piezo up to your front door, and plug your computer into your stereo.
Every time someone knocks on your door, a scary sound is played
Processing to Arduino
real quick
“http_rgb_led”

Fetch a web page,


get a color value from
it, send the color to
Arduino with RGB LED

This is not to build, just quickly cover. It’s not in the handout, but,
full details at: [Link]
Fun Uses
External Power
Arduino can run off USB power or external power
External power connector USB connector

voltage
regulator

jumper switch to choose power source


External Power
You can use an AC adpater

Connector is
standard barrel
connector

Make sure it’s


“center positive”

Voltage can be
9-15 V DC center
positive
Amps is > 200mA
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
Or you can use a battery

Be careful about polarity! And shorts!


On the prototyping shield you plug in on top, the “9V” socket is called “raw”
External Power
An easier way to connect a battery

also solves polarity concerns


Power connector input has protection diode.
Also it’s easier with the connector
External Power
Battery life
How long does Arduino last on 9V battery?

• Arduino board draws about 40 mA by itself


• Each LED adds about 20mA when on
• Each servo maybe 100 mA when running
• Switches, pots, etc. are effectively zero

• Battery capacity rated in milliamp-hours (mAh)


• 9V batteries have about 400 mAh capacity

Thus, Arduino by itself lasts 400/40 = 10 hours


Take all your power, add it up, divide it into your battery capacity to get time in hours.
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.
For more on batteries and their capacities: [Link]
Summary
You’ve learned many different physical building blocks

resistive sensors
switches/buttons

LEDs

piezos
fast
prototyping
servos
Summary
And you’ve learned many software building blocks

serial
communication
pulse width
modulation
analog I/O
digital I/O

data driven frequency


code modulation
multiple tasks
Summary
Some things we didn’t cover, like:

+5V +5V
1N4004 1N4004
DC motor M 5V relay
Arduino Arduino
board board
1k 1k
pin 7 pin 7 to load

TIP120 TIP120
gnd gnd

motors relays

But they use concepts you know


Summary

Hope you had fun and learned something

Feel free to contact me to chat about this stuff


END Class 4

[Link]

Tod E. Kurt
tod@[Link]
A little extra: MIDI
Combine everything, add a MIDI jack

Arduino
board
piezo pin 7
buzzer pin 6
pin 5
+ analog 0
pin 1
– 5.1v gnd
zener 1M A B C

MIDI jack
+5V
3 1
5 4 220
2
A little extra: MIDI
A little extra: MIDI

sends MIDI note-on & note-off messages

MIDI is just serial at 31250 baud


buttons are drum triggers
end

Common questions

Powered by AI

Fundamentally, piezoelectric elements convert mechanical stress (like squeezing or flexing) into electrical charges and vice versa. In piezo buzzers, an oscillating voltage causes the piezo element, typically a ceramic disc, to flex and produce sound. Conversely, when used as a sensor, the element generates a voltage when struck or stressed, which can be read by an Arduino. The polarity of connections is crucial for proper operation in both applications .

Both Arduino and Processing sketches share a structural similarity, employing setup() and a repeatedly called function, draw() in Processing and loop() in Arduino. In setup(), the initial configurations (like size, framerate in Processing, and pinMode in Arduino) are specified. Both environments support additional functions and library usage, facilitating code organization and interaction with hardware and visual elements. This similarity simplifies transitioning between coding for physical hardware and graphical software environments .

To configure an AVR microcontroller's input pins on an Arduino to work without external resistors for switches, you use the internal pull-up resistors available on the chip. This is achieved by writing a HIGH value to the pin using digitalWrite(pin, HIGH), which saves a lot of wiring compared to using external resistors. However, wiring the switch between the pin and ground is crucial, as the internal pull-up sets the default state of the pin to HIGH. This configuration allows the pin to read LOW when the switch is pressed .

A piezo buzzer connected to a light-sensitive circuit acts like a Theremin where the pitch varies with changes in light intensity. This frequency modulation occurs because a photocell alters resistance based on ambient light, thereby affecting the voltage and changing the oscillation frequency sent to the piezo. Movement of objects (like a hand) over the photocell changes the light exposure, thus altering the sound pitch without direct contact .

Integrating Processing with Arduino enhances physical projects by providing a platform for sophisticated audio-visual feedback. For instance, sound and movement detected by an Arduino via piezo sensors can trigger visualizations or further audio effects in Processing, increasing interactivity. By using serial communication, real-world actions can dynamically influence digital content, achieving richer user experiences beyond an Arduino's processing capacity alone. This approach forms a bridge between the physical environment and digital expressions .

Project power considerations include understanding the current draw of the Arduino and any connected components (LEDs, servos). With an Arduino's baseline draw of around 40mA and added draws from peripherals, the project duration can be calculated by dividing the battery's capacity by the total current consumption. For instance, a 9V battery with ~400mAh capacity will run a basic setup for approximately 10 hours without additional load from peripherals. Effective power management might involve using sleep modes, though these are less documented for Arduino convenience .

When building a melody player on an Arduino, different notes naturally correspond to different frequencies, leading to different playback times for the same cycle count if not adjusted. This discrepancy means that lower frequency notes (like a low C) will take longer to play for a given cycle count than higher frequency notes (like a high B), affecting musical timing and giving an inconsistent rhythmic playback if not properly calibrated .

When using a piezo element as an input sensor on an Arduino, one must consider voltage protection to prevent damage to the microcontroller. This involves connecting the piezo to an analog input through a high-value resistor to drain excessive voltage and a zener diode to clip the voltage at safe levels (e.g., 5.1 volts) before it reaches the processor. This protects against transient high voltages that the piezo element could generate during operation .

Using a zener diode for voltage protection implies that the circuit can safely handle occasional high voltage spikes, as the diode becomes conductive at its threshold (e.g., 5.1V) and shunts excess voltage to ground. This is crucial in an Arduino piezo input setup to prevent damage from high voltages generated by piezo elements when struck vigorously. Thus, the zener diode ensures reliability and longevity of the Arduino's input pins by protecting them from over-voltage conditions .

Processing can be used with Arduino for interactive projects by allowing the Arduino to interface with computer software. This is achieved through the serial library in Processing, which can open serial ports to communicate with the Arduino. By reading and writing serial data, Processing can control Arduino sensors and outputs dynamically. However, only one program can connect to a serial port at a time, so the Arduino's Serial Monitor must be turned off when using Processing, and vice versa. This limits the ability to debug using the Serial Monitor while actively using Processing .

Spooky Projects
Introduction to Microcontrollers with Arduino
Class 4
28 Oct 2006 - machineproject - Tod E. Kurt
What’s For Today
• Switches without Resistors
• All about piezos
• Building a melody player
• Using piezos as pressure & knoc
Recap: Programming
Reset
Edit
Compile
Upload
Like always, just make sure.  Make “led_blink” come alive again.  Do it.  Trust
Switches w/o Resistors
AVR chip has internal “pull-up” resistors
to input
Instead of this:
You can just do this:
+5V
to input
Switches w/o Resistors
Arduino
board
pin 7
gnd
pin 6
pin 5
A
B
C
Answer: use digitalWrite(pin,HIGH) on the input
Seems a litt
Switches w/o Resistors
Can make a button box easily 
if no resistors are needed
Plugs right into Arduino board
A
B
C
=
Piezoelectrics
• Big word – piezein is greek for “squeeze”
• Some crystals, when squeezed, make a spark
• Turns out the proce
Piezo Buzzers
• Two wires, red & black.
Polarity matters: black=ground
• Apply an oscillating voltage to 
make a noise
• The
What’s in a Piezo Buzzer?
You can get at the piezo 
element pretty easily.
Be careful not to crack 
the white disc that is 
t
Piezo Buzzer
Arduino
board
pin 7
gnd
+
–
piezo
buzzer
Piezo leads are very thin. The breadboard holes grab them better than t

You might also like