BT.Serial.available() always returns 0

I have set up 2 arduino unos with HC-05 bluetooth modules with a simple circuit to get a button on one board to turn the other board's LED on. The first board (with the module set up as master) is sending messages through BT.serial but the other board (with the module set up as slave) never receives the message: Bt.Serial.available always returns as 0.

I've put the code for both boards below :slight_smile:

Master Code:

#include <SoftwareSerial.h>
SoftwareSerial BTserial(10, 11); // RX | TX
 
boolean DEBUG = true; //changing to true outputs debug information to serial monitor

const int ledPin = 2;
const int buttonPin = 5;
int buttonState = 0;

void setup()  
{
    pinMode(ledPin, OUTPUT);
    pinMode(buttonPin, INPUT);
  
    if (DEBUG)
    {
        // open serial communication for debugging and show 
        // the sketch filename and the date compiled
        Serial.begin(9600);
        Serial.println(__FILE__);
        Serial.println(__DATE__);
        Serial.println(" ");
    }
 
    //  open software serial connection to the Bluetooth module.
    BTserial.begin(9600); 
    if (DEBUG)  {   Serial.println("BTserial started at 9600");     }
 
} // void setup()
 
 
void loop()  
{
   buttonState = digitalRead(buttonPin);
   
   Serial.println(buttonState); // check if button is working because of wrong message sending
   
   if (buttonState == 1) {
      digitalWrite(ledPin, HIGH);
      BTserial.println("<LEDON>");
      if (DEBUG) {Serial.println("LEDON command sent");}
      delay (500);
   }
   if (buttonState == 0) {
      digitalWrite(ledPin, LOW);
      BTserial.println("<LEDOFF>");
      if (DEBUG) {Serial.println("LEDOFF command sent");}
      delay (500);
   }
 }

Slave Code:

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

boolean DEBUG = true; //changing to true outputs debug information to serial monitor
 
 
// Variables used for incoming data
 
 
const byte maxDataLength = 20;          // maxDataLength is the maximum length allowed for received data.
char receivedChars[maxDataLength+1];
boolean newData = false;               // newData is used to determine if there is a new command
 
 
 
void setup()  
{
    pinMode(3, OUTPUT); 
    digitalWrite(3,LOW);
 
 
    if (DEBUG)
    {
        // open serial communication for debugging and show the sketch name and the date compiled
        Serial.begin(9600);
        Serial.println(__FILE__);
        Serial.println(__DATE__);
        Serial.println(" ");
    }
 
    //  open software serial connection to the Bluetooth module.
    BTserial.begin(9600); 
    if (DEBUG)  {   Serial.println(F("AltSoftSerial started at 9600"));     }
 
    newData = false;
 
} // void setup()
 
 
 
void loop()  
{
         recvWithStartEndMarkers();                // check to see if we have received any new commands
         if (newData)  {   processCommand();  }    // if we have a new command do something
}
 
 
void processCommand()
{
    newData = false;
    if (DEBUG)  {   Serial.print("recieved data = ");  Serial.println(receivedChars);         }
    if      (strcmp ("LEDON",receivedChars) == 0)  { digitalWrite(3,HIGH);   }
    else if (strcmp ("LEDOFF",receivedChars) == 0) { digitalWrite(3,LOW);    }
}
 
// function recvWithStartEndMarkers by Robin2 of the Arduino forums
// See  http://forum.arduino.cc/index.php?topic=288234.0
void recvWithStartEndMarkers() 
{

    
    
     static boolean recvInProgress = false;
     static byte ndx = 0;
     char startMarker = '<';
     char endMarker = '>';
 
     if (BTserial.available() > 0) 
     {
          char rc = BTserial.read();
          if (recvInProgress == true) 
          {
               if (rc != endMarker) 
               {
                    if (ndx < maxDataLength) { receivedChars[ndx] = rc; ndx++;  }
               }
               else 
               {
                     receivedChars[ndx] = '\0'; // terminate the string
                     recvInProgress = false;
                     ndx = 0;
                     newData = true;
               }
          }
          else if (rc == startMarker) { 
            recvInProgress = true; 
            }
     }
}

