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.

LeoZ115 days 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_uk5 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_uk4 months ago

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

framirez114 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)  framirez114 months ago

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

pigman_uk5 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-_-K5 months ago

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

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

cdragos george11 months ago
It could be much better if you expand this tutorial in order to build more complex robotics applications with ultrasonic sensors
ricarz1611 months 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... :(
fgarci0311 months 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?
fgarci03 fgarci0311 months ago
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)  fgarci0311 months 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