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
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.
/*
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.
The same reading in MicroPython, for an ESP32, an ESP32-S3 or a Pico. read_u16 returns SIGNAL as a count from 0 to 65535 on all three. Thonny's View > Plotter draws it.
"""
Analog Microphone - first reading, MicroPython TK27 / /p/tk27
Wiring. Count from the square pad on the TinkerBlock board, parts
up, header at the bottom:
GND -> GND
VCC -> 3V3 (never 5V: a loud sound takes SIGNAL up to VCC)
NC -> nothing (unconnected on the board)
SIGNAL -> GPIO 34 on an ESP32, GPIO 4 on an ESP32-S3,
GP26 on a Raspberry Pi Pico
Thonny
Run > Configure interpreter MicroPython (ESP32) or
MicroPython (Raspberry Pi Pico)
View > Plotter to see the wave
Nothing to install: machine, sys and time are built in.
"""
import sys
import time
from machine import ADC, Pin
# The GPIO number SIGNAL is wired to. ESP32: 34. ESP32-S3: 4. Pico: 26.
MIC_PIN = 4
adc = ADC(Pin(MIC_PIN))
if sys.platform == "esp32": # ESP32 and ESP32-S3
adc.atten(ADC.ATTN_11DB) # the full range, to about 3.1 V
while True:
print(adc.read_u16()) # one instant of the wave
time.sleep_ms(2)There is no Uno here: an Uno cannot run MicroPython. On an ESP32 the atten line widens the ADC's range to about 3.1 V; without it the range stops near 1 V and the resting level is off the top. Stop it with Ctrl-C.
When it does not work
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.
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.
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.
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.
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
Questions about this product
See what other owners have asked, and read their solutions.
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.