Perhaps you have the Bluetooth RX and TX the wrong way around.

1. Check that you wiring agrees with the following setup of Fig-1:
Hc5UnoUno
Figure-1:

2. Check that the BTs are paired.

3. Upload the following sketch in UNO-1.

#include<SoftwareSerial.h>
SoftwareSerial SUART(10, 11);//SRX = 10, STX = 11

void setup() 
{
  Serial.begin(9600);
  SUART.begin(9600);
  pinMode(5, INPUT_PULLUP);
  pinMode(2, OUTPUT);
}

void loop() 
{
  while(digitalRead(5) != LOW)
  {
    ;
  }
  digitalWrite(2, HIGH);
  delay(500);
  digitalWrite(2, LOW);
  SUART.write(0x02);
  delay(2000);
}

4. Upload the following sketch in UNO-2.

#include<SoftwareSerial.h>
SoftwareSerial SUART(10, 11);//SRX = 10, STX = 11

void setup()
{
  Serial.begin(9600);
  SUART.begin(9600);
  pinMode(3, OUTPUT);
}

void loop()
{
  byte n = SUART.available();
  if (n != 0)
  {
    byte y = SUART.read();
    if (y == 0x02)
    {
      digitalWrite(3, HIGH);
      delay(500);
      digitalWrite(3, LOW);
    }
  }
}

5. Gently, press and release K1 at UNO-1.
6. Check that LED1 at UNO-1 has turned ON for a while; LED2 at UNO-2 has also turned ON for a while.

7. Goto Step-5 to repeat the process.

How do you know that?

This sounds like a class assignment because exactly the same thing is being asked here, only they have got one step further than you.

1 Like

The pins should be the correct way around because upon start up the lights on the modules flash once every few seconds which indicates that they are connected.

I have triple checked and the circuits are wired up like the diagram above and when powered, the modules appear to connect (because the lights on the HC05 lights up once every few seconds). However, I have the same issue as I had with my program: the 2nd Uno isn't responding.
I haven't used SUART before, but, similarly to BTSerial, it is only showing 0.

This is for a class project, but not the same one because I'm the only one in my class using bluetooth. The linked project doesn't look similar apart from the fact that it also uses bluetooth because the wiring is very different. I am using a set up like the one posted previously on this thread. I'm also not quite sure what problem they were trying to solve because I couldn't understand the grammar?

The serial monitor prints "LEDON command sent" so I am assuming that this means that it sent the message above. Would there be a more accurate way to check if the message is being sent?

The modules connection status and transmission to each other is independent of the modules talking to the Arduinos.

You can verify the slave unit by using a phone app like Serial Bluetooth Terminal to connect to the slave HC05 and verify that it responds to the phone and the printing to the Arduino is correct.

If all is well with the slave unit, the Arduino, and the phone, then you can focus on the issue with the HC05 master.

That only means that that line of the programme has been executed. It would be just as relevant to say "The moon is made of cheese".

Your programme is more complicated than it needs be, but may be indeed 100% kosher. Nonetheless, all that then means is that Arduino has sent the relevant signal to the Tx pin. It is no guarantee that Bluetooth is properly connected thereto, and not the slightest guarantee of successful reception by the other Arduino.

I've used an app like the one you suggested and the phone said the module connected successfully. However, the light didn't turn on when I send the same message the master would have sent. I haven't manually sent commands to a module before so is there a chance I sent the wrong thing or the program needs to be changed to cater specifically to connection to a phone?

I got this code from a tutorial online to work with because I haven't used bluetooth before and couldn't find a more simple version. You say that my program is more complicated than it needs to be, so, do you have or know where I could learn how to create a simpler version of code that I could try out?

I just tested the slave code with The Kai Morich Serial Bluetooth Terminal app recommended above. I can send and and see an led on pin3 respond as well as the correct serial output.

Serial output

20:28:51.538 -> AltSoftSerial started at 9600
20:31:27.804 -> recieved data = LEDON
20:36:06.536 -> recieved data = LEDOFF

Getting the slave to work with a phone is the first order of business. You say the light didn't turn on, but did you see the correct Serial output?

Can you verify that the led works correctly? Do you have an inline resistor? Is the polarity correct? Can you get the led to light by connecting to 5v and ground?

Perhaps it would be best to use the onboard led on pin 13 incase you have something wrong in the wiring of the led on pin 3.

Another way to test your code independent from the bluetooth modules to disconnect the two HC05 modules from the Arduinos, and cross connect the two Arduinos on the software serial pins (Rx>Tx and Tx>Rx) and verify that the code works independently of the radios.

Your code appears to be kosher - just beyond my comprehension, and I am not in a position to test it. Just follow cattledog, and note particularly the last par in reply #14.

The arduino I am using as master is the one that is sending the LEDON and LEDOFF messages. I changed the setting for this module to slave so I could connect it to the phone and got the responses I was expecting:

<LEDON> (when the button was pressed)
<LEDOFF> (when the button was off)

This means that the messages from the master module are sending correctly so the error must be with the slave module receiving messages.
When I connected the slave arduino to the phone, it connected but I am not sure if I am sending messages correctly or if there is something wrong with my code because on startup it only prints '^@'. (i'm not sure why it does that or what it means)
To send the command that should turn the light on I'm assuming I should send

<LEDON>

but this doesn't seem to do anything?

What should my next steps be here to try and get the light on the slave board to turn on with a command from my phone?

Not necessarily. It may have to do with your configuration of the module as a master.

Do you have an Android phone?

Please use the Kai Morich Serial Bluetooth Terminal app.

You may wish to modify the start and end markers used in the code as the '<' and' >' are a little tricky to find. I have sometimes used

char startMarker = '*';
char endMarker = '#';

I do have an android phone and I'm using the Kai Morich Serial Bluetooth Terminal app. Thank you for the suggestion for changing start and end markers but I am okay with finding them.
I don't think that the problem is with the configuration with the master because when I ignore the master board and just use the slave board connected to a phone I still can't get the slave's lights to turn on, suggesting the problem is somewhere is in the slave board?

I've moved onto trying to get just one Bluetooth module to turn a light on when the message is received from a phone. I used this (https://create.arduino.cc/projecthub/mayooghgirish/arduino-bluetooth-basic-tutorial-d8b737) tutorial. However, I have the same error that I started with originally where the serial.available() is returning 0 so the program never enters the if statement. Therefore, the problem is most likely to do with that specifically but I am not sure how to fix this in any way.

Here is the most simple code to use between the slave HC05 and the Kai Morich phone app. It sounds like the light pattern on the slave indicates connected and disconnected properly.

If using this code you can't send text back and forth between the phone and the module then the slave is likely defective if you have connected the Rx to Tx and Tx to Rx and are using 5v and ground to power the module.

If you can not see communication with this sketch, then please attach a photo of your connections.

If you can exchange text, then we can work on the led and the command set.

#include <SoftwareSerial.h>
SoftwareSerial BTserial(10, 11); // RX | TX  pins on Arduino
// Connect the HC-05 TX to Arduino pin 10(as RX).
// Connect the HC-05 RX to Arduino pin 11 (as TX) 
//using the voltage divider to keep the module Rx at 3.3v is best but not necessary

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

void loop()
{
  if (BTserial.available())
  {
    char x = BTserial.read();
    Serial.print(x);
  }

  if (Serial.available())
  {
    char y = Serial.read();
    BTserial.print(y);
  }
}