HC-SR04 and US-015 – Ultrasonic sensors

In this example we are going to connect to HC-SR04 and or US-015 ultrasonic sensor.

(Updated 18. Oct 2015, adding US-015 sensor and sensor specs)
(Updated 18. Apr 2016, adding more info for accuracy for the HC-SR04 sensor)

A ultrasonic sensor will send ultrasonic sound out which will echo on objects in-front of it and come back to the sensor. Using speed of sound then from the echo we can calculate the distance to the object.

A sensor like this does not give good readings if object in-front of it is not at straight angle.

We do not take any responsibility for possible errors in the guide or errors that you might do wiring it up. Incorrect wiring can result in damaged sensor or damaged Raspberry PI.

The specs for the sensors:

Both sensors have pretty much same spec and in our test on short distance there was no notable difference. That does not mean there is no difference, their ability to detect small objects in the way at long distance may differ for example.

Both sensors have exactly the same pin layout and functionality, so same wiring and same programming will apply for both of them.

HC-SR04 Ultra sonic sensor
HC-SR04 Ultra sonic sensor
US-015 Ultra sonic sensor
US-015 Ultra sonic sensor



Range: 2 – 400 cm
Resolution: 3 mm
Measuring angle: 15 degrees
Working voltage: DC 5 V
Pins: 4


Range: 2 – 400 cm
Resolution: 3 mm
Measuring angle: 15 degrees
Working voltage: DC 5 V
Pins: 4

Pieces we use in this example:

  • Raspberry PI 2
  • Cobbler and Cobbler cable
  • Bread board
  • 1 K-ohm resistor
  • 470 ohm resistor and 120 ohm resistor (or some combination that can combined provide a little above 515 ohm)(Resistors are cheap, if your going to experiment a lot then do your self a favour and just buy a big set, I bought full set with 2420 of them for only $16) – I don’t got them yet so in this example I just used whatever I had, I probably would have used 470 and 100 ohm instead of 470 and 120, but in this example it makes no difference.) 

If you are not familiar with breadboards, how to place a cobbler on the breadboard or need to know how to read the color codings on the resistors then click here bellow

   Using breadboard     Resistor color codings

First thing is understanding the wiring on the sensor:

HC-SR04 Ultra sonic sensor
HC-SR04 Ultra sonic sensor

This sensor comes with good markings so its very easy to wire it. We will be putting it on breadboard so I am not showing it with wires at this time like I usually do.

  • (5V) Red is the 5V input which goes in VCC on the sensor – I always use red for the power input.
  • (GPIOX1) Yellow is will go in a pin called Trig on the sensor, its used to trigger the sensor to let it start measuring.
  • Echo this one will be connected to resistors, see bellow. Note the echo will come back as 5V. (Be warned do not connect this pin directly to the Raspberry, it will damage the GPIO on the Raspberry)
  • (GND) Blue is the ground which goes in GND on the sensor, I always use blue for ground.

Understanding what we are going to do:

Because the Echo pin is putting out to high voltage for the GPIO pin then we will be reducing its voltage by using resistor.

But first we will make a decision to protect the pin from programming mistakes, that is cases like if you by accident put output on the input pin. Therefore we choose to put 1K resistor between the pin and the ground. To lower the voltage from the sensor Echo before it goes into the GPIO we have to calculate:

We know the following things:

  • Voltage that we have: 5 V
  • Desired voltage: 3,3 V
  • Resistor that we choose to go to the ground: 1K = 1000 Ohm

So we can calculate:

3,3 = 5 * (1000 / (R + 1000))
0.66 =  (1000 / (R + 1000))
0.66R + 660 = 1000
0.66R = 340
R = 515 Ohm

This will give us the following circuit:

Ultrasonic wiring

(Use this circuit diagram to help you when wiring the breadboard further down in this article)

Connecting cobbler and breadboard:

Make sure your Raspberry PI is not powered on when you do the connections. In this example then we have put the cobbler onto a breadboard.

For the 515 Ohm I serial connect a 470 Ohm and 120 Ohm Resistor, getting total of 590 Ohm which is good value to generate HIGH but yet not damage the pin. Its important here when finding resistor to fit into the 515 calculated value to not go under 515, while going higher is always safe it will just drop the voltage more. If dropping it to much then you will not get HIGH pulse that you need.

