Industrial Anomaly Detection on Arduino® Opta® PLC

This tutorial demonstrates how to implement anomaly detection on an Arduino® Opta® PLC using Edge Impulse. Anomaly detection is a machine learning technique that identifies unusual patterns in data, making it ideal for monitoring industrial processes and equipment. By training a model to recognize normal behavior, you can detect deviations that may indicate faults or malfunctions.

Overview

To showcase how easy it is to integrate Edge Impulse with the Arduino® Opta® PLC, we'll walk through a practical example using the Arduino DIN Celsius board that comes with the kit, but also a Motor to demonstrate this setup can be used interchangeably. This example demonstrates how to set up a temperature-controlled system, collect data, train a machine learning model, and deploy it for anomaly detection.

In this tutorial, you will:

  • Collect temperature data from a sensor connected to the Opta® PLC.

  • Train a machine learning model in Edge Impulse to detect anomalies in the data.

  • Deploy the model to the Opta® PLC for real-time inference.

  • Optionally, integrate the model with Arduino Cloud for remote monitoring and visualization.

Prerequisites

Hardware:

Software:

Step 1: Set Up Your Hardware

Hardware Setup

About the DIN Celsius Board

The DIN Celsius is an all-in-one temperature laboratory offering two independent heaters and a temperature sensor. It allows you to simulate heating scenarios and monitor temperature changes, making it ideal for testing our anomaly detection use case, we can introduce an anomaly by turning off one of the heaters to cause a deviation from the normal condition.

Connecting the DIN Celsius to the Opta® PLC

Safety First: Before making any connections, ensure that all power sources are disconnected to prevent electric shock or short circuits.

Connections Overview:

Power Connections:

  • Connect the +24V and GND terminals of the DIN Celsius to the corresponding power supply pins.

Heater Control:

  • Connect Relay 3 (pin 2) on the Opta® PLC to Input Heat 1 on the DIN Celsius.

  • Connect Relay 4 (pin 3) on the Opta® PLC to Input Heat 2 on the DIN Celsius.

  • These connections will control the two independent heaters on the DIN Celsius.

Temperature Sensor Input:

  • Connect the Output Voltage from the DIN Celsius to the I8 input (analog pin A7) on the Opta® PLC.

  • This connection allows the PLC to read the temperature sensor data.

Pin Definitions:

  • HEAT_LEFT (Relay 3) connected to pin 2

  • HEAT_RIGHT (Relay 4) connected to pin 3

  • TEMP_SENS connected to analog pin A7

  • BTN (User Button) on the Opta® PLC

Step 2: Set Up Your Software

Arduino IDE Configuration

Install Necessary Libraries:

  • Ensure you have the latest Arduino IDE 2 installed.

  • Install any required libraries via the Library Manager, such as Edge Impulse SDK and Arduino_HTS221 if using temperature/humidity sensors.

Define Pin Constants:

#define BTN         BTN_USER       // User button on Opta WiFi
#define HEAT_LEFT   2              // Left heater control pin
#define HEAT_RIGHT  3              // Right heater control pin
#define TEMP_SENS   A7             // Temperature sensor analog pin

Testing the Connections

Heater Control Test Code:

void setup() {
  pinMode(HEAT_LEFT, OUTPUT);
  pinMode(HEAT_RIGHT, OUTPUT);
}

void loop() {
  digitalWrite(HEAT_LEFT, HIGH);
  digitalWrite(HEAT_RIGHT, HIGH);
  delay(1000);
  digitalWrite(HEAT_LEFT, LOW);
  digitalWrite(HEAT_RIGHT, LOW);
  delay(1000);
}
  • Upload the Code: Use the Arduino IDE to upload the sketch to the Opta® PLC.

  • Verify Operation: The LEDs on the DIN Celsius should blink, indicating the heaters are being activated.

Temperature Sensor Reading Test:

void setup() {
  Serial.begin(9600);
  pinMode(TEMP_SENS, INPUT);
}

void loop() {
  int sensorValue = analogRead(TEMP_SENS);
  Serial.println(sensorValue);
  delay(250);
}
  • Open Serial Monitor: Set the baud rate to 9600.

  • Observe Readings: You should see numerical values corresponding to the temperature sensor output.

Step 3: Collecting Data with Edge Impulse

Create a New Project

  • Log In: Access your Edge Impulse account.

  • New Project: Create a project named, for example, "Opta PLC Anomaly Detection."

Data Forwarding Sketch

Upload a sketch to the Opta® PLC that reads the temperature sensor and sends data to Edge Impulse.

Steps:

  1. Upload the Data Forwarder Sketch: Use the Arduino IDE to upload the sketch to the Opta® PLC.

  2. Run Data Forwarder: In your terminal, execute the data forwarding command.

  3. Select Serial Port: Choose the serial port corresponding to the Opta® PLC.

  4. Label the Data: As you collect data, assign labels to your data (e.g., "normal," "anomalous") based on the system's behavior.

Note: If you are new to Edge Impulse, please refer to our Data Forwarder documentation for detailed instructions.

Data Forwarder Sketch:

void setup() {
  Serial.begin(115200);
  while (!Serial);

  pinMode(TEMP_SENS, INPUT);
  Serial.println("Edge Impulse Data Forwarder Example");
}

