Running a neural network with matlab and arduino

Hello, I am trying to run a neural network using Matlab and sending the outputs back to Arduino. Any ideas on suggestions on how to approach this? My current approach is not working.

Matlab code.

s = serialport('COM3', 115200);
while true  
    if s.NumBytesAvailable > 0
        data = strtrim(string(readline(s)));  
        data = split(data,',');
        data = str2double(data);
        values = [data(1), data(2),...];
      
        prediction = predict(network,values); 

        write(s, prediction,"double"); %send value to serial
       
    end

end
%code to read in arduino. 

double nnOutput = Serial.read();
Serial.println(nnOutput);

For informed help, please read and follow the instructions in the "How to get the best out of this forum" post, linked at the head of every forum category.

Hint: this line reads one ASCII character from the serial port, if one happens to available:

double nnOutput = Serial.read();

Recommended tutorial:

I'm very interested in this topic! If you don’t mind, I’ll leave a reply here and follow the updates on this discussion. Hopefully, more experts can contribute and help with the development of your project.

Lately, I’ve also been working on a project involving Arduino and MATLAB, specifically for real-time data acquisition. In the future, I might also explore signal processing to apply machine learning to the acquired signals.

Could you share more details on your current approach and what specific issues you’re facing? Are you using MATLAB’s Serial Communication (Serial.write/Serial.read) to send and receive data between MATLAB and Arduino? Also, what kind of neural network are you running—are you using MATLAB’s Deep Learning Toolbox or a custom model?

Looking forward to seeing how this project develops!

Which Arduino? Not all have a double data type.

I have used this code to receive float values from Python :

#include <LiquidCrystal.h>

#include "analog_button.h"

//LCD pin to Arduino
const int pin_d4 = 4;
const int pin_d5 = 5;
const int pin_d6 = 6;
const int pin_d7 = 7;
const int pin_RS = 8;
const int pin_EN = 9;

const int pin_BL = 10;

LiquidCrystal lcd(pin_RS, pin_EN, pin_d4, pin_d5, pin_d6, pin_d7);

void setup()
{
  // put your setup code here, to run once:

  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("Hello LCD");

  Serial.begin(115200);
  while (!Serial)
    ;
  Serial.write("\nStarted\n");

  // note the default is 1000
  Serial.setTimeout(100);
}

bool readFloat (float &value)
{
  uint8_t buf[4];

  int len = Serial.readBytes(buf, 4);

  if (len == 4)
  {
    memcpy(&value, buf, 4);
    return true;
  }
  else
  {
    return false;
  }
}

void loop()
{
  float value;

  if (readFloat (value))
  {
    lcd.setCursor(0, 1);
    lcd.print(value);
    lcd.print("   ");
  }
}

However, this code is a very simple approach and may need more work to cope with errors etc.

I am using an arduino mega.

In that case you don't have double, only float (IEEE single precision). (On AVR architecture such as the Mega, you can declare a variable double, but actually is a float).

First, learn how to transfer data from MATLAB to Arduino.

The tutorial in reply #1 is a good starting point, and the MATLAB site shows other approaches.