Sound sensor on A0 and pin 3, on an Arduino Uno
A sound sensor module with its analog output on A0 and its digital output on pin 3: the level is printed, and the Uno’s LED on pin 13 lights while the sound is over the threshold. 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 $31. On Mokxi they're free.
What you'll learn
- Analog versus digital outputs on a sensor module
- A comparator and a threshold
- Reading both with
analogReadanddigitalRead - Showing a state on an LED
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Arduino Uno R3
- Parts: 3
How it works
The KY-038 sound module has a microphone and two outputs. AO goes to A0 and gives the microphone's amplified level. DO goes to pin 3 and goes HIGH when the level passes a threshold, which a little screw on the module sets. It goes LOW again when the sound falls back.
Five times a second the sketch reads A0 and pin 3. It copies the digital output to the Uno's LED on pin 13 and prints the level, adding "loud" when the digital pin is HIGH. Drag the sound level on the module to make noise.
Parts list
3 parts, plus the jumper wires. They retail for about $31; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × Sound sensor, KY-038In Sensor Starter Kit.
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 pin | Connects to |
|---|---|
| Sound sensor, KY-038 pin DO | Arduino Uno R3 pin 3 |
| Sound sensor, KY-038 pin VCC | Arduino Uno R3 pin 5V |
| Sound sensor, KY-038 pin GND | Arduino Uno R3 pin GND |
| Sound sensor, KY-038 pin AO | Arduino 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.
// Sound sensor module: how loud, and is it over the line?
//
// module AO -> A0 module DO -> pin 3 VCC -> 5V GND -> GND
// the Uno's own LED on pin 13 shows the digital output
//
// The module has a microphone and two outputs. AO is the microphone's level,
// amplified, for analogRead(). DO is a comparator: it goes HIGH when the level
// passes the threshold set by the little screw on the module, and LOW again
// when it falls back.
//
// Drag the sound level on the module to make some noise.
const int ANALOG_PIN = A0;
const int DIGITAL_PIN = 3;
const int LED = 13;
void setup() {
pinMode(DIGITAL_PIN, INPUT);
pinMode(LED, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: sound sensor on A0 and pin 3");
}
void loop() {
int level = analogRead(ANALOG_PIN);
bool loud = digitalRead(DIGITAL_PIN) == HIGH;
digitalWrite(LED, loud ? HIGH : LOW);
Serial.print("level ");
Serial.print(level);
Serial.println(loud ? " loud" : "");
delay(200);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Sets DIGITAL_PIN (pin 3) as an input and LED (pin 13) as an output, then starts serial at 9600 and prints a title. A0 needs no setup.
void setup() {
pinMode(DIGITAL_PIN, INPUT);
pinMode(LED, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: sound sensor on A0 and pin 3");
}loop sketch.ino
Reads the level from ANALOG_PIN and checks whether DIGITAL_PIN is HIGH, storing that in loud. It writes loud to the LED and prints "level" and the number, with " loud" added when loud is true. Then it waits 200 ms.
void loop() {
int level = analogRead(ANALOG_PIN);
bool loud = digitalRead(DIGITAL_PIN) == HIGH;
digitalWrite(LED, loud ? HIGH : LOW);
Serial.print("level ");
Serial.print(level);
Serial.println(loud ? " loud" : "");
delay(200);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The level changes but the LED never lights This project
The LED follows the digital output on pin 3. Check that DO goes to pin 3, and that the level passes the module's threshold. - The level never changes This project
AO must go to A0, not DO. - 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.