Controlling the On-Board RGB LED with Microphone

Learn how to create a soundmeter using the built-in microphone on the Nano 33 BLE Sense Rev2.

In this tutorial we will use an Arduino Nano 33 BLE Sense Rev2 board to measure and display the sound values of your surroundings, made possible by the embedded MP34DT06JTR sensor.

Warning: A very small percentage of people may experience a seizure when exposed to flashing lights or patterns. Even people who have no history of seizures or epilepsy may have an undiagnosed condition that can cause these “photosensitive epileptic seizures” while watching lights blinking fast. Immediately stop and consult a doctor if you experience any symptoms.


The goals of this project are:

  • Learn how to output raw sensor data from the Arduino Nano 33 BLE Sense Rev2.
  • Use the PDM(Pulse-density modulation) library.
  • Print sound values in the Serial Monitor.
  • Create your own RGB sound meter.

Hardware & Software Needed

  • Arduino Nano 33 BLE Sense Rev2.
  • This project uses no external sensors or components.
  • In this tutorial we will use the Arduino Web Editor to program the board.

The MP34DT06JTR Sensor

The MP34DT06JTR microphone sensor.
The MP34DT06JTR microphone sensor.

Microphones are components that convert physical sound into digital data. Microphones are commonly used in mobile terminals, speech recognition systems or even gaming and virtual reality input devices.

The MP34DT06JTR sensor is a ultra-compact microphone that use PDM (Pulse-Density Modulation) to represent an analog signal with a binary signal. The sensor's range of different values are the following:

  • Signal-to-noise ratio: 64dB
  • Sensitivity: -26dBFS ±3dB
  • Temperature range: -40 to 85°C

If you want to read more about the MP34DT06JTR sensor you can take a look at the datasheet.

Creating the Program

1. Setting up

Let's start by opening the Arduino Web Editor, click on the Libraries tab, search for the PDM FOR MBED library, then in Examples, open the PDMSerialPlotter example. Once the sketch is open, rename it as Sound_Meter.

Finding the library in the Web Editor.
Finding the library in the Web Editor.

2. Connecting the board

Now, connect the Arduino Nano 33 BLE Sense Rev2 to the computer to check that the Web Editor recognizes it, if so, the board and port should appear as shown in the image. If they don't appear, follow the instructions to install the plugin that will allow the Editor to recognize your board.

Selecting the board.
Selecting the board.

3. Printing and displaying sound values

Now we will need to modify the code of the example, in order to print the sound values and turn on a different LED based how noisy is the sound.

Before the

there are two types of variables initialized. One is a
variable and the other is a
volatile int
variable. We use the
type variable to store 16-bit data-types as the
. The other,
, is a keyword known as a variable qualifier. It is usually used before the datatype of a variable, in order to modify the way in which the compiler and subsequent program treat the variable. In this case, it directs the compiler to load the variable
from RAM and not from a storage register.

1#include <PDM.h>
3// buffer to read samples into, each sample is 16-bits
4short sampleBuffer[256];
6// number of samples read
7volatile int samplesRead;

In the

, we use the
function to configure the data receive callback. Lastly, the
sets the sensor to read data from just one channel and a sample rate of 16 kHz, this statement is inside an
that will print a message, as a string, in case the sensor has not been properly initialized.

