Sending distance data of HC-SR04 via Bluetooth HC-05

Hi everyone,

I have been struggling for a while now with my little project and I am simply lost on why it is not working as I expect it to work. I am fairly new to Arduino and generally doing hardware stuff, so forgive me for any mistakes or oversights.

The project in general is pretty simple: I have an ultrasonic sensor to measure distance, two 28byj-48 step motors and a HC-05 Bluetooth module. I want to control via Bluetooth from another device. On command the distance sensor should measure the distance and if its greater than I want it to be the step motors should drive until the desired distance is reached. Obviously I am having some gears etc. ready for this.

For whatever reason though sending and receiving data does not work, with the ultrasonic module and the Bluetooth module plugged in at the same time. Because then using the serial monitor with the correct port I can't even turn on the LED (which I do by sending 1), nor can I start the measurement process at all. If I do it via the USB connection it works as intended. I can also send and receive data with the Bluetooth module if I don't use the ultrasonic sensor. As it works via Bluetooth if I omit the measurement and leave the ultrasonic sensor unplugged.

Any help or points would be appreciated.
I have a schematic attached, though for demonstration purposes the step motors are not plugged in correctly. The lower line with the motors and the ultrasonic device are powered with 5v the upper line with the HC-05 is powered with 3.3v I am using an HW-131 for this purpose.

The code reads as follows:

int delaytime = 2;
int trigPin = A1;    // Trigger
int echoPin = A2;    // Echo
long duration, cm;
char blueToothVal; 
char lastValue;   



// ports used to control the stepper motor
// if your motor rotate to the opposite direction, 
// change the order as {4, 5, 6, 7};
int port[4] = {4, 5, 6, 7};
int portTwo[4] = {8,9,10,11};

// sequence of stepper motor control
int seq[8][4] = {
  {  LOW, HIGH, HIGH,  LOW},
  {  LOW,  LOW, HIGH,  LOW},
  {  LOW,  LOW, HIGH, HIGH},
  {  LOW,  LOW,  LOW, HIGH},
  { HIGH,  LOW,  LOW, HIGH},
  { HIGH,  LOW,  LOW,  LOW},
  { HIGH, HIGH,  LOW,  LOW},
  {  LOW, HIGH,  LOW,  LOW}
};


void rotate(int step) {
  static int phase = 0;
  int i, j;
  int delta = (step > 0) ? 1 : 7;

  step = (step > 0) ? step : -step;
  for(j = 0; j < step; j++) {
    phase = (phase + delta) % 8;
    for(i = 0; i < 4; i++) {
      digitalWrite(port[i], seq[phase][i]);
      digitalWrite(portTwo[i], seq[phase][i]);
    }
    delay(delaytime);
  }
  // power cut
  for(i = 0; i < 4; i++) {
    digitalWrite(port[i], LOW);
    digitalWrite(portTwo[i], LOW);
  }
}



void MoveDeviceToDistanceSmaller(float d){
   do{
    Serial.println("MovingSmaller");
    delay(250);
    digitalWrite(trigPin, LOW);
    delayMicroseconds(5);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);

    pinMode(echoPin, INPUT);
    duration = pulseIn(echoPin, HIGH);
    Serial.println(duration);
    // Convert the time into a distance
    cm = (duration/2) / 29.1;     // Divide by 29.1 or multiply by 0.0343
    Serial.println(cm);
    if(cm>d){
      rotate(100);
    }
    delay(250);
    
  }while(cm > d);
}

void MoveDeviceToDistanceLarger(float d){
  do{
    Serial.println("MovingLarger");
    delay(250);
    digitalWrite(trigPin, LOW);
    delayMicroseconds(5);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
 

    pinMode(echoPin, INPUT);
    duration = pulseIn(echoPin, HIGH);
 
    // Convert the time into a distance
    cm = (duration/2) / 29.1;     // Divide by 29.1 or multiply by 0.0343
    if(cm<d){
      rotate(100);
    }
    delay(250);

  }while(cm < d);
}

void setup() {
  pinMode(port[0], OUTPUT);
  pinMode(port[1], OUTPUT);
  pinMode(port[2], OUTPUT);
  pinMode(port[3], OUTPUT);
  pinMode(portTwo[0], OUTPUT);
  pinMode(portTwo[1], OUTPUT);
  pinMode(portTwo[2], OUTPUT);
  pinMode(portTwo[3], OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);  
  pinMode(13,OUTPUT);  
}
void loop() {
  
  if(Serial.available()) //if data received   
  {
    blueToothVal=Serial.read();//read data
  }
  if (blueToothVal=='1') //receive 1
  {
    digitalWrite(13,HIGH);   //turn on led
    if (lastValue!='1') //if last value was not 1
      Serial.println("On"); //send back that led is turned on now
    lastValue=blueToothVal;
  }
  else if (blueToothVal=='0') //receive 0
  {           
    digitalWrite(13,LOW);  //turn off led
    if (lastValue!='0')  //if last value was not 0
      Serial.println("Off"); //send back that led is turned off now
    lastValue=blueToothVal;
  }else if(blueToothVal=='3'){ //receive 3
    MoveDeviceToDistanceSmaller(15.0); //move device until distance is 15cm
    
  }
  
  
}

