Introduction: Hack an HC SR04 to a 3 Pin Sensor

I hope you all have heard of that expensive-but-awesome parallax ping))) sensor. I cannot buy one of them it blasts my budget into tiny little pieces of debris floating around (30$ i mean). So what do you do? literally hack your problems into pieces. So guys with the hcsr04 if you are like me pondering over the ping))). Follow this hack and get amazed:)

Step 1: Get the Stuff

1x HC SR04 ultrasonic distance sensor

Lot of 22 awg solid core hookup wire



Programming A/B cable.

Now let us begin

Step 2: The Circuit

Connect this circuit.

Step 3: Code

//this is a code for using a 4 pin hc sr04 sensor as a 3 pin ping sensor By robobot3112

#include <NewPing.h>

#define PING_PIN 12 // Arduino pin for both trig and echo

NewPing sonar(PING_PIN, PING_PIN );

void setup() {



void loop() {

delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay

unsigned int uS =; // Send ping, get ping time in microseconds (uS).

Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); // convert time into distance

Serial.println("cm"); }

Step 4: Results

Here are the results.

Let me know any error i shall revise it.

Please vote


knachuthan made it! (author)2015-01-20

Am doing a project on obstacle detector using Hcsr04 (I need to use it outdoor so going for ultrasonic). Earlier i had DYP-ME007TX which is 3 pin sensor gives out serial output. as i have problem in converting 32 bit serial output to 8 bit, i brought this HC-SR04 , Am using it with AT89S52. I dono where am making mistake.

I gave 5v to vcc, then gnd. later high pulse to trig input and after that no response from echo. Please help me out!

Thanks in advance.

robobot3112 made it! (author)robobot31122015-04-27

Does the code get uploaded to your microcontroller ? if so; test the hc-sr04 with an arduino or a clone.

ps sorry for the late reply

knachuthan made it! (author)knachuthan2015-04-27

Yes i completed the project successfully. The thing i missed early was time frame. when the trig pin receives high pulse for 10 us it emits ultrasonic test signals immediately. thus we must be ready right from the 11th microsec to recieve the signal.

robobot3112 made it! (author)robobot31122015-04-27

good that you where successful.

robobot3112 made it! (author)robobot31122015-01-26

I'll look into the microcontroller you are using and send you the problem. Sorry for the late reply.

knachuthan made it! (author)knachuthan2015-01-27

Thanks! And you are always welcome :)

knachuthan made it! (author)knachuthan2015-01-27

Thanks! And you are always welcome :)

AravindP4 made it! (author)2017-03-24

hello sir i want to use two sensors .

but trigger on one hscr04

echo on other ultrasonic sensor .

can u please tell me how to do

robobot3112 made it! (author)robobot31122017-03-25

Im not sure what you mean, but i think youre trying to integrate two sensors into one.

So, instead of using trig on one an echo on one,

just short the two pins, and connect the wire as shown in the instructable, because integrating the 2 sensors into one is a difficult task.

Best of luck.

AravindP4 made it! (author)AravindP42017-03-26

first i will say thank you for answering me.

what i am trying to do is transmit on one ultrasonic .receive from another ultrasonic sensor .

Bogdan_pit made it! (author)2016-09-25

Thank you very much!!!!

It worked!!

HC-SR04 Success.jpg
robobot3112 made it! (author)robobot31122016-10-03

Glad you had success hope it helped.

Titannet1 made it! (author)2016-04-04

I can't find the NewPing.h file. Where it is?

Titannet1 made it! (author)Titannet12016-04-04

I've found it:

freduj made it! (author)2016-02-06

3112 Hello robot,

thank you for your work , because these cheap sensors are really difficult to it's ok

.is it possible to run three sensors at the same time?

thank you

robobot3112 made it! (author)robobot31122016-02-06

yes you can run upto as many sensors as pins on your arduino as long as you have powered it correctly.

freduj made it! (author)freduj2016-02-07

thank you,

i will try to make this project but I'm beginning in arduino world, it's not simple for me.if I can I put my code here.

MaxneilQ made it! (author)2015-10-09

do I need a library for the sensor to work?

robobot3112 made it! (author)robobot31122015-10-12

yes, newping library

clark01 made it! (author)2015-09-27

PeterS48 made it! (author)2015-08-07

hi! can you help please? I put the extracted the newPing file containing the header into the same folder as my file is with your code and it still can not find the header and I get an error message, when I try to compile. Any ideas what I am doing wrong? thanks in advance!

robobot3112 made it! (author)robobot31122015-08-08

could you copy the errors and send them it would help.

PeterS48 made it! (author)PeterS482015-08-30

also I forgot to put your header file in a library called libraries that was another mistake

PeterS48 made it! (author)PeterS482015-08-30

thanks for the fast reply, sorry for not writing earlier. I just got the HC SR04 and experimented a bit and I got it working by using #include "NewPing.h" instead of #include <NewPing.h> and I also used Serial.begin(9600). Thanks for the brilliant header file, it works perfectly!:)

PeterS48 made it! (author)2015-08-30

VaibhavC1 made it! (author)2015-07-02


SofianT made it! (author)2015-05-10

Hi robobot3112.This hack looks great ! I want to know where to find the NewPing.h library ? thank you in advance.

robobot3112 made it! (author)robobot31122015-05-11

here is the link

SofianT made it! (author)SofianT2015-05-12

thanks a lot. where you from ? me from Aix-en-provence, France.

diy_bloke made it! (author)2014-09-13

does it have the same precision?

robobot3112 made it! (author)robobot31122014-09-13

I donot have a parallax ping))) to compare with but i compared it with an HC SR04 and got almost similar readings.

diy_bloke made it! (author)diy_bloke2014-09-14

ah i see, so it was just about using the3 pin library then. Someone here on instructables has the HC SR04 and adapted the library fro 4 pins

robobot3112 made it! (author)robobot31122015-04-27

it worked fine for me with the 3 pin library. Great idea!

robobot3112 made it! (author)robobot31122014-09-14

Not using the 3 pin library. Instead i did it to save a pin on the arduino. I did not try it with the 3 pin library. I will try it with the 3 pin library soon and find whether it works.Thanks for the idea

BurakTutal made it! (author)2015-04-26

Nice Project. I have made a smilar one. But mine is talking according to distance :) You can check my project. :)

robobot3112 made it! (author)robobot31122015-04-27

Thanks and btw cool project checked it out while reading.

UtkarshJ made it! (author)2015-03-27

ok i am getting fed up i get this



i even checked the pind but still i get the same thing

robobot3112 made it! (author)robobot31122015-03-28

you have set the wrong baud rate it should be 112500. you might be using 9600.

i ll explain what i mean.

look at the bottom left corner of the serial monitor in the screenshot i posted.

one of them reads 115200. check the same thing in yours. If it is 9600 change the baud.

UtkarshJ made it! (author)UtkarshJ2015-04-10

Thnaks a lot

fsamara made it! (author)2014-12-23


It doesn't seem to work when i am trying it!! I am trying to modify the code from the Arduino Website:

Its not working its giving me a 0 distance all the time.


NicholasF2 made it! (author)NicholasF22015-03-27

have you set up the pin as input?

robobot3112 made it! (author)robobot31122014-12-28

I'm very sorry for not informing that you need to use the code that i gave on this instructable. Please try with the code i have given, If any error please inform .

EwanS1 made it! (author)2015-03-05

I am going to do this for a robot I am making but what 3 pins will be what

will be glad if you can help many thanks

robobot3112 made it! (author)robobot31122015-03-06

sorry i did not understand your question.

EwanS1 made it! (author)EwanS12015-03-08

what 3 pins will become the power ground and signal pins after the hack

robobot3112 made it! (author)robobot31122015-03-08

the older power and ground stay the same. The trig will be the signal.

EwanS1 made it! (author)EwanS12015-03-10

ok thanks :)

EwanS1 made it! (author)EwanS12015-03-10

ok thanks :)

lptbt08 made it! (author)2015-02-18

sketch_feb18a:8: error: 'NewPing' does not name a type
sketch_feb18a.ino: In function 'void loop()':
sketch_feb18a:20: error: 'sonar' was not declared in this scope
sketch_feb18a:23: error: 'US_ROUNDTRIP_CM' was not declared in this scope

Could you help me fix that? :( thanks

robobot3112 made it! (author)robobot31122015-03-03

library was not correctly downloaded. you could try downloading once more by deleting your previous download of the library, sorry for the late reply.

About This Instructable




Bio: The world is a Roller coaster, Im the operator.
More by robobot3112:Race Spec 260 Class Fpv Racing Quadcopter Ultimate guide to building your multirotor frames cheapDiy Rc Offroad FPV Rover
Add instructable to: