Lab: a sound alarm
The TK27 microphone listens, and a sound 200 mV louder than the quiet room sets the TK36 buzzer and the XL LED beeping for five seconds. The one thing the alarm must not do is listen while it beeps: the buzzer is far louder than the threshold, and an alarm that hears itself never stops.
This lab uses the TK27 microphone, the TK36 active buzzer and the TK01 XL LED. It measures loudness the way the microphone's clap switch does, with analogRead.
What goes where
| Module | Socket | Pin | Move |
|---|---|---|---|
| TK27 microphone | D9/A0 | A0 | Unplug the TK07 knob and plug the TK27 in its place |
| TK36 active buzzer | D10/D11 | D11 | Stays where it is |
| TK01 XL LED | D2/D3 | D3 | Stays where it is |
The TK57 from the last lab can stay in the A0/A1 socket; this sketch does not read A1.
Loudness is a swing
The TK27 is a capsule and one transistor, and its SIGNAL swings both ways around a resting level. That level depends on the transistor's gain, which its datasheet grades anywhere from 120 to 400, so from 5 V it can sit anywhere from about 1.6 V to 4.0 V depending on the board. One reading says almost nothing.
So the sketch measures a swing: it reads A0 as fast as it can for 50 ms and keeps the highest and lowest readings. The difference, times 4.88 mV a count, is the loudness. For the first second it measures the quiet room and keeps the biggest swing it saw. The alarm goes off at that plus 200 mV, the microphone book's margin: well above talking, well below a clap at arm's length.
An alarm that hears itself
Run it with Listens while it beeps. The TK36 is loud: its datasheet guarantees at least 95 dB at 10 cm, at 2400 Hz. The microphone hears best from about 75 Hz to 1 kHz, so 2400 Hz reaches it weakened, but by the microphone book's model each beep still swings SIGNAL by about 300 mV, over the line. That model borrows another capsule's sensitivity, so treat the number as a size, not a measurement. Every beep restarts the five seconds, and the alarm never ends.
Run it with Deaf while it beeps. The sketch does not read the microphone at all while it beeps, nor for 500 ms after, while the last beep and its echo die away. One clap is one alarm.
The microphone's clap switch needs a pause for a different reason: a clap rings on for a window or two. Here the pause covers the same ringing, and the much louder noise the alarm makes itself.
The sketch
swingMv() is the whole measurement: a while loop on millis() for
WINDOW_MS, tracking lo and hi. loop() calls it and compares the
result with thresholdMv.
ring() blocks for the five seconds on purpose. While it runs, nothing reads
A0, which is the point. The beep pattern is arithmetic on the time since it
started: (millis() - start) / BEEP_MS counts 200 ms steps, and an even step
is on. both() switches the TK36 and the TK01 together, so you can see the
beeps with the buzzer unplugged.
Make it yours
- Add the TK04 button on D7 as a silence key: read it inside
ring()'s loop and return early when it is pressed. - Make the alarm latch: keep beeping until the button is pressed, and count how many sounds it heard meanwhile.
- Move the microphone away from the buzzer, print the swing during a beep,
and see how short
DEAF_MScan get before the alarm starts hearing itself.
The code
Measures the quiet room for a second: the biggest swing, highest reading minus lowest, over each 50 ms window. The alarm goes off when a window swings 200 mV more than that. It then beeps the TK36 and the TK01 together, 200 ms on and 200 ms off, for five seconds, and waits half a second more before it listens again.
/*
A sound alarm TinkerBlock UNO kit, /manuals/tkbu
The TK27 microphone listens. When a sound swings SIGNAL 200 mV
more than the quiet room did at start-up, the TK36 active buzzer
and the TK01 XL LED beep together for five seconds. While it beeps,
and for half a second after, the sketch does not listen: the
buzzer is louder than the threshold, and an alarm that hears
itself never stops.
Loudness is the swing: the highest minus the lowest reading over
50 ms. One reading says nothing, because SIGNAL swings both ways
around a resting level that differs from board to board.
Wiring, on the TinkerBlock UNO shield:
TK27 microphone in the D9/A0 socket, so it is on A0
(it takes the TK07 knob's place)
TK36 active buzzer in the D10/D11 socket, so it is on D11
TK01 XL LED in the D2/D3 socket, so it is on D3
A module is on the second pin printed beside its socket.
Arduino IDE
Tools > Board: Arduino Uno
Tools > Port: the CH340 port
No library needed. Serial Monitor at 115200.
Keep quiet for the first second while it measures the room.
*/
const int MIC_PIN = A0; // TK27 SIGNAL
const int BUZZER_PIN = 11; // TK36 SIGNAL: HIGH sounds it
const int LED_PIN = 3; // TK01 SIGNAL: HIGH lights it
const float MV_PER_COUNT = 5000.0 / 1024; // 5 V reference
const unsigned long WINDOW_MS = 50; // one loudness measurement
const unsigned long LEARN_MS = 1000; // the quiet room
const float MARGIN_MV = 200; // how much louder than quiet
const unsigned long RING_MS = 5000; // how long the alarm sounds
const unsigned long BEEP_MS = 200; // on 200, off 200
const unsigned long DEAF_MS = 500; // still deaf after it stops
float thresholdMv;
unsigned long alarms = 0;
// Highest minus lowest reading over one window, in mV.
float swingMv() {
int lo = 1023;
int hi = 0;
unsigned long start = millis();
while (millis() - start < WINDOW_MS) {
int r = analogRead(MIC_PIN);
if (r < lo) lo = r;
if (r > hi) hi = r;
}
return (hi - lo) * MV_PER_COUNT;
}
void both(bool on) {
digitalWrite(BUZZER_PIN, on ? HIGH : LOW);
digitalWrite(LED_PIN, on ? HIGH : LOW);
}
// Beeps for RING_MS, then waits out DEAF_MS. It does not read the
// microphone at all in that time, so it cannot hear its own beeps.
void ring() {
unsigned long start = millis();
while (millis() - start < RING_MS) {
both(((millis() - start) / BEEP_MS) % 2 == 0);
}
both(false);
delay(DEAF_MS);
}
void setup() {
Serial.begin(115200);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
both(false);
Serial.println(F("Measuring the quiet room. Keep quiet..."));
float quiet = 0;
unsigned long start = millis();
while (millis() - start < LEARN_MS) {
float s = swingMv();
if (s > quiet) quiet = s;
}
thresholdMv = quiet + MARGIN_MV;
Serial.print(F("Quiet swing "));
Serial.print(quiet, 0);
Serial.print(F(" mV; the alarm goes off above "));
Serial.print(thresholdMv, 0);
Serial.println(F(" mV"));
}
void loop() {
float s = swingMv();
if (s > thresholdMv) {
alarms++;
Serial.print(F("Alarm "));
Serial.print(alarms);
Serial.print(F(": "));
Serial.print(s, 0);
Serial.println(F(" mV"));
ring();
}
}Keep quiet for the first second. The sketch prints the quiet swing and the threshold it chose; if the threshold is far above what a clap gives, something made a noise while it was measuring, so press reset.
View on GitHub · kits/tkbu/clap-alarm/Sound_Alarm/Sound_Alarm.ino @ v1.14When it does not work
It is hearing itself. Check that ring() is called as it is and the sketch does not read the microphone while it beeps. If it restarts just after the five seconds, the echo outlasts DEAF_MS: raise it to 1000, or move the microphone farther from the buzzer.
The margin is too small for your room or your desk. A knock on the desk the microphone sits on is a loud, low sound, exactly the band it hears best. Raise MARGIN_MV from 200 to 400, and keep the block off a surface that gets bumped.
Look at the threshold the sketch printed. If it is hundreds of millivolts, something was loud during the first second: press reset and keep quiet. If the quiet swing printed as 0, SIGNAL is not reaching A0: check the TK27 is in the D9/A0 socket and not the knob.
The TK36 is not on D11. It belongs in the D10/D11 socket. The D9/A0 socket also prints a D9, but a module there is on A0.
The same microphone, driving the WS2812 bar as a VU meter or a beat flash.
Lab: disco lights →Edit this page — content/books/tkbu/clap-alarm.mdx
Questions about this product
See what other owners have asked, and read their solutions.
TinkerBlock UNO R3 Starter Kit, 12 Modules
Loading discussions…
Discuss this article
Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.