Arduino disconnects from the computer after a few seconds

I’m using a nano to control a DC motor pump that uses an I2C pressure sensor for feedback in a PID loop. The computer passes target values to the arduino using the serial monitor. The serial monitor also receives output data from the controller.

I’ve checked the loads on the 5V pin of the arduino and they are good. The DC motor is controlled with a MOSFET so little current is sourced. The sensor draws 6 mA from the power supply.

For some reason the arduino disconnects from the serial monitor after a few seconds. I’m out of ideas for why that happens. I thought it was a ram issue so I reduced ram usage by decreasing the samples in our moving average filter. I’ve heard that electrical interference of other devices connected to plug ins in the same room can send ripples to the USB connections of a computer causing trouble. I doubt it’s this because there is another board connected to the same computer that has no issues.

Anybody know of anything else I should check?

Here is my code if anybody things there is something in there.
The only thing I can think of is the use of delayMicroseconds(). Since that stalls the processor I believe it could cause timing issues.

//Connect digital pin 5 to MOSFET controller ciruit, SDA to A4, SCL to A5
//Send pressure signal in kPa from labview to arduino using Serial interface
#include <PID_v1.h>
#include <Wire.h>

//Define Variables we'll be connecting to
double Setpoint, Input, PWM;
double Tunings[2];

//Specify the links and initial tuning parameters
PID myPID(&Input, &PWM, &Setpoint, 2, 0, 2, DIRECT);
int prev, current;

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

  pinMode(5, OUTPUT);
  while (!Serial) {
    //wait for serial port to open
  }
  //input
  Serial.println("Input PID controller values separated by commas");
  while (!Serial.available()) {
    //wait for input
  }
  for (int i = 0; i < 3; i++) {
    Tunings[i] = Serial.parseFloat();
    Serial.print(Tunings[i]);
    Serial.print(" ");
  }
  Serial.println();
  myPID.SetTunings(Tunings[0], Tunings[1], Tunings[2]);

  //initialize the variables we're linked to
  Input = getPressure();
  Serial.println("Input initial target pressure value in kPa");
  while (!Serial.available()) {
    //wait for pressure value
  }
  Setpoint = Serial.parseFloat();
  delay(20);
  Serial.println(Setpoint);

  myPID.SetSampleTime(50);  //milliseconds between PID evaluations
  //turn the PID on
  myPID.SetMode(AUTOMATIC);
  prev=millis();
}

void loop()
{
  
  if (Serial.available()) { //checks to see if new pressure value has been sent
    Setpoint = Serial.parseFloat();  //send pressure value in kPa
  }
  Input = getPressure();
  myPID.Compute();
  analogWrite(5, round(PWM));
  current=millis();
  if(current-prev>200){
    prev=millis();
    Serial.print(Setpoint); Serial.print(" ");
    Serial.print(Input); Serial.print(" ");
    Serial.print(Setpoint-Input); Serial.print(" ");
    Serial.print(PWM); Serial.print(" ");
    Serial.println();
  }
}
float scale(float pressure) { //scale pressure to counts
  float m = 4.103;  //determine using calibration test
  float b = 516;
  float counts = m * pressure + b; //first degree equation
  counts=round(counts);
  return counts;
}

double getPressure() {
  Wire.begin();
  Wire.requestFrom(0x28, 2);
  unsigned int Praw;

  byte c = Wire.read(); // receive a byte as character
  c = c << 2;  //remove 2 status bits
  c = c >> 2;
  Praw = c << 8;
  unsigned int d = Wire.read();
  Praw = Praw + d; //14 bit pressure reading
  double pressure = ((Praw - 8192.0) / 16384.0) * 120 * 0.24908887;
  return pressure;
}

Do you only have a problem with that program on your Nano?

...R

The computer passes target values to the arduino using the serial monitor.

How can it possibly do this?

For some reason the arduino disconnects from the serial monitor after a few seconds. I’m out of ideas for why that happens. I thought it was a ram issue so I reduced ram usage by decreasing the samples in our moving average filter. I’ve heard that electrical interference of other devices connected to plug ins in the same room can send ripples to the USB connections of a computer causing trouble. I doubt it’s this because there is another board connected to the same computer that has no issues.

Anybody know of anything else I should check?

Check your circuit diagram and connections. I wouldn’t mind taking a look but can’t seem to find it.

I didn't post the circuit diagram because that's one part I know is working. The motor turns how it should, I2C communication with the pump is working fine, and the on board control system will track a signal even after it disconnects.

I'll clarify what I meant by passing target values. PID control is a feedback loop that compares the output of a sensor to a reference signal and reduces the difference between them by controlling some kind of actuator. I used target value to refer to changing the reference signal. So basically passing them involves typing them into the serial monitor and pressing enter.

Could it also be the PID library I'm using? It sometimes lags.

Swapped the nano for a micro, no change.

It worked fine when the pump power source was turned off which makes me think the problem is the PID library. I think the ram is getting filled up and causing issues.

It worked fine when the pump power source was turned off which makes me think the problem is

in the software. I'm seriously wondering how you came to THAT conclusion.

You have not answered the question in Reply #1 - is this problem particular to this program? Or does it happen with any program?

...R

The problem is particular to this program. I used a PWM motor controller before adding PID control and it worked fine.

I got confused and somehow read that I should try a different board. Sorry.

Henradrie: The problem is particular to this program.

Then it may be worth disabling (commenting out) different parts of the program to isolate the piece that is causing the problem.

I got confused and somehow read that I should try a different board. Sorry.

I am prepared to share the blame. I can see now how my question was ambiguous :)

...R