void loop() {
  float temperature = analogRead(TEMP_SENS) * (10.0 / 1024.0); // Convert to voltage
  Serial.print("Temperature: ");
  Serial.println(temperature);

  // Send data in Edge Impulse format
  Serial.print("1,");
  Serial.println(temperature);

  delay(1000);
}
  • Run Data Forwarder: In your terminal, execute:

    edge-impulse-data-forwarder
  • Select Serial Port: Choose the serial port corresponding to the Opta® PLC.

  • Label the Data: Assign labels to your data (e.g., "normal," "anomalous") as you collect it.

If you are new to Edge Impulse please see our Data Forwarder documentation here

Step 4: Training the Machine Learning Model

Create an Impulse

Add Blocks: 2. Add Blocks:

  • Processing Block: Select a Time Series or Spectral Analysis block based on your data characteristics.

  • Learning Block: Choose Anomaly Detection using the Gaussian Mixture Model (GMM).

Configure the Impulse

  • Window Size: Set according to the data frequency (e.g., 1000 ms).

  • Window Increase: Set overlap (e.g., 500 ms).

Generate Features

  • Compute Features: Navigate to the Generate Features tab and run feature generation.

Train the Model

Training Parameters:

  • Epochs: Start with 100 and adjust as needed.

  • Learning Rate: Default of 0.005 is usually sufficient. Start Training: Monitor the accuracy and loss graphs.

Validate the Model

  • Model Testing: Use a separate dataset to evaluate model performance.

  • Adjust if Necessary: Retrain or adjust parameters based on results.

Step 5: Deploying the Model to the Opta® PLC

Download the Arduino Library

  • Deployment Tab: In Edge Impulse, go to Deployment.

  • Select Arduino Library: Download the library tailored for your model.

Read on here

Include the Library in Your Sketch

  • Add Library to IDE: Import the downloaded library into your Arduino IDE.

Add the Inference Code

If you are new to Arduino inference code, see our Arduino inference code documentation here for more information.

#include <Your_Edge_Impulse_Inference_Library.h>
#include <edge-impulse-sdk/classifier/ei_run_classifier.h>

// Define the feature buffer
float features[EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE];

void setup() {
  Serial.begin(115200);
  while (!Serial);

  pinMode(TEMP_SENS, INPUT);
  Serial.println("Edge Impulse Inference Example");
}

void loop() {
  // Collect data
  for (size_t i = 0; i < EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE; i++) {
    features[i] = analogRead(TEMP_SENS) * (10.0 / 1024.0);
    delay(10); // Adjust delay as needed
  }

  // Prepare signal
  signal_t signal;
  numpy::signal_from_buffer(features, EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE, &signal);

  // Run inference
  ei_impulse_result_t result = { 0 };
  EI_IMPULSE_ERROR res = run_classifier(&signal, &result, false);

  if (res != EI_IMPULSE_OK) {
    Serial.print("ERR: Failed to run classifier (");
    Serial.print(res);
    Serial.println(")");
    return;
  }

  // Print results
  Serial.println("Inference results:");
  for (size_t ix = 0; ix < EI_CLASSIFIER_LABEL_COUNT; ix++) {
    Serial.print(result.classification[ix].label);
    Serial.print(": ");
    Serial.println(result.classification[ix].value);
  }

  // Control heaters based on anomaly detection
  if (result.anomaly > ANOMALY_THRESHOLD) {
    // Anomaly detected, take action
    Serial.println("Anomaly detected!");
    digitalWrite(HEAT_LEFT, LOW);
    digitalWrite(HEAT_RIGHT, LOW);
  } else {
    // Normal operation
    digitalWrite(HEAT_LEFT, HIGH);
    digitalWrite(HEAT_RIGHT, HIGH);
  }

  delay(1000);
}
  • Replace Your_Edge_Impulse_Inference_Library.h with the actual header file name from your downloaded library.

  • Set ANOMALY_THRESHOLD to an appropriate value based on your model's performance.

Upload the Sketch

  • Compile and Upload: Use the Arduino IDE to program the Opta® PLC with your new inference code.

  • Monitor Output: Open the Serial Monitor to observe inference results and system behavior.

Step 6: Viewing Data on Arduino Cloud via Blues Wireless (Optional)

See the full Arduino OPTA for Arduino Cloud guide here.

Blues for Wireless Connectivity:

Explore how Blues' Wireless for PLC Expansion enables seamless, secure cellular connectivity, allowing your Opta® PLC to communicate with your cloud-based monitoring systems, regardless of location and without the hassle of local Wi-Fi.

See the Blues Wireless here.

This section will be updated following the Blues Wireless Webinar on the 30th of October Sign up here

Conclusion

Congratulations! You have successfully implemented anomaly detection on an Arduino® Opta® PLC using Edge Impulse. This tutorial demonstrates the seamless integration of machine learning models with industrial automation systems, enabling real-time monitoring and fault detection.

Please share your experiences and feedback with us on the Edge Impulse forum.

For more information on Edge Impulse and Arduino® Opta® PLC, visit the official websites:

Troubleshooting

  • Arduino Cloud Issues: We are aware of issues with Arduino Cloud .properties file format vs IDE and are working with Arduino. If you have issues try moving the .properties file to the same folder as the .ino file and re-uploading the sketch.

Last updated