APC Australia

Make a scrolling sound level meter

Darren Yates explains how to turn a microphone, an 8x8 LED backpack and an Arduino Nano into a scrolling sound level meter.

-

Learning to code is one of the most important skills you can pick up — but if you can add to it ‘maker’ skills, understand­ing electronic­s and microcontr­ollers, you open up a whole world of possibilit­ies. Really, the sky and your imaginatio­n are the only limits. A couple of months ago, we built a small voice-powered oscillosco­pe using a budget microphone module. This month, we’re switching the display to an 8x8-LED pack and making a sound level meter with scrolling display that could be used by the hearing-impaired or as the basis of a triggered alarm project.

ARDUINO ANALOG INPUTS

The Arduino Nano is a compact USBpowered microcontr­oller board with multiple I/O (input/output) pins you can connect up to various devices. It also has a number of analog inputs that connect in turn or ‘multiplex’ to a single analog-to-digital converter (ADC). Digital inputs on the Nano have two levels or ‘states’ — zero-volts, also called ‘ground’ or ‘digital-zero’, and 5V, also known as ‘Vcc’ and ‘digital-one’. By contrast, the Nano’s analog inputs have many levels — up to 1,024 in fact. Those levels are proportion­al to the input voltage applied to an analog pin, anywhere between 0V and 5V.

CONNECT UP A MICROPHONE

Over the years, we’ve had lots of requests from readers asking how to connect up a microphone to an Arduino analog input. The short answer is you can’t — at least not without help. The problem is, microphone­s generate a very small voltage on their own, typically less than one-millivolt (1/1000th of a volt), whereas the Arduino analog input expects the voltage to swing between 0 and 5V. At that rate, a tiny 1mV signal just isn’t going to register. Search eBay and you’ll find this tiny microphone amplifier module pictured for about $1. It features a microphone and preamplifi­er, which increases or ‘amplifies’ the audio signal from the microphone to something we can use.

However, we still need to set or ‘bias’ the analog input with a DC voltage that sets the input at half its maximum level. Why? Audio signals are alternatin­g- current (AC) voltages, which means the voltage swings positive and negative about its steady level. To ensure the Arduino ADC can pick up the whole signal, we bias the input to half the supply rail using two 10kohm resistors and connect the output from the microphone amplifier via a 10uF capacitor, allowing the audio (AC) signal through to the analog input but blocking the DC voltage.

HT16K33 LED PACK

The only other major component, apart from the Arduino Nano on the

170 tie-point breadboard, is the KT16K33 8x8-LED pack. It only has four pins to connect up all 64 LEDs, but does so through its own microcontr­oller chip and talks to our Arduino Nano chip over a digital interface connection or ‘bus’ called ‘Inter-integrated circuit’ or I2C. The Nano only has one I2C bus, located on analog pins A4 and A5, which is where we plug in the LED pack. However, since pins A2 and A3 are also available, we switch these analog inputs into digital outputs using software, setting A2 to 5V output and A3 to ground, creating a simple power supply to run the LED pack. Normally, this isn’t a great idea, because these pins can only supply 40mA of current before they get cranky. But by setting the brightness to near-minimum, we can keep the display’s demand for current down to happy levels and the whole thing works.

HOW THE CODE WORKS

That’s the hardware, now for the Arduino code or ‘sketch’. What we want to do is have the display scroll at the rate of roughly 10fps, so that any audio noise will register vertically and proportion­al to its volume, while scrolling across the display. To do that, we start with creating a special code device called a ‘timer’ using the SimpleTime­r library. It allows us to create a trigger every 100millise­conds that launches a separate code function we call ‘updateMete­r’. It updates the display with the latest sound values. This code function only takes a few millisecon­ds to run, so for the rest of the time, the Arduino is sampling the analog input A1 and recording the largest digital value during that time. Recording the largest value is important, for otherwise, a short sound burst could register a value but disappear back to zero before the next display update function trigger occurs and we don’t see any display change. By recording the maximum sound level for that period, even the shortest peak, say from clapping your hands, is recorded and displayed. This is the same basic technique used in most sound alarms.

BUILDING THE PROJECT

We purchased all the components from eBay and built the whole thing on a tiny 170-tiepoint breadboard. Use the overlay wiring diagram as a guide to building your own. Really, it shouldn’t take you more than 15 minutes. The next task is flashing the source code to the Arduino chip. For that, grab our source code pack from our website ( apcmag.com.magstuff), unzip the file, copy the contents of the /libraries subfolder into the same of your Arduino IDE installati­on (don’t have the IDE? Download the latest version free from arduino.cc/downloads). Shut down the Arduino IDE and restart it so the new libraries take hold. Select File > Open and choose the ‘soundmeter_v1.ino’ source file from the download, then choose Tools > Board and set it to ‘Arduino Nano’. Plug the Nano board into computer via a USB cable. Back on the Arduino IDE, select Tools again, this time choose Port and select the COM port for your board (don’t choose COM1, that’s your mouse). Now when you’re ready, select Sketch from the menu and Upload — the source code will compile and begin uploading to the Arduino Nano board (you should see LEDs on the board start flashing). Provided you’ve build the project, the bottom row of the 8x8 LED pack should light up a second or two after the upload is complete. Make a noise into the microphone and it should register and scroll across the display. If so, congrats — you’re done.

SERIAL MONITOR

Clicking the green magnifying glass icon at top-right of the Arduino IDE window starts the Serial Monitor, which displays data coming back from the Arduino — in this case, the array of LED values as they scroll across. Come up with your own code to get the Arduino to send across a trigger code to launch your own code to sound an alarm, send a tweet or an email — your choice.

 ??  ?? Our Scrolling Sound Level Meter uses an 8x8-LED pack and microphone.
Our Scrolling Sound Level Meter uses an 8x8-LED pack and microphone.
 ??  ?? The Arduino Nano microcontr­oller connects to your PC via USB cable.
The Arduino Nano microcontr­oller connects to your PC via USB cable.
 ??  ?? The Arduino IDE Serial Monitor will output the LED array data as it scrolls.
The Arduino IDE Serial Monitor will output the LED array data as it scrolls.
 ??  ?? Use this overlay wiring diagram to build your own version.
Use this overlay wiring diagram to build your own version.
 ??  ?? You can code each LED in the HT16K33 8x8-LED pack individual­ly.
You can code each LED in the HT16K33 8x8-LED pack individual­ly.
 ??  ?? You’ll find this tiny three-pin microphone amplifier module on eBay for $ 1.
You’ll find this tiny three-pin microphone amplifier module on eBay for $ 1.

Newspapers in English

Newspapers from Australia