analog microphone/Reading it/06. The first reading
Reading it · 06 of 10

The first reading

Three wires, no library, and a sketch that prints the raw reading as fast as the serial port allows. In the Serial Plotter silence is a flat line at your board's resting level, somewhere in the middle of the scale, and a clap is a burst above and below it. Any one reading is the wave caught at one instant.

Three wires

GND to GND. VCC to your board's logic supply: 5V on an Uno, 3V3 on the three 3.3 V boards. SIGNAL to an analog pin. NC stays unconnected.

The analog pin is the same in every sketch in this book, and the same as in the ambient light sensor's: A0 on an Uno, GPIO 34 on an ESP32, GPIO 4 on an ESP32-S3, GP26 on a Pico. The two ESP32 pins are on ADC1, the converter that keeps working when Wi-Fi is on.

Never VCC from 5V beside a 3.3 V board. In silence SIGNAL sits well below VCC, but a loud sound turns the transistor off for an instant and SIGNAL goes all the way up to VCC.

The sketch

analogRead(MIC_PIN) measures the voltage on the pin and returns it as a count, and the sketch prints each one on its own line, about every 2 ms plus the time the printing takes. There is no pinMode: an analog pin does not need one to be read.

Open Tools > Serial Plotter instead of the monitor. The plotter draws the numbers as a line, which is the only sensible way to look at a microphone.

What you should see

One reading catches the wave anywhere
Sound
Resting level
587
Readings so far
none
Spread
0
SIGNAL rests at about 587 on this Uno. Press run and the sketch takes eight readings of the same sound, a few milliseconds apart.

In silence the line is flat, give or take a count or two: your board's resting level. On an Uno it can be anywhere from about 325 to 815, not 512. Clap near the capsule and the line bursts above and below it, then settles back.

Press run in the figure and read what the sketch prints for a sound that lasts. The readings scatter above and below the resting level, because each one catches the wave at a different instant. A single reading of 700 does not mean loud and a single 450 does not mean quiet: both can be the same sound. The old page's sketch did exactly that, sorting single readings into loud and quiet by their size. How far the readings spread is what grows with loudness, and resting level and swing measures it.

The code

No library. analogRead returns SIGNAL as a count: 0 to 1023 on an Uno and a Pico, 0 to 4095 on an ESP32 or ESP32-S3. Open Tools > Serial Plotter at 115200 rather than the monitor, and change MIC_PIN to the pin you wired SIGNAL to.

mic_first_reading.ino
/*
  Analog Microphone - first reading                     TK27 / /p/tk27

  Wiring. Count from the square pad on the TinkerBlock board, parts
  up, header at the bottom:

    GND    -> GND
    VCC    -> 5V on an Uno; 3V3 on an ESP32, ESP32-S3 or Pico
              (a loud sound can take SIGNAL up to VCC)
    NC     -> nothing   (unconnected on the board)
    SIGNAL -> A0 on an Uno, GPIO 34 on an ESP32, GPIO 4 on an
              ESP32-S3, GP26 on a Raspberry Pi Pico

  Arduino IDE
    Tools > Board                 your board, e.g. Arduino Uno
    Tools > Port                  the one that appears when you plug in
    Tools > USB CDC On Boot       Enabled   (ESP32-S3 only)
    Tools > Serial Plotter        at 115200, to see the wave
    No library needed.
*/

// The analog pin SIGNAL is wired to.
// Uno: A0. ESP32: 34. ESP32-S3: 4. Pico: 26.
const int MIC_PIN = A0;

void setup() {
  Serial.begin(115200);
}

void loop() {
  // One instant of the wave: silence is the resting level,
  // a sound swings above and below it.
  Serial.println(analogRead(MIC_PIN));
  delay(2);
}

The flat line is your board's resting level, and it will not be 512. Each point is one instant of the wave, so a clap draws a burst above and below the line rather than a single high number. The next article turns the burst into one number.

When it does not work

The plotter shows a flat line and clapping does nothing.

Clap closer, within a hand's length of the capsule. If it still does not move, check the three wires by counting from the square pad: GND, VCC, NC, SIGNAL, and check that MIC_PIN names the pin SIGNAL is on. A line flat at 0 means VCC is missing; flat at the top of the scale means GND is.

The flat line is nowhere near the middle of the scale.

That is normal. The resting level depends on the gain of your board's transistor, so from 5V on an Uno it can be anywhere from about 325 to 815. Write down yours: the next sketch measures it for you.

Talking does not show up at all.

Ordinary speech a metre away moves SIGNAL by a few millivolts, which is a few counts on an Uno, lost in the line's own wobble. The block is for loud sounds. Talk close to it, or clap, and the swing is obvious.

The serial monitor stays empty on my ESP32-S3.

Set Tools > USB CDC On Boot to Enabled and upload again. Without it the S3's USB port does not bring up a serial port at boot, so the sketch runs with nowhere to print.

Where this goes next

Measure the resting level once, then the swing over a window: the number that grows with loudness.

Resting level and swing

Edit this page — content/books/analog-microphone/the-first-reading.mdx

Community

Questions about this product

See what other owners have asked, and read their solutions.

Ask a question ↗

Analog Microphone

Loading discussions…

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum