MAX9814 Arduino: AGC Microphone Gain, Sound Level and LED Code
MAX9814 Arduino guide: 40/50/60 dB gain selection, AGC, analog audio reading, peak and dB calculation, KY-038 and INMP441 comparison, beat-reactive LED code.
The MAX9814 Arduino combination is one of the easiest ways to read sound as a clean, strong analog signal. The MAX9814 module is a small board with an electret microphone, a low-noise preamplifier and automatic gain control (AGC). Thanks to AGC, both a whisper and loud music come out as a wave of similar size, so the Arduino's analog input never sees a signal that is too weak or one that clips. That is why the MAX9814 is popular for sound level indicators, clap or sound triggers and music-reactive LED projects.
In this article you'll learn how the MAX9814 works, the 40 / 50 / 60 dB gain selection and the A/R pin, and how to wire it to an Arduino Uno, ESP32 and Raspberry Pi Pico. Then you'll read analog audio and calculate its peak-to-peak value, its voltage and relative dB, measure RMS on the ESP32, compare the KY-038, MAX4466, MAX9814 and INMP441 modules in one table, and finally build a WS2812B strip that changes colour to the beat of the music.
What is the MAX9814 and how does it work?
The MAX9814 is a microphone amplifier IC made by Maxim (today Analog Devices). It has three stages inside: a low-noise 2 V microphone bias source that powers the electret mic, an amplifier whose gain changes automatically (VGA), and an output amplifier whose gain is selected with the GAIN pin. As the sound gets louder, the AGC circuit lowers the gain by up to 20 dB, and when the sound drops it slowly raises the gain again. The output signal swings around a DC bias of about 1.25 V and reaches at most about 2 V peak-to-peak. So in a quiet room the Uno's analogRead() value sits steady around 256, and as sound arrives this value moves up and down. You find the sound level not from the raw signal itself but by measuring the amplitude of this wave.
| Feature | MAX9814 module |
|---|---|
| Supply | 2.7–5.5 V (3.3 V or 5 V on the Uno, 3.3 V on ESP32 and Pico) |
| Current | About 3 mA |
| Gain | 40, 50 or 60 dB (100, 316 or 1000 times), via the GAIN pin |
| AGC range | Reduces the gain by up to 20 dB |
| Output | Analog, about 1.25 V bias, at most about 2 V peak-to-peak |
| Frequency range | About 20 Hz – 20 kHz |
| Pins | GND, VDD, GAIN, OUT, AR |
Gain selection: 40, 50 and 60 dB
The GAIN pin is a three-state input that sets the module's maximum gain. If the pin is left unconnected, the gain is 60 dB; that amplifies the microphone signal 1000 times and picks up even speech a few metres away. Tie it to GND for 50 dB (about 316 times) or to VDD for 40 dB (100 times). AGC works downwards from this maximum. A high gain increases sensitivity in a quiet room, but right next to a speaker the signal keeps hitting the AGC limit and room noise is amplified too. The rule is simple: start with 60 dB for distant sources, 50 dB when music is playing in the room and 40 dB when you are right next to the speaker.
| GAIN pin | Gain | When to choose it |
|---|---|---|
| Unconnected | 60 dB (1000x) | Distant speech, quiet room, clap detection |
| GND | 50 dB (316x) | Music at normal volume, desktop projects |
| VDD | 40 dB (100x) | Very close to a speaker, loud environment |
The AR pin selects the attack / release ratio, which sets how fast the AGC reacts. The attack time is how quickly the gain drops when the sound suddenly gets louder, and it is fixed by a capacitor on the board. The release time is how slowly the gain comes back up after the sound stops, and it is chosen as a multiple of the attack time. Left unconnected, the ratio is 1:4000; this is the slowest release and stops the gain from going up and down with every beat in music, an effect called "pumping". Tying it to GND gives 1:500 and to VDD gives 1:2000; a shorter release can help with fast-changing sounds such as voice recording.
MAX9814 wiring: Arduino Uno, ESP32 and Pico
| MAX9814 pin | Arduino Uno | ESP32 | Raspberry Pi Pico |
|---|---|---|---|
| VDD | 3.3V (or 5V) | 3V3 | 3V3(OUT), pin 36 |
| GND | GND | GND | GND |
| OUT | A0 | GPIO 34 (ADC1) | GP26 (ADC0) |
| GAIN | Unconnected = 60 dB, GND = 50 dB, VDD = 40 dB | Same | Same |
| AR | Leave unconnected (1:4000) | Leave unconnected | Leave unconnected |
Step by step: reading analog audio with Arduino
- Connect the MAX9814's VDD to the Uno's 3.3V pin, GND to GND and OUT to A0. Leave GAIN and AR unconnected for now (60 dB, 1:4000).
- Upload the code below and open the Serial Plotter from the Tools menu at 115200 baud.
- In a quiet room, check that bias_V is about 1.25 V and p2p is small (usually 5–30).
- Talk into the microphone, clap or play music, and watch the p2p and dB lines rise.
- If p2p keeps sitting around 300–400 when it's loud, tie GAIN to GND (50 dB) or VDD (40 dB) and try again.
1// MAX9814 ile tepe-tepe ses seviyesi / MAX9814 peak-to-peak sound level
2const int MIC_PIN = A0; // MAX9814 OUT -> A0
3const unsigned long WINDOW_MS = 50; // ölçüm penceresi / measurement window
4const float VREF = 5.0; // Uno ADC referansı / Uno ADC reference
5
6void setup() {
7 Serial.begin(115200);
8}
9
10void loop() {
11 unsigned long start = millis();
12 int minV = 1023;
13 int maxV = 0;
14 long sum = 0;
15 unsigned int n = 0;
16
17 // 50 ms boyunca olabildiğince çok örnek al / take as many samples as possible for 50 ms
18 while (millis() - start < WINDOW_MS) {
19 int s = analogRead(MIC_PIN);
20 if (s < minV) minV = s;
21 if (s > maxV) maxV = s;
22 sum += s;
23 n++;
24 }
25
26 int p2p = maxV - minV; // tepe-tepe (ADC adımı) / peak-to-peak (ADC steps)
27 float vpp = p2p * VREF / 1023.0; // volt cinsinden / in volts
28 float bias = (sum / (float)n) * VREF / 1023.0; // DC orta nokta (~1,25 V) / DC bias (~1.25 V)
29 // 1 mVpp'e göre bağıl dB, kalibre SPL değildir / relative dB vs 1 mVpp, not calibrated SPL
30 float db = (vpp > 0.001) ? 20.0 * log10(vpp / 0.001) : 0.0;
31
32 // Seri Çizici için etiketli çıktı / labelled output for the Serial Plotter
33 Serial.print("p2p:");
34 Serial.print(p2p);
35 Serial.print(",bias_V:");
36 Serial.print(bias, 2);
37 Serial.print(",dB:");
38 Serial.println(db, 1);
39}
40Instead of looking at a single analogRead() result, the code collects samples over a 50-millisecond window and keeps the smallest and largest values seen. The Uno takes about 112 microseconds per analogRead(), so each window holds about 440 samples, and even bass notes down to 20 Hz fit in the window as at least one full wave. The difference between the two extremes is the peak-to-peak amplitude and shows how strong the sound is. The average is the DC bias; if that number is far from 1.25 V, something is wrong with the wiring. The last lines print the result in the "label:value" format the Serial Plotter understands. For details on the reading technique, see the what is an ADC article.
Peak value, sound level and dB calculation
To turn the peak-to-peak value into volts, multiply the ADC step by the reference voltage: on the Uno, 5 V / 1023 = about 4.9 mV. To show the sound level closer to how the human ear hears it, the amplitude is converted to logarithmic decibels: dB = 20 × log10(V / Vref). In the code Vref is 1 mV, so the result is relative dB, not the dB SPL value a sound level meter shows. When the amplitude doubles, the result rises by 6 dB; when it rises tenfold, by 20 dB. If you want a real sound pressure value, you need to calibrate the module next to a known sound source or a phone app and add a constant.
| Peak-to-peak (ADC) | Voltage (Vpp) | Relative dB (vs 1 mV) | Typical situation |
|---|---|---|---|
| 5 | 24 mV | 28 dB | Quiet room, noise floor at 60 dB gain |
| 40 | 196 mV | 46 dB | Normal speech from 1 m away |
| 150 | 733 mV | 57 dB | Medium-volume music on the desk |
| 400 | 1.96 V | 66 dB | AGC limit: the output no longer grows |
Using the MAX9814 with ESP32
The ESP32's ADC is 12-bit (0–4095) and with the default 11 dB attenuation reads roughly 0–3.1 V. The MAX9814's 1.25 V bias lands close to the middle of this range, so the signal can swing comfortably in both directions. The ESP32 ADC isn't linear near the ends, so the code below reads millivolts directly with analogReadMilliVolts(), which uses the factory calibration instead of raw numbers. Besides peak-to-peak, it also calculates the RMS value: first the average of the samples (the DC part) is found, then the square root of the mean of the squared deviations from that average is taken. Because RMS looks at the energy of the whole window rather than a single instantaneous peak, it is less affected by clicks and short crackles. If you'll run it together with Wi-Fi, MQTT or a web server, stick to the ADC1 pins.
1// MAX9814 + ESP32: tepe-tepe ve RMS ölçümü / peak-to-peak and RMS measurement
2const int MIC_PIN = 34; // ADC1_CH6, Wi-Fi açıkken de çalışır / works with Wi-Fi on
3const unsigned long WINDOW_MS = 50; // ölçüm penceresi / measurement window
4
5void setup() {
6 Serial.begin(115200);
7 analogReadResolution(12); // 0-4095
8}
9
10void loop() {
11 unsigned long start = millis();
12 uint32_t minMv = 4000, maxMv = 0, n = 0;
13 double sum = 0, sumSq = 0;
14
15 while (millis() - start < WINDOW_MS) {
16 uint32_t mv = analogReadMilliVolts(MIC_PIN); // kalibre mV / calibrated mV
17 if (mv < minMv) minMv = mv;
18 if (mv > maxMv) maxMv = mv;
19 sum += mv;
20 sumSq += (double)mv * mv;
21 n++;
22 }
23
24 double mean = sum / n; // DC orta nokta / DC bias
25 double var = sumSq / n - mean * mean; // AC bileşenin varyansı / variance of the AC part
26 double rms = var > 0 ? sqrt(var) : 0; // RMS (mV)
27 double db = rms > 1 ? 20.0 * log10(rms) : 0; // 1 mV RMS'e göre bağıl dB / relative dB vs 1 mV RMS
28
29 Serial.printf("p2p_mV:%u,rms_mV:%.1f,dB:%.1f,bias_mV:%.0f,ornek:%u\n",
30 maxMv - minMv, rms, db, mean, n);
31}
32On the Raspberry Pi Pico the wiring follows the same logic: OUT goes to GP26 (ADC0), and in MicroPython ADC(26).read_u16() returns a value between 0 and 65535. The Pico's ADC reference is 3.3 V, so in silence you'll see a middle value of about 24,800; the peak-to-peak calculation is the same loop as in the Arduino code. The Pico's ADC is affected by its own supply noise, so power the module from the 3V3(OUT) pin and use the AGND pin.
KY-038, MAX4466, MAX9814 and INMP441 comparison
The four sound modules most used in hobby electronics are designed for very different jobs. The KY-038 is really a threshold detector: the mic signal goes to a comparator without amplification, and the AO output swings only a few tens of millivolts. The MAX4466 is an op-amp amplifier with fixed but adjustable gain. The MAX9814 sets its gain by itself. The INMP441 sends sound directly as 24-bit digital data over I2S and is immune to analog noise, but it needs a board with an I2S interface such as the ESP32 or Pico.

