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.




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.


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

Ask me if anything is unsure.


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.


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


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.


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 :)

<p>Anyone here who actually know how to measure signal strength between ultrasonic receiver and transmitter?</p>
<p>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?</p>
<p>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.</p>
<p>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 </p>
<p>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</p>
<p>can we send text instead of numbers?</p>
<p>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?</p>
<p>the bluetooth sensor and ultrasound sensor combo requires more power than what the arduino is able to provide. thats why they dont work properly</p>
<p>Could I read the distance values with the RC android app with this code?</p><p>I'm trying it but nothing appears at android's serial monitor</p>
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.
On arduino terminal, I see correct distances of 300cm but connecting the Bluetooth module, is not reading more than 60cm. <br>Thank you
<p>HC-06 is not visible by Apple devices only. Android devices and computer can comunicate with it. Nice project. Now I'm building it. </p>
<p>So basically, the device the Bluetooth is connected to, the Android Phone in this case, acts as the Serial Monitor?</p>
<p>In a way kinda yes.</p>
<p>hey can you tell me how to make a robot with the sensor . like a motor responds if the distance is 10 cm.</p>
So that the robot for say stops at the close distance... Therefore like my robot that mentioned. <br>I can make an instructable showing how to make and programme the bot if that would help you. <br><br>Cheers
<p>hey can you tell me how to make a robot using that like a motor responding based on the distance calculated</p>

About This Instructable



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 Bynk Simple 20 LED VU meter + datasheet guide Guide to Neopixel 
Add instructable to: