Two Arduinos talking via Bluetooth

Hey guys,

I'm trying to connect two Arduinos with each other via BT - but it doesn't work like expected. Maybe you know what the problem might be and give me a hint or something like that. That would be great!

Here is what i've done:

  1. I configured two HC-05 modules as Master and Slave, Baud 38400.

  2. BT-Master connection to Arduino('master'):

VCC <-> 5V
GND <-> GND
TXD <-> RXD
RXD <-> TXD

  1. Arduino('master') config:

void setup() {
Serial.begin(38400);
}

void loop() {
Serial.write('1');
delay(2000);
Serial.write('0');
delay(2000);
}

  1. BT-slave connection to Arduino('slave'):

VCC <-> 5V
GND <-> GND
TXD <-> TXD
RXD <-> RXD

in addition there is a LED on pin 13 (connected correctly).

  1. Arduino('slave') config:

int ledpin = 13;
char val;

void setup() {
Serial.begin(38400);
pinMode(ledpin,OUTPUT);
}

void loop() {
if(Serial.available()){
val = Serial.read();
}
if(val == '1'){
digitalWrite(ledpin,HIGH);
}
else{
digitalWrite(ledpin,LOW);
}
delay(100);
}

the BT modules are connected and flash synchronously.

did I make any mistakes here?
why doesn't it work like expected?

it would be great if you could help me and give me any hint.

Regards, Marc

marCenzeichen:
the BT modules are connected and flash synchronously.

This is meaningless, but probably means they are not connected.

I'm gonna check that!

But the wire connections (especially RX, TX) and the codes/configs are OK?

Regards, Marc

Connect the two Arduinos using wires between the pins that you plan to use with the Bluetooth modules and get that working first. Then you will know your code is OK.

If you are using an Uno it will probably make sense to use SoftwareSerial for the Bluetooth module so that HardwareSerial is available for debugging messages. But start with SoftwareSerial at 9600 baud.

Have a look at the examples in Serial Input Basics. The third example will be most reliable.

...R

I have to cross the TX/RX wires when I connect the two Arduinos by wires (without BT), right?

OK I'm gonna check the SoftwareSerial.

Thanks for your help!

Regards, Marc

The Rx receives from the Tx transmitter irrespective of the device. It would be smart to use a 1k/2k divider on the Arduino Tx, in order to put 3.3v onto bluetooth.

Thanks for your help so far!

Connect the two Arduinos using wires between the pins that you plan to use with the Bluetooth modules and get that working first. Then you will know your code is OK - Robin2

That was a good tip! I connected the two Arduinos by using (crossed) wires between the RX/TX pins. I figured out that my code was OK except for the baudrate of 38400. I changed it to 9600 and it worked fine (with wires).

To make headway, I reconfigured the BT-modules by using

Phillipe Cantin: HC-05 Bluetooth link with zero code - Nick_Pyner

and replaced the wire connection by the BT-modules

BT-Master connection to Arduino('master'):

VCC <-> 5V
GND <-> GND
TXD <-> RXD
RXD <-> TXD

BT-slave connection to Arduino('slave'):

VCC <-> 5V
GND <-> GND
TXD <-> TXD
RXD <-> RXD

When they power up, they start connecting and flash synchronously when they are connected. BUT that's it. The code of Arduino('master') is not transmitted to Arduino('slave') any more.

by the way..

It would be smart to use a 1k/2k divider on the Arduino Tx, in order to put 3.3v onto bluetooth.

is this necessary?

Regards

Marc

marCenzeichen:
I connected the two Arduinos by using (crossed) wires between the RX/TX pins. I figured out that my code was OK except for the baudrate of 38400. I changed it to 9600 and it worked fine (with wires).

Since you are working with proper hardware serial, it should be fine with 38400. Check the wiring, but at least the code is essentially OK. (Oh, Gawd) Maybe you changed it to software serial. That was not a good idea, it hardly ever is, and maybe that is why it wouldn't work at 38400.

To make headway, I reconfigured the BT-modules by using and replaced the wire connection by the BT-modules When they power up, they start connecting and flash synchronously when they are connected.

As I said, I think this is meaningless, but maybe it depends on the module you are using, and what you mean by flash. I don't think they are connected, and the fact that your code works OK with wires tends to prove that they aren't.

by the way.. is this necessary?

Sort of. I didn't say it was necessary, I said it was smart. The bluetooth is usually a 3.3v device.

Here is the code i'm using right now (with wires):

Master:

void setup() {

  • Serial.begin(9600);*
    }
    void loop() {
  • Serial.print('1');*
  • delay(1000);*
  • Serial.print('0');*
  • delay(1000);*
    }

Slave:

int ledpin = 13;
char val;
void setup() {

  • Serial.begin(9600);*
  • pinMode(ledpin,OUTPUT);*
    }
    void loop() {
  • if(Serial.available()){*
  • val = Serial.read();*
  • }*
  • if(val == '1'){*
  • digitalWrite(ledpin,HIGH);*
  • }*
  • else{*
  • digitalWrite(ledpin,LOW);*
  • }*
  • delay(1);*
  • }*

It works great with wires but doesn't work with BT.

The fact that the hardware serial is on 9600 baud and the BT-module on 38400 baud (by default), might that be the problem?

(Oh, Gawd) Maybe you changed it to software serial.

You mean pin 0 (RX) and pin 1 (TX), right? No I didn't change them to software serial.

what you mean by flash

When I power them up they start flashing/blinking like 2-3 times a second (like very fast). I think that indicates that the master searches for the slave and the slave waits for the master. When they found each other, they start flashing/blinking like 2 times (fast) every 2 seconds.

I didn't say it was necessary, I said it was smart. The bluetooth is usually a 3.3v device.

The label on the BT-Module says: Power 3.6 V - 6V and TX RX Level: 3.3 V. So are they already irreparably damaged? I watched and read lots of stuff about the BT-module config but no one has said anything about the voltage. They connected the BT-modules directly to the Arduino.
I still can configure the BT-modules and get OKs when I type AT in the AT command line. So the modules are not damaged, right?

Thanks again for your help!

Regards

Marc

Ok I changed 9600 baud back to 38400 baud and it works fine for wire connection. But still not for BT.

OK

  1. You can conclude that the code is 110% kosher. Don't alter it, other than the speed, and even then only after proper consideration.

  2. You can set both arduinos to any rate you like for comms by wire, but you must set arduino to the rate at for which bluetooth is configured for comms by bluetooth

  3. 38400 is not normally the default speed for communication. It is the default speed for configuration, but only for configuration. Since you know about config. I suggest you check to see that the rate for comms really is set to what you think it is. Whatever it is, don't bother to change it, and ensure that the code is set for that. You can't simply change the code without changing the bluetooth config. They must match.

  4. Yes Pin 0 Rx is connected to bluetooth Tx, and pin 1 Tx is connected to bluetooth Rx. Double check this - twice. Disconnect the serial monitor. Stay away from software serial.

  5. The very fast flash simply means power on, ready to connect. I think you are right about what I understand is a double blink every two secs. That might vary according to the board but it makes me suspect bluetooth is fine but one, or the other, or both, can't talk to Arduino. Read item 4 again.

  6. Only an idiot will say don't bother with a divider on bluetooth Rx. Having said that, I ran an HC-06 continuously for about three years without one, and without a problem. I did this because I didn't know. You do, so it's your call, but you are surely correct, you have not done any damage - yet. Note there is no need to worry about Bluetooth Tx. That is a 3.3v signal and Arduino is OK with it.

  7. The backboard has an on-board regulator to bring 3.6v - 6v down to 3.3v. Don't ask why there is no on-board divider for the Rx

You are right :wink:

38400 is not normally the default speed for communication

I checked the baudrate of the BT-modules and both are set to 9600 baud (by default). So this means my arduino codes are fine by Serial.begin(9600) - is that right? (BT module and arduino need to be at the same speed.)

I know you said don't bother to change it but doesn't it make sense to speed it up like on 115200 baud for example?

Yes Pin 0 Rx is connected to bluetooth Tx, and pin 1 Tx is connected to bluetooth Rx. Double check this - twice

But that just goes for one site - either Arduino('master') and master BT or Arduino('slave') and slave BT - am I right? The other one will be TX-TX and RX-RX like this:

Arduino('master') and master BT:
TX-RX
RX-TX