You apparently do not realise that you cannot connect the Bluetooth module to the Tx and Rx pins if you are going to use the serial monitor or IDE which is the same connection.

It is unclear from your comments whether the serial port is disconnected while using Bluetooth?

If so, then the problem might be one of inadequate supply current. Plugging in the USB connection, adds a little extra current capacity, which perhaps might be enough then to drive all the components together so that they work properly. Are you able to measure the supply voltage when both are plugged in, only one plugged in, and with USB plugged in? What are you using to supply the HW-131?

Generally one would use VIN to connect the supply to the Nano. You are supplying a regulated 5V from the HW-131, so connecting to the regulated 5V terminal should be OK, but the stepper motor drivers probably ought to be on a separate supply. If that is what you mean by "for demonstration step motors are not plugged in correctly" then I guess you are aware so fair enough.

Also I second Paul_B's comment. You can't run with Bluetooth connected at the same time as USB. One will interfere with the other. Serial was designed as a 1:1 protocol, not for multiple connections. You can connect Bluetooth-MCU, or USB-MCU, or even USB-Bluetooth if you hold the processor in reset, but not all three at the same time.

Hi everyone,

terribly sorry there seems to be a misunderstanding.
The arduino is not connected via USB to the laptop when I am using the Bluetooth connection. I just wanted to confirm that everything is working properly. So I tested it using the USB connection but leaving the rest as it is (removed the Bluetooth module of course), and with this everything works as intended.
When using the Bluetooth module, the arduino is also powered via the HW-131, the HW-131 is the only source of power and is hooked up to my laptops USB outlet.

I sadly don't have a multimeter, so I can't measure it yet.

Generally not. Very bad idea! :roll_eyes:

I assume those things with lightning bolts on them are HW-131s and, since you say they are connected to the laptop's USB, are not only redundant but also the most likely cause of the problem. Why don't you simply connect Arduino to the USB with a USB cable like everybody else does, and run Bluetooth off Nano's 5v pins?

I'm not going to discuss the code - I can't even find the Setup, but I assume it is kosher and the only things you need worry about as far as serial is concerned is that you cannot send from the monitor and you must have Bluetooth disconected when you upload the programme - both of which I'm sure you already know. If you have any doubts about Bluetooth, leave it disconnected and send the commands from the serial monitor- no change to code.

Note that it is good practice to use a 1k/2k voltage divider on Arduino's Tx line. I have never heard of it's omission being fatal.

My apologies if this is incorrect, but my understanding is that VIN is the unregulated voltage input, whereas the 5V pin is a voltage output from the onboard regulator, which is used to power the MCU as well as shields and the like.

It might almost be useful if the on-board regulator had a heatsink which it simply does not.

The regulator is a throwback from times in the very beginning of the Arduino project when "9V" power packs were common and this was a practical way to power a lone Arduino board for initial demonstration purposes with a few simple LEDs connected but almost nothing else (because there was not much else to connect :grin:).

And even then it was limited because an unloaded 9 V transformer-rectifier-capacitor supply would generally provide over 12 V which the regulator could barely handle.

Nowadays, 5 V regulated switchmode packs are arguably the most readily available in the form of "Phone chargers" and switchmode "buck" regulators to regulate down from 12 V or other available voltages are cheap on eBay or Aliexpress so these can be fed into the USB connector or (more appropriately) 5 V pin to provide adequate power for most applications. Unfortunately, many tutorials or "instructables" are seriously outdated or misleading and have not been updated to reflect the contemporary situation.

Thank you for the clarification. That makes sense. Because the input is rated nominally up to 12v, I have heard of people attempting to power an Arduino board from a 12V power supply and after a relatively short time ending up with it toasted. I would agree that the onboard regulator is ill equipped to handle that kind of voltage drop, mainly due to the absence of a heat sink. I would personally probably not use more than a 6V supply, but might have used a 9V at a push. Your comment has me re-considering whether to even risk 9V. However, I take your point that a regulated 5V supply can be connected to the 5V rail.

I have heard that the 5V regulator can dissipate 1W and still operate. That figure is untested and has been disputed, though. So if we take, say, 0.5W as the power that the regulator can safely dissipate, you could draw 125mA at 9V (0.5 / (9 - 5)). The Arduino needs about 50mA so that leaves 75mA for peripherals. So, in a pinch you can use 9V, just can't power much. At 12V input and 0.5W there is just over 20mA for peripherals.

My point is that you can use Vin or the power plug, just have to know the limit (and the limit is pretty low).

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.