Ultrasonic sensor not working with Bluetooth Hc05 module

I have a program which includes the software serial library for bluetooth and I have an ultrasonic finding command. These commands work in seperate projects but they do not work together. The attachment of the programs (.ino files) have been put on. There is also an app that has been created by me which sends a letter 'F' to the arduino which makes it move forward. How do I combine these two modules to work together? If it is possible, can you also send the solution for it? I just want the robot to stop moving when it detects an object in front or give distance data from arduino via bluetooth to phone.

I'm a new person to arduino and I don't have much experience in programming

Components:
Arduino Uno
Motor Shield v1
Bluetooth HC-05 module
Two motors connected to motor shield
Ultrasonic HRS04 module

Ultrasonic only.ino (1.05 KB)

Bluetooth to motor only.ino (900 Bytes)

The failed combination.ino (2.1 KB)

Is this fast enough?

Please post your code
In code tags

#include <NewPing.h>
#include <AFMotor.h>
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(0, 1);

// Defining Trig and Echo pin:
#define trigPin 3
#define echoPin 4
// Defining variables:
int distance = 0;
int front_d = 0;
int max_d = 6;

//defining motors in the motor shield
AF_DCMotor motor1(1);
AF_DCMotor motor4(4);
NewPing sonar(trigPin, echoPin, max_d);

//by default, the bt will be 's' to not make it move
char bt = 'S';

void setup()
{
 // Define inputs and outputs:
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 //Begin Serial communication at a baudrate of 9600:
 Serial.begin(9600);
 motor1.setSpeed(75);
 motor4.setSpeed(185); 
 Stop();

}

//ping reader
int readPing() { 
 delay(70);
 int cm = sonar.ping_cm();
 if(cm==0)
 {
   cm = 250;
 }
 return cm;
}

//
//loop command
void loop() {
 bt = Serial.read();
 //if bluetooth gives 'f' then move forward
 if (bt == 'F')
 {
   forward();
 }
  //if bluetooth gives 'b' then move backward
 if (bt == 'B')
 {
   backward();
 }
  //if bluetooth gives 'f' then move left
 if (bt == 'L')
 {
   left();
 }
  //if bluetooth gives 'f' then move right
 if (bt == 'R')
 {if (readPing(); < max_d)
  {
   Stop();
  }
  else
  {
   right();
  }
 }

 if (bt == 'S')
 {
   Stop();
 }

 get_d();
}
//forward function
void forward()
{
 motor1.run(FORWARD);
 motor4.run(FORWARD);
}
//backward function
void backward()
{
 motor1.run(BACKWARD);
 motor4.run(BACKWARD);
}
//left function
void left()
{
 motor1.run(FORWARD);
 motor4.run(BACKWARD);
}
//right function
void right()
{
 motor1.run(BACKWARD);
 motor4.run(FORWARD);
}
//stop function
void Stop()
{
 motor1.run(RELEASE);
 motor4.run(RELEASE);
}
//get distance function
void get_d()
{
  // Clear the trigPin by setting it LOW:
 digitalWrite(trigPin, LOW);
 delayMicroseconds(5);
 // Trigger the sensor by setting the trigPin high for 10 microseconds:
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin, LOW);
 // Read the echoPin, pulseIn() returns the duration (length of the pulse) in microseconds:
 duration = pulseIn(echoPin, HIGH);
 // Calculate the distance:
 distance= duration*0.034/2;
 // Print the distance on the Serial Monitor (Ctrl+Shift+M):
 front_d = distance;  
}

Here's the code of the failed combination. How do I make the ultrasonic sensor give data through serial communication so that I can get the ultrasonic sensor data on my phone? Should I create a new serial command?

What happened to the code tags?
Also you forgot to explain what it does do, and how it's different from what you want it to do.

Superknowsstuff:
How do I make the ultrasonic sensor give data through serial communication so that I can get the ultrasonic sensor data on my phone?

Most ultrasonic sensors don't have serial data output, so they can't send data to your phone. That includes the HC-SR04. You'll need an Arduino to read the sensor, process the data, and send it to your phone. Then you need to have some software on your phone that can read this communication.

Superknowsstuff:
SoftwareSerial BTSerial(0, 1);

