Lab 5: Piezo Speakers

Sam Stromberg
3 min readOct 5, 2021

This lab adds a controllable output device to our toolkit, so that the microcontroller can not only respond to the physical world, but also change or affect it. While the LED outputs we explored earlier in the semester do effect change in the environment, the brightness of a small number of bulbs could be thought of more as an indicator than an actuator. From a circuit-design perspective, though, using the piezo speaker does not add much complication beyond last week’s activity: one circuit takes variable input (I used a photoresistor), while another sends an appropriate mapping of that signal to the output device.

Because the piezo speaker only has two states (powered/flexed and unpowered/relaxed), its pitch is controlled by the frequency of its oscillation between them. Therefore, the signal we send to it takes the form of a square wave, with amplitude between 0 and 5V and equal time each cycle in each state. On the Arduino, we can control this by setting delay, so a PWM-compatible pin is not needed.

Components

  • Arduino Uno
  • Mini breadboard
  • 2x Resistors, 10k Ohms
  • 1x Photoresistor
  • 1x Piezo speaker
  • Hand-cut and jumper wires — the piezo speaker has very fine gauge leads built into it, so it can’t plug directly into the Arduino; they were even difficult to seat in the breadboard! If we keep using this device, I may have to solder in robust replacements.

Code

The provide example code required minimal adaptation to create the theramin.

int photocellPin = 0;
int speakerPin = 7;

int photocellVal = 0;
int toneVal;

// try changing the noteDuration to hear how that changes the sound
// I decreased the sampling rate to smooth the changes in tone
int noteDuration = 15; // ms

void setup() {
pinMode(speakerPin, OUTPUT);
Serial.begin(9600);
}

void loop() {
// read the value from the sensor
photocellVal = analogRead(photocellPin);
Serial.print(“photocellVal: “);Serial.println(photocellVal);

// decide what tone to play based on the sensor value
// the constant applied here depends in part on the effective
// range of input values (eg in daylight vs a dark room)
// and in part on the desired upper limit on pitches
toneVal = photocellVal * 3;

// we could also consider more complicated implementations
// such as playing harmonizing tones or using the map( ) function

// play the tone
tone(speakerPin, toneVal, noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note’s duration + 30% seems to work well:
// try changing the pauseBetweenNotes to hear how that changes the sound
int pauseBetweenNotes = noteDuration * 1.3;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}

Exploration

We were tasked with designing an artifact that, using the types of transducers we’ve worked with so far, both accepts input and produces output. I was inspired by the way mobile phones modulate screen brightness based on ambient conditions; it could be useful to have a lamp or light fixture that is similarly responsive. Photoresistors could be used to measure both the intensity of already-present light and its color to modulate the parameters of light output. In particular, a very warm color temperature for an artificial light can evoke coziness, but not if it is operating against a background of full-spectrum daylight (in which case a different output may be preferred, i.e. as a task lamp).

If we connect the microcontroller to a clock (or even use a pot to set its calibration when turned on), we could also replicate the “night mode,” shift away from blue wavelengths, at a desired point in time.

This of course brings to mind the wake-up alarms made by Philips et al — which suggests that the piezo buzzer could be used as a (very annoying) alarm, perhaps tied to daybreak, if a photoresistor is placed on the other side of the curtains. The force sensor could be used as a snooze function — without pressing it at regular intervals, the alarm will increase in volume — perhaps by switching among multiple leads to the piezo speaker with different sizes of resistor.

--

--

Sam Stromberg

2nd-year Masters student at the UC Berkeley School of Information. Moving into Product; interested in data and uncertainty, sensor data, behavioral change.