Pages: [1]   Go Down
Author Topic: HC-SR04 Ultrasonic sensor without Ultrasonic.h library?  (Read 6732 times)
0 Members and 1 Guest are viewing this topic.
Fort Smith, AR - USA
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Denmark
Offline Offline
Jr. Member
**
Karma: 0
Posts: 72
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

London
Offline Offline
Full Member
***
Karma: 0
Posts: 146
Ugi was not declared in this scope
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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/

« Last Edit: December 12, 2011, 02:18:33 pm by Dr_Ugi » Logged

Baltic states
Offline Offline
Jr. Member
**
Karma: 0
Posts: 97
new day, new try
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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?
Logged

London
Offline Offline
Full Member
***
Karma: 0
Posts: 146
Ugi was not declared in this scope
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: December 21, 2011, 05:54:08 am by Dr_Ugi » Logged

UK
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The ultrasonic library link seems to be broken but I found it at http://freecode.com/projects/hc-sr04-ultrasonic-arduino-library
Logged

BAN ALL THE SIGNATURES

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25760
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Pages: [1]   Go Up
Jump to: