Instructables

Easy ultrasonic 4-pin sensor monitoring (hc-sr04)

Picture of Easy ultrasonic 4-pin sensor monitoring (hc-sr04)
hcsr04+arduino.png
hello Instructables,
I have had some trouble with my cheap ultrasonic sensor (hc-sr04) and today I found a really easy solution.
It is a really simple edit of the normal 3pin code.


The new code(4pin):

void setup() {
  pinMode (2,OUTPUT);//attach pin 2 to vcc
  pinMode (5,OUTPUT);//attach pin 5 to GND
  // initialize serial communication:
  Serial.begin(9600);
}

void loop()
{
digitalWrite(2, HIGH);
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(3, OUTPUT);// attach pin 3 to Trig
  digitalWrite(3, LOW);
  delayMicroseconds(2);
  digitalWrite(3, HIGH);
  delayMicroseconds(5);
  digitalWrite(3, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode (4, INPUT);//attach pin 4 to Echo
  duration = pulseIn(4, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
 
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
 
  delay(100);
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}
 
Remove these adsRemove these ads by Signing Up
viper556 days ago

whats supposed to happen?

tolstoyan1 year ago
can i just short the echo and trig pin in the ultra sonic sensor so that i dont have to changed anything in the code?
Giedow (author)  tolstoyan1 year ago
Probably not, wouldn't try it

ahhh but you are wrong my freind tolstoyan is correct that is how i do myne and yes you should hear it i have no clue why but you can anyway you can short them Becouse! if you short them they still have enouph time to jump from the arduino then both ways then into them both (The Echo Pin Practicly Ignores The Input Signal) Then The Trig Pin TAkes That Power Gladly And Actavates The Sensor While Still Dischargeing Intime To Have The Sensor Send Power Back To The Arduino And It Dose This The Whole Time You Want It To Contnue Runing.

it works but i can here the ping is that normal? also i am not getting a distance input to the computer so it isn't sending back a read is it something i am doing wrong?
Giedow (author)  Dietrick K.6 months ago
I don't think you should be able to hear it, because it is utrasonic. Have you checked all the connections, software inputs, serial ports, etc.
I don't recall having these kinds problems, so I am not sure, sorry.
chanakyapm1 year ago
Shorting the echo and trig pin may not work! Read more about it here http://www.digibay.in/hc-sr04-ultrasonic-proximity-sensor
NaderM1 year ago
Is there any way to convert the 4 pin HCSR04 to a 3 pin? I want to connect 5 sensors to an RJ45, and it would be fine with the 3 pin Parallax))) sensor since there are 8 inputs, but I don't have that unfortunately. I need basically need the HCSR04 to be 3 pins before I connect it to the board, is it possible?
Giedow (author)  NaderM1 year ago
If u use a breadboard you can just hook the 5v input and the ground to the standard ground and 5v output on the board. That way pin 2 and 5 are free to use.
You just have to delete these lines:
- pinMode (2,OUTPUT);//attach pin 2 to vcc
- pinMode (5,OUTPUT);//attach pin 5 to GND
-digitalWrite(2, HIGH);
can you explain a little bit more about what you changed and why? thanks!
Giedow (author)  amandaghassaei1 year ago
Sure,
the only thing I had to change were:
-const int pingPin = 7
This was excess material, because pin7 doesn't function as output and input
anymore (I changed those in 3 and 4 so I could just plug the sensor in the
arduino)
-pinMode(3, OUTPUT)
pin 3 became the output instead of pin 7(pingPin)
So digitalWrite under "pinMode(3, OUTPUT)" was changed into 3 aswell
-pinMode (4, INPUT)
Here I changed pingPin into pin 4
And in the line underneath I did the same thing

That is all. I did it because I saw that pingPin(pin 7) functioned as both INPUT and OUTPUT and I thought why not make it two different pins, because I have two different pins for InPUT and OUTPUT.
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!