Build a sound-level bar graph

Eight LEDs jump with the music, like the level meter on a mixing desk, with a peak dot that falls slowly. A knob sets the sensitivity.
  • Level:Intermediate
  • Duration: 50 min
  • Ages 11+
  • Platform: Arduino

Tools for this lesson

Helpful for this build. Open a tool to see tips and more projects that use it.

Build a sound-level bar graph – EEMENTOR STEM™ lesson

Mixing desks and old stereos have a row of lights that jump with the music: the VU meter. In this lesson the high-sensitivity sound sensor listens, and eight LEDs show how loud it is: four green, two yellow and two red. A "peak" LED stays lit for a moment at the loudest point and then falls slowly, just like on professional equipment. A knob sets how sensitive it is.

What you'll learn

  • How to measure loudness as the swing of a sound wave
  • How to turn a value into a number of lit LEDs
  • How to make a slowly falling peak indicator
  • How to adjust a sketch with a knob while it runs

What you need

  • UNO R3 board and USB cable (UNO R3 Starter Kit)
  • High-sensitivity sound sensor module (45-in-1 Sensor Kit)
  • 4 green, 2 yellow and 2 red LEDs (UNO R3 Starter Kit)
  • 8 resistors, 220 Ω (UNO R3 Starter Kit)
  • 10 kΩ potentiometer (UNO R3 Starter Kit)
  • The printed parts from the 3D printing box below (light bar), or the cardboard version described there
  • Breadboard and jumper wires (UNO R3 Starter Kit)

Wiring

Breadboard wiring diagram: Build a sound-level bar graph
Breadboard view. Check each connection against the wiring table below.
From To
Sound sensor AO UNO pin A0
Sound sensor + and G 5V and GND
Sound sensor DO Not used
UNO pins 2, 3, 4, 5 220 Ω resistor each, then the green LEDs' long legs
UNO pins 6, 7 220 Ω resistor each, then the yellow LEDs
UNO pins 8, 9 220 Ω resistor each, then the red LEDs
All LED short legs GND
Potentiometer outer pins 5V and GND
Potentiometer middle pin UNO pin A1

The sketch

const int SOUND_PIN = A0;
const int KNOB_PIN = A1;
const int LED_PINS[8] = {2, 3, 4, 5, 6, 7, 8, 9};   // green ... red

int peak = 0;
unsigned long peakTime = 0;

// Loudness = how far the sound wave swings in 30 ms
int loudness() {
  int lowest = 1023, highest = 0;
  unsigned long start = millis();
  while (millis() - start < 30) {
    int v = analogRead(SOUND_PIN);
    if (v < lowest) lowest = v;
    if (v > highest) highest = v;
  }
  return highest - lowest;
}

void setup() {
  Serial.begin(9600);
  for (int i = 0; i < 8; i++) pinMode(LED_PINS[i], OUTPUT);
}

void loop() {
  int swing = loudness();
  // the knob sets the swing that lights all eight LEDs: 20 (sensitive) ... 400
  int fullScale = map(analogRead(KNOB_PIN), 0, 1023, 20, 400);
  int level = constrain(map(swing, 0, fullScale, 0, 8), 0, 8);

  if (level >= peak) {
    peak = level;
    peakTime = millis();
  } else if (millis() - peakTime > 600 && peak > 0) {
    peak--;                              // after 0.6 s the peak falls one step...
    peakTime = millis() - 450;           // ...and then one more step every 150 ms
  }

  for (int i = 0; i < 8; i++) {
    bool on = i < level || i == peak - 1;
    digitalWrite(LED_PINS[i], on);
  }
  Serial.println(swing);                 // open the Serial Plotter to see the sound
}

How it works

Sound is air that vibrates, and the microphone turns it into a voltage that wobbles up and down around a middle value. A loud sound wobbles a lot, a quiet one a little. loudness() reads the sensor as often as it can for 30 ms and keeps the lowest and highest values; the difference, the swing, is a good measure of loudness.

map() turns the swing into a number of LEDs from 0 to 8, and the knob changes the top of that range. The peak works with a timer: it jumps up at once, but only falls when it has been 0.6 seconds since it last moved. Setting peakTime back by 450 ms makes the next step come after only 150 ms, so the dot first waits and then glides down.

Try this next

  • Light the LEDs from the middle outwards for a "stereo" look.
  • Count how many times the red LEDs light in a minute: a noise warning for the classroom.
  • Send the level to Google Sheets with the ESP32, like in the classroom noise logger.

Troubleshooting

  • All LEDs stay on: turn the knob towards 400, or turn the small screw on the sound sensor so its DO LED just goes off in a quiet room.
  • Nothing lights, even when you clap: the sensor must be on A0 and the AO pin, not DO.
  • One LED never lights: check that its long leg goes to the resistor.
  • A printed part is too tight or too loose: printers differ by a few tenths of a millimetre. Sand or file a tight hole, and fix a loose part with a drop of hot glue or a strip of tape.

Build it yourself

Everything used in this lesson comes in one kit, with parts tested by our engineers.

UNO R3 Starter Kit | Arduino IDE Compatible | Free Tutorials $56.00

Shop the kit