Why are you using SoftwareSerial on the hardware Serial pins?

Does that mean I have to get another arduino to process the hcsr04 sensor? I was able to get the distance of objects in front of the sensor in cms using the serial monitor WITHOUT the bluetooth module connected and programmed. The app that I used is created by me using MIT app inventor. MIT app inventor has a function that can read any serial data that are able to receive from the arduino. There are also many tutorials showing that.

Sorry for forgetting those reasons.. Here are the replies
Q 1. I don't really get what you mean by code tags.... My bad

Q.2. What I meant to do is I had created a robot that moves according to the instructions from phone via bluetooth. If you press the forward button, it sends the signal 'F' and moves forward. Likewise the other functions like left arrow for left, right arrow for right, back arrow for back. What I wanted to add is if the ultrasonic sensor should give real time monitoring of the distance of the object in the front of the ultrasonic sensor... I couldn't do that.. :frowning:

Q.3. I have the bluetooth module's RX and TX pins connected to the TX and RX pin of the arduino (I remove them when uploading the code) So the bluetooth receives the signal from the phone and sends it to the arduino through those pins.

I don't really get what you mean by code tags

There is a thread at the top of every topic page, in bold type, named "how to use this forum-please read" that tells how to post code in code tags. I guess that it is easy to miss cause almost no one reads it.

Superknowsstuff:
Sorry for forgetting those reasons.. Here are the replies
Q 1. I don't really get what you mean by code tags.... My bad

That's just because you're lazy, obviously. After all, if you had just bothered to read the "How to use this forum" sticky, on top of EVERY forum, you would have known.

right, back arrow for back. What I wanted to add is if the ultrasonic sensor should give real time monitoring of the distance of the object in the front of the ultrasonic sensor... I couldn't do that.. :frowning:

Have your Arduino (not a second one, the one in the robot) read the sensor, send the data back to your phone via Bluetooth.

Q.3. I have the bluetooth module's RX and TX pins connected to the TX and RX pin of the arduino (I remove them when uploading the code) So the bluetooth receives the signal from the phone and sends it to the arduino through those pins.

The question remains: why using SoftwareSerial on the hardware serial pins - as in, instead of simply using Serial?

I didn't read that code tags stuff.. forgive me for I am a newbie and half of the program you see is copied from the internet :frowning: :frowning: :(. I just modified the parts I needed to get it going.

wvmarle:
Have your Arduino (not a second one, the one in the robot) read the sensor, send the data back to your phone via Bluetooth.

I'll try and make the arduino read the sensor and send the data back to phone via bluetooth.

wvmarle:
The question remains: why using SoftwareSerial on the hardware serial pins - as in, instead of simply using Serial?

