Microphones convert sound waves into electrical signals. Different types of microphones use various principles to achieve this conversion.
// Sound level detection const int micPin = A0; const int sampleWindow = 50; unsigned int sample; float getSoundLevel() { unsigned long startMillis = millis(); unsigned int signalMax = 0; unsigned int signalMin = 1024; while (millis() - startMillis < sampleWindow) { sample = analogRead(micPin); if (sample < 1024) { if (sample > signalMax) { signalMax = sample; } if (sample < signalMin) { signalMin = sample; } } } return signalMax - signalMin; }