sending data from one arduino to another via bluetooth

Hi there.

For a project I'm working on, I need to send data from one Arduino to another using HC-05 bluetooth modules. Here is what I'm trying to accomplish:

Arduino 1 -> Bluetooth 1 -> (wirelessly) Bluetooth 2 -> Arduino 2

So the first thing I did was configure one module as a master and one as a slave and then paired them (using this guide: Bluetooth HC05- How to pair two modules | alselectro)

Here is my code that transmits data from Arduino 1, the master arduino:

#include <SoftwareSerial.h>
#define BT_SERIAL_RX 11
#define BT_SERIAL_TX 10
SoftwareSerial BluetoothSerial(BT_SERIAL_RX, BT_SERIAL_TX);

void setup() {
    Serial.begin(9600);
    BluetoothSerial.begin(9600);
}

void loop() {

  BluetoothSerial.write("test");
  BluetoothSerial.println("test"); 
  Serial.println("test"); 
  delay(100); // prepare next data
}

Here is the code I have on the slave Arduino:

String message; //string that stores the incoming message
void setup()
{
  Serial.begin(9600); //set baud rate
  pinMode(2, OUTPUT); 
}

void loop()
{
  while(Serial.available())
  {
    message+=char(Serial.read());
  }
  if(!Serial.available())
  {
    if(message != "") // if we received some data, then light up an LED
    {   
      digitalWrite(2, HIGH);    
      Serial.println(message);
          message="";
    }
  }
  delay(200);
}

However it doesn't work (pin 2 of the slave should become high, but it doesn't). The receive light on the slave Arduino is never on so it doesn't receive anything.

I'd really appreciate some help. :confused:

I believe that the HC-05 defaults to slave.

Get the project working using wires between the arduino serial ports like below, then bring in the wireless part. makes troubleshooting much easier.

Arduino 1 -> wires > Arduino 2

1 Like

inkosi:
So the first thing I did was configure one module as a master and one as a slave and then paired them (using this guide: Bluetooth HC05- How to pair two modules | alselectro)

You have two issues to address:

  1. proper communication, hence zoomkat's comment
  2. proper connection.

There may be a language problem here. Pairing does not mean communication or connection. So are they programmed to actually connect? And do they do that? The LEDs on the bluetooth modules are far more important than lights on the arduino.

These notes are about auto-connect with HC-05s

Thank you for the advice. ieee488, you are right they both are slaves by default. But I configured one of them to be a master by changing the ROLE command to 1.

I took zoomkat's advice and successfully managed to get serial communication using wires, so I decided to try out wireless using the link you posted, Nick_Pyner. I managed to bind them, and then uploaded the following codes (changed a little from my previous post):

MASTER:

#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX

void setup() 
{
  Serial.begin(9600);
  BTSerial.begin(38400);  // HC-05 default speed in AT command more
}

void loop()
{
  BTSerial.write('A');
  Serial.print('A');
  delay(1000);
  BTSerial.write('B');
  Serial.print('B');
  delay(1000);
}

The TX light flashes every second so I assume the data is being SENT to the master HC-05 module. Then I have this code in the other, slave Arduino:

#include <SoftwareSerial.h>

SoftwareSerial BTSerial(10, 11); // RX | TX
String readdata;

void setup() 
{
  pinMode(2, OUTPUT);
  Serial.begin(9600);
  BTSerial.begin(38400);  // HC-05 default speed in AT command more
}

void loop()
{
    while (BTSerial.available()) {
    delay(10);
    char c = BTSerial.read();
    readdata += c;
  }
  if (readdata.length() > 0) {

    if (readdata == "A")
    {
      digitalWrite(2, HIGH);
      delay(100);
    }
        else if (readdata == "B")
    {
      digitalWrite (2, LOW);
      delay (100);
    }

    readdata = "";
  }
}

However I am not getting any output on pin 2 of the slave Arduino. I know the HC-05 modules are BINDED now because one was flashing quickly, but started flashing slowly once I connected the other HC-05. Is there any mistake in my code?

Here are my connections:

Arduino 1:

PIN 10 - TX Bluetooth 1
PIN 11 - RX Bluetooth 1
GND -> GND of the other Arduino + Bluetooth 1
5V -> VCC Bluetooth 1