| Feature | KY-038 | MAX4466 | MAX9814 | INMP441 |
|---|---|---|---|---|
| Output type | Analog (weak) + digital threshold | Analog | Analog | Digital I2S, 24-bit |
| Gain | None (the pot only sets the threshold) | 25–125 times, via trimpot | 40/50/60 dB + AGC | Fixed, scaled in software |
| Supply | 3.3–5 V | 2.4–5.5 V | 2.7–5.5 V | 1.8–3.3 V |
| Bias | Depends on pot setting | VCC / 2 | About 1.25 V | None (digital) |
| Arduino Uno | Yes | Yes | Yes | No (no I2S) |
| Best use | Clap switch, sound yes/no | Sound level measurement, recording | Reacting to music, voice detection | Audio recording, FFT, dB measurement |
| Weak point | Can't hear distant sound | Clips at high volume | Can't measure dB because of AGC | Doesn't work with the Uno |
Music-reactive LED: a strip that changes colour with the beat
The MAX9814's AGC makes music projects much easier: whether the song plays quietly or loudly, the output amplitude stays in a similar range, so you don't need to readjust thresholds every time. The code below measures peak-to-peak in short 15 ms windows and compares it with a sliding average of about half a second. When the instantaneous level rises 40% above the average, it counts a beat, lights the strip at full brightness in a new colour and then fades it slowly. The 180 ms hold time stops a single drum hit from being counted twice. For the LEDs, connect the WS2812B's DIN to D6 through a 330 Ω resistor, power the strip from a separate 5 V supply and join the grounds. If you'd like a classic VU meter that fills up with the level, you can use the code from the music-reactive LED article with the MAX9814 as is.
1// MAX9814 ile ritme duyarlı WS2812B / beat-reactive WS2812B with MAX9814
2#include <Adafruit_NeoPixel.h>
3
4const int MIC_PIN = A0; // MAX9814 OUT
5const int LED_PIN = 6; // WS2812B DIN (330 ohm ile / through 330 ohm)
6const int NUM_LEDS = 30; // şerit uzunluğu / strip length
7const unsigned long WINDOW_MS = 15; // kısa pencere, hızlı tepki / short window, fast response
8const unsigned long MIN_BEAT_MS = 180; // iki vuruş arası en kısa süre / min time between beats
9
10Adafruit_NeoPixel strip(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
11
12float avgLevel = 0; // uzun süreli ortalama / long-term average
13float noiseFloor = 0; // sessizlik seviyesi / silence level
14float glow = 0; // parlaklık zarfı (0-1) / brightness envelope (0-1)
15uint16_t hue = 0; // o anki renk / current colour
16unsigned long lastBeat = 0;
17
18int readPeakToPeak() {
19 unsigned long start = millis();
20 int minV = 1023, maxV = 0;
21 while (millis() - start < WINDOW_MS) {
22 int s = analogRead(MIC_PIN);
23 if (s < minV) minV = s;
24 if (s > maxV) maxV = s;
25 }
26 return maxV - minV;
27}
28
29void setup() {
30 strip.begin();
31 strip.setBrightness(80); // toplam akımı sınırla / limit total current
32 strip.show();
33 delay(300);
34 long sum = 0;
35 for (int i = 0; i < 40; i++) sum += readPeakToPeak(); // sessizlikte kalibrasyon / calibrate in silence
36 noiseFloor = sum / 40.0 + 4;
37 avgLevel = noiseFloor;
38}
39
40void loop() {
41 float level = readPeakToPeak();
42 avgLevel = avgLevel * 0.97 + level * 0.03; // yaklaşık 0,5 sn'lik ortalama / roughly 0.5 s average
43
44 // Vuruş: anlık seviye ortalamanın %40 üstünde mi? / Beat: is the level 40% above average?
45 bool beat = level > avgLevel * 1.4 && level > noiseFloor * 2 &&
46 millis() - lastBeat > MIN_BEAT_MS;
47 if (beat) {
48 lastBeat = millis();
49 hue += 9000; // her vuruşta yeni renk / new colour on each beat
50 glow = 1.0;
51 }
52 glow *= 0.90; // vuruşlar arasında sön / fade between beats
53
54 uint8_t v = (uint8_t)(glow * 255);
55 for (int i = 0; i < NUM_LEDS; i++) {
56 // şerit boyunca hafif renk kayması / slight hue shift along the strip
57 strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(hue + i * 600, 255, v)));
58 }
59 strip.show();
60}
61MAX9814 project ideas
- Beat-reactive room lighting: apply the code above to a 60-LED strip; for the power calculation of long strips see the WS2812B guide.
- Baby room sound alarm: detect crying at 60 dB gain and send a notification to your phone with the ESP32.
- Classroom noise traffic light: light green, yellow and red LEDs based on the RMS value; since no exact measurement is needed, the AGC's relative value is enough.
- Sound-triggered camera or recorder: trigger a camera with a relay or GPIO when the sound goes above a threshold.
- Simple voice recorder: sample at 8–16 kHz with the ESP32 and write the samples to an SD card as WAV.
- Microphone VU display: draw the waveform and a bar graph on an OLED screen.
Common mistakes and fixes
| Symptom | Likely cause | Fix |
|---|---|---|
| Value stays around 256, no reaction to sound | VDD not connected or OUT on the wrong pin | Check the supply and the A0 wire, measure 1.25 V on OUT with a multimeter |
| Constant hum, a 50 Hz wave | USB noise, long wires, ground problem | Power from 3.3V, shorten the wires, add 10 µF + 100 nF next to the module |
| p2p stays the same at high volume | AGC limit reached | This is normal; use a MAX4466 to measure level or set the gain to 40 dB |
| ESP32 always reads 0 | ADC2 pin used together with Wi-Fi | Move OUT to a pin between GPIO 32 and 39 |
| LEDs flash with noise, not with music | The room wasn't quiet during calibration | Keep it quiet for the first second after upload or raise noiseFloor |
| Strip flickers or the Arduino resets | LED current drawn from USB | Power the strip from a separate 5 V supply, add a 1000 µF capacitor at the input |
The MAX9814 is the most practical way to read sound reliably with a single analog input: you pick the gain with one wire, and thanks to AGC you can use the same code at different volume levels. In the BlueGrays app you can browse projects that combine sound sensors with LEDs, buzzers and displays, each with a step-by-step circuit diagram and ready-made code, and add the peak-to-peak measurement you learned here to your own project right away.
Frequently asked questions
How do you change the gain of the MAX9814?
The gain is selected by how the GAIN pin is connected: left unconnected it is 60 dB, tied to GND it is 50 dB and tied to VDD it is 40 dB. This is the maximum gain; when the sound gets louder, the AGC pulls the gain down from this value by up to 20 dB.
Can the MAX9814 measure decibels?
You can measure a relative level, but you won't get an accurate dB SPL value, because the AGC lowers the gain at high volume and keeps the output at about 2 Vpp. For real sound level measurement use a fixed-gain MAX4466 or a digital microphone such as the INMP441 and calibrate it.
Does the MAX9814 work with 5V?
Yes, the module runs from 2.7 to 5.5 V, so it can be powered from the Arduino's 5 V pin. Since the output stays at a 1.25 V bias, 5 V brings no advantage; a 3.3 V supply often gives less noise and is the right choice for the ESP32 and Pico anyway.
What is the difference between MAX9814 and MAX4466?
The MAX4466's gain is set by hand with a trimpot and stays fixed, which suits sound level measurement but can clip at high volume. The MAX9814 adjusts its own gain with AGC, which makes music and voice detection easier but makes level measurement misleading.
Why does the MAX9814 analogRead value sit around 256?
In silence the module's output rests at about 1.25 V, and on the Uno 1.25 / 5 × 1023 is about 256. Sound swings up and down around this value, so you should measure the level not with a single reading but with the difference between the largest and smallest values in a window.
Can you record audio with the MAX9814?
Yes, with a fast board such as the ESP32 or Pico sampling at 8–16 kHz you can record at voice quality. AGC evens out the sound, so it works well for speech, but gain changes can be audible in music; for high-quality recording an I2S microphone like the INMP441 is better.
Projects to build after reading this
See all projects
Measuring Light Intensity with an LDR
Want to see how bright your room is as a number? The LDR, a small part that reacts to light, measures the light around it, and the Arduino writes the value to your computer screen every half second.

Using a WS2812B LED Strip
Meet a smart LED strip where you can colour every single light on its own. The Arduino first lights the first five LEDs red, a second later turns the other five blue, then switches them all off and…

ESP32 WS2812B NeoPixel Strip Control
WS2812B (NeoPixel) addressable LEDs are smart RGB LEDs that can be chained and controlled from a single data pin. In this project we drive an 8-LED WS2812B strip with the Adafruit_NeoPixel library and create a smooth rainbow animation. The ESP32's 3.3V logic works with most WS2812B batches; if it gets unstable, we show the fix with a 470Ω series resistor and a 1000µF capacitor. The animation shifts the hue in HSV color space.

WS2812B LED Chaser
On a strip of ten LEDs, a green dot of light moves from start to finish one LED at a time, then jumps back to the beginning; it stays on each LED for a third of a second.

WS2812B Animation 1
On a strip of 56 LEDs, all the colours of the rainbow line up side by side and flow without ever stopping, as if a river of light were gliding along the strip.

WS2812B Animation 2
Set up a light show in your room that changes colors all by itself. The Arduino keeps colors flowing along a 30-LED strip and switches to a new color set every few seconds: rainbow, purple and green…

ESP32 LED Brightness Control with a Potentiometer (Analog → PWM)
In this project, we control the brightness of an LED analogically using a potentiometer. The ESP32's ADC pin reads the 0-3.3V voltage from the potentiometer, and this value is then converted into a PWM (Pulse Width Modulation) signal and sent to the LED. As a result, as you turn the potentiometer, the LED gradually brightens and dims. With Arduino Core 3.0+, analogWrite() can be used directly; on the ESP32-S2/S3, the ADC resolution is fixed at 12-bit.

Using an RGB LED
Three colours hide inside a single LED: the RGB LED lights up red, then green, then blue, each colour on for half a second and off for half a second, before the cycle starts again.
More articles
BlueGrays app
Build this circuit step by step on your phone
Wiring diagram, code and parts list on one screen. 500+ Arduino, ESP32, ESP8266, Pico, mBot and micro:bit projects ready to build.
Scan with your phone
Point your camera at the code and your phone's app store opens.
or open the store

