Go Down

Topic: Problem with HC-05 bluetooth module (Read 311 times) previous topic - next topic

purplepanda

I am able to pair my bluetooth with my computer, but when I check the ports, there is no information being recieved.

Since i am using the USB to power for right now when I use the USB port I am able to read the sensor data, so I would have thought that the bluetooth would allow me to read the same data from the ports connected to the bluetooth.

I was wondering what the problem could be?

Instead of using a voltage divider, I connected directly to 3.3V on arduino.

This is my arduino code

#include <SoftwareSerial.h>
#include <OneWire.h>
#include <DallasTemperature.h>

SoftwareSerial BTserial(0, 1); // RX | TX

char c = ' ';
#define ONE_WIRE_BUS 2

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

void setup()
{
   Serial.begin(9600);
   sensors.begin();
   Serial.println("Communication Established");
   //Serial.println("Remember to select Both NL & CR in the serial monitor");

   // HC-05 default serial speed for Command mode is 9600
   BTserial.begin(9600);  
}


void loop()
{
 // Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
 sensors.requestTemperatures();
 
 Serial.print("Celsius temperature: ");
 Serial.print(sensors.getTempCByIndex(0));
 Serial.print(" - Fahrenheit temperature: ");
 Serial.println(sensors.getTempFByIndex(0));

 delay(1000);
 
   // Keep reading from HC-05 and send to Arduino Serial Monitor
   if (BTserial.available())
   {  
       c = BTserial.read();
       Serial.write(c);
   }

}



Because the problem seemed to be with the bluetooth, I tried to test just the bluetooth with the example from how to mechanics.

using the below as my arduino code
#define ledPin 7
int state = 0;

void setup() {
 pinMode(ledPin, OUTPUT);
 digitalWrite(ledPin, LOW);
 Serial.begin(38400); // Default communication rate of the Bluetooth module
}

void loop() {
 if(Serial.available() > 0){ // Checks whether data is comming from the serial port
   state = Serial.read(); // Reads the data from the serial port
}

if (state == '0') {
 digitalWrite(ledPin, LOW); // Turn LED OFF
 Serial.println("LED: OFF"); // Send back, to the phone, the String "LED: ON"
 state = 0;
}
else if (state == '1') {
 digitalWrite(ledPin, HIGH);
 Serial.println("LED: ON");;
 state = 0;
}
}


and using the below as code for processing ide
import processing.serial.*;
Serial myPort;
String ledStatus="LED: OFF";

void setup(){
 size(450, 500);
 myPort = new Serial(this, "COM4", 38400); // Starts the serial communication
 myPort.bufferUntil('\n'); // Defines up to which character the data from the serial port will be read. The character '\n' or 'New Line'
}
void serialEvent (Serial myPort){ // Checks for available data in the Serial Port
 ledStatus = myPort.readStringUntil('\n');
}

void draw(){
 background(237, 240, 241);
 fill(20, 160, 133); // Green Color
 stroke(33);
 strokeWeight(1);
 rect(50, 100, 150, 50, 10);  // Turn ON Button
 rect(250, 100, 150, 50, 10); // Turn OFF Button
 fill(255);
 
 textSize(32);
 text("Turn ON",60, 135);
 text("Turn OFF", 255, 135);
 textSize(24);
 fill(33);
 text("Status:", 180, 200);
 textSize(30);
 textSize(16);
   text(ledStatus, 155, 240); // Prints the string comming from the Arduino
 
 // If the button "Turn ON" is pressed
 if(mousePressed && mouseX>50 && mouseX<200 && mouseY>100 && mouseY<150){
   myPort.write('1'); // Sends the character '1' and that will turn on the LED
   // Highlighs the buttons in red color when pressed
   stroke(255,0,0);
   strokeWeight(2);
   noFill();
   rect(50, 100, 150, 50, 10);
 }
 // If the button "Turn OFF" is pressed
 if(mousePressed && mouseX>250 && mouseX<400 && mouseY>100 && mouseY<150){
   myPort.write('0'); // Sends the character '0' and that will turn on the LED
   stroke(255,0,0);
   strokeWeight(2);
   noFill();
   rect(250, 100, 150, 50, 10);
 }
}