Arduino('slave') and slave BT:
TX-TX
RX-RX

Read item 4 again

Do you mean the reply #4 so the Serial Input Basics? What exactly do you mean there? Do I have to set something like an endmarker?
Cause when I start the serial monitor for the master Arduino it sends 10101010101 in one line. Might that be the problem?

Regards

Marc

marCenzeichen:
You are right :wink:
I checked the baudrate of the BT-modules and both are set to 9600 baud (by default). So this means my arduino codes are fine by Serial.begin(9600) - is that right? (BT module and arduino need to be at the same speed.)

Yes.

I know you said don't bother to change it but doesn't it make sense to speed it up like on 115200 baud for example?

I agree but, if you really need to do it, which you probably don't, do it later. Get this problem solved first. Having said that I'm of the view that there only two speeds for bluetooth:

  1. whatever it is out of the box, usually 9600

  2. 115200

I don't know why you would ever use anything else.

But that just goes for one site

No. It applies to everything on everything.

  • either Arduino('master') and master BT or Arduino('slave') and slave BT - am I right? The other one will be TX-TX and RX-RX like this:

Arduino('master') and master BT:
TX-RX
RX-TX

Arduino('slave') and slave BT:
TX-TX
RX-RX

This is rubbish. Read point 4. in reply #10 - twice.

Do you mean the reply #4 so the Serial Input Basics? What exactly do you mean there? Do I have to set something like an endmarker?
Cause when I start the serial monitor for the master Arduino it sends 10101010101 in one line. Might that be the problem

No comment. While Serial Input Basics is the recognised masterwork in this arena, I have never read it, and I don't think you need need to either. Your code is already proven kosher. What you do need to do is read point 4. in reply #10 - twice, understand the logic, which is simply the names clearly written by the pins, and then write it out 50 times. I suspect that wrong wiring is the only problem. Read point 5. in reply #10 again(!).

I submit you can only have three areas of grief

  1. Arduino code - no, it's fine
  2. Bluetooth code - probably not, I think the lights might signify it is OK
  3. Wiring bluetooth<>arduino - very likely

OMG I can't believe it :o FINALLY IT WORKS!!! :smiley:

Since I was dealing with bluetooth and arduino I thought TX and RX is just crossed at one site - either master or slave. I was totally wrong!

A big BIG thank you!!

Ok now I changed the breadboard construction like in the attached pic.

-> There is a button connected to Arduino master and a LED connected to Arduino slave.

The codes are:

Arduino master

int pushButton = 3;
int buttonState = 0;
void setup() {

  • Serial.begin(9600);*
  • pinMode(pushButton, INPUT);*
    }
    void loop() {
  • buttonState = digitalRead(pushButton);*
  • if (buttonState == HIGH) {*
  • Serial.write('1');*
  • } else {*
  • Serial.write('0');*
  • }*
  • }*

Arduino slave

int ledpin = 13;
char val;
void setup() {

  • Serial.begin(9600);*
  • pinMode(ledpin,OUTPUT);*
    }
    void loop() {
  • if(Serial.available()){*
  • val = Serial.read();*
  • }*
  • if(val == '1'){*
  • digitalWrite(ledpin,HIGH);*
  • }*
  • else{*
  • digitalWrite(ledpin,LOW);*
  • }*
  • delay(1);*
  • }*

The good news is that it WORKS! So when I press the button the LED starts to shine.

The bad news is that there is a kind of time lag which is growing in the course of time. So when I power them up (and they got connected via bt) the time lag is like 1 second. After some minutes the time lag is about 3 seconds. After some more minutes the time lag is like 5 seconds and so on until nothing happens any more.
Besides that I spotted/picked out (I don't know what's the right word in this case) that the synchronous flashing/blinking of the bluetooth modules slowly turns into an asynchronus flashing/blinking. So there's a kind of time lag, too.

When I open the serial monitor for the master, there're lots of 0000000.....000s with a rapidly growing count.

So is there a function/code that writes just ONE 0 instead of hundreds, nay thousands.
Same goes for the 1.

Regards

Marc

