Devices & Components
Arduino® UNO R4 WiFi
MAX4466 microphone
Software & Tools
NanoEdge AI Studio
Arduino IDE
Project description
Code
datalogger code
c
1/* Defines ----------------------------------------------------------*/ 2#define SENSOR_SAMPLES 1024 //buffer size 3#define AXIS 1 //microphone is 1 axis 4#define DOWNSAMPLE 32 //microphone as a very high data rate, we downsample it 5 6 7/* Prototypes ----------------------------------------------------------*/ 8void get_microphone_data(); //function to collect buffer of sound 9 10/* Global variables ----------------------------------------------------------*/ 11static uint16_t neai_ptr = 0; //pointers to fill for sound buffer 12static float neai_buffer[SENSOR_SAMPLES * AXIS] = {0.0}; //souhnd buffer 13int const AMP_PIN = A0; // Preamp output pin connected to A0 14 15/* Setup function ----------------------------------------------------------*/ 16void setup() { 17 Serial.begin(115200); 18 delay(10); 19} 20 21/* Infinite loop ----------------------------------------------------------*/ 22void loop() { 23 if (analogRead(A0)> 400){ 24 get_microphone_data(); 25 } 26 27} 28 29 30/* Functions declaration ----------------------------------------------------------*/ 31void get_microphone_data() 32{ 33 static uint16_t temp = 0; //stock values 34 int sub = 0; //increment to downsample 35 //while the buffer is not full 36 while (neai_ptr < SENSOR_SAMPLES) { 37 //we only get a value every DOWNSAMPLE (32 in this case) 38 if (sub > DOWNSAMPLE) { 39 /* Fill neai buffer with new accel data */ 40 neai_buffer[neai_ptr] = analogRead(AMP_PIN); 41 /* Increment neai pointer */ 42 neai_ptr++; 43 sub = 0; //reset increment 44 } 45 else { 46 //we read the sample even if we don't use it 47 //else it is instantaneous and we don't downsample 48 temp = analogRead(AMP_PIN); 49 } 50 sub ++; 51 } 52 //print the buffer values to send them via serial 53 for (uint16_t i = 0; i < SENSOR_SAMPLES; i++) { 54 Serial.print(neai_buffer[i]); 55 Serial.print(" "); 56 } 57 Serial.print("\n"); 58 neai_ptr = 0; //reset the beginning position 59}
main code
c
requires neai library, follow the tutorial
1/* Libraries ----------------------------------------------------------*/ 2#include "NanoEdgeAI.h" 3#include "knowledge.h" 4 5/* Defines ----------------------------------------------------------*/ 6#define SENSOR_SAMPLES 1024 //buffer size 7#define AXIS 1 //microphone is 1 axis 8#define DOWNSAMPLE 32 //microphone as a very high data rate, we downsample it 9 10 11/* Prototypes ----------------------------------------------------------*/ 12void get_microphone_data(); //function to collect buffer of sound 13 14/* Global variables ----------------------------------------------------------*/ 15static uint16_t neai_ptr = 0; //pointers to fill for sound buffer 16static float neai_buffer[SENSOR_SAMPLES * AXIS] = {0.0}; //souhnd buffer 17int const AMP_PIN = A0; // Preamp output pin connected to A0 18 19/* NEAI PART*/ 20uint8_t neai_code = 0; //initialization code 21uint8_t similarity; // Point to id class (see argument of neai_classification fct) 22 23 24/* Setup function ----------------------------------------------------------*/ 25void setup() { 26 Serial.begin(115200); 27 delay(10); 28 29 /* Initialize NanoEdgeAI AI */ 30 neai_code = neai_anomalydetection_init(); 31 if (neai_code != NEAI_OK) { 32 Serial.print("Not supported board.\n"); 33 } else { 34 neai_anomalydetection_knowledge(knowledge); 35 } 36 37 38 39} 40 41/* Infinite loop ----------------------------------------------------------*/ 42void loop() { 43 if (analogRead(A0) > 400) { 44 get_microphone_data(); 45 neai_anomalydetection_detect(neai_buffer, &similarity); 46 Serial.println(similarity); 47 if (similarity > 90){ 48 Serial.println("Doorbell ringing!"); 49 //do stuff you want 50 } 51 } 52} 53 54 55/* Functions declaration ----------------------------------------------------------*/ 56void get_microphone_data() 57{ 58 static uint16_t temp = 0; //stock values 59 int sub = 0; //increment to downsample 60 //while the buffer is not full 61 while (neai_ptr < SENSOR_SAMPLES) { 62 //we only get a value every DOWNSAMPLE (32 in this case) 63 if (sub > DOWNSAMPLE) { 64 /* Fill neai buffer with new accel data */ 65 neai_buffer[neai_ptr] = analogRead(AMP_PIN); 66 /* Increment neai pointer */ 67 neai_ptr++; 68 sub = 0; //reset increment 69 } 70 else { 71 //we read the sample even if we don't use it 72 //else it is instantaneous and we don't downsample 73 temp = analogRead(AMP_PIN); 74 } 75 sub ++; 76 } 77 neai_ptr = 0; //reset the beginning position 78}
Downloadable files
Datalogger code
arduino_demo_doorbell_datalogger.ino
main code
requires neai library, follow the tutorial
arduino_demo_doorbell.ino
main code
require neai library, follow the tutorial
arduino_demo_doorbell.ino
Documentation
NanoEdge AI Studio documentation
https://wiki.st.com/stm32mcu/wiki/AI:NanoEdge_AI_Studio
Comments
Only logged in users can leave comments