Summary
This Lab mainly involves the audio aspect of the robot, ie. audio detection using a microphone.As such the lab,involves various steps that are needed for adequate and fairly adequate audio detection to occur. The steps are listed below:
1. Simulation of highpass, lowpass and bandpass passive filters
2. Build the basic microphine and test with Matlab and the Nano
3. Build the amplifier circuit for the mic and test it out with Matlab and Nano
4. Perform FFT on the Nano
5. Install an override button
Background
During the final demo of the robot, the robot should be able to listen to a melody. In that melody is a specific note that will be played. When the note is played, the robot should start moving and navigating the maze. In event of failure to move upon note detection, there should be an override button that will cause it to start navigating anyway.Note Detection and Its Relation to Fourier Analysis
Sound is in the form of waves. The microphone picks up sound and converts the sound to electrical signals which are then sent to the Nano. These signals are usually processed in the time domain. ie. When graphed, it is usually a graph of Amplitude of sound vs Time. This kind of signal processing however does not help us in our aim to detect the particular note. Before mentioning how Fourier analysis helps, I should mention that each note ie. musical notes, correspond to a particular frequency on the spectrum. Hence detecting a note on the Nano means detecting a frequency. Fourier Analyis is a means of converting signals from the time domain into the frequency domain.Using LTSpice to simulate filters
LTSpice is a free circuit simulation tool used to provide a fairly accurate description of how a constructed circuit should behave. Using LTSpice, three filters were simulated. A lowpass filter, a highpass filter and a bandpass passive filter.A lowpass filter filters out low frequencies below a certain threshold.
A highpass filter filters out high frequencies above a certain threshold.
A bandpass filter is like a combination of a highpass and lowpass filter. It only allows a range of frequencies to pass through. ie. frequencies below a certain threshold are blocked, as well as frequencies above a certain threshold.
High Pass Filter

Low Pass Filter

Setting up the Microphone
The microphone has two wires, red and black. The black wire is connected to ground and the red wire is connected to An Arduino input pin to read input from the mic. However, the connection is not straightforward. It is connected to a resistor and capacitor in series and then to the Arduino Analog Input.
The wires of the microphone are too thin. To enable a proper connection, a piece of jumper wire is connected to both the ground and red wire. However we were careful about the jumper wire length so as to reduce noise.
Next, we set up the Nano to collect sound from the microphone by reading from the input pin and printing to the Serial Monitor. For this lab, analogRead() cannot be used as it has become to slow to satisfy the requirements of the data collection. This time, the ADC(Analog to Digital Converter) is coded manually and set up in free running mode. Disconnect the USB cable and make sure the Serial Monitor is closed after verifying that the ADC free running mode works.
The next step is to set up MATLAB to collect and analyze the data from the Serial Monitor. Using the MATLAB code, set the initial Frequency and Final Frequency to play, as well as the Serial port from which the data should be read.
The Matlab code should then do the following:
1. Play a sound of increasing frequency over the computer's speakers.
2. Collect with MATLAB, the sound from the Nano. The Nano performs the ADC conversion while the sound is being played.
3. Fourier Analysis is performed on the data in MATLAB
4. The Fourier Graph is displayed.
The same steps are then repeated. However, this time, the usb device is first disconnected, the 9v battery plugged in and the USB reconnected so communication can resume. The 9V battery ensures the Nano runs on its own power source and reduces the noise in the data collected by MATLAB, providing a clearer plot.
Upon zooming into the spectrum, one notices a peak that represents 500Hz, the signal picked up by the mic. One also tends to notice smaller peaks, representing the harmonics of the frequency.
There were no issues that required troubleshooting at this point, except the issue that came about because I copied wrong code from the datasheet.
The graph with the 500Hz frequency is shown below:
Amplifier Circuit
If you look closely at the graphs above, you would realise that the 500Hz signal is relatively weak. The basic microphone circuit is actually not good and needs to be amplified. Hence the Amplifier Circuit. The next step is to construct the Amplifier Circuit. As in earlier setups, care must be taken to make the connecting wires as short as possible to reduce noise. The signal after amplification is shown below:
Compared to the previous graph, one can see that the magnitude of the 500 Hz signal is greater than when it wasn't amplified. The noise in the amplified signal also seems to be sparse.
Performing the Fast Fourier Transform (FFT) on Arduino
For the Final Demo, the robot needs to be able to perform Fourier Analysis on its on. Therefore , it has to be performed on the Nano itself. It's not as powerful but does the trick.
To be able to do this we installed an FFT library on the Arduino IDE and we editing an already created Arduino file to make the library work.
Once everything is coded, the sketch will read 257 ADC values at a specific time interval called by an interrupt routine. The first value is then discarded, then the remaining used for calculations through the FFT library.
The final result is 128 bins printed to the serial monitor. The data can be taken and plotted to visualize the spectrum.
500 Hz Frequency FFT plot
700 Hz Frequency FFT plot

900 Hz Frequency FFT plot

Override Button
As stated earlier, should the sound detection feature fail, an override button is pushed to cause the robot to start navigating the maze.
Current State of the Board

Note: No Serial Monitor Snippets were added because they appear to be gibberish. Rather, the printouts of the Serial Monitor have been used to plot the graphs show in the webpage