Introduction: Arduino Bluetooth and Ultrasonic Sensor TUTORIAL

Picture of Arduino Bluetooth and Ultrasonic Sensor TUTORIAL

Hello again,

As I promised, I uploaded my next instructable. This one is going to cover Bluetooth and Ultrasonic sensor(HC-SR04).

Why those two. I made an RC 2 wheel car for my school project. It' controlled with a free app via bluetooth. To make it "half robot", it has distance sensor, that mesures distance. If the distance is (in my case) equal or lower than 10cm, the car stops and after 1,5s the person can controll it again.

The code will be added in the end of this instructable.

*UPDATE*
MAKE SURE YOU WATCH MY NEW INSTRUCTABLE ON AN ARDUINO WEATHER STATION WITH BLUETOOTH THAT WILL SOON CONTAIN ESP8266 AS I PROMISED I WILL DO A PROJECT WITH IT AND A WEATHER SERVER SOUNDS FANTASTIC.:

https://www.instructables.com/id/DIY-Weather-Statio...

Step 1: THE HC-SR04 ULTRASONIC SENSOR

Picture of THE HC-SR04 ULTRASONIC SENSOR

Now, we are going to look at the HC_SR04 ultrasonic sensor.

This component is very simple, as it only has 4 pins.
Firs and the last are power pins Vcc and GND. The middle ones are Trigger pin and Echo pin.

To understand what these pins do, you must understand how this thing works. It's actually very simple as the component is not doing any math or work by itself.
So, this module is sending ultrasonic waves. Than these waves reflect from a surface and come back to the module. Arduino is triggering those waves on the trigger pin and than listening for the “echo”. Once it receives it, it calculates the distance base on the time spend waiting for the wave to come back. In the code, we are also going to change that value to cm, as it it easier to read.

The module will be connected to any I/O pins of the arduino. The trigger pin will be output and the echo pin will be input.

I will also describe how the code works. in the programming step.

Step 2: PROGRAMMING THE ULTRASONIC SENSOR

Picture of PROGRAMMING THE ULTRASONIC SENSOR

Same deal here. The code is nicely comentated so there sould be no problems.

Ask me if anything is unsure.

Step 3: THE BLUETOOTH MODULE

Picture of THE BLUETOOTH MODULE

So first I'm going to show you the bluetooth module.

Probably all Bluetooth modules have the same pin-out.
It has power pins Vcc and GND and communication pins RX and TX.

Now you can probably see, that this module connects to the RX and TX pins of a micro controller such as Arduino or a USB to serial converter.

This module works fully with 5V, so there is no problem connecting it to Arduino. All the pins are labeled on the back of the module(pic. 2).

The connections are labeled in the picture 3.

Step 4: PROGRAMMING THE BLUETOOTH MODULE

Picture of PROGRAMMING THE BLUETOOTH MODULE

I'm not going to talk about how the code is working because it it very well comentated. If you have any question, leave it in the comment section. The code is made for turning LED on and OFF, but with the same method of "else if" statements seen in the code, you can do enything.

One more thing to remember. When you have your bluetooth module connected to the TX and RX pins to te arduino, remember to pull them out every time you' re uploading the sketch or it will display an error like on the picture 2, than disconnect the RX and TX pins from the bluetooth module and upload the code again. Once the code is uploaded, connect the pins again.

After all that, you need to connect your phone to the bluetooth module. Power the arduino(and at the same time the bluetooth module) and find it in. My was named HC-06, so you can search for semething like that.

Once you added the device, download this app for Android. I don't know if this works with Apple devices. Correct me in the comments.

the app: https://play.google.com/store/apps/details?id=eu.j... , so it's called Arduino RC

Step 5: TESTING THE BLUETOOTH!

Picture of TESTING THE BLUETOOTH!

Once you downloaded the app, run it and click connect to device. Choose your bluetooth module(my is the first one) and you will be taken to "the lobby". If you have noticed, this app is for RC(remote controll) which is perfect for controllng a car or sth. It has buttons or gyro sensoring. But what makes it not only for RC is the "TERMINAL MODE". There you can type in anything that will be sent to the arduino. In our case, If you send 1, the LED will come on, or if you type 0, the LED will go off.

