SM2716 Tangible Media
Making and sensing Sound
Kening Zhu (Ken)
Associate Professor
School of Creative Media
City University of Hong Kong
Email: keninzhu@[Link]
Sound (audio signal)
Sound is a propagating wave of pressure (change of density) in a material
The ear or microphone measures the local pressure of
the air and transforms it into an electrical signal.
This is called the “waveform”. It is an analog signal!
Frequency (pitch) and amplitude (loudness) of a pure tone
Aire pressure
Hearing
Producing
Sound wave signal
Real voice
Music Knock
Noise
I) Sound generation: speakers
Buzzer Piezo Moving Coil Speaker
Output components we have
Active Buzze
has a s mple osci lator ci cuit to
convert direct cu ent nput nto a p lse
signal of a ce tain frequency
ideal signal is d rect c rrent
Passive Buzzer
without oscillato circ it
the ideal inp t is the positive square
wave
The pie elect ic effect i eve sible in that
piezoelect ic c ystal whe subjected to an
exter ally applied voltage can change shape
by smal amount
How to use a buzzer
The “tone” function
Generates a square wave of the
specified frequency (always 50% duty
cycle):
tone( pin , frequency);
tone( pin , frequency, duration);
• In any digital pin(*)
• A duration can be specified, otherwise the
wave continues until a call to noTone().
(*) Use of the tone() function will interfere with PWM output on pins 3 and 11
Sample code fo creating a melody
The Code
This pitches h contains
the frequencies for each
note;
This file is #included in
the main program
Exercise 1: Create your own melody
Store the notes and durations of
your own melody
More notes and frequencies can
be found here: https://
[Link]/formulae/
[Link]
II) Audio acquisition: the microphone
• Sound are variation of “air pressure” (or other material)
• A microphone “transduces” it in electrical signal
Carbon (oldest...):
resistance changes with sound Dynamic
microphone:
output is a voltage
Electret: capacitance is
modulated (“lapel mics”)
MEMS
microphone (very
Piezo “transducer” compact!)
output is a current (pick smartphones...
up guitars, contact mic)
Piezo transducer as a “contact mic”
• A piezoelectric ”transducer” transforms a mechanical deformation into a voltage.
• Pressure waves in the air or the guitar frame deforms the piezo.
Piezo transducer: as knock sensor
.. this is exactly what
you find under the
drum pad!
Electronic drum pad
[Link]
Fun example: piezo activated LED drums
[Link]
Piezo transducer: as knock sensor
• Piezos are polarized;
• Put 1-megohm resistor in parallel to the piezo element (limit the voltage/current produced
by the piezo and protect the analog input).
const int ledPin = 13; // LED connected to digital pin 13
Code const int knockSensor = A0; // the piezo is connected to analog pin 0
const int threshold = 100; // threshold value to decide when the detected sound is a knock or not
Check on serial monitor // these variables will change:
int sensorReading = 0; // variable to store the value read from the sensor pin
or plotter to decide int ledState = LOW; // variable used to store the last LED status, to toggle the light
your threshold
void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
[Link](9600); // use the serial port
}
void loop() {
// read the sensor and store it in the variable sensorReading:
sensorReading = analogRead(knockSensor);
// if the sensor reading is greater than the threshold:
if (sensorReading >= threshold) {
// toggle the status of the ledPin:
ledState = !ledState;
// update the LED pin itself:
digitalWrite(ledPin, ledState);
// send the string "Knock!" back to the computer, followed by newline
[Link]("Knock!");
}
delay(100); // delay to avoid overloading the serial port buffer
}
Exercise 2 : Tap a melody
How about we combine the piezo-detected knocks with the code for generating a
melody?
• Create your own melody;
• Each time you knock on the table, a note of the melody will play indefinitedly (don’t
tone(...) with duration);
• When the melody is over, it starts from the first note again
Hint: in the code for the melody, there is a “blocking delay”. You have to get rid of it,
otherwise you won’t be able to continue detecting knocks
Better debouncing: control the sampling period
Adding a delay(...) blocks the execution. We may want
to do other things (including reading other sensors for
instance)
How to sample at fixed intervals, without blocking the program (no “delay”)?
Using a clock!
unsigned long time = millis();
• millis() returns the number of milliseconds passed
since the Arduino board began running the current
program.
• It is a long number (up to 50 days in milliseconds),
hence “unsigned long”
Non-blocking delay
Other fun projects you can try
”Theremin”: capacitive sensor / use a light sensor to control the frequency of a speaker
Secret knock code to open doors or drawers!
Heartbeat sensor
Sound is just a temporal signal…
The techniques we are learning here can be applied to
other things like measuring the heart beat.
You can even measure the heart rate in “BPM” (beats
per minute) by averaging the times between knocks.
Next week:
Advanced sensing with accelerometer (motion),
distance, etc.