Error 516:Unable to write:Broken pipe (Bluetooth)(Arduino)(MIT App Inventor)

I was trying to make this Arduino project where I would control a toy car using an app made with MIT App inventor via Bluetooth. I referred to the website- Arduino Robot Car Wireless Control using HC-05 Bluetooth, NRF24L01 and HC-12 Transceiver Modules - HowToMechatronics (blocks, and source code is there in the website)

The circuit, code all looks fine to me. The app consists of a joystick, I move the car and based on the x and y value(processed by Arduino) of the joystick, I the car moves in that direction.

The PROBLEM is that when I move the joystick, the app crashes that is, it freezes and gives the following error- "Error 516:Unable to write:Broken pipe". I have observed that the bluetooth module(HC-05) I'm using starts disconnecting everytime I connect it, hence "broken pipe".

So, I would like to know is there any solution to fix it, if yes then please let me know it.

Thank You!

Any more information you need?
Just ask me

Without seeing any code, let me guess..........

#define enA 9
#define in1 4
#define in2 5
#define enB 10
#define in3 6
#define in4 7
int xAxis, yAxis;
unsigned int  x = 0;
unsigned int  y = 0;
int motorSpeedA = 0;
int motorSpeedB = 0;
void setup() {
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  Serial.begin(38400); // Default communication rate of the Bluetooth module
}
void loop() {
  // Default value - no movement when the Joystick stays in the center
  x = 510 / 4;
  y = 510 / 4;
  // Read the incoming data from the Joystick, or the master Bluetooth device
  while (Serial.available() >= 2) {
    x = Serial.read();
    delay(10);
    y = Serial.read();
  }
  delay(10);
  // Convert back the 0 - 255 range to 0 - 1023, suitable for motor control code below
  xAxis = x*4;
  yAxis = y*4;
  // Y-axis used for forward and backward control
  if (yAxis < 470) {
    // Set Motor A backward
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    // Set Motor B backward
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    // Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
    motorSpeedA = map(yAxis, 470, 0, 0, 255);
    motorSpeedB = map(yAxis, 470, 0, 0, 255);
  }
  else if (yAxis > 550) {
    // Set Motor A forward
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    // Set Motor B forward
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
    motorSpeedA = map(yAxis, 550, 1023, 0, 255);
    motorSpeedB = map(yAxis, 550, 1023, 0, 255);
  }
  // If joystick stays in middle the motors are not moving
  else {
    motorSpeedA = 0;
    motorSpeedB = 0;
  }
  // X-axis used for left and right control
  if (xAxis < 470) {
    // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value
    int xMapped = map(xAxis, 470, 0, 0, 255);
    // Move to left - decrease left motor speed, increase right motor speed
    motorSpeedA = motorSpeedA - xMapped;
    motorSpeedB = motorSpeedB + xMapped;
    // Confine the range from 0 to 255
    if (motorSpeedA < 0) {
      motorSpeedA = 0;
    }
    if (motorSpeedB > 255) {
      motorSpeedB = 255;
    }
  }
  if (xAxis > 550) {
    // Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value
    int xMapped = map(xAxis, 550, 1023, 0, 255);
    // Move right - decrease right motor speed, increase left motor speed
    motorSpeedA = motorSpeedA + xMapped;
    motorSpeedB = motorSpeedB - xMapped;
    // Confine the range from 0 to 255
    if (motorSpeedA > 255) {
      motorSpeedA = 255;
    }
    if (motorSpeedB < 0) {
      motorSpeedB = 0;
    }
  }
  // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)
  if (motorSpeedA < 70) {
    motorSpeedA = 0;
  }
  if (motorSpeedB < 70) {
    motorSpeedB = 0;
  }
  analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
  analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
}

I’m sing a L298N module and approx. 12V power supply and a BT module.
Sorry! ;D

A wiring diagram would be useful.
Have you the BT connected to pins 0 and 1 (bad)? Are you still connected with your PC via USB?

Disconnect the motors, and see if the bluetooth still disconnects - if it does, it's probably noise from the motors getting onto the power rail....

I doubt it are the Tx and Rx pins.
Since I use Arduino Mega 2560, I tried all Tx and Rx connection, still the same problem just that it gave no error for 15 sec without moving the motors.
No, I’m giving external power supply.

Two classic HC-05 mistakes.

  1. connected to pin 0 and 1 (the USB<>Serial pins).
  2. powered from the 3.3volt pin (module needs 3.6volt minimum on the VCC pin).

If you use a Mega, you must ofcourse use one of the other Serial ports (and adjust the code for that).
And power the module from the 5volt pin.
Leo…

DrAzzy:
Disconnect the motors, and see if the bluetooth still disconnects - if it does, it's probably noise from the motors getting onto the power rail....

I tried it, it doesn't give the "Broken Pipe" error, but t started to give error such as "Couldn't fit 256 into I byte" And this value just increased over time, it had reached almost 478 until I switched everything off.

Wawa:

  1. connected to pin 0 and 1 (the USB<>Serial pins).

Its connected to the other Tx and Rx Pins(Rx3 and Tx3)

Wawa:
If you use a Mega, you must ofcourse use one of the other Serial ports (and adjust the code for that).
And power the module from the 5volt pin.

I’ve taken care of that.

The diagram suggests there are de-coupling capacitors on the L298N module so motor noise shouldn't be an issue. Can you try NOT using your Android app? Download a simple BT terminal app and try establishing a connection with that. Maybe your app is looking for some feedback that it's not getting. I use Bluetooth Terminal and have never had problems.

Impostor:
I tried it, it doesn't give the "Broken Pipe" error, but t started to give error such as "Couldn't fit 256 into I byte" And this value just increased over time, it had reached almost 478 until I switched everything off.

Well, that seems to prove that the motors are a key factor in the problem.

The other error is a bug in the app you wrote.

No luck yet, I used one of those BT terminal apps, I sent some ASCII codes but its not even working.

DrAzzy:
Well, that seems to prove that the motors are a key factor in the problem.

May I know how?

DrAzzy:
The other error is a bug in the app you wrote.

I have experienced this problem before, as I stated in the very first post, i’m using a Joystick to move the car(in app),The extra size of the canvas is causing the problem(more than 255) hence I decreased the canvas size, yet it gives me the same error.

Update: I'm not receiving the "Error 516:Unable to write:Broken pipe" as of now yet it disconnects. Now its giving the "Couldn't fit 256 into I byte" And this value just increased over time, it had reached almost 478 until I switched everything off.

Hello ... i use application app invertor 2 and give me error 516 broken pipe how i solve that problem

Hello, at first sorry for my english level.

The problem in your project is the battery because when you use the robot whit the USB out the bluetooth dont have muh electricity and always disconect. If you add to your robot a other battery for example a powerbank only for the arduino shield and other battery for the l298n driver the problem disapear.

Hola, aqui de dejo en mensaje en castellano por si te hes mas facil leerlo.

Yo tuve ese mismo problema hace tiempo y el problema es de la alimentación al dispositivo confome se van descargando las baterias deja de tener suficiente alimentación y empieza a dar problemas el bluetooth, yo lo resolví poniendo dos alimentaciones, una para los motores del dirver l298n que ademas llevaba corriente al VIN de la placa y otra para el la placa de arduino sola para la placa de arduino use un power bank de manera que cuando usaba el proyecto no le faltba electricidad a la shiel de bluetooth y así dejo de darme problemas por cierto ese problema aparece tanto con la HC-05 como con la HC-06.