HC-05 Bluetooth connection

Hi all,

I'm having some difficulty getting my bluetooth modules to send information to eachother. One is configured as master and one as slave, they are connected as seen from the lights flashing every 2 seconds or so. They are configured the same default baud rate of 9600.

The project is to measure a fluid level as distance using an ultrasound sensor, and send the data to the other arduino which displays it on a computer. Attached is the code I've been trying to use.

Distance measuring arduino

#include <SoftwareSerial.h>

SoftwareSerial BTSerial(2,3);

int trigger_pin = 7;
int echo_pin = 6; 
float flightTime;
float distance;

void setup() {
  // put your setup code here, to run once:
  BTSerial.begin(9600);
  
  pinMode (trigger_pin, OUTPUT); 
  pinMode (echo_pin, INPUT);
}

void loop() {

  digitalWrite (trigger_pin, HIGH);
  delayMicroseconds (10);
  digitalWrite (trigger_pin, LOW);
  flightTime = pulseIn(echo_pin, HIGH);
  distance = (flightTime * 0.343) / 2;

  BTSerial.print(distance);
  BTSerial.print("#");
  delay(500);
  }

Printing to computer arduino

const byte numDigits = 5;
char rc;                       // rc = Received Character
char recvChars[numDigits];     // Array for number received
boolean newData = false;       // Internal print switch
    
void setup() {
  Serial.begin(38400);     // PC Communication
  Serial1.begin(9600);     // Bluetooth connection to other arduino

  Serial.println("<Arduino is ready>");
}

void loop() {

recvData();                // Function interpretting incoming series
printData();               // Function printing if new data present

}

//================== Functions Below ===============================

void recvData(){
  static byte n = 0;
  char endMarker = '#';     // Signifying end of data piece
  char rc;
  
    if (Serial1.available() > 0) {
        rc = Serial1.read();

        if (rc != endMarker) {
            recvChars[n] = rc;
            n++;
            if (n >= numDigits) {
                n = numDigits-1;
            }
        }
        else {
            recvChars[n] = '\0'; // end string
            n = 0;
            newData = true;
        }
    }
}

void printData(){
    if (newData == true) {
        int dataInt = 0;
        dataInt = atoi(recvChars); // COnverting function from char to int
        Serial.print("Height of missing fluid: ");
        Serial.print(dataInt);
        Serial.println(" mm");
        newData = false;
    }
  }

The code did work when using an Arduino Mega (Data hub) and Arduino Uno (FMD), when wired directly to eachothers Serial ports. But when expanding to the HC-05 module it doesn't work, even when wired to pins 0 and 1.

The aim is to get the ultrasound sensor on an arduino nano sending the data, with an arduino mega receiving it.

All help is welcomed, thanks :smiley:

fmd_code.ino (635 Bytes)

data_hub.ino (1.48 KB)

If you post your code as described in the how to use this forum sticky more members will see it.

.

PapaCrumb:
The code did work when using an Arduino Mega (Data hub) and Arduino Uno (FMD), when wired directly to eachothers Serial ports. But when expanding to the HC-05 module it doesn't work, even when wired to pins 0 and 1.

In that case the code is fine. All that should be done is substitute the wires for Bluetooth, and it is just that - substitution, not expansion. I guess your problem may be

  1. You have changed the code unnecessarily
  2. you have wired Bluetooth incorrectly, even though you clearly know the correct method Rx>Tx,TX>Rx
  3. You have not properly configured one HC-05 as a master in order to initiate communication with the other as a slave.

I've been using a voltage divider on the circuit with a 1k and 2k resistor so the module doesnt get damaged.

The code works when wired directly.

I have set one module to master with the specific address of the slave, using AT+CMODE=0 and AT+BIND=. I have changed the baud rate of each module to 38400. And the light flash pattern indicates that a connection has been made.

Is there an extra step in the module configuration that I'm missing?

Many thanks for your help.

EDIT: Rewired the cricuit in another breadboard and operates well! Thanks for all your help!