470 Ohm =  Yellow – Violet – Yellow (Read towards the golden stripe)
120 Ohm =  Brown – Red – Brown  (Read towards the golden stripe)

The 1K resistor we also use would be:
1 KOhm =  Brown – Black – Red (Read towards the golden stripe)

GPIO Header

In my example then I have it connected as follows:

ultrasonic sensor on bread board

Use common sense when connecting those look at the circuit further up in this guide, and if your breadboard is different in any way then the breadboard coords in this guide will not be accurate or you, so don’t rely on them.

Ultra Sonic_bb

  • Cobbler placed on breadboard, placed so that Pin 1 goes to D1, (or if you have huge breadboard then make sure Pin 1 goes to row 1, and that pin 2 is on the other side of the middle canal)
  • I placed the sensor at far bottom, making sure it turns so that each pin on the sensor has its own row or own lane in the breadboard.
  • Red wire from 5V Cobbler Pin 2 (J-1 on my breadboard) to the sensors VCC (I-30 on my breadboard).
  • Yellow wire from GPIO4 which is cobbler pin 7 (B-4 on my breadboard) to the sensors Trig pin (F-29 on my breadboard).
  • Blue wire from GND Cobbler pin 39 (B-20 on my breadboard) to the sensors GND (I-27 on my breadboard)
  • 470 ohm resistor from sensors Echo pin (F-28 on my breadboard) to the other side of the canal (D-28 on my breadboard) 
  • 120 ohm resistor  from the 470 ohm one (C-28 on my breadboard) to empty lane (C-25 on my breadboard)
  • Green wire from GPIO5 which is cobbler pin 29 (B-15 on my breadboard) to the 120 ohm resistor (B-25 on my breadboard) 
  • 1K resistor from the 120 ohm resistor (A-25 on my breadboard)  to the ground,  (A-20 on my breadboard)

The Xojo code:

Important !! Paul Lefebvre’s GPIO library for Xojo has a tiny bug in it, check if yours has it and fix it if needed before using this code. The bug is that the constant HIGH is defined as 0, but should be defined as 1. 

If it is not fixed then this sensor code will just put your application in endless loop.

The open event of the Window:

Sub Open()
End Sub

Our function to talk to the sensor:

Function ReadSensorHCSR04(triggerPin as Integer,echoPin as Integer) As Double
Dim startTime as UInteger
Dim endTime as UInteger
const kSpeedOfSound = 340.29
while(GPIO.DigitalRead(echoPin) = GPIO.LOW)
// We just wait for it to send the signal back so nothingto do here
// We want to measure the time lenght of the HIGH pules in the echo.
startTime = GPIO.Micros
while(GPIO.DigitalRead(echoPin) = GPIO.HIGH)
// We just wait for it to send the signal back so nothingto do here
endTime = GPIO.Micros
//speed = distance / time as we all should know !
return (endTime - startTime) * 0.000001* (kSpeedOfSound / 2) * 100 // * 100 to get it in cm
End Function

Our button to test the function:

Sub Action()
MsgBox Format(ReadSensorHCSR04(4,5),"#.0") + " cm"
End Sub


18. April 2016 – Ulrich Bogon pointed out to me that the specs for the HC-SR04 tell that if not echo is detected then the sensor will stay at high for 200 m/s to indicate the failure.

To take this into account then you might want to change the return code to:

Dim timeDelta as UInteger = (endTime - startTime)
return if(timeDelta >= 20000, -1, (timeDelta) * 0.000001* (kSpeedOfSound / 2) * 100) // * 100 to get it in cm
// Where return value of -1 would then hint that nothing was detected.

further more then he also pointed out that you can improve accuracy by measuring temperature to choose the best speed of sound constant.

A good method to set that would be something like:

temperature = 20.0 // In C
speedOfSound = 331.5 + (0,6 x 20)

When this is written then we do not have any information if same or something similar applies to the US-015 sensor.


Thats it for now !

AliExpress.com Product - Carbon film resistor assorted kit 0.33 ohm - 4.7M ohm 1/4w 5% tolerance commonly used 122 values 20x 122= 2440pcs

AliExpress.com Product – HoldPeak HP-770D Multimeter

Leave a Reply

Einhugur technical blog that involves Xojo and Einhugur Xojo plugin related topics

%d bloggers like this: