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

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

Hi

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

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

The results are the same.

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 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.

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.

:slight_smile:

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.

What device are you using for Bluetooth?

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.

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

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.

Hello @Myrdalm ,

Which Bluetooth module(s) are you using?

:)

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

1 Like

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

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

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

What should be used for HC05?

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.

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:

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:

Arduino code using SoftSerial works ok:

#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:

#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);
      }
  }

}

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.

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.

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?