HC-SR04 Ultrasonic sensor without Ultrasonic.h library?

Ok. So Arduino 1.0 is out and now my Ultrasonic.h library isnt working. Anyone using the HC-SR04 without using the library? If so, anychance can share code with me? I was just beginning to learn how to use this sensor and was playing with code when library stopped working. Or, if anyone know wheres a new updated library is, that will work also. I tried using library updated by Stigern, but it didn't work. Thank you!


I have been using this library for quite some time now with Arduino 0022, and having no problems what so ever..


Give it a try, if it ain't the same :wink:

Or stop using Arduino 1.0, i think it coursing to many problems with the old libraries, unfortunately

The Arduino Team should put up a site with all the available libraries, where we could check compatibility between Arduino IDE versions...

You can always look at the code from the library in Wordpad - it's normal Ardu' sketch code so you can easily take small chunks that you need and drop them into your own sketches.

I have seen the sensor driven without a library but I don't remember where - will post if I can. IIRC you simply send a brief pulse to the first (Trig) pin (write HIGH for 10us or something) and then use pulseIn() to measure the time that the echo pin stays high after. Distance is related to time from the pulseIn value by the speed of sound (330 m/s or thereabouts = 330 mm / ms = 0.33 mm / us), so divide the value by 3 for approx distance in mm. Edit - oh yes, and divide by 2 again to give the one-way distance rather than the return distance!

Edit - yes, they did it here (google is your friend):


Hello everybody,
What you think if I connect four PINGs trigger to one arduino pin(for example 12), but four PINGs echo to different arduino pins(for example 6,7,8,9). For what? To save time.. The code should be like this:

int echoPinPingone = 6;
int echoPinPingtwo = 7;
int echoPinPingthree = 8;
int echoPinPingfour = 9;
int trigPin = 12;

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPinone, INPUT);
  pinMode(echoPintwo, INPUT);
  pinMode(echoPinthree, INPUT);
  pinMode(echoPinfour, INPUT);

What you think will it work correctly?

From the point of view of the code and electronics, I don't see why this wouldn't work, although I'm not sure whether you can have more than one simultaneous pulseIn function. However, you might find that in practice it's rather prone to error.

Remember that the sensors use a pulse of ultrasound and then listen for the echo. So far as I know all sensors use the same frequency. If you set off four pulses at the same time you are rather likely to get all (or at least several) sensors reading the first echo time, or the last, or the loudest time-point, or some similar mash-up of the different echos. Remember also that sound is a wave energy and so two pulses at the same frequency can interfere destructively if they happen to return out of phase.

Can you imagine four people all trying to shout at the same time and listen for their own echos?

Your delay is only 6ms/M so if you left, say, 25ms between pulses (i.e. use the timeout on the pulseIn function), you could fire each one in turn and allow it to read up to a 4m distance before you pulse the next. Is your robot or whatever really moving so fast that 100ms makes a significant difference? As I say, I'm also not sure how you would read 4 echos simultaneously because I don't think pulseIn will do it. You could read 2 on the interrupt lines but I'm not sure about 4.

The ultrasonic library link seems to be broken but I found it at Best Open Source Mac Software Development Software 2023

Can you imagine four people all trying to shout at the same time and listen for their own echos?

It's actually worse than that; at least people might have a chance of recognizing their own voice, but with no way of uniquely identifying ultrasound pulses, you can never know which transmitter's pulse you received.