Disclaimer: Keuwlsoft is a participant in the Amazon EU Associates Programme, an affiliate advertising programme designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.co.uk (also .com .de .fr .it .es & .ca). This means that if you purchase from any of the Amazon links on this page you will also be supporting Keuwlsoft.

ultrasonic distance demo

Ultrasonic Distance Sensor Demo

Measure distance with an HC-SR04 ultrasonic distance sensor and display the result on an Android device.

About this demo

In this demo, we use an HC-SR04 ultrasonic distance sensor to measure distance, controlling it with an Arduino Uno and then using a Bluetooth HC-06 module transmit the results to an Android device.

Keuwlsoft's Bluetooth Electronics app is used to communicate to the Arduino via the Bluetooth module. The app is set up to show the echo time in µs, and the distance in cm. It also illustrates the light indicator within the app, setting the light to red if the distance is less than 20cm, setting it to orange between 20 and 50 cm and green if the distance is above 50cm.

ultrasonic distance sensor demo build
Ultrasonic Distance Sensor Demo Build


HC-SR054 Ultrasonic transducer
HC-SR04 ultrasonic transducer.

The HC-SR04 sends 40kHz ultrasound pulses from one transducer, and then listens for the return echo on the other transducer. By measuring the time between the sent pulse and received echo, and knowing the speed of sound, then the distance can be calculated.


The factor of 2 is because the sound travelled there and back, i.e. twice the distance to the target.

The speed of sound in air is temperature dependent (see Table), but we shall assume that its about 15 °C and the speed of sound is 340 m/s.

Temperature °C Speed of Sound

The HC-SR04 has 4 pins: Vcc, Trig, Echo and GND. Vcc connects to a 5V power source. Trig is used it initiate a distance reading. To trigger the measurement, a HIGH of at least 10 ms is applied to the trigger pin.

After measuring the distance, the HC-SR04 will hold the echo pin high for the same length of time. Thus by measuring the time the echo pin goes high and using the above formula, we can calculate the distance to the target object.

The HC-SR04 has a range of 2 to 400 cm, with a resolution of 0.3cm. It is directional, and its ability to measure distance to objects diminishes rapidly if more that 15 degrees from straight ahead.

Components used

Circuit Diagram

circuit diagram

The Bluetooth module has 4 connections, GND, 5V, RX and TX. The TX pin on the Bluetooth module connects to the RX pin on the Arduino and visa versa. For serial communication, a transmit (TX) connection need to be received by a (RX) connection.

Note that the Bluetooth module operates at 3.3V. Supplying a 5V to the Bluetooth RX pin could damage it, such that a voltage divider should be used to supply a 3.3V signal to the RX pin. This is achieved in this demo with a 20k and 10k resistor. The TX pin of the Bluetooth module does not need modification and can connect directly to the Arduino RX pin. This is because the HIGH on 3.3V logic will still be recognised as a HIGH on the 5V logic circuitry on the Arduino.

Arduino code

// Ultrasonic Distance Sensor Demo
// By keuwlsoft:  www.keuwl.com  16th Sept 2015
// cc Attribution-ShareAlike
// This sketch controls a HC-SR04 Ultrasonic Distance sensor on pins 8 & 9
// The distance is calculated and sent over using an HC-06 Bluetooth module
// Keuwlsofts 'Bluetooth Electronics' App is used to view the results

int trig_pin = 8;
int echo_pin = 9;
long echotime; //in micro seconds
float distance; //in cm

void setup() {
  Serial.begin (9600);
  pinMode(trig_pin, OUTPUT); 
  pinMode(echo_pin, INPUT);
  digitalWrite(trig_pin, LOW); //Start with trigger LOW

void loop() {
  //trigger a pulse-echo measurement
  digitalWrite(trig_pin, HIGH);
  digitalWrite(trig_pin, LOW);

  //get the result
  echotime= pulseIn(echo_pin, HIGH);
  distance= 0.0001*((float)echotime*340.0)/2.0;
  //send over Bluetooth 
  if (distance<20) Serial.print("*LR255G0B0*"); //Red
  if (distance>=20&&distance<=50) Serial.print("*LR255G200B0*"); //Orange
  if (distance>50) Serial.print("*LR0G255B0*"); //Green

Programming the Arduino

To program the device, make sure you remove pins 0 and 1 to the Bluetooth module otherwise the Arduino will get confused trying to communicate to two serial devices simultaneously on the same pins. Re-connect them after programming. Select the correct COM Port and Arduino device in the Tools menu. Copy and paste the above sketch and click upload.

The delayMicroseconds(); command is used to hold the trigger pin high for 10 ms.

The pulseln() command returns the time in microseconds that a pin is high or low. e.g. echotime =pulseIn(echo_pin, HIGH); starts counting when the echo pin goes HIGH and stops counting when it goes LOW again, returning the time in micro seconds that the echo pin was held HIGH into the variable echotime.

The strings sent over Bluetooth begin with “*T”, “*D” or “*L” depending on which indicator on the Android device we want the information sent to (In this case a Time indicator, a Distance indicator and a Light that is set either Red, Orange or Green). This 'receive character' can be set in the Edit screen of the 'Bluetooth Electronics' app. The string is also terminated with a “*” to tell the app where the end of the string is so that it can start processing the command.

For the Light indicator, the amount of red, green and blue is specified by an integer between 0 and 255. These values are prefixed by 'R', 'G' or 'B' and strung together in a string. e.g. 'R0G255B255' will give a cyan color. Wrap this with the '*' code and receive character, we have a message that will be understood by the android 'Bluetooth electronics' app.

Bluetooth Electronics App

1) Run the Bluetooth Electronics app, click edit.

2) Select library and navigate to the Ultrasonic Distance Sensor demo and copy to panel.

3) Now connect to the Bluetooth device. Turn on power to your circuit so that the LED on the Bluetooth module starts flashing. Click connect on the main screen of the app. If not already paired, click on discover and wait for the device to appear in the list below. Select the device (e.g. HC-06) and click on pair. When requested you will need to enter a pin number, which is usually 1234 for these devices. Once paired, the device will appear on the right hand side. Select it and click on connect. Hopefully this was successful, return to the main screen.

4) The run button should be enabled now that we have connected to the Bluetooth device. Click run and test it out.