Step 6: COMBINING DEVICES

Picture of COMBINING DEVICES

Now, I've combine both of the codes, so you can turn on the LED remote and turn it off for 2s when the distance is less than 15cm.

All I did is that:

-I combined all the int variables above the setups

-I combine both setups into one

-I named each loop of each code "bluetooth" and "sesor"

-in the main loop I added those 2 loops

-I added stopping function in the sensor part.

It's very simple. The new code is well comentated as well, so no problem there.

I also added a short video and of course the code.

Thank you for watching. I hope you make something amazing with this and remember to have fun :)

Comments

Mark JaysonP (author)2016-12-26

Anyone here who actually know how to measure signal strength between ultrasonic receiver and transmitter?

s555 (author)2016-10-23

Which bluetooth module did you use? Does it support sensor with I2C communication protocol? Do I need two such modules (one for sending and receiving data from sensor and other for sending and receiving data from Arduino?

Matej Blagšič (author)s5552016-10-24

As I mentioned, I use HC-06 module. It sadly can only be used as a slave, as for HC-05 can be use as master or slave, so you can add the HC-05 module and set it up as Master, as one in the connection has to be master, and other one is slave. You can also enter in the mac address of the other slave module, so they auto connect to each other when they power on. As for using it stand alone, I have no experience. i think it is possible as it is just another microcontroller, but it's pins are inaccessible in most breakout modules, only reset, rx and tx and power lines. here is a video I saw where a phone talks to hc-06 without a microcontroller.

MouradB9 (author)2016-06-27

Hey, I used ur final code with the bluetooth and the sensor combined, but the sensor doesn't go further than 4 cm. I have replaced everything from the actual arduino to the sensor to every cable. It does work however with the sensor code alone and my personally added LED

newbeduino (author)2016-05-04

I made this thanks for the help it was a great learning experiance. I would like to see the one you do with the rc car

YuvrajR3 (author)2016-03-31

can we send text instead of numbers?

MoniCris made it! (author)2016-03-06

I need to measure and send on bluetooth distances from 1 to 300. Using serial terminal I can read values correctly, but when I connect the bluetooth module, the ultrasonic sensor read maximum 50-60 cm. Your project is ok, but if I try to set distance to more than 60cm, is not working. Have any idea?

YuvrajR3 (author)MoniCris2016-03-31

the bluetooth sensor and ultrasound sensor combo requires more power than what the arduino is able to provide. thats why they dont work properly

JuanR111 (author)MoniCris2016-03-31

Could I read the distance values with the RC android app with this code?

I'm trying it but nothing appears at android's serial monitor

Matej Blagšič (author)MoniCris2016-03-06

The sensors should be able to work to at least 2 m as those are the cheaper and most common ones. I will hit back to you when I put the circuit back together to see If I get more than 60 cm.

MoniCris (author)Matej Blagšič2016-03-06

On arduino terminal, I see correct distances of 300cm but connecting the Bluetooth module, is not reading more than 60cm.
Thank you

MoniCris (author)2016-03-06

HC-06 is not visible by Apple devices only. Android devices and computer can comunicate with it. Nice project. Now I'm building it.

ShashankR3 (author)2015-08-04

So basically, the device the Bluetooth is connected to, the Android Phone in this case, acts as the Serial Monitor?

In a way kinda yes.

maker200 (author)2015-05-16

hey can you tell me how to make a robot with the sensor . like a motor responds if the distance is 10 cm.

Matej Blagšič (author)maker2002015-05-16

So that the robot for say stops at the close distance... Therefore like my robot that mentioned.
I can make an instructable showing how to make and programme the bot if that would help you.

Cheers

maker200 (author)2015-05-16

hey can you tell me how to make a robot using that like a motor responding based on the distance calculated

About This Instructable

47,570views

107favorites

Bio: I'm a high school student that likes electronics, Arduino projects and photography. I began working with electronics when I was 11 and got my ... More »
More by Matej Blagšič:Wi-fi weather station with BynkSimple 20 LED VU meter + datasheet guideGuide to Neopixel
Add instructable to: