Make a digital voice-powered
Add a microphone module and OLED panel to your Arduino Nano and make a voice-powered oscilloscope. Darren Yates explains how.
The fact we can hear changes in air pressure and recognise different frequencies is fascinating. But what does sound look like? What can we see by looking at it? It’s likely a question asked in the early 1920s when the first reliable cathode-ray tubes were developed. For the first time, scientists could watch how sound captured by a carbon microphone caused variations in voltage that could be seen on a new cathode-ray oscilloscope or ‘CRO’. This month, we’re replicating that experiment with modern-day components, combining an Arduino Nano, a 128 x 64-pixel OLED panel and a low-cost audio microphone module to create a digital voice-operated oscilloscope.
ADDING A MIC TO ARDUINO
It’s probably been one of the most requested topics for us to cover, how to add a microphone to an Arduino. It’s tricky for a number of reasons, but mostly comes down to whether you want to just detect sound or have access to actual audio. You can buy microphone modules to do either task, but you have to choose carefully — that’s because sound detection modules deliver a digital ‘1 or 0’ voltage output, whereas a genuine audio microphone module must give you an analog audio output. We spent all of $1.50 purchasing a MAX9812 microphone module from eBay, a complete mic setup with a fixed 20dB amplification factor or ‘gain’ amplifier and a small electret mic on top. It requires 5VDC power, which you can take from the Arduino board.
To feed the module’s audio output into an Arduino board, you have to hook it up to one of the Arduino’s analog-to-digital converter (ADC) inputs. But there’s a catch — you must also bias the ADC input to half-supply voltage. The ADC inputs of the Arduino Nano are designed to turn DC voltage into a digital number between 0 and 1,023. But audio from the MAX9812 output is AC voltage only. To ensure we capture the full swing of that AC voltage, we need to set or ‘bias’ the ADC input to half the supply voltage (half of 5VDC is 2.5VDC). This way, the audio signal can swing equally above and below this ‘bias voltage’ and we’ll capture the full range of that signal.
The easiest way to create the bias voltage is to connect up two 10kohm resistors as a voltage divider connected between a 5VDC supply rail and ground (0V). Ohm’s Law ensures the DC voltage at the junction of the two resistors is 2.5VDC and we connect this to the ADC input A0, along with the microphone module audio output. If you don’t do this, you’ll lose the negative-going half of each audio cycle since the ADC input can’t go any lower than zero-volts.
We cheat and use analog input A3 as a digital output to supply 5VDC power — the microphone needs little current, so this works just fine. the ADC input voltage with a reference voltage. The default on the Arduino Nano is 5VDC for standard 5V/USB-powered boards and the ADC has a 10-bit range, giving a digital value of between 0 and 1,023. So if the input voltage is 2.5VDC (ie, half the reference voltage), the ADC will create a digital sample of 512 (half the 1,024 range) and if it’s 1.25V (a quarter of Vref), the ADC sample will be 256 (a quarter of 1,024). This continues for every sample. The actual conversion process is known as ‘successive approximation analog to digital conversion’ and you’ll find at least one ADC in every smartphone turning your voice into digital phone calls.
TURNING SOUND INTO VISION
Now for the fun bit — we need to capture audio samples at a set rate, so that we can reproduce the waveform on the display. The OLED panel is 128 pixels wide, so we set aside a 128element byte array buffer called ‘buf’. We then capture 128 samples of audio and store those samples in the buffer. As soon as that last sample is captured, we display each sample on the OLED panel set as a standard X-Y Cartesian plot, where the Y-axis corresponds to each sample value, representing the voltage on the ADC input at time of sampling. As soon as the last pixel is drawn, we grab the next round of 128 samples and display those. With a sample rate of 22,050Hz, it takes just 5.8 milliseconds to grab 128 samples and around another 15 milliseconds or so to display them on the OLED panel. That means each pass or ‘sweep’ takes around 20 milliseconds, giving us a visual frame rate of roughly 50 frames per second or 50Hz. You can pick up a 0.96-inch 128 x 64p OLED panel for around $5 on eBay but make sure you get the four-pin version supporting the I2C (inter-integrated circuit) bus. And most importantly, if you’re following our overlay diagram, make sure you watch the VCC and GND pin polarity. The panel version we’ve used has the VCC pin on the outside — there are older versions with these two pins swapped. Plug an older module in with the voltage pins reversed and you’ll blow it up, so be very careful.
WATCH DIFFERENT SOUNDS
If you have an Android phone, head to Google Play, download Waveform Generator ( tinyurl.com/yddv7una) and place your phone’s speaker near the microphone. Press the power button, change the frequency and see what effect that has on the display.
Unfortunately, 20dB isn’t really enough gain to give a decent visual output (you really need nearer to 40dB), so we’ve increased the gain in code to boost the sample level, a bit like using digital zoom on your phone camera — unfortunately, this also boosts the noise, which is why the oscilloscope line or ‘trace’ isn’t dead-flat with no input.
GIVE IT A GO
You’ll find the source code on our website at apcmag.com/magstuff. Download it, unzip the file and copy the contents of the ‘libraries’ subfolder into the same of your Arduino IDE (get this free from arduino.cc/downloads). Open the voicescope.ino source code, flash it to your Arduino Nano board, build the project and away you go.
It certainly won’t worry professional oscilloscope makers like Tektronix, but for a $10 oscilloscope, our VoiceScope follows the same principles as the big models. It’d even make a great science experiment, so why not give it a go!
HOW ANALOG-TO-DIGITAL CONVERSION WORKS ADC samples are created by comparing
The Arduino Nano is the brains behind our VoiceScope.
This tiny OLED panel displays the oscilloscope trace. This MAX9812 microphone module will set you back $ 1.50 on eBay.
Use this overlay diagram to build your own VoiceScope.
The VoiceScope is built on a tiny 170-tiepoint breadboard.
Waveform Generator turns your Android phone into a signal generator.