Instructables

Ultrasonic Range detector using Arduino and the SR04 Ultrasonic sensor

This is a preliminary instructable that will take you through the steps I took to get the SR04 ULTRASONIC SENSOR  connected  and working to the Arduino. It will then be incorporated to my mini environment monitor.  Eventually, if it all fits, it will end up in a Startrek  toy tricorder casing I found in my junk box..
 
Remove these adsRemove these ads by Signing Up

Step 1: Connecting the SR04 Ultrasonic Sensor to the Arduino

Step 3: Connect Ultrasonic Sensor to Arduino

You Need 4 Jumper wires to conect the SR04 Ultrasonic Sensor to the Arduino:

1  From the SR04  VCC pin to the Arduino 5v
1 From the SR04  GND pin to the Arduino GND
1 From the SR04  TRG pin to the Arduino Digital pin 12
1 From the SR04  ECHO pin to the Arduino Digital pin 11


That's All The wireing you need... Easy,  no?

Next Step, load the Software library and sketches.

Step 4: Step 4 Download SR04 Library and install to Arduino IDE

Picture of Step 4 Download SR04 Library and install to Arduino IDE
WP_20130613_010.jpg
You need to download another Library  to get the Arduino to talk with the SR04 sensor. The sensor I got didn't come with any documentation, so I Googled around until I found a library that works.
I found an optimized library here
Download the library unzip it,  and install it in the Arduino IDE. Place it inside your Arduino libraries folder , and restart the Arduino IDE. For convenience, I included the file  in  the files section below.

You should now be able to see the  library and examples in  select File > Examples > NewPing > NewPingexample sketch.
load the sketch to your Arduino.

If you were successful at installing the libraries, and loading the NewPingexample sketch,  Compile the sketch  by clicking on the verify button and make sure there are no errors.

It's time to connect your Arduino to your PC using the USB cable.  Click on the upload button  to upload the sketch to the Arduino.

Once uploaded to the Arduino, open the serial monitor, and you should see the distance  data stream   coming from the sensor.

Step 5: Final step and preview of the sensor conected to my Mini enviroment monitor, soon to be Tricorder.

fansblink made it!20 days ago

Cool...............

IMG_20141126_224104.jpg
can i use more than one ultrasonic sensor with arduino uno
and if not which arduino should I buy to use several ultrasonic
chong.sime1 month ago

Hi, I am very new in Arduino and programming. I have recently purchased a SR04 and got it working in detecting distance.

Is there a way to turn off the emitter, and only produce the graph of ultrasonic sound wave produced by other sources? In other words, i just wanna make use of the ultrasonic receiver.

Thanks.

i've following the instructions, but after i finish, the result just 0cm 0cm 0cm on the serial monitor, whats wrong with this?.. did my sensors got error?.. seems the code was right. help me please.

For anyone wondering how to use this with servos, as in; "if a certain distance is measured stop servo motors" I have managed to change the code slightly allowing for this, Enjoy!

// ---------------------------------------------------------------------------

// Example NewPing library sketch that does a ping about 20 times per second.

// ---------------------------------------------------------------------------

#include <NewPing.h>

#include <Servo.h>

Servo myservo;

#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.

#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.

#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {

Serial.begin(9600); // Open serial monitor at 9600 baud to see ping results

myservo.attach(9);

}

void loop() {

delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.

unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).

Serial.print("Ping: ");

Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)

Serial.println("cm");

if(uS / US_ROUNDTRIP_CM < 10) {myservo.write(82);}

if(uS / US_ROUNDTRIP_CM > 10) {myservo.write(180);}

}

saymowan2 months ago

Cool project! :D

chestef3 months ago

I am extremely interested in the many applications of this type of sensor I find on "Instructibles.

What is the sensitivity of these sensors? I wish to control a probe that will extend to an object and stop at a distance closer than the 3mm I see in advertising.

Can anyone clarify if I can use this type of sensor and is it variable, say through a potentiometer, to set the range closer than 3mm?

I will certainly post my project if I can get throught this type of hurdle.

John Johnson

LeoZ14 months ago

Thanks for the nice and straightforward tutorial! One problem is occurring for me: I hooked up the Ping sensor like shown and I keep getting 0cm. In the example code, a value of 0cm is stated to be "outside set distance range." What does this mean and how can I fix it?

pigman_uk8 months ago

I used a MiniPro and added a i2c 16 X 2 LCD Display. Here is the battery operated version. Note that the output from the 12v battery pack must go to the RAW pin of the MiniPro and not the Vcc otherwise it will emit a small puff of smoke and die.

DSC_0398a.jpg
johnag (author)  pigman_uk8 months ago

cool project, just try not to burn too many componemts!

framirez118 months ago

Hi, really straightforward, easy and useful instructable. Thanks!
I wonder if the New Ping library supports analog input pins, or can this even be done with these sensors?

johnag (author)  framirez118 months ago

I haven't heard of any ultrasonic analog sensors...

pigman_uk8 months ago

I've added an i2c LCD display and this works great .. can someone explain why the maximum distance using the NewPing library is not as far as the normal non-interrupt method ? Thanks for the article .. well explained and it works.

T-_-K9 months ago

how to connect the SR04 with a 3pins sensor shield, i need a work around

GeckoScraps9 months ago

I just received one of these, haven't found a practical purpose but did take note of the fact that Cats can here the sound clearly.

It could be much better if you expand this tutorial in order to build more complex robotics applications with ultrasonic sensors
ricarz161 year ago
well, it works, but there are just two conditions, 0cm and 1423cm i don´t know why? is my sensor defect? or am i just a little noob who cannot connect four wires... :(
fgarci031 year ago
When I open the serial monitor it just writes non-sense random characters there, everything on the same line.

Any idea what it might be?
Ok I found it. Just changed "Serial.begin(115200); to Serial.begin(9600);"

The Serial Monitor is 9600baud by default, so in order for the sketch to work, you either have to change the code like I said, or manually change the "speed" of the serial monitor on the dropdown menu of it.

Nice! Been messing with my SR04 for a couple of hours and I love it :)
johnag (author)  fgarci031 year ago
Glad you could help yourself.. You could also change the baud rate in the serial monitor, in the bottom right there is a pull-down option to change the TX rx speed..good job