Trouble with RX/TX! Where am I going wrong?

Hey Folks!

I am trying to interface my arduino with an HC-05 bluetooth module! The module itself seems to pose no problem to me. I am able to wire its RX/TX together and connect to it and receive whatever I send to it.

Now on the end of the Arduino, I find trouble.

My goal is to receive a command from my computer to my arduino! I see the RX led flash, but no action! What am I doing wrong? This doesnt work on either the RX/TX pins nor the ones I set them to be!

My code is below!

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX
int led=13;

void setup()  
{
  pinMode(led,OUTPUT);
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.println("Goodnight moon!");

  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  mySerial.println("Hello, world?");
}
void loop(){
  
    
    int data = Serial.read(); // Call in the serial data and assign it to a variable
    
  if(data==1) //If we see 1, turn on the led... Reference OnLED()
  {
    OnLED();
  }
 delay(100); 
 Serial.write(data); //To check and see if it's even getting this far
 delay(500);
}

void OnLED(){
  
  digitalWrite(led,!digitalRead(led)); //Write the LED to High or low depending on what it was before.
}

Thanks for being kind and taking the time to read my code! :slight_smile:

P.S: I’m using a Leonardo!

hii, i think using serial communication with leonardo is quite difficult.,, even i tried to do the serial communication but leonardo do not support. even a normal (printing analog values on serial monitor) it don't do that.

Processed: I am able to wire its RX/TX together and connect to it and receive whatever I send to it.

What Arduino pins is the bluetooth module connected to? You define a SoftwareSerial but don't seem to be reading from it, and you seem to be using the hardware Serial both to receive data (presumably from the bluetooth module), and for trace output (presumably to the USB serial and hence the Arduino IDE Serial Monitor).

I'd expect to find one serial port (probably the hardware Serial, if the USB is connected) used for trace messages and the other (probably SoftwareSerial) used to talk to the bluetooth module.

Well, I plug in the device separately in the wall and I can find the Bluetooth serial port on my computer.

The problem I'm having is communication between the module and the arduino board! I can loop a jumper between the rx/tx pins and receive what I send on both boards (Bluetooth and arduino). I don't know what the problem is. I believe it maybe a hardware mistake...

Processed: The problem I'm having is communication between the module and the arduino board!

Are you going to tell us how they're connected, or is that a secret?

Sorry!

5V from arduino to BT module (it's what its rated for -- it works)

Ground - Ground

RX of arduino -> TX on BT module

TX of arduino -> RX on BT module

That's it! In the code above I tried declaring pins 11 and 10 as the RX/TX pins, that doesn't work either

Thanks...

int data = Serial.read(); // Call in the serial data and assign it to a variable

Why are you reading data without checking if there is data to be read?

  if(data==1)

Are you absolutely certain that your phone is sending 1 rather than '1'? What application are your using to send the data? How about, with the device connected to the computer, and the Bluetooth connected to the SoftwareSerial pins, forward any input from the phone to the computer's serial monitor and vice versa. Perhaps even as hex so you know the exact value.

We're at the point where my knowledge becomes hazy about this!

I am unfamiliar of the difference... I know it's got something to do with ASCII/Hex stuff but I don't know how to send or receive specific ones. Do the quotation marks make the difference?

I have a Mac and I'm using CoolTerm (it's a lot like Tera Term). There is an option to send it as a hex but I try both options!

I don't understand the forwarding part of that... You are saying I should just try to send and receive from the device itself? Something in the loop like:

if (Serial.available() > 0) {
                // read the incoming byte:
                incomingByte = Serial.read();

                // say what you got:
                Serial.print("I received: ");
                Serial.println(incomingByte, DEC);
}

If you have connected the Rx/Tx pins (0 and 1) to the BT module as well as connecting the USB interface then the USB interface and BT module will both be trying to drive the Rx pin and will end up fighting against each other. That's not a good idea. It may be that one of them consistently "wins" and you actually can receive serial data from the winning device, but it's still not a good way to do it. If you're using the USB serial port for trace messages as it seems, then it would be best to use a different serial port to connect to the BT module. Given that your Arduino only has one hardware serial port, the second port used for the BT interface would need to be a software serial port. And look, you already have a software serial port which you aren't using. Isn't that a happy coincidence?