Encoding, analyzing, and playback of sound data With Arduino. Sound data represents values of air pressure over time. In the graph below the y axis represents the air pressure (with the value 75 being no change in pressure). The x axis represents the slices of time. In this sample we take 1000 time slices. Microphones act as a variable resistor to encode changes in air pressure into changes of resistance. A typical microphone has a diaphragm that moves with air pressure. This diaphragm is connected to a small magnet that moves with the diagram. Around the magnet is a coil of wire that carries an electrical current. As the magnet moves the resistance in the wires changes, converting the changes in air pressure to changes in resistance. http://www.mediacollege.com/audio/images/mic-dynamic.gif Note that the wire has two leads. One lead is positive and the other is ground. A layout drawing of a microphone would look like: 5 Volts Ground In this project our objective is to wire a microphone to the Arduino board as a variable resistor sensor and then write code that will read, encode, analyze, and playback sound data. Part 1: Wiring the Microphone to the Arduino and Breadboard. Microphones have two types of connectors; XLR and Tip / Sleeve. We will use Tip / Sleeve microphones for this lesson. Tip / Sleeve connectors come in two sizes (3.5 millimeters and ¼ inch). In a Tip / Sleeve connector the tip carries the 5 Volt current and the sleeve is the ground: Sleeve = ground Tip = 5V 1. The microphone plugs into a receiver jack and the tabs on the jack allow for wires to connect to 5 volts and ground. Take your 3.5 mm receiver jack and the alligator clips and connect them as shown: Sleeve = Ground Tip = 5 volts 2. The schematic drawing for a microphone looks like this: 3. Wire the Microphone, Red, Yellow, and Piezo Speaker to the Breadboard and Arduino: Part 2: Encoding Sound Data To store sound data, we will create an array of bytes (8 bit values from 0 to 255) to store the microphone data. We will store 1 second of sound data taken in 1000 millisecond slices. (Because of the size of the Arduino RAM – we are limited in array size to 1000 items.) We will also iterate through this array to playback data through the piezo speaker. 1. Start a new Arduino Sketch and save it as lastname sound encoding. 2. Setup the arrays and variables to work with sound: 3. We want to test the microphone. Write a function to read and Serial print the Microphone data. 4. Write the setup function to prepare the LED Pins, Serial Monitor, and Piezo: 5. Write the Loop Function to display microphone data: 6. Save and run the program. Your Serial monitor should show the sound data. My numbers centered around 975: 7. After collecting some numbers. Stop the program and copy all the data from the Serial Monitor. (Control – A, then Control – V) 8. Open Excel and paste the numbers into a column. 9. Use the Line Graph Tool and create a Line graph of the data. It should look like a sound wave. 10. Do some experiments and make sure the microphone works and take note of the ‘center’ of the readings. 11. We want to use the lights to tell us the microphone is active and give us feedback on the level of the sound. Write an indicator function to blink the yellow light based on amplitude of signal: Sound Waves and Integrals: Many of the goals of calculus depend on the concept of measuring the area under a curve of a function. With sound, the area under a sound wave can be measured computationally (arrays and for loops) and then used to analyze the wave and differentiate between sounds: 12. Modify the loop() function to show the indicator lights: 13. Write a record function to take the microphone data and store it in an array: 14. Write a playback function to take the data from the array and convert it to HIGH or LOW pulses for the piezo. NOTE: Because of memory limitations with the Arduino, we are taking a 1000 Hz sample speed for one second. Typically we sample sound at 44,000 Hz to 192,000 Hz. Our playback will not quite be recognizable as sounds we record. 15. Add the record function to the setup() to the board will record 1 second of sound data on startup: 16. Add the playback function beneath the record in the setup() function so the piezo will playback one time. 17. Add a function to print the values from the sV array to the Serial Monitor. 18. You can call this function in the setup() and then collect the sound data to paste in excel and examine the waveforms. Additional Ideas: Write a function to calculate the average value of the sound data in the array. This average value can be used to make decisions for voice/sound controlled devices. Research RMS calculation (Root Mean Squared). Write a function to calculate the Root Mean Squared of the sound data. Combine the Sound control and your vehicle robot to make a voice controlled car. Reverse the data in the sV array and play the sound backward. Write a function to have the piezo ‘match pitch’ with your voice. There is a library available on the internet that does voice analysis (identifies vowel and consonant sounds). Research the voice recognition library to get the Arduino to recognize specific words. (https://arjo129.wordpress.com/experiments/%C2%B5speech/)
© Copyright 2026 Paperzz