Raspberry pi, an Ultrasonic Sensor and Node.js – Measure Distances4 min read

In this post, we will interface an Ultrasonic sensor – HC-SR04 and Raspberry Pi B+. And then we will write a Node.js program to detect the distances between an object and the Ultrasonic sensor. We will be using an awesome Node.js module named r-pi-usonic to interact with the Ultrasonic sensor from our Pi.

We will be implementing 2 variations of measuring distances using the Ultrasonic sensor. One variation is to get the distance on demand. And the second one is to continuously measure the distance.

Below is a quick demo on

Measuring on-demand

A continuous measurement of distance

You can find the completed code here.

So, let us get started.


If you are new to Raspberry pi and have not yet installed Node.js on it, I would recommend going through Getting Started with Raspberry pi and Node.js.

If you are new to electronics devices and circuits, I would recommend going through the video lectures from All About Circuits.

Components needed

  1. 1 – Raspberry pi B+
  2. 1 – Breadboard
  3. 1 – 1K Ohm resistor
  4. 1 – HC-SR04 Ultrasonic sensor
  5. 4 – Jumper wires

If you are new to Raspberry Pi GPIO, please refer this.

Understanding HC-SR04 Ultrasonic Sensor

HC-SR04 sensor works with a power of 5v. It has a range of approximately 2cm to 4m. It outputs a frequency of 40khz, twice the human ear can hear. (more info on the sensor here)

As you can see from the above diagram, the HC-SR04 sensor has 4 pins.

  1. VCC  : +5v power supply
  2. Trigger : A high input to this pin for a duration of 10 micro seconds, activates the sensor
  3. Echo : This pin returns the time taken for the transmitted signal to come back after hitting an object.
  4. Ground : Connected to the ground

So, this is how the sensor works. First, we send a high signal to the trigger pin for a duration of 10 micro seconds. This dispatches a sound wave of 40khz frequency via the the Transmitter (speaker on the left hand side of the sensor) and when the sound wave hits an object, it will return an “echo” to the Receiver (mic on the right hand side of the sensor).

When the echo comes back, the echo pin will output a high signal for a duration that is equal to the time taken for the soundwave to originate and echo back. So, the duration of the high signal is directly proportional to twice the distance of the obstacle from the sensor.

Now, we will monitor the duration of the high signal on the echo pin and using this value, we will calculate the distance.

We know that

speed = (distance travelled) / (time taken)

Here the distance will be equal to twice the distance between the obstacle and the sensor. Because, the time we are using is equal to the time taken for the signal to originate and echo back.

We also know that speed of sound is 340 m/s. So, the above equation will become

340 = 2d / time


d = 170 * time

So, if we plug in the time from the echo pin, we can calculate the distance between the obstacle and the sensor.

Simple and Easy right!!

Setup HC-SR04 Ultrasonic Sensor

Screen Shot 2014-11-21 at 7.31.26 pm

As you can see from the above diagram, we will connect the VCC pin of the sensor to +5v and GND to GND. We will connect the Trigger pin to GPIO pin 17 and finally we will connect a 1K Ohm resistor between the Echo pin and GPIO pin 18.

The Echo pin outputs a 5V output, which will damage the GPIO pin, if interfaced directly. This is why we connect a 1K Ohm resistor between the Echo and GPIO pin 18.

Node.js Program

Now that we have connected the hardware, we will write a program to measure the distance. We will measure the distance on demand first.

Login to your pi via ssh – terminal/putty. As soon as you ssh into pi, you will be landing inside the /home/pi folder. We will create a new folder here named node_programs. And inside this folder, we will be maintaining all our programs. Run

mkdir node_programs

To step inside that folder, run

cd node_programs

For this post, we will create a new folder named ultraSonicSensor and will step inside this folder. Run

mkdir ultraSonicSensor && cd ultraSonicSensor

Note : You can run multiple commands separated by a &&.

First we will initialize a new node project here. Run

npm init

Fill it up as applicable.

Now we will install a node module named r-pi-usonic to interact with the Ultrasonic sensor. Run

npm install r-pi-usonic --save

Next, we will create a new file named sense.js. And we will open the same in the nano editor. Run

nano sense.js

Paste the below code into the nano editor

Yup, 3 instructions to get the distance.

Line 1 : We require the module

Line 2 : We initiate the module passing in the Trigger GPIO pin number, Echo GPIO pin number and a timeout.

Line 3 : We wait for 60 milliseconds and then print the distance

Let us save the file and run the program. To save the program, press (ctrl+x). This will ask you to save the file. Press Y and press enter key to complete the operation.

To run the program, execute

sudo node sense.js

Now, the distance will be measured from the first object it encounters. If you want you can explicitly place an object for the sensor to detect as show below

Sweet right!

Now, let us say that you are building a robot and you would like to utilize the Ultrasonic sensor to detect obstacles in your path. This way, if the path is clear, you can move your robot ahead. Else, you can look other way and then see if you can proceed in that direction.

The setup we have now is pretty perfect for that scenario. So, instead of measuring the distance on demand, we will be continuously measuring the distance between the first obstacle and our sensor. And below is how we do it.

I have borrowed this code from here and made some changes to it.

Create a new file

nano surveyor.js

And paste the below code into the editor

Things to notice

Line 1,2 : We require the dependencies

Line 4 : Init function that takes

  • Echo pin connected GPIO pin number
  • Trigger pin connected GPIO pin number
  • Timeout
  • Delay
  • Rate

Line 5 :  We initialize the sensor

Line 9 : IIFE – Immediately Invoked Function Expression, that iterates every 60ms (delay) and takes 5 samples (rate) and stores them inside the  distances variables. Finally when 5 samples are completed, we print the median of the distances.

Line 26 : Takes in the 5 samples and prints the median of the distances.

Line 39 :  We invoke the init function.

Save the file and run

sudo node surveyor.js

And you should see

A powerful way to utilize the Ultrasonic sensor.

Hope this post gave you an idea how to work with a Ultrasonic sensor and Raspberry PI B+.

Thanks for reading! Do comment.