15 of June 2024
This template is for the Gravity Analog Sound Meter released by DFRobot. The code has some boilerplate functionality where the template is able to connect to a designated Wifi network, as well as listen to over the wire OTP updates, whilst continuously polling the Gravity Analog Sound Meter and publishing the measured value to an MQTT bus.
At the time of writing, there is no runtime functionality and the template should just be modified in order to set the Wifi and/or OTP credentials after which it can be wired up and left to run on its own.
The theory behind the template revolves around the linear design of the Analog Sound Meter in that the output voltage is calibrated out of production to correspond to some amount of decibels. As per the specifications, the noise level that the Analog Sound Meter is capable of registering ranges from to with those two values corresponding to , respectively the maximum voltage that the analog pin is capable of reading (for most micro-arduinos, such as the WeMoS, that would be ). Given the linear progression, the only thing left to do is just measure the voltage of the analog pin connected to the meter's signal pin and then linearly map the voltage range into the decibels range .
Here is the code responsible for measuring noise levels using DFRobot's Gravity Analog Sound Level Meter 1.0 within the template provided in the code section:
// https://grimore.org/fuss/mathematics/algebra#linearly_map_a_value_in_a_range_into_another_range float mapValueToRange(float value, float xMin, float xMax, float yMin, float yMax) { return yMin + ((yMax - yMin) * (value - xMin)/(xMax - xMin)); } // measure noise level (dbA) float ai, vc, noise; ai = analogRead(DECIBELMETER_PIN_ANALOG); vc = mapValueToRange(ai, 0, 4095, 0, MAX_ANALOG_VOLTAGE); noise = vc * 50.0;
where:
DECIBELMETER_PIN_ANALOG
is the analog pin that the Analog Sound Level Meter connects to on the ESP,MAX_ANALOG_VOLTAGE
is the maximum in voltage on the ESP (sometimes ranges from to but can also range from to )
As can be observed, after the value from the analog pin is measured, the value is mapped to the voltage range of the pin. The resulting scaled voltage is then multiplied by , which is a cheap operation compared to mapValueToRange
that is meant to scale the measured voltage to the decibel range, based on the observation that the lowest voltage corresponding to the lowest amount of decibels that the board can measure, when multiplied by yield a nice lowest decibel value of .
Aside the boilerplate Arduino code, the former short snippet is the whole nucleus that is responsible of measuring the noise level that is characteristic to the provided template in the code section.
Unable to display file "http://svn.grimore.org/arduino-sketches/arduinoAnalogSoundMeter/arduinoAnalogSoundMeter.ino": It may not exist, or permission may be denied.