Bukit Mertajam, Penang

Ultrasonic Ranging Module, SN-HC-SR04

1.0 Introduction

Recently Cytron is carrying another new ultrasonic sensor which is called HC-SR04. Like other ultrasonic sensor, HC-SR04 uses sonar to determine distance to an object like bats or dolphins do. This sensor require a digital output (Trig) and input (Echo) pin to use it. We will show you the performance of this sensor by using Arduino. In order to make it easier, the HC-SR04 library in Arduino is also provided!

2.0 Requirement

2.1 Hardware


Screw Terminal Shield

LCD Keypad Shield

Ultrasonic Ranging Module                                                   


Jumper Wire Male to Female


2.2 Software

(i) Arduino IDE and Arduino Uno Driver – download from the Arduino download page

3.0 Methodologies

1. Prepare the hardware of the Arduino. Stack the Screw Terminal Shield and LCD Keypad Shield on the Arduino Uno.

2. Download the library and sample code of the ultrasonic sensor, SN-HC-SR04 under the attachment of this tutorial.

3. Open the sample code with Arduino IDE and upload it to the Arduino Uno. If you are a beginner to Arduino Uno, make sure you have already installed the driver on your computer. You can refer to the link below for getting started with the Arduino Uno.

4. After the sketch is uploaded, the LCD will display as below:

5. After making sure the Arduino is ready, it is time to interface with the ultrasonic sensor. Power off the arduino and connect the wire as shown below. Careful! Make sure the connection between Arduino and SN-HC-SR04 is correct. Wrong connection may spoil both hardware.

  • VCC <–> 5V
  • Trig <–> D12
  • Echo<–> D13
  • GND <–>GND

6. Power up the Arduino and now start testing the sensor by holding some object in front of the sensor. The LCD will show the distance between the sensor and the object in cm.

7. Done!


SN-HC-SR04 with Arduino library and Demo


Related Posts

Comments (15)

hey, i was wondering…
is this ultrasonic ranging sensor powered up by battery or the usb cable connected to the arduino????

zaim, it powered up by arduino via pin +5v and GND (which is arduino is powered up by the usb cable connected to the arduino)

There is no battery in the example, the ultrasonic sensor is being powered from USB cable, same as Arduino board.

could you do example for pic16f877a ?

Hello, Could you help me install the library? I tryed and didint work.. I need rename the library with some diferente name? or can I paste with this name in download? any nice codes for good precision? thanks

Can i dont connect this sensor to arduino uno…i connect it to my own main board which has pic f18…
Can it function as normal?

how to connect the ultrasonic sensor with the servo on the arduino board?

It could be much better if you expand the tutorial in order to build more cmplex robotics applications

can i connect the hc sr04 to a picaxe 28×2 board and if so how thank you

can u plz send me ur code?

where do i put file Ultrasonic.cpp and Ultrasonic.h

could you send your code for me please.

Leave a comment