Can't get output waveform form distance sensor HC-SR04

Hello!

I have tested my HC-SR04 distance sensor and it performs really well. The readings I get from it are really good. Since I use it in my dissertation I need more evidence from it working, so I thought it would be nice to include it's waveform. So I connected it to an oscilloscope, firstly to ensure that he is supplied with 5V. No problem there, was expecting a straight line at 5V, I got one. The problem is on the pulse that transmits. Althougth the HIGH part of the pulse is 10 microseconds and the LOW is 2 microseconds (overall period therefore 12 microseconds), I get something entirely different on the oscilloscope. The HIGH pulse was about 10 miliseconds and the LOW pulse was 100 miliseconds ( overall indicated period of the pulse 110 miliseconds), so there was a really huge difference there. And I also noticed that whenever and object was in the way, the HIGH pulse was even lowe, but I couldn't measure it. I can't understand why I had a totally different duty cycle from the one I set on the sensor. I used just a test code, only to demonstrate some simple readings, nothing excited there. The test code is the following, which can be found in here as well Modified Arduino Ping))) example to work with 4-Pin HC-SR04 Ultrasonic Sensor Distance Measuring Module · GitHub ( it's the same code, I just excluded the comments and the conversion to inches)

const int trigPin = 3;
const int echoPin = 2;
 
void setup() 
{
  Serial.begin(9600);
}

void loop()
{
  long duration, cm;
 
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
 
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
 
  cm = microsecondsToCentimeters(duration);
  
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  
  delay(100);
}
 
long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

You would really help a lot if you provide any ideas here.

Thank you very much in advance.

const byte trigPin = 3;
const byte echoPin = 2;
 
void setup() 
{
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  pinMode(echoPin, INPUT);
}

void loop()
{
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
 
  long cm = microsecondsToCentimetres(pulseIn(echoPin, HIGH));
  
  Serial.print(cm);
  Serial.println("cm");
  
  delay(100);
}
 
long microsecondsToCentimetres(long microseconds)
{
  return microseconds / 29 / 2;
}

And now?

nope, still the same. High pulse 10miliseconds, Low pulse 100 miliseconds. Am I making any mistake on the declaration of the initialised pulse? I mean, I am using the correct structure to give the sensor the pulse, right??

I changed the High pulse to 50 microseconds just to see any difference, still no changes on the oscilloscope. High 10 miliseconds, Low 100 miliseconds and really no damn idea why

I don't have the correct sensor here, but wrote this:

const byte trigPin = 4;
const byte echoPin = 2;
 
void setup() 
{
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  pinMode(echoPin, INPUT);
}

void loop()
{
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
 
  long duration = 11764;
  delayMicroseconds (duration); // simulate a 4 metre round-trip
  long cm = microsecondsToCentimetres(duration);
  
  
  Serial.print(cm);
  Serial.println("cm");
  
  delay(100);
}
 
long microsecondsToCentimetres(long microseconds)
{
  return microseconds / 29 / 2;
}

On pin 4 (because that's where the scope probe happened to be) I see a nice 13.6us pulse (overhead due to digitalWrite, some times glitching to around 17us because of the granularity of the timer), repeating around every 112ms or so.
Obviously, I don't have an echo pin to look at.

I used your program, but I get the same reading all the time, 202 cm regardless the actual distance of the object. The waveform is the same though (what the hell?). How can you see the waveform you describe?

I get the same reading all the time, 202 cm regardless the actual distance of the object

< sigh >

How can you see the waveform you describe?

I have an old (though calibrated) Agilent scope on my desk. It’s only a 200MSa/s job, but quite sufficient for this.

What I use is a Hameg Instruments HM407 analog oscilloscope, 100MS/s. Still though, I should be able to get the waveform. Would it be too much to ask if you can upload the waveform showing clearly the voltage at 5V (5V/div and the period of the waveform High x microseconds, Low y microseconds). At least I could use your waveform, as it should be more or less the same with mine. Just remember to initialise the sensor with 10 microseconds High and 2 microseconds Low. If, it’s an old oscilloscope, it probably won’t be able to output data to a computer, so a decent camera shot would be much much appreciated.

I was measuring on the echo pin all the time. Now I measured the trigger pin out of curiosity and the High pulse is 16.6 microseconds. Can you please tell me if the correct pin to measure is the trigger or the echo pin?

Would it be too much to ask if you can upload the waveform showing clearly the voltage at 5V

Sadly it would be too much to ask - the only camera I have here is the forward-facing one on my Nexus 7.
I've given you my code, simulating a two metre range.
You'll just have to trust me that my scope is not showing me anything I don't expect.

Can you please tell me if the correct pin to measure is the trigger or the echo pin?

Correct for what?

All in all I want to see the square waveform from the sensor. The pin that transmits and receives the pulse is the echo or the trigger? I just want to see a square waveform with microseconds in it and I just was it (kind of) on the trigger pin :stuck_out_tongue:

''Basic operating principle:

IO port TRIG trigger ranging to at least 10us high level signal;
the module automatically sends eight 40khz square wave, automatically detects whether a signal return;
a signal to return to a high output through the IO port ECHO high duration of ultrasound wave from the transmitter to the time of the return. Test distance = (high level time * sound velocity (340M / S)) / 2;''

I just found this, and this is how the sensor works. Does by any chance yours works the same way? Sending eight 40KHz square wave? And if it does, can you see it on your oscilloscope?

Moreover, the above text states that the echo pin is the correct one to get the signal from, but I get miliseconds on the echo pin. The trigger pin is where I get microseconds.

The pin that transmits and receives the pulse is the echo or the trigger?

You question doesn't make sense - there is no single pin that both transmits and receives, unlike the Ping sensors.

You have a pin called "trigger" and another called "echo".
One "triggers" the U/S module to produce a ping, and the other indicates the length of time the ping is in flight before its reflection is detected by the U/S module, and is called - you've guessed it - "echo".

As I said, I don't have an U/S sensors here with me, so getting an echo trace is not possible.

but I get miliseconds on the echo pin.

You get microseconds on both of them, you obviously get more of them on the echo pin because sound only travels at 340 metres per second.