Do you like noisy open plan offices? Neither do we! This project helps visualizing the amount of decibels in real time, to encourage employees to work more calmly.
Creating the deviceThe device is really easy to build. We only need an Android Things board (e.g. a Raspberry Pi 3), an USB microphone and an APA102 LED strip. See the schematics below for additional information.
Getting the decibel valueThe USB microphone will record audio using the MediaRecorder from the Android SDK. Since we do not want to keep the audio file, both for security and privacy reasons, but also because the device internal space is limited, we will record to /dev/null. Then, we can call the mediaRecorder.maxAmplitude method every 250ms to get the sound pressure value and convert it to decibels using the following formula:
val decibels = 20 * Math.log10(volume.toDouble()).toFloat()
Once we have the decibels value, we can forward this data to the LED strip.
Displaying the decibel value with the LED stripWe are going to use the driver-apa102 from the official contrib-drivers github repository. First, we need to initialize the LED strip:
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun start() {
ledStrip = Apa102(SPI_NAME, LED_MODE).apply {
brightness = LED_BRIGHTNESS
write(IntArray(NUM_LEDS))
}
}
Then, we need to show the decibels. Our LED strip has 60 LEDS. It should start from Green to Red, which means in HSB from 90° to 0°.
val colors = IntArray(NUM_LEDS)
for (i in 0 until NUM_LEDS) {
colors[i] = Color.HSVToColor(255, floatArrayOf((HSV_GREEN - (i.toFloat() * (HSV_GREEN - HSV_RED) / (NUM_LEDS - 1)) + 360) % 360, 1.0f, 1.0f))
}
ledStrip!!.write(colors)
The LED strip will look like that:
To show a progress bar, we will turn off LEDs above the current amount of decibels setting the color to 0:
var decibelLevel: Float
for (i in 0 until NUM_LEDS) {
decibelLevel = i.toFloat() * (MAX_DECIBELS - MIN_DECIBELS) / (NUM_LEDS - 1) + MIN_DECIBELS
colors[i] = if (decibels > decibelLevel) getColor(i) } else 0
}
ledStrip!!.write(colors)
And we have our decibel indicator working!
This would be a good idea to periodically send those data into Google Cloud IoT Core, so that we could inspect those data over the time, and potentially add some machine learning to detect abnormal noisy behaviors and warn people when they are making too much noise.
Comments