Devices & Components
Arduino Uno Rev3
Green LEDs
74HC595 Shift Register
Red LEDs
Resistor 330 ohm
Blue LEDs
MAX9814
Software & Tools
Arduino IDE
Project description
Code
Arduino LED Sound Level Meter: Code
cpp
Arduino LED Sound Level Meter
1const int micPin = A0; // Pin A0 is used for the microphone input 2 3int sample; // Variable to store the current sound sample 4int maxVal = 0; // Variable to track the maximum sound level detected 5int minVal = 1023; // Variable to track the minimum sound level detected 6 7// Shift register pins 8int LatchPin = 3; // Pin 3 is connected to the LatchPin (ST_CP) of the 74HC595 9int ClockPin = 5; // Pin 5 is connected to the ClockPin (SH_CP) of the 74HC595 10int DataPin = 2; // Pin 2 is connected to the DataPin (DS) of the 74HC595 11 12// Binary patterns for the LEDs, representing different sound levels 13byte LEDs0 = 0b10000000; // LED pattern for the lowest sound level 14byte LEDs1 = 0b10000001; 15byte LEDs2 = 0b10000011; 16byte LEDs3 = 0b10000111; 17byte LEDs4 = 0b10001111; 18byte LEDs5 = 0b10011111; 19byte LEDs6 = 0b10111111; 20byte LEDs7 = 0b11111111; // LED pattern for the highest sound level 21 22void setup() { 23 // Setting the shift register pins as outputs 24 pinMode(LatchPin, OUTPUT); 25 pinMode(ClockPin, OUTPUT); 26 pinMode(DataPin, OUTPUT); 27 28 // Setting the microphone pin as an input 29 pinMode(micPin, INPUT); 30 31 // Starting the serial communication for debugging (optional) 32 Serial.begin(9600); 33} 34 35void loop() { 36 // Process microphone data to determine the peak-to-peak value 37 for (int i = 0; i < 100; i++) { // Take 100 samples to find the sound range 38 sample = analogRead(micPin); // Read the current sound level from the microphone 39 if (sample > maxVal) { // Update maxVal if the current sample is greater 40 maxVal = sample; 41 } 42 if (sample < minVal) { // Update minVal if the current sample is lower 43 minVal = sample; 44 } 45 } 46 47 int peakToPeak = maxVal - minVal; // Calculate the peak-to-peak value 48 49 // Convert the peak-to-peak value to a voltage level (0-5V range) 50 float voltage = (peakToPeak * 5.0) / 1023.0; 51 52 // Prepare to send data to the shift register by setting the latch pin low 53 digitalWrite(LatchPin, LOW); 54 55 // Determine which LED pattern to display based on the voltage level 56 if(voltage > 2.4){ // Highest sound level 57 shiftOut(DataPin, ClockPin, LSBFIRST, LEDs7); // Send the LED pattern for the highest sound 58 digitalWrite(LatchPin, HIGH); // Latch the data to display the LEDs 59 } 60 else if(voltage > 2.1){ // Second highest sound level 61 shiftOut(DataPin, ClockPin, LSBFIRST, LEDs6); // Send the corresponding LED pattern 62 digitalWrite(LatchPin, HIGH); 63 } 64 else if(voltage > 1.8){ // Third highest sound level 65 shiftOut(DataPin, ClockPin, LSBFIRST, LEDs5); // Send the corresponding LED pattern 66 digitalWrite(LatchPin, HIGH); 67 } 68 else if(voltage > 1.5){ // Medium-high sound level 69 shiftOut(DataPin, ClockPin, LSBFIRST, LEDs4); // Send the corresponding LED pattern 70 digitalWrite(LatchPin, HIGH); 71 } 72 else if(voltage > 1.2){ // Medium sound level 73 shiftOut(DataPin, ClockPin, LSBFIRST, LEDs3); // Send the corresponding LED pattern 74 digitalWrite(LatchPin, HIGH); 75 } 76 else if(voltage > 0.9){ // Low-medium sound level 77 shiftOut(DataPin, ClockPin, LSBFIRST, LEDs2); // Send the corresponding LED pattern 78 digitalWrite(LatchPin, HIGH); 79 } 80 else if(voltage > 0.6){ // Low sound level 81 shiftOut(DataPin, ClockPin, LSBFIRST, LEDs1); // Send the corresponding LED pattern 82 digitalWrite(LatchPin, HIGH); 83 } 84 else if(voltage > 0.0){ // Lowest sound level 85 shiftOut(DataPin, ClockPin, LSBFIRST, LEDs0); // Send the corresponding LED pattern 86 digitalWrite(LatchPin, HIGH); 87 } 88 89 // Reset maxVal and minVal for the next set of samples 90 maxVal = 0; 91 minVal = 1023; 92}
Downloadable files
Arduino LED Sound Level Meter: Circuit
Arduino LED Sound Level Meter
loudness_meter_bb.jpg

Documentation
Arduino LED Sound Level Meter: Circuit
Arduino LED Sound Level Meter
loudness_meter_bb.jpg

Comments
Only logged in users can leave comments