# PC to Bluetooth (with Arduino) does not work when USB is unplugged

**URL:** <https://discourse.processing.org/t/pc-to-bluetooth-with-arduino-does-not-work-when-usb-is-unplugged/42156>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [June 6, 2023, 2:23pm UTC](https://discourse.processing.org/t/pc-to-bluetooth-with-arduino-does-not-work-when-usb-is-unplugged/42156 "2023-06-06T14:23:20Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Myrdalm](https://avatars.discourse-cdn.com/v4/letter/m/bcef8e/32.png) [@Myrdalm](https://discourse.processing.org/u/Myrdalm)\
**Post date:** [June 6, 2023, 2:23pm UTC](https://discourse.processing.org/t/pc-to-bluetooth-with-arduino-does-not-work-when-usb-is-unplugged/42156/1 "2023-06-06T14:23:20Z")

</div>

I am currently working on a project connecting and controlling Arduino Uno via Bluetooth, using Processing via PC, but the problem is, it works when the USB port with Arduino is connected, but when I disconnect it and just power the Arduino with a battery, it does not work.

Here is the processing code:

import processing.serial.\*;

Serial myPort;

int val;  
int value;

int circleX, circleY;  
int circleSize = 133;  
boolean circleOver = false;  
PImage img1;  
PImage img2;  
PImage img3;

void setup() {  
size(240, 320);

printArray(Serial.list());  
myPort = new Serial(this, Serial.list()[0], 9600);  
//String portName = Serial.list()[1];  
//myPort = new Serial(this, portName, 9600);

circleX = width-120;  
circleY = height-175;  
ellipseMode(CENTER);

img1 = loadImage(“ButtonUp.png”);  
img2 = loadImage(“ButtonOver.png”);  
img3 = loadImage(“ButtonDown.png”);  
}

void draw(){  
update(mouseX, mouseY);

if (myPort.available()\>0) {  
value = myPort.read()-48;  
print(value);  
}

if(circleOver && mousePressed == true){  
background(170,50,255);  
image(img3,0,0);  
myPort.write(‘H’);  
} else if (circleOver && mousePressed == false && value == 1){  
background(170,50,255);  
image(img2,0,0);  
} else if (value == 1){  
background(170,50,255);  
image(img2,0,0);  
} else {  
background(170,50,255);  
image(img1,0,0);  
myPort.write(‘L’);  
}  
}

void update(int x, int y) {  
if( overCircle(circleX, circleY, circleSize) ) {  
circleOver = true;  
} else {  
circleOver = false;  
}  
}

boolean overCircle(int x, int y, int diameter) {  
float disX = x - mouseX;  
float disY = y - mouseY;  
if(sqrt(sq(disX) + sq(disY)) \< diameter/2) {  
return true;  
} else {  
return false;  
}  
}

Here is the Arduino code:  
#include \<SoftwareSerial.h\>

#define RxPin 0  
#define TxPin 1

SoftwareSerial BTSerial(RxPin, TxPin);

const int ledPin = 3;  
const int switchPin = 4;  
char val;

void setup()  
{

pinMode(RxPin, INPUT);  
pinMode(TxPin, OUTPUT);

pinMode(ledPin, OUTPUT);  
pinMode(switchPin, INPUT);

BTSerial.begin(9600);  
}

void loop() {  
if(digitalRead(switchPin) == HIGH) {  
BTSerial.print(1);  
} else {  
BTSerial.print(0);  
}  
delay(16);

if (BTSerial.available());  
{  
val = BTSerial.read();  
if (val == ‘H’) {  
digitalWrite(ledPin, HIGH);  
} else {  
digitalWrite(ledPin, LOW);  
}  
}  
}

Any help is appreciated

Thank you

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [June 6, 2023, 3:59pm UTC](https://discourse.processing.org/t/pc-to-bluetooth-with-arduino-does-not-work-when-usb-is-unplugged/42156/2 "2023-06-06T15:59:03Z")

</div>

Hi

Here s how to enable your Bluetooth connection for you Arduino and PC with simple sketch

[![](https://img.youtube.com/vi/VGJCj0Hr1vQ/maxresdefault.jpg "Arduino Bluetooth Communication with Processing") ](https://www.youtube.com/watch?v=VGJCj0Hr1vQ)

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [June 6, 2023, 4:05pm UTC](https://discourse.processing.org/t/pc-to-bluetooth-with-arduino-does-not-work-when-usb-is-unplugged/42156/3 "2023-06-06T16:05:45Z")

</div>

> [@Myrdalm](#):
>
> works when the USB port with Arduino is connected, but when I disconnect it and just power the Arduino with a battery, it does not work.

Watch the video Link as your sketch working on USB connection I think you need to configure your Bluetooth com Port on PC

---

<div class="post-metadata">

**Author:** ![Myrdalm](https://avatars.discourse-cdn.com/v4/letter/m/bcef8e/32.png) [@Myrdalm](https://discourse.processing.org/u/Myrdalm)\
**Post date:** [June 6, 2023, 6:32pm UTC](https://discourse.processing.org/t/pc-to-bluetooth-with-arduino-does-not-work-when-usb-is-unplugged/42156/4 "2023-06-06T18:32:50Z")

</div>

The results are the same.

---

<div class="post-metadata">

**Author:** ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)\
**Post date:** [June 6, 2023, 8:20pm UTC](https://discourse.processing.org/t/pc-to-bluetooth-with-arduino-does-not-work-when-usb-is-unplugged/42156/5 "2023-06-06T20:20:17Z")

</div>

Hi @Myrdalm, In your Processing sketch you are connecting to ‘Serial.List()[0]’ Are you sure that’s the BT com port? What’s in that list is going to change when you add/remove the Arduino. I think it’s more reliable to put the name of the port there hard coded as e.g. “COM4”. I hope you’re looking at the available ports in device manager to check what each one is.

If the BT software/hardware on the PC is not working well, please see the comments here about using a [USB-TTL-HC05](https://discourse.processing.org/t/accessing-ble-input-to-processing/35560/2) as an alternative.

In your Arduino code, I don’t think you should have pinMode settings on the SoftwareSerial pins. That’s not done in the given [example](https://docs.arduino.cc/tutorials/communication/SoftwareSerialExample).

Please edit your first post, select the Processing code and press control-e. This will format it nicely and make it easier for anyone to copy and test. Repeat for Ard code.

🙂

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [June 6, 2023, 8:24pm UTC](https://discourse.processing.org/t/pc-to-bluetooth-with-arduino-does-not-work-when-usb-is-unplugged/42156/6 "2023-06-06T20:24:02Z")

</div>

How is your demo supposed to work? On a Mac I see the following screen. The output in the console window is either a zero or a one, but I have no control on toggling from one value to the other.

 ![one](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/1/d/1d3809c25c044472b8b85a83a93cd05a9e7bc1aa.png)

What device are you using for Bluetooth?

---

<div class="post-metadata">

**Author:** ![Myrdalm](https://avatars.discourse-cdn.com/v4/letter/m/bcef8e/32.png) [@Myrdalm](https://discourse.processing.org/u/Myrdalm)\
**Post date:** [June 6, 2023, 8:46pm UTC](https://discourse.processing.org/t/pc-to-bluetooth-with-arduino-does-not-work-when-usb-is-unplugged/42156/7 "2023-06-06T20:46:29Z")

</div>

I already test hardcoding it, it connects with Bluetooth but does not make the led light. In the Arduino Code using a pinMode or not does not make any difference. Though I will try additional tests.

---

<div class="post-metadata">

**Author:** ![Myrdalm](https://avatars.discourse-cdn.com/v4/letter/m/bcef8e/32.png) [@Myrdalm](https://discourse.processing.org/u/Myrdalm)\
**Post date:** [June 6, 2023, 8:47pm UTC](https://discourse.processing.org/t/pc-to-bluetooth-with-arduino-does-not-work-when-usb-is-unplugged/42156/8 "2023-06-06T20:47:41Z")

</div>

My apologies, the code includes graphics from a data folder, but I don’t know how to include it here.

---

<div class="post-metadata">

**Author:** ![Myrdalm](https://avatars.discourse-cdn.com/v4/letter/m/bcef8e/32.png) [@Myrdalm](https://discourse.processing.org/u/Myrdalm)\
**Post date:** [June 7, 2023, 7:38am UTC](https://discourse.processing.org/t/pc-to-bluetooth-with-arduino-does-not-work-when-usb-is-unplugged/42156/9 "2023-06-07T07:38:21Z")

</div>

It is my bad guys, sorry about this. The code I send works, and the tutorial in the video works as well. The problem was the Bluetooth module, which I think is not working anymore. Everything worked once I tried changing the Bluetooth module. I appreciate everyone giving solutions. Thank you.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 8, 2023, 3:43pm UTC](https://discourse.processing.org/t/pc-to-bluetooth-with-arduino-does-not-work-when-usb-is-unplugged/42156/10 "2023-06-08T15:43:50Z")

</div>

Hello @Myrdalm ,

Which Bluetooth module(s) are you using?

`:)`

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [June 8, 2023, 4:08pm UTC](https://discourse.processing.org/t/pc-to-bluetooth-with-arduino-does-not-work-when-usb-is-unplugged/42156/11 "2023-06-08T16:08:22Z")

</div>

@Myrdalm hi with this APK you can figure out your Bluetooth working or not

> <https://play.google.com/store/apps/details?id=de.kai_morich.serial_bluetooth_terminal>
>
> Terminal for serial devices connected with Bluetooth Classic / L...

---

<div class="post-metadata">

**Author:** ![Myrdalm](https://avatars.discourse-cdn.com/v4/letter/m/bcef8e/32.png) [@Myrdalm](https://discourse.processing.org/u/Myrdalm)\
**Post date:** [June 8, 2023, 5:29pm UTC](https://discourse.processing.org/t/pc-to-bluetooth-with-arduino-does-not-work-when-usb-is-unplugged/42156/12 "2023-06-08T17:29:56Z")

</div>

HC-05 and HM-01, both of them works the same

---

<div class="post-metadata">

**Author:** ![Myrdalm](https://avatars.discourse-cdn.com/v4/letter/m/bcef8e/32.png) [@Myrdalm](https://discourse.processing.org/u/Myrdalm)\
**Post date:** [June 8, 2023, 5:30pm UTC](https://discourse.processing.org/t/pc-to-bluetooth-with-arduino-does-not-work-when-usb-is-unplugged/42156/13 "2023-06-08T17:30:40Z")

</div>

My HC-05 was not working already, but other modules worked fine

---

<div class="post-metadata">

**Author:** ![sterretje](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@sterretje](https://discourse.processing.org/u/sterretje)\
**Post date:** [June 9, 2023, 3:05pm UTC](https://discourse.processing.org/t/pc-to-bluetooth-with-arduino-does-not-work-when-usb-is-unplugged/42156/14 "2023-06-09T15:05:05Z")

</div>

> [@Myrdalm](#):
>
> Here is the Arduino code:  
> #include \<SoftwareSerial.h\>
> 
> #define RxPin 0  
> #define TxPin 1
> 
> SoftwareSerial BTSerial(RxPin, TxPin);

It’s a waste to use SoftwareSerial on HardwareSerial pins.

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [June 9, 2023, 3:14pm UTC](https://discourse.processing.org/t/pc-to-bluetooth-with-arduino-does-not-work-when-usb-is-unplugged/42156/15 "2023-06-09T15:14:49Z")

</div>

What should be used for HC05?

---

<div class="post-metadata">

**Author:** ![sterretje](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@sterretje](https://discourse.processing.org/u/sterretje)\
**Post date:** [June 9, 2023, 3:53pm UTC](https://discourse.processing.org/t/pc-to-bluetooth-with-arduino-does-not-work-when-usb-is-unplugged/42156/16 "2023-06-09T15:53:54Z")

</div>

If one wants to replace the normal serial connection to a PC by a Bluetooth connection, one uses HardwareSerial (Serial) when using boards with processors that only have one UART

If one wants to use both normal Serial and Bluetooth, one uses SoftwareSerial on different pins for the Bluetooth (e.g. pins 2 and 3) on boards with one UART or, for boards with free UARTs (Mega, Leonardo etc), one uses one of the available UARTs (e g. Serial1).

Uno, Mini and Nano are based on a 328P processor and only have one UART fir serial communication.

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [June 9, 2023, 4:08pm UTC](https://discourse.processing.org/t/pc-to-bluetooth-with-arduino-does-not-work-when-usb-is-unplugged/42156/17 "2023-06-09T16:08:54Z")

</div>

I just tried using regular serial with an UNO + HC05 (pins 2,3) for bluetooth connection to Android mobile device and it failed. Stopped working after short period; had buffer set to 2048. No problem when I switched back to SoftSerial. Same thing happens when I use the Processing serial library with HC05 on a Mac; does not work without SoftSerial. Consider the following simple demo on PC:

```auto
import processing.serial.*;

Serial myPort;

void setup() {
  surface.setTitle("Press 'h' -> LED ON : 'l' -> LED OFF");
  size(400, 0);
  printArray(Serial.list());
  myPort = new Serial(this, Serial.list()[2], 9600);
}

void draw() {
}

void keyPressed() {
  if (key == 'h') {
    println("h pressed.");
    myPort.write('h');
  }
  if (key == 'l') {
    println("l pressed.");
    myPort.write('l');
  }
}

void serialEvent (Serial myPort) { // Checks for available data in the Serial Port
  String inStr = myPort.readStringUntil('\n');
  if (inStr != null) {
    String trimStr = trim(inStr);
    println("returned str = " + trimStr);
    int value = parseInt(trimStr);
    println("key =", char(value));
  }
}

```

**Hardware setup:**

 ![setup](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/1/a/1a7a80ee6a5e1ec085dc293ad0cf5d8d8a4af80f.jpeg)

**Arduino code using SoftSerial works ok:**

```auto
#include <SoftwareSerial.h>

#define RxPin 2 // -> HC05 TX
#define TxPin 3 // -> HC05 RX

SoftwareSerial BTSerial(RxPin, TxPin);

const int LED = 12;

void setup() {
  pinMode(RxPin, INPUT);
  pinMode(TxPin, OUTPUT);
  pinMode(LED, OUTPUT);
  BTSerial.begin(9600);
}

void loop() {
  if (BTSerial.available() > 0) {
      int value = BTSerial.read();
      BTSerial.println(value); // Send back what you received.
      //'h' = 104 : 'l' = 108 -> ASCII values
      if (value == 104) {
        digitalWrite(LED, HIGH);
      } else {
        digitalWrite(LED, LOW);
      }
  }

}

```

**Arduino code without SoftSerial fails on my system:**

```auto
#define RxPin 2 // -> HC05 TX
#define TxPin 3 // -> HC05 RX

const int LED = 12;

void setup() {
  pinMode(RxPin, INPUT);
  pinMode(TxPin, OUTPUT);
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
      int value = Serial.read();
      Serial.println(value); // Send back what you received.
      //'h' = 104 : 'l' = 108 -> ASCII values
      if (value == 104) {
        digitalWrite(LED, HIGH);
      } else {
        digitalWrite(LED, LOW);
      }
  }

}

```

---

<div class="post-metadata">

**Author:** ![sterretje](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@sterretje](https://discourse.processing.org/u/sterretje)\
**Post date:** [June 9, 2023, 6:49pm UTC](https://discourse.processing.org/t/pc-to-bluetooth-with-arduino-does-not-work-when-usb-is-unplugged/42156/18 "2023-06-09T18:49:35Z")

</div>

> [@svan](#):
>
> Arduino code without SoftSerial fails on my system

With that code, where is your Bluetooth module connected to? Pins 0 and 1 or pins 2 and 3? You need to physically use pins 0 and 1 when using HardwareSerial (`Serial.begin`). You also need a level converter / voltage divider as a HC05 is a 3.3V device. The 5V on the Arduino’s Tx pin can damage your HC05.

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [June 9, 2023, 6:52pm UTC](https://discourse.processing.org/t/pc-to-bluetooth-with-arduino-does-not-work-when-usb-is-unplugged/42156/19 "2023-06-09T18:52:22Z")

</div>

It won’t run on my system if I use pins 0,1. It hasn’t blown up yet using 5v and I’ve used it quite a bit.

---

<div class="post-metadata">

**Author:** ![sterretje](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@sterretje](https://discourse.processing.org/u/sterretje)\
**Post date:** [June 9, 2023, 7:06pm UTC](https://discourse.processing.org/t/pc-to-bluetooth-with-arduino-does-not-work-when-usb-is-unplugged/42156/20 "2023-06-09T19:06:58Z")

</div>

> [@svan](#):
>
> It won’t run on my system

What does that mean? If you can’t upload, that makes sense as the Bluetooth module connected to the Arduino on pins 0 and 1 will interfere with the upload communication.

If you don’t get output after you uploaded the sketch, have you tried swapping Rx and Tx?

What happens when you use Serial Monitor (connected to whatever `Serial.list()[2]` is) instead of your Processing program?

[Next page](https://discourse.processing.org/t/pc-to-bluetooth-with-arduino-does-not-work-when-usb-is-unplugged/42156.md?page=2)