1void setup() {
2 Serial.begin(9600);
3 while (!Serial);
5 // configure the data receive callback
6 PDM.onReceive(onPDMdata);
8 // optionally set the gain, defaults to 20
9 // PDM.setGain(30);
11 // initialize PDM with:
12 // - one channel (mono mode)
13 // - a 16 kHz sample rate
14 if (!PDM.begin(1, 16000)) {
15 Serial.println("Failed to start PDM!");
16 while (1);
17 }

Then, in the

, let's modify the example code by adding the following portion of code inside the
loop, after the

1// check if the sound value is higher than 500
2 if (sampleBuffer[i]>=500){
3 digitalWrite(LEDR,LOW);
4 digitalWrite(LEDG,HIGH);
5 digitalWrite(LEDB,HIGH);
6 }
7 // check if the sound value is higher than 250 and lower than 500
8 if (sampleBuffer[i]>=250 && sampleBuffer[i] < 500){
9 digitalWrite(LEDB,LOW);
10 digitalWrite(LEDR,HIGH);
11 digitalWrite(LEDG,HIGH);
12 }
13 //check if the sound value is higher than 0 and lower than 250
14 if (sampleBuffer[i]>=0 && sampleBuffer[i] < 250){
15 digitalWrite(LEDG,LOW);
16 digitalWrite(LEDR,HIGH);
17 digitalWrite(LEDB,HIGH);
18 }

With this portion of the code, we will turn on the RGB LED based on the amount of sound that the microphones is receiving.

4. Complete code

If you choose to skip the code building section, the complete code can be found below:

2 This example reads audio data from the on-board PDM microphones, and prints
3 out the samples to the Serial console. The Serial Plotter built into the
4 Arduino IDE can be used to plot the audio data (Tools -> Serial Plotter)
6 Circuit:
7 - Arduino Nano 33 BLE Sense Rev2 board
9 This example code is in the public domain.
12#include <PDM.h>
14// buffer to read samples into, each sample is 16-bits
15short sampleBuffer[256];
17// number of samples read
18volatile int samplesRead;
20void setup() {
21 Serial.begin(9600);
22 while (!Serial);
24 // configure the data receive callback
25 PDM.onReceive(onPDMdata);
27 // optionally set the gain, defaults to 20
28 // PDM.setGain(30);
30 // initialize PDM with:
31 // - one channel (mono mode)
32 // - a 16 kHz sample rate
33 if (!PDM.begin(1, 16000)) {
34 Serial.println("Failed to start PDM!");
35 while (1);
36 }
39void loop() {
40 // wait for samples to be read
41 if (samplesRead) {
43 // print samples to the serial monitor or plotter
44 for (int i = 0; i < samplesRead; i++) {
45 Serial.println(sampleBuffer[i]);
46 // check if the sound value is higher than 500
47 if (sampleBuffer[i]>=500){
48 digitalWrite(LEDR,LOW);
49 digitalWrite(LEDG,HIGH);
50 digitalWrite(LEDB,HIGH);
51 }
52 // check if the sound value is higher than 250 and lower than 500
53 if (sampleBuffer[i]>=250 && sampleBuffer[i] < 500){
54 digitalWrite(LEDB,LOW);
55 digitalWrite(LEDR,HIGH);
56 digitalWrite(LEDG,HIGH);
57 }
58 //check if the sound value is higher than 0 and lower than 250
59 if (sampleBuffer[i]>=0 && sampleBuffer[i] < 250){
60 digitalWrite(LEDG,LOW);
61 digitalWrite(LEDR,HIGH);
62 digitalWrite(LEDB,HIGH);
63 }
64 }
66 // clear the read count
67 samplesRead = 0;
68 }
71void onPDMdata() {
72 // query the number of bytes available
73 int bytesAvailable = PDM.available();
75 // read into the sample buffer
76, bytesAvailable);
78 // 16-bit, 2 bytes per sample
79 samplesRead = bytesAvailable / 2;

Testing It Out

After you have successfully verified and uploaded the sketch to the board, open the Serial Monitor from the menu on the left. You will now see the new values printed.

Microphone data in the Serial Monitor.
Microphone data in the Serial Monitor.

If you want to test it, the only thing you need to do is to place the board next to a speaker and play some music to see how the colors of the RGB LED change based on the music.

RGB LED blinking according to the music.
RGB LED blinking according to the music.

Warning: Remember that depending of the music, lights might blink too fast. Immediately stop playing and consult a doctor if you experience any symptoms of “photosensitive epileptic seizures”.


Sometimes errors occur, if the project is not working as intended there are some common issues we can troubleshoot:

  • Missing a bracket or a semicolon.
  • If your Arduino board is not recognized, check that the Create plugin is running properly on your computer.
  • Accidental interruption of cable connection.
  • The sound of the music is too low or the board is too far from the speaker.


In this simple tutorial we learned how to read sound values from the MP34DT06JTR sensor using the PDM library, and how to use the sensor embedded in the Arduino Nano 33 BLE Sense Rev2 board in order to print out sound values from the environment and visualize them through the RGB LED.

Suggest changes

The content on is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.


The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.