Does it work when I just use the "Serial" command? I thought you needed "SoftwareSerial" for it to work because people have made these bluetooth cars and they had their codes (I didn't copy and paste them) with the "SoftwareSerial" command.

Thanks for replying :slight_smile:

Well those folk probably had another use for the hardware Serial. Such as the common debugging output.

wvmarle:
Well those folk probably had another use for the hardware Serial. Such as the common debugging output.

Oh....

So should I use the 'Serial' instead 'SoftwareSerial' for test? Will it work?
What is the difference between them?

wvmarle:
That's just because you're lazy, obviously. After all, if you had just bothered to read the "How to use this forum" sticky, on top of EVERY forum, you would have known.

Sorry for being like a slouch not reading that forum post.. I wanted to urgently fix my robot problem :frowning: I fixed the code tag problem now :slight_smile:

Going to test it now. Will tell the results.
If that succeeds, what would be the next step we can take?

Thanks for replying :slight_smile:

Superknowsstuff:
So should I use the 'Serial' instead 'SoftwareSerial' for test? Will it work?
What is the difference between them?

Why don't you simply look up words you don't know?

In Google just type the word you don't know plus "Arduino" and you quickly get heaps of info on things like Serial and SoftwareSerial.

Working with microcontrollers does require some understanding of the actual hardware you're using, as you're basically working bare metal. Not like on modern-day PCs or smartphones where it's all abstracted away from you.

You know what? It didn't work with the arduino ultrasonic sensor :frowning:
What you said is right.. The ultrasonic sensor can't send the data due to the reason you said (serial data output).. But I'm confused with the fact that if I plug in my Arduino thorugh the cable with the Arduino IDE (laptop, not phone obviously) it shows the data on the serial monitor. How does that happen?

I have an hc 05 bluetooth module... I tried what I could do but failed.
People have used hc06 bluetooth modules to their arduinos and plugged it into different ports and have got the serial data. (I'm also having a small feeling that modules don't matter.. the code and components that have been used matter except the bluetooth module)

If this doesn't work... is there any other option I can do in which the ultrasonic sensor interferes with the bluetooth connection but not by sending ultrasonic data to the phone but by not listening to what the phone says i.e. making the vehicle stop automatically when an object less than 6 cm is in front of the ultrasonic sensor even though the move forward event by bluetooth from the app is called..

I thought of making it like this:
if the forward function is called i.e. if (bt == 'F') function is called then if the front ranging of the arduino is less than 6 cm call the halt function else let it move forward

I thought about the method I thought of but my code doesn't seem to be right when I operate the vehicle... Is there anyway I can run it like that? Can you give any code suggestion for what I have said?

Can you pair your phone with the HC-05?

Can you receive data from your phone on the Serial monitor? (of course you have to use different pins for your HC-05 than 0 and 1)

Can you send data from your Arduino to your phone, any data?

Start with those.

Hi.

You've done a lot of work to get this far, so you're not lazy.

What you want to achieve is certainly possible (and a lot more). You do not need a second Arduino. The difference between HC-05 & HC-06 is that the 05 can be set to either master or slave, while the 06 is always slave. This only matters if you want to pair two of these devices to each other, like you would if you wanted two Arduinos to communicate via bluetooth. Don't worry about this.

Software serial versus hardware serial trips up everyone to begin with. The difference is highly technical, but you can wrangle it by following a few rules.

Hardware serial (uses pins 0 & 1 on UNO & Nano) is built in. It can be used to communicate with an external device like an HC-05. It is always used by the IDE to upload a sketch to the Arduino and by the IDE's monitor to display data sent from the Arduino and to send data to the Arduino.

BUT YOU CANNOT DO BOTH -- you cannot communicate with the external device thru pins 0 & 1 AND use the serial monitor in the same sketch.

Never use software serial on pins 0 & 1.

Software serial is not builtin to the Arduino; it is managed by software. That's why you have to include the library and use the function it provides.

You haven't actually used software serial, only initiated it with the statement "SoftwareSerial BTSerial(0, 1);".

Do you want the distance to appear on the monitor? The following statement will not do it.

// Print the distance on the Serial Monitor (Ctrl+Shift+M):
front_d = distance;

There might be a problem with this statement:

{if (readPing(); < max_d)
{
Stop();
}

Semi colon signals the end of a statement, so shud there be one after readPing() ?

If you need to display data on the monitor, I would use software serial on pins other than 0,1,3 & 4 (and any other digital pins already used).

Try a simple sketch that just displays the data sent from the phone. Get that working. Follow the tutorials.

Always run Windows Device Manager. This tells you what ports are in use; you will see yr HC-05.
Always use a Bluetooth Serial Terminal app on yr phone to check on the BT/Arduino comms.

Good luck. You'll get there. Bluetooth is very handy.

John.

Thanks for answering about the difference between Hardware Serial and Software Serial. I checked it on so many sites in Google and I finally got out the answer. Your answer helped me a LOT as I understood more about the Serial command.

jpom:
Never use software serial on pins 0 & 1.

Oh.... is it because SoftwareSerial is not supposed to interfere or be replaced by HardwareSerial? I'm not sure.. Just asked the question. Anyways I will try to use ports 10 and 9 and see how it goes :slight_smile:

jpom:
Do you want the distance to appear on the monitor? The following statement will not do it.

// Print the distance on the Serial Monitor (Ctrl+Shift+M):
front_d = distance;

It doesn't?? My bad at coding :stuck_out_tongue: Why won't it work? Is there any other possible to make it work with a command like that? (I feel like I sound lazy now)

jpom:
There might be a problem with this statement:

{if (readPing(); < max_d)
{
Stop();
}

Semi colon signals the end of a statement, so shud there be one after readPing() ?

Oh... Another mistake at coding by me... That was a careless mistake.

jpom:
Always run Windows Device Manager. This tells you what ports are in use; you will see yr HC-05.
Always use to a Bluetooth Serial Terminal app on yr phone to check on the BT/Arduino comms.

Gotta try Windows Device Manager too. Nice point :slight_smile: I should also check the bluetooth serial terminal apps on my phone..

jpom:
Good luck. You'll get there. Bluetooth is very handy.

John.

Thanks a lot! Appreciate it :slight_smile:

Good news people!! :slight_smile: :slight_smile: :slight_smile:
This was my code

#include <SoftwareSerial.h>

SoftwareSerial BTSerial (9, 10);

const int trigPin = 3; const int echoPin = 4; long duration, cm;

void setup() {
  // initialize serial communication with HC-06:
  BTSerial.begin(9600);  pinMode(trigPin, OUTPUT);  pinMode(echoPin, INPUT);
}

void loop()
{
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:  
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);  
  digitalWrite(trigPin, HIGH);  
  delayMicroseconds(10);  
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  cm = (duration/2)/29.1;

  BTSerial.print(cm);
  delay(400);

  //the code for calculating the ultrasonic distance was taken from the internet
  //just modified it to send the data over bluetooth.. it works :)
  //I dropped the "NewPing" library code in case you were wondering
}

I connected my bluetooth module to the pins written in the code and I was able to get data in the bluetooth serial monitor. Then I made mit app inventor read the code and it works perfectly well! The distance was accurate and I was so excited!

Now all I have to do is to connect the motor shield and see if it works when the signal is sent and the ultrasonic data is received. Can't wait to try that out :smiley:

I will post the results soon
-Thanks to jpom for making me understand a lot of things I didn't know. To wvmarle too :slight_smile:

Well done!

Next step: get rid of that pesky delay....

The robot FINALLY WORKS!
Thanks to wvmarle and jpom for all the help! I've written down the code (in code tags), got rid of the delay and the code works like a charm with my phone! :smiley: :smiley: :smiley:

#include <AFMotor.h>
#include <SoftwareSerial.h>

SoftwareSerial BTSerial(A5, A4);
const int trigPin = A2; const int echoPin = A1; long duration, cm;
//
AF_DCMotor motor1(1);
AF_DCMotor motor4(4);

char bt = 'S';

void setup()
{
  BTSerial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  motor1.setSpeed(75);
  motor4.setSpeed(185);
  Stop();

}

//
void loop() {
  get_d();
  bt = BTSerial.read();

  if (bt == 'F')
  {
    forward();
  }
  if (bt == 'B')
  {
    backward();
  }
  if (bt == 'L')
  {
    left();
  }
  if (bt == 'R')
  {
    right();
  }
  if (bt == 'S')
  {
    Stop();
  }
}

void forward()
{
  motor1.run(FORWARD);
  motor4.run(FORWARD);
}

void backward()
{
  motor1.run(BACKWARD);
  motor4.run(BACKWARD);
}
void left()
{
  motor1.run(FORWARD);
  motor4.run(BACKWARD);
}
void right()
{
  motor1.run(BACKWARD);
  motor4.run(FORWARD);
}
void Stop()
{
  motor1.run(RELEASE);
  motor4.run(RELEASE);
}

void get_d()
{
  digitalWrite(trigPin, LOW);
  digitalWrite(trigPin, HIGH);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  // convert the time into a distance
  cm = (duration / 2) / 29.1;
  if (cm < 7)
  {
    BTSerial.print("D"); delay(400);
  }
  else
  {
    BTSerial.print("N"); delay(400);
  }
}

My bot was able to detect objects in the front and print it on the app's screen after hours of finding out what was wrong with it... I'm so glad it finally worked :smiley:

I hope this post helped other people too (I don't know if they had the same problem because I'm just a beginner and they are not lazy like me and know more than me.)

Anyways thanks for all your help :smiley: :smiley: :smiley:

You still have that delay(400) there... it's just a bit more hidden, but it will still cause problems with poor responsiveness.

Use millis() timing for reading your ultrasound. See the "blink without delay" example or this tutorial on how that works.

Arduino rule: you are NOT allowed to use delay() until you understand why that is.