Built-in project · Arduino Uno R3

Analog sound module on A0, on an Arduino Uno

A microphone module’s analog output on A0, printed twice a second: clap and the number jumps. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $28. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Reading a microphone module with analogRead
  • Why a quiet signal rides in the middle of the range
  • Printing readings to the Serial Monitor
  • Powering a sensor module from 5V and GND
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Arduino Uno R3
  • Parts: 2

How it works

The KY-038 module has a microphone. Its signal sits on about half the supply voltage, so a quiet room reads near the middle of the range. A loud sound swings the voltage up and down. The module sends it out on AO to A0.

The sketch reads A0 with analogRead, a number from 0 to 1023, and prints it twice a second. Click the module in the simulator to clap and watch the number jump.

Parts list

2 parts, plus the jumper wires. They retail for about $28; on Mokxi they're free.

Kits that can build it

These 2 starter kits have every part in the circuit. Each kit's page lists what else it can build.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Sound sensor, KY-038 pin VCCArduino Uno R3 pin 5V
Sound sensor, KY-038 pin GNDArduino Uno R3 pin GND
Sound sensor, KY-038 pin AOArduino Uno R3 pin A0

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Analog sound module on A0.
//
//   module AO -> A0   VCC -> 5V   GND -> GND
//
// The microphone's signal rides on half the supply, so a quiet room reads
// near the middle of the range and a loud sound swings it.
//
// Click the module to clap.

void setup() {
  Serial.begin(9600);
  Serial.println("Mokxi Uno: Sound module on A0");
}

void loop() {
  int reading = analogRead(A0);
  Serial.println(reading);
  delay(500);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Runs once. Serial.begin(9600) opens serial output at 9600 baud and prints a title line.

void setup() {
  Serial.begin(9600);
  Serial.println("Mokxi Uno: Sound module on A0");
}

loop sketch.ino

Reads A0 with analogRead, prints the number with Serial.println, then delay(500) waits half a second. Since it only samples twice a second, a short clap can be missed on a real board.

void loop() {
  int reading = analogRead(A0);
  Serial.println(reading);
  delay(500);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The reading barely moves This project
    Check that AO goes to A0, VCC to 5V and GND to GND. In the simulator, click the module to make a sound.
  • The digital output is on all the time or never. The pot sets the threshold, and the sensor is only a microphone with a comparator. Sound sensor
    Turn the pot until the DO LED just goes out in quiet, then a clap will trip it. Use the AO pin and analogRead for a level rather than a clean on or off.
  • Analog readings sit at a middle value and look flat, so it seems dead. A loudness change is small and fast. Sound sensor
    Sample many readings quickly and take the peak minus the low. Power the module at 3.3 V or 5 V to match your ADC range, and keep it away from motor noise.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.