How to use data from external sensor in Arduino Nano 33 BLE sense for machine learning

Hi, I want to know if its possible to use external sensors connected to analog input of arduino nano 33 BLE sense for machine learning.

I have collected the data from an external sensor via the analog port in arduino nano 33 BLE sense and the trained a model in edge impluse and build the arduino library.

How can I use the analog sensor data to make the real time prediction similar to the example using build-in sensors?

Hello @monunith

To forward your data to Edge Impulse directly using your board, you can use the data forwarder.

You can use something like that (I have not tested the code):

#define FREQUENCY_HZ        50
#define INTERVAL_MS         (1000 / (FREQUENCY_HZ + 1))

static unsigned long last_interval_ms = 0;

void setup() {
    Serial.begin(115200);
    Serial.println("Started");
}

void loop() {
    int sensorValue0, sensorValue1;

    if (millis() > last_interval_ms + INTERVAL_MS) {
        last_interval_ms = millis();

        sensorValue0 = analogRead(A0);
        sensorValue1 = analogRead(A1);

        Serial.print(sensorValue0);
        Serial.print('\t');
        Serial.print(sensorValue1);        
    }
}

Then, you can use the same method to fill your buffer when running the inference.

Best,

Louis

Hi Louis,

Thank you for the reply,
I have done the data collection and forwarding to the edge impluse. I have completed the training of the ml model and created the library. I am stuck at how to use the created library for implementing the project.

When i ran the example code that i got in the library the following error is coming as i have provided the data from a0 and a1 for the training but for running the library code i haven’t called a0 and a1.
“ERR: Sensors don’t match the sensors required in the model
Following sensors are required: a0 + a1”

Any suggestion or tutorials available on how to implement the code for the same.

Hello!

You could do something like the following in your loop function (again did not test). I used the generated Arduino example for the IMU and replaced the IMU data by AnalogRead:

// Allocate a buffer here for the values we'll read from the IMU
    float buffer[EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE] = { 0 };

    for (size_t ix = 0; ix < EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE; ix += 2) {
        // Determine the next tick (and then sleep later)
        uint64_t next_tick = micros() + (EI_CLASSIFIER_INTERVAL_MS * 1000);

        buffer[ix] = analogRead(A0);
        buffer[ix + 1] = analogRead(A1)

        delayMicroseconds(next_tick - micros());
    }

    // Turn the raw buffer in a signal which we can the classify
    signal_t signal;
    int err = numpy::signal_from_buffer(buffer, EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE, &signal);
    if (err != 0) {
        ei_printf("Failed to create signal from buffer (%d)\n", err);
        return;
    }

    // Run the classifier
    ei_impulse_result_t result = { 0 };

    err = run_classifier(&signal, &result, debug_nn);
    if (err != EI_IMPULSE_OK) {
        ei_printf("ERR: Failed to run classifier (%d)\n", err);
        return;
    }

    // print the predictions
    ei_printf("Predictions ");
    ei_printf("(DSP: %d ms., Classification: %d ms., Anomaly: %d ms.)",
        result.timing.dsp, result.timing.classification, result.timing.anomaly);
    ei_printf(": \n");
    for (size_t ix = 0; ix < EI_CLASSIFIER_LABEL_COUNT; ix++) {
        ei_printf("    %s: %.5f\n", result.classification[ix].label, result.classification[ix].value);
    }
#if EI_CLASSIFIER_HAS_ANOMALY == 1
    ei_printf("    anomaly score: %.3f\n", result.anomaly);
#endif
}

Let me know if that helps.

Best,

Louis

1 Like

Hi Louis,

Thank you. it worked.