In the world of DIY electronics, there's a certain joy that comes from building something functional from scratch. Whether it's a simple LED blinker or a complex robotics project, every step along the way brings a sense of accomplishment. One such project that combines simplicity with practicality is the task of detecting sound levels with an Arduino. But before diving into the solution, let's understand the problem.
The Problem
Imagine you want to measure sound levels using an Arduino. Perhaps you're working on a project that needs to react to environmental noise or monitor sound levels in a room or for building diy FM radio. To achieve this, you require a preamplifier for an electret microphone. This preamplifier is crucial for further amplification of the microphone's signal, especially if you intend to use speakers or interface with an Arduino.
The Solution
The solution lies in utilizing a single-supply rail-to-rail op-amp, such as the OPA365. By amplifying the microphone's signal by a factor ranging between 30 and 100, you can effectively capture the desired sound levels. The choice of gain depends on your specific application. For instance, a gain of 30 might be suitable if you're speaking directly into the microphone, while a gain of 100 is preferable for picking up ambient sounds which is effectively like the ky-038 sound sensor.
The Circuit
The picture below illustrates the schematic diagram for the microphone preamplifier with a gain of 101.
This configuration effectively amplifies the signal while filtering out any DC bias present in the microphone's output. Capacitor C1 plays a crucial role in AC coupling the microphone's weak output to the non-inverting input of the op-amp, allowing only the AC component of the signal to pass through.Understanding Electret Microphones
To comprehend how the microphone preamplifier works, it's essential to grasp the underlying mechanism of electret microphones. These microphones function akin to capacitors, where sound waves cause fluctuations in capacitance by moving one of the capacitor's plates closer or further away. The built-in FET transistor within the microphone converts these capacitance changes into small voltage variations.
Practical Implementation
With the circuitry in place, you can directly interface the preamplifier's output, centered around 2.5V, with an Arduino's analog input. This enables you to measure sound levels and perform various actions based on the detected amplitude. The following photo depicts the circuit used in conjunction with an Arduino Uno.
Coding for Sound Measurement
To complete the setup, a corresponding Arduino sketch for measuring sound level is provided below and is utilized to sample the analog input and report the maximum amplitude sampled over a specific period. This sketch can be modified to trigger specific actions based on predefined sound level thresholds.
// Arduino Sketch for measuring sound label
const int soundPin = A0;
const long samplePeriod = 100; // ms
long lastSampleTime = 0;
int maxAmplitude = 0;
int n = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
long now = millis();
if (now > lastSampleTime + samplePeriod) {
processSoundLevel();
n = 0;
maxAmplitude = 0;
lastSampleTime = now;
} else {
int amplitude = analogRead(soundPin) - 512;
if (amplitude > maxAmplitude) {
maxAmplitude = amplitude;
}
n++;
}
}
void processSoundLevel() {
// Replace or add your own code to use maxAmplitude
Serial.print("Of ");
Serial.print(n);
Serial.print(" samples, the maximum was ");
Serial.println(maxAmplitude);
}
The following shows the sound monitor output on the arduino serial monitor at 9600 bauds/sec.
Thus by combining the hardware setup of the microphone preamplifier with the corresponding Arduino sketch, you can effectively detect sound levels and tailor your projects to respond accordingly. Whether it's creating responsive ambient lighting or developing a noise monitoring system, the ability to detect sound levels with Arduino opens up a realm of possibilities for DIY enthusiasts and electronics hobbyists alike. So, grab your components and start building!