I'm trying to get distance using HC-SR04 sensor and arduino. I've connected together Trig and Echo pins on the sensor, then it goes to port A7(pin 4) of multiplexer(selectors configured as follows: S0 high S1 high S2 high). I uploaded code from examples(sensors/ping) and pingPin connected to in/out of multiplexer: only I've changed duration of trig impulse to 20uS. If I don't use multiplexer and connect pingPin directly to sensor everything works. If I disconnect pin of sensor from mux, I get some random values from mux out pin, it's okay. If connect pin from sensor to A7 it reads only zeroes from mux in/out. And it prints zeroes more slowly than when connected directly and working. So I guess I'm not getting any impulses from sensor or my trig signal can not go through. I've just tested port A7 from mux with long trig impulse. When not connected to sensor it's 0 5 0 5 so it's okay. But when connected to sensor it's only 0V 1V 0V 1V and not 5. Maybe problem is here?

Why doesn't it work?

It works like that: arduino sends HIGH impulse 20uS long to sensor and then waits for HIGH impulse from the same pin and I get duration of this HIGH impulse.

Code from examples:

const int pingPin =3;

void setup() {
  // initialize serial communication:

void loop()
  // 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(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  digitalWrite(pingPin, HIGH);
  digitalWrite(pingPin, 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(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print("in, ");


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;

datasheet 74hc4051 sensor datasheet enter image description here

Maybe it's important?

Effectively the selected channel has around a 80 ohm resistance (between the selected pin and the common pin) achieved by side-by-side MOSFETs (one P-channel and one N-channel). The unselected channels have a very high resistance between them and the common pin.

Maybe I should connect trig through transistor and A7 to its base?.. tried. didn't work. So I think problem is really in low voltage from A7.. 1V there, not 5..

  • \$\begingroup\$ An actual schematic of your whole circuit (instead of describing it), some of the code used and a few links to relevant datasheets would be really helpful here. \$\endgroup\$
    – Ricardo
    Commented Jun 10, 2014 at 17:05
  • 1
    \$\begingroup\$ @Ricardo I've added circuit. \$\endgroup\$
    – a_vasilkov
    Commented Jun 10, 2014 at 17:22

1 Answer 1


I've found solution. Maybe I was wrong from the beginning.. Solution

I've done as described in the link above, connected trig and echo with 1.8k resistor and A7 pin to trig pin. I should have googled more.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.