This is not working either. There is no change when I manually turn LED on and off. It shows as off no matter what. Which means that it is not reading because the default is off.


I am desperately hoping you can help me find a solution.

pert

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn't show the posting toolbar, then you can manually add the code tags:
[code]// your code is here[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do an Auto Format (Tools > Auto Format in the Arduino IDE or Ctrl + B in the Arduino Web Editor) on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.


Which Arduino board are you using?

purplepanda

Thanks for the advice. Im using an arduino uno.

pert

Pins 0 and 1 on the Arduino Uno are used for serial communication between the Uno and the computer via the USB cable. Not only are they used for communicating with the Serial Monitor using Serial.print(), etc., it is also used for uploading sketches to your Uno. For this reason, you should avoid connecting anything to pins 0 and 1.

It also makes no sense to use SoftwareSerial on pins 0 and 1 because these pins have hardware serial functionality.

My recommendation is to connect your Bluetooth module to some other pins on your Uno and then update this line of your code accordingly:
Code: [Select]
SoftwareSerial BTserial(0, 1); // RX | TX

cattledog

Quote
Instead of using a voltage divider, I connected directly to 3.3V on arduino.
The HC05 module is a 5v Vcc. I think that 4 is the minimum specified input voltage.

The voltage divider is for the Arduino TX to the module RX.

purplepanda

#5
Jan 17, 2019, 03:05 pm Last Edit: Jan 17, 2019, 04:28 pm by purplepanda
Thanks for all your help, but I'm still having the same problem. I used the voltage divider with 1k and 2k and changed the communication pins to 10 and 11

cattledog

Quote
Thanks for all your help, but I'm still having the same problem. I used the voltage divider with 1k and 2k and changed the communication pins to 10 and 11
Did you change the supply voltage to 5v for the module?

Quote
I am able to pair my bluetooth with my computer, but when I check the ports, there is no information being recieved.
Can you work with a phone and a bluetooth serial terminal app (I like Kai Morich's "Serial Bluetooth Terminal") until you get this sorted out. You would not be the first person to have difficulty with bluetooth dongles and computer connections.

The debug process I would follow is to first get working code with the Serial monitor and no bluetooth connected. Then, when you have solid code downloaded on the Arduino,  connect the HC05 to the hardware serial pins 0/1 (remember to cross connect TX>Rx, RX>TX), pair/connect the phone and let the phone with terminal app replace the monitor. Finally, switch out the phone for the computer bluetooth.

purplepanda

Did you change the supply voltage to 5v for the module?

Can you work with a phone and a bluetooth serial terminal app (I like Kai Morich's "Serial Bluetooth Terminal") until you get this sorted out. You would not be the first person to have difficulty with bluetooth dongles and computer connections.

The debug process I would follow is to first get working code with the Serial monitor and no bluetooth connected. Then, when you have solid code downloaded on the Arduino,  connect the HC05 to the hardware serial pins 0/1 (remember to cross connect TX>Rx, RX>TX), pair/connect the phone and let the phone with terminal app replace the monitor. Finally, switch out the phone for the computer bluetooth.
I used 5 volts instead of 3.3v. I will try with my phone and an app. Thank you!

cattledog

#8
Jan 18, 2019, 03:19 am Last Edit: Jan 18, 2019, 03:24 am by cattledog
Quote
I will try with my phone and an app.
Code: [Select]
// HC-05 default serial speed for Command mode is 9600
   BTserial.begin(9600);
 
  Serial.begin(38400); // Default communication rate of the Bluetooth module


You have posted two different things in the two different codes you posted, and the comments appear backwards. The default baud rate in the communication mode is 9600. The default baud rate in the command mode (AT commands) is 38400. You will be in the communication mode when the phone or PC bluetooth talks wirelessly to the module and the module talks to the Arduino.

Go Up