Go Down

Topic: HC-SR04 Ultrasonic sensor without Ultrasonic.h library? (Read 7443 times) previous topic - next topic

jamos82

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!

kucza83

Hi...

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

http://iteadstudio.com/application-note/arduino-library-for-ultrasonic-ranging-module-hc-sr04/

Give it a try, if it ain't the same ;)

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...

Dr_Ugi

#2
Dec 12, 2011, 07:47 pm Last Edit: Dec 12, 2011, 08:18 pm by Dr_Ugi Reason: 1
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):

http://iteadstudio.com/application-note/arduino-ultrasonic-ranging-with-hc-sr04/


Mastino

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

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?

Dr_Ugi

#4
Dec 21, 2011, 11:50 am Last Edit: Dec 21, 2011, 11:54 am by Dr_Ugi Reason: 1
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.

merlinstail

BAN ALL THE SIGNATURES

AWOL

Quote
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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up