-
Notifications
You must be signed in to change notification settings - Fork 0
Ears
- Introduction
- Reading loudness
- Reacting to a clap
- Finding your threshold
- What the percentage actually measures
- Timing
- Wiring and the pin
- Going lower: the SoundSensor module
Ears is the analog microphone board -- Otto's hearing. It is a facade over the SoundSensor module.
uint8_t loudness = Ears::loudnessPercent(); // 0..100
if (Ears::hearsSoundLouderThanPercent(60)) { /* a clap */ }Nothing needs enabling. The microphone is a plain analog pin, so these calls are safe to make before the hardware is wired -- an unconnected pin just reads noise.
void loop() {
Serial.println(Ears::loudnessPercent());
delay(200);
}The value is a percentage of the full ADC range, 0 for silence and 100 for a sound loud enough to swing the sensor output from rail to rail. A quiet room typically reads a few percent -- never exactly zero, because the analog input always carries some noise.
Most sketches want a decision, not a number:
void loop() {
if (Ears::hearsSoundLouderThanPercent(70)) {
Voice::play(Sound::Happy);
Legs::jump();
}
}The comparison is strict -- the call is true only when loudness is greater than the threshold you pass.
Because each call listens for its own window, a single clap fires it once, and the next call starts a fresh window. There is no latching and nothing to clear.
Do not guess. Read the room first, in the Serial Console:
> mic
12 %
> watch mic
watch mic streams the value and prints only when it changes, so you can clap, talk, and play music while watching the numbers move. Pick a threshold comfortably above the idle level and below your clap -- the gap between them is what makes detection reliable.
Note Cheap microphone boards carry a physical gain potentiometer that changes every number on this page. Set it once, then find your threshold. If you turn the pot afterwards, find the threshold again.
The sensor's output rests near the middle of its range and swings both ways as a sound wave passes. A single ADC sample can therefore land exactly on a zero crossing and read as silence in the middle of a shout.
loudnessPercent() avoids that by sampling continuously for a window -- 50 ms by default -- tracking the lowest and highest values seen, and reporting the swing between them as a percentage of the ADC range. That swing is the amplitude of the sound, which is what "loud" means.
Both calls block for the whole sample window: 50 ms by default. That is 50 ms in which your loop() does nothing else, and it is the cost of a trustworthy reading.
The module layer lets you trade accuracy for speed, or the reverse:
uint8_t quick = SoundSensor::loudnessPercent(20); // 20 ms, twitchier
uint8_t sure = SoundSensor::loudnessPercent(200); // 200 ms, steadierA window shorter than about 20 ms starts to miss the low frequencies in speech; longer than about 200 ms and a short clap gets averaged into a long quiet stretch.
The default pin is A6:
OttoConfig cfg;
cfg.soundSensor.pin = A6; // the default
OttoFlow::start(cfg);Note
A6andA7on the Arduino Nano are analog-input only -- they have no digital circuitry, sodigitalRead()andpinMode()do nothing on them. That is fine for this sensor, which is read withanalogRead(), but it is why you cannot move a touch sensor or a button to those two pins.
Connect the microphone board's analog output (often labelled AO or OUT) to the pin -- not its digital output, which only reports "above the pot threshold" and loses everything this page describes.
int raw = SoundSensor::rawLevel(); // one sample, 0..1023
uint8_t p = SoundSensor::loudnessPercent(200); // custom window
bool loud = SoundSensor::isLouderThanPercent(60, 200);rawLevel() is the honest bottom layer: one analogRead() of the pin, taken instantly with no window. Use it when you want to do your own windowing or plot the waveform; use it also to check wiring, where a value pinned at 0 or 1023 means the board is not connected the way you think.
See also Voice for making sound, and Sensors for the other senses.
OttoFlow is licensed under GPL-3.0 and wraps OttoDIYLib. Buy the kits at ottodiy.com.
Prologue
Getting Started
The Basics
Digging Deeper
- Bench Testing
- Serial Console
- Calibration
- Bluetooth and the Otto App
- Modules
- The Driver
- Extending OttoFlow
Reference