marCenzeichen:
The bad news is that there is a kind of time lag which is growing in the course of time. So when I power them up (and they got connected via bt) the time lag is like 1 second. After some minutes the time lag is about 3 seconds. After some more minutes the time lag is like 5 seconds and so on until nothing happens any more.
Besides that I spotted/picked out (I don't know what's the right word in this case) that the synchronous flashing/blinking of the bluetooth modules slowly turns into an asynchronus flashing/blinking. So there's a kind of time lag, too.

OK, so we conclude that the only real problem was Bluetooth/Arduino wiring.

I don't think there is ever any synchronous blinking, even if it looks like there is. There may be blinking
triggered by communication action, but I don't know about that.

I can only suggest you go back to wire connection between Arduinos and run the code for a similar period of time, something you may not have actually done before. If the time lag problem is still there, then clearly there is a problem in the code after all - and probably a minor one.

If not, then the bluetooth config is suss, but I really don't know how that can happen.

Ok I went back to wire connection and everything worked and still works great...

When I press the button the LED is shining immediately even after a similar period of time. Besides that the BT modules are just powered up (without TX RX connection) and they're blinking synchronously without losing it. So no time lag at all.

My hypothesis is that the BT modules got a problem with the permanent sending of data. So when I do not press the button the master sends thousands of '0's although one '0' would be enough. When I press the button it sends as many '1's as long as I do press the button (although one '1' would be enough too). Maybe the buffer is some kind of overloaded?

The question is: is there a possibility to program the master arduino in a way that it just sends one '1' when I press the button and sends one '0' when I don't press it?

Another idea is to change the baud rate to 115200 but the question is: what would be the effect? Does it cause even more '0's and '1's?

Regards

Marc

I don't know much about this stuff, as I'm really only interested in sending data, and I'm afraid I certainly don't understand why what you have works with wires and gives grief with bluetooth, but clearly this

  if (buttonState == HIGH) {
    Serial.write('1');
    } else {
    Serial.write('0');
    }

means there will be Os for ever, or so long as you don't send a 1, hence lots of Os at breathtaking speed.

I suggest you put up a flag following the

Serial.write('1');

and make it being up a condition for

Serial.write('0');

and take it down after you do write an 0. There may be more elegant ways of doing it in C, but I recall it worked in 1966.

The button press may be flagged similarly, but there is no real need for it.

The wiring around the switch in your Fritz may be kosher, but it makes no sense to me, and I think you should check it.

marCenzeichen:
Another idea is to change the baud rate to 115200 but the question is: what would be the effect? Does it cause even more '0's and '1's?

Very likely.

Ok I fixed the time lag problem and I think that may be interesting to anyone.

My hypothesis is that the BT modules got a problem with the permanent sending of data. So when I do not press the button the master sends thousands of '0's although one '0' would be enough. When I press the button it sends as many '1's as long as I do press the button (although one '1' would be enough too). Maybe the buffer is some kind of overloaded?

My hypothesis was right! I tried to use some kind of flag to make the Arduino sends just one '0' when I don't press the button and sends one '1' when I press it.

So I changed the code of the master as follows:

const int pushButton = 3;     //constant number of the pushButton pin
int buttonState = 0;          //variable for reading the pushbutton status
int Flag = 0;                 //variable for the flag

void setup() {
  Serial.begin(9600);          //opens serial with 9600 baud/bites per second
  pinMode(pushButton, INPUT);  // initialize the pushButton pin as input

}

void loop() {

  buttonState = digitalRead(pushButton);  // read the state of the pushButton value

  if (Flag == 0) {               //if the flag is '0'
    if (buttonState == HIGH) {   // plus if the buttonState is 'high'
      Serial.write('1');         // then write '1' to the serial
      Flag = 1;                  // plus set flag to '1'
    } else {                     // if the flag is '0' plus buttonState is 'low' do nothing
      // empty    
    }
  }
  if (Flag == 1) {               //if the flag is '1' 
    if (buttonState == LOW) {   // plus if the buttonState is 'low'
      Serial.write('0');         // then write '0' to the serial
      Flag = 0;                  // plus set flag to '0'
    } else {                     // if the flag is '1' plus buttonState is 'high' do nothing
      // empty
    }     
  }
}

It may be not the best/ most elegant way, but it works!

No time lags in case of transmitting even after hours - but there are time lags in case of synchronous blinking (master bt and slave bt) which doesn't matter and doesn't have any effects.

Regards

Marc