Arduino 2:

PIN 10 - TX Bluetooth 2
PIN 11 - RX Bluetooth 2
PIN 2, GND - to LED anode + cathode respectively
GND -> GND of the other Arduino, Bluetooth 2
5V -> VCC Bluetooth 2

If you want to try some direct serial testing, load the bottom non serial dummy code on both arduinos, then attach the bluetooth pins like below to the arduinos. This should allow direct communication between the two serial monitors via the bluetooth modules without having the arduino itself in the mix.

serial monitor 1 > bluetooth<>bluetooth< serial monitor 2

Arduino 1:

PIN 1 - TX Bluetooth 1
PIN 0 - RX Bluetooth 1
GND -> GND of the other Arduino + Bluetooth 1
5V -> VCC Bluetooth 1

Arduino 2:

PIN 1 - TX Bluetooth 1
PIN 0 - RX Bluetooth 1
GND -> GND of the other Arduino, Bluetooth 2
5V -> VCC Bluetooth 2

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

You have code

  BTSerial.begin(38400);  // HC-05 default speed in AT command more

This may not be a problem but reads as nonsense. The code is for use in communications made and the speed in AT mode is irrelevant. Since the command is for 38400, has the bluetooth been configured to work at the rate?

I know the HC-05 modules are BINDED now because one was flashing quickly, but started flashing slowly once I connected the other HC-05.

"Slowly" is a relative term. I recognise that there have been some changes in the way the LED on the module operates with some versions, and the wiring suggests you are in comms mode but what you say doesn't sound right. Typical LED operations are

  1. Power on, ready to connect Flash 2Hz

  2. AT mode Flash once every 2 sec

  3. Comms mode Steady on OR off with flashes when data actually carried.

"flash slowly" rather suggests AT mode.

I believe you should have a current limiting resistor between Pin 2 and LED.

Thanks. I believe I have finally gotten it to work. The default communication rate is 9600 baud (38400 was only default for AT mode) so that was one error. Another error was, I believe, not having the KEY pin on my HC-05 module (I had a wakeup pin which I thought was the same thing -- but I think it isn't). I had to solder an extra wire from PIN34 of the HC-05 module.

Thanks for the help!

I have a new task now. I need to make the communication two-way (to make a closed loop system). So my first idea was to make the slave HC-05 a master so it can send data back. However as soon as I do this (by simply changing AT-ROLE of the slave from 0 to 1), I am no longer able to receive data from the original master bluetooth (both of their lights flash rapidly).

I haven't been able to find some guidance on this online, so help would be appreciated.

inkosi:
Thanks. I believe I have finally gotten it to work. Another error was, I believe, not having the KEY pin on my HC-05 module (I had a wakeup pin which I thought was the same thing -- but I think it isn't). I had to solder an extra wire from PIN34 of the HC-05 module.

It might be best to confirm that it DID work, particularly since you don't need to connect the Key pin to communicate, and you shouldn't have to solder anything.

inkosi:
I have a new task now. I need to make the communication two-way (to make a closed loop system). So my first idea was to make the slave HC-05 a master so it can send data back. However as soon as I do this (by simply changing AT-ROLE of the slave from 0 to 1), I am no longer able to receive data from the original master bluetooth (both of their lights flash rapidly).

You do not need make the slave a master in order to get two way communication. You only need one master and its job as a master is only to make the connection. Once the connection is made, they both just get on with the job and it doesn't matter which is which. For all that, I'm sure the device that was originally a master should still be able to establish connection with the other device, even if the latter is now a master, not a slave.

I can only conclude:

  1. Despite the belief in reply #8, it never actually worked. OR
  2. You have made a change somewhere that you should not have, and aren't telling me.

The rapid flash means power on, ready to connect. It also means that any auto-connection has not happened.

Bongani's post was moved to Gigs & Collaborations.

hi,
we need to interconnect arduino to Bluetooth module .
to send data from arduino to android (phone).
so please send the code.

Serial.print(data);

Hello,
I am doing a project in which I have to send RSSI value from one Bluetooth module to another. Here, I am using RN-42 FLY477 Bluetooth module with Arduino Mega ADK. It would be great if you could help me on how to do this.
Thanks in advance