In this tutorial you will learn how to detect sound with a MAX4466 microphone and an Arduino or ESP32. We will use the MAX4466 to measure loudness, to switch an LED on if it is too loud, and to control a device such a lamp with claps.
Let’s start with the required parts.
Required Parts
You will need an Arduino, ESP32, ESP8266 or similar microcontroller and the MAX4466 Microphone Module. To build the application examples you will also need a Relay module, some wires, resistors and LEDs.
Arduino Uno
MAX4466 Microphone
Relay Module
Dupont Wire Set
Breadboard
USB Cable for Arduino UNO
Resistor & LED kit
Makerguides is a participant in affiliate advertising programs designed to provide a means for sites to earn advertising fees by linking to Amazon, AliExpress, Elecrow, and other sites. As an Affiliate we may earn from qualifying purchases.
Features of the MAX4466 Microphone Module
The MAX4466 Microphone module is a breakout board with a 20-20KHz electret microphone and the MAX4466 preamplifier IC. On the back of the board is a small trimmer that allows you to adjust the gain from 25x to 125x. The picture below shows the front and the back of the board:
The module runs on 2.4…5.5V with a very low quiescent power supply current of <24μA. For good performance, use the ‘quietest’ supply available. This would be the 3.3V pin of an Arduino or ESP32.
Note that the maximum output voltage on the OUT pin is from 0 V to VCC. If you connect the output to the Analog-Digital-Converter (ADC) of your microcontroller make sure that it can handle the maximum output voltage! The output will have a bias of VCC/2. So when it’s perfectly quiet, the output voltage will be VCC/2 V.
For more technical details have a look at the Datasheet of the MAX4466 that is linked below:
Connecting the MAX4466 to Arduino/ESP32
Connecting the MAX4466 module to an Arduino UNO is easy. Just connect GND to GND, VCC to 3.3V and the output pin OUT of the MAX4466 to the analog input A0 as shown below:
You could also connect the VCC pin of the MAX4466 module to 5V and you would get a better resolution on the analog input A0 but the input would be a bit more noisy.
If you use an ESP32 you must run the MAX4466 module on 3.3V, since the maximum output of the module is based on VCC and with 5V as power supply, the voltage on the analog input would be too high!
Code Examples
In the following sections you will find three code examples on how to use the MAX4466.
Code: Show MAX4466 Measurements
In this first example we display the readings of the MAX4466 on the Serial Monitor or Plotter:
const byte micPin = A0; void setup() { Serial.begin(115200); pinMode(micPin, INPUT); } void loop() { int value = analogRead(micPin); Serial.println(value); delay(100); }
The code is very simple. We first define the pin where the MAX4466 microphone is connected to (micPin = A0
).
In the setup
function we then initialize the serial communication and declare the micPin
as an input. In the loop function we read the microphone signal from the micPin
and print the measured sensor value to the serial monitor.
The following plot shows the output on the Serial Plotter when clapping three times. You can clearly see the three spikes in signal in the plot:
I used an ESP32 for this plot, which has a better resolution on the analog input. If you use an Arduino UNO, you will see values between 0 and 1024 on the y-axis.
Note that signal at complete silence is around half of the maximum of the analog input. In case of an ESP32, where the analog input has a resolution of 0…4095, the value is roughly around 2000. In case of an Arduino UNO the value at silence will be around 500.
If you look at the plot again, you will notice that the spikes can be positive and negative. This is because the MAX4466 microphone essentially measures the change in air pressure caused by a sound. Depending on at what point in time during the sound wave the measurement is taken, this can be low or high pressure.
This means if we want to measure the loudness of a sound, we can’t just take the maximum but must compute the difference to the baseline (silence). And this is what we are going to do in the next example to control an LED.
Code: Switch on LED if it is too loud
In this example we will switch on an LED if the microphone detects a loud sound. Below is the wiring. It is the same as before with the addition of an LED connected to pin 11. Note that you will need a 220 Ω resistor for the LED to limit the current.
The following code reads the microphone signal, computes a loudness value and if the loudness is larger than a given thresholds, switches on the LED for a second:
const byte micPin = A0; const byte ledPin = 11; const int threshold = 100; void setup() { pinMode(micPin, INPUT); pinMode(ledPin, OUTPUT); } void loop() { static int last = 0; int value = analogRead(micPin); int loudness = abs(last-value); if (last && loudness > threshold) { digitalWrite(ledPin, HIGH); delay(1000); digitalWrite(ledPin, LOW); } last = value; delay(1); }
The code starts by defining the pins for the microphone and the LED, and the loudness threshold. In the setup function we declare the microphone pin as input and the LED pin as output.
In the setup
function, we declare a static variable last
that keeps track of the last
(previous) value the microphone recorded. By computing the absolute difference abs
between the last
and the current value
, we get some measure of loudness:
int loudness = abs(last-value);
If we have a last
value and the loudness
is greater than the threshold
, we switch on the LED for a second and then switch it off again:
if (last && loudness > threshold) { digitalWrite(ledPin, HIGH); delay(1000); digitalWrite(ledPin, LOW); }
You can adjust how loud a sound needs to be to trigger the LED by changing the threshold. A lower threshold will make the sound detection more sensitive, for instance. Similarly, you can change the delay to switch on the LED for a longer or shorter time.
This circuit would be suitable as a simple touchless light or alarm system that activates a lamp when a sound is detected.
Code: Control Relay/Light with Claps
In this example we switch a relay on when a first clap is detected and switch it off when a second clap is detected. Below is the wiring for this example. It essentially replaces the LED in the previous example by a relay.
The DC+ (VCC) pin of the relay is connected to 5V and the DC- (GND) pin is connected to GND. The input pin IN is connected to pin 11 of the Arduino.
The relay can be used to switch high-voltage and current devices such as a lamp as shown in the circuit. Be very careful when working with 110 or 220 voltage! Also make sure your relay is rated high enough for the devices you want to control.
If you want to learn more about relays have a look at our How To Use A Relay With Arduino or the Interfacing a Relay Module With ESP32 tutorial. Also the tutorial on How To Control a Fan using Arduino might be useful.
Below is the code for this example. As before it measures the loudness but it also keeps track of the state
of the relay (HIGH or LOW). If the loudnes
s exceeds the threshold
the state
of the relay is changed. This means clapping twice, for instance, first switches the relay on and then switches it of. There is no fixed on-time anymore, as we had in the previous example.
const byte micPin = A0; const byte relayPin = 11; const int threshold = 100; void setup() { pinMode(micPin, INPUT); pinMode(relayPin, OUTPUT); } void loop() { static uint8_t state = LOW; static int last = 0; int value = analogRead(micPin); int loudness = abs(last-value); if (last && loudness > threshold) { state = state == HIGH ? LOW : HIGH; digitalWrite(relayPin, state); value = 0; delay(100); } last = value; delay(1); }
Otherwise the code is pretty much identical to the previous example. The only difference is the handling of the event, when the loudness exceeds the threshold. In this case the state of the relay is flipped and the new state is written to the digital output:
if (last && loudness > threshold) { state = state == HIGH ? LOW : HIGH; digitalWrite(relayPin, state); value = 0; delay(100); }
As before, you can change the sound level needed to activate the relay by adjusting the threshold
parameter.
Conclusions
This article introduced the MAX4466 microphone module to detect sound with an Arduino, ESP32 or similar microcontrollers.
We used the MAX4466 to measure the loudness of sound, to switch on an LED for a fixed period if the sound exceeds a certain threshold, and to switch a Relay with clapping sounds. If you have any questions, feel free to leave them in the comment section.
Happy Tinkering ; )
Stefan is a professional software developer and researcher. He has worked in robotics, bioinformatics, image/audio processing and education at Siemens, IBM and Google. He specializes in AI and machine learning and has a keen interest in DIY projects involving Arduino and 3D printing.