The AudioEventSensor library allows easy integration with an audio event sensor module (like the XU316) over UART, enabling event detection, threshold configuration, and more.
-
Download the library:
- Download the repository as a
.ZIPfile. - In Arduino IDE, go to Sketch → Include Library → Add .ZIP Library.
- Select the
.ZIPfile you downloaded to install the library.
- Download the repository as a
-
Manual Installation:
-
Download the repository.
-
Place the folder
AudioEventSensorin your Arduino libraries folder:- Windows:
C:\Users\<your_username>\Documents\Arduino\libraries - macOS/Linux:
~/Documents/Arduino/libraries
- Windows:
-
- Description: Initializes the
AudioEventSensorwith the given serial port (e.g.,Serial1).
- Description: Begins communication with the sensor at the specified baud rate.
- Example:
audio.begin(115200);
- Description: Checks if the sensor has data available to read.
- Returns:
trueif data is available,falseotherwise.
- Description: Reads and returns a formatted event, including the event name and confidence percentage (e.g.,
"glass_break 78% confidence"). - Returns: A formatted string like
"glass_break 78% confidence".
- Description: Queries the supported event types (e.g., "baby_cry", "glass_break").
- Returns:
trueif supported events are fetched,falseotherwise. - Example:
"baby_cry,glass_break,gunshot,snore,T3,T4"
- Description: Retrieves the types of events currently being detected by the sensor.
- Returns:
trueif successfully fetched,falseotherwise. - Example:
"gunshot,glass_break"
- Description: Sets the types of events to be detected (e.g.,
"gunshot,glass_break"). - Returns:
trueif the types were set successfully,falseotherwise.
- Description: Retrieves the detection threshold for a specific event type.
- Returns:
trueif successful,falseotherwise. - Example:
"glass_break" -> 50
- Description: Sets the detection threshold for a specific event type.
- Returns:
trueif the threshold was set successfully,falseotherwise.
- Description: Retrieves the firmware version of the sensor.
- Returns:
trueif successful,falseotherwise. - Example:
"1.0.0"
- Description: Resets the sensor to its default settings.
- Returns:
trueif successful,falseotherwise.
#include <AudioEventSensor.h>
#define UART1_TX 43
#define UART1_RX 44
AudioEventSensor audio(Serial1);
void setup() {
Serial.begin(115200);
Serial1.begin(115200, SERIAL_8N1, UART1_RX, UART1_TX);
audio.begin(115200);
String supportedEvents;
if (audio.getSupportedList(supportedEvents)) {
Serial.println("Supported Events: " + supportedEvents);
}
audio.setDetectTypes("gunshot,glass_break");
String currentDetectTypes;
if (audio.getDetectTypes(currentDetectTypes)) {
Serial.println("Currently Detecting: " + currentDetectTypes);
}
}
void loop() {
if (audio.available()) {
String event = audio.readEvent();
if (event.length() > 0) {
Serial.println("Detected Event: " + event);
}
}
delay(500);
}This library is released under the MIT License.