Ultrasonic Range Detector Using Arduino and the SR04 Ultrasonic Sensor

453,925

156

71

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

Step 1: Connecting the SR04 Ultrasonic Sensor to the Arduino

Step 2: Parts List

1               Arduino uno R3, or any Arduino for that matter
1               SR04 Ultrasonic Sensor
1               breadboard
4               Jumper Wires

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

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.

5 People Made This Project!

Recommendations

  • Plastics Contest

    Plastics Contest
  • Optics Contest

    Optics Contest
  • Make it Glow Contest 2018

    Make it Glow Contest 2018

71 Discussions

0
None
Tanmay Deuskar

11 months ago

I've tried this but no matter what the sensor returns 0. Why?

0
None
JohnH845

1 year ago

Hey guys, I work with livestock animals, one of my duties it to make sure all feed bins never run out of feed. So, how I incorporate this concept into my application? Basically I would like to install the ultrasonic sensor inside the feed bin, measuring the distance from the top of the bin to feed below, then I would like the info to be transmitted to a LCD screen inside my feed making facility in the form of a graph. Can someone point in th right direction? Thank You!

1 reply
0
None
Omere19JohnH845

Reply 11 months ago

you should install the sensor on the ceiling, and then measure the distance when the bins are empty, and then you can program it so it will say out of food when the distance=this distance

0
None
LydiaR16

1 year ago

I hooked up two sensors using the same pins. The serial monitor shows the closest distance of the two.

0
None
raspio

1 year ago

boss the program is working but display in serial monitor irrelevant

0
None
raspio

1 year ago

error compiling on the board

0
None
mniyaz

1 year ago

Hi, I used the same code. The sensor is giving wrong data, it shows only around half of the distance. Say if actual distance is 60cm's it returns 30cm's.

0
None
DavidM458

2 years ago

hi, how can i use one ultrasonic sensor to obtain two readings and compute area

0
None
kavalcoco

2 years ago

I will build it tomorrow . I want to install it to measure hight of water in our pond and display the result in my room. I have all the parts, and hope to complete and test tomorrow. Thanks for clear instructions

0
None
RohanG28

2 years ago

hey guys i got the 0cm all the time problem too, and then some weird characters.

the problem was that the baud rate in the program was 115200 and the baud rate in the serial monitor(lower right) was set to the default 9600. change it to 115200 and you'll be set :D

2 replies
0
None
BrunoS35RohanG28

Reply 2 years ago

Thanks a lot, Rohan. I had not realized it, but got it working now...

0
None
ChrisP278RohanG28

Reply 2 years ago

You might have the wires reversed

Try swapping the wires or swapping the pin numbers in the code

0
None
Lee Wilkerson

2 years ago

The link you have listed in step 4 no longer works. The new one:

https://bitbucket.org/teckel12/arduino-new-ping/wiki/Home

0
None
RaviP82

2 years ago

For some waterproofing experiment, I covered my receiver and trigger alternately with thin latex film getting correct reading. Then I covered both the receiver and transmitter with latex but now I am getting -1,means the pulse is not getting received.

0
None
kunj kapadia

2 years ago

Arduino: 1.7.10 (Windows 8.1), Board: "Arduino Uno"

C:\Users\DELL\AppData\Local\Temp\build6771404923751407265.tmp/core.a(wiring.c.o): In function `delay':

C:\Users\DELL\Desktop\Saraswati Diksha\Arduino\hardware\arduino\avr\cores\arduino/wiring.c:114: undefined reference to `yield'

collect2.exe: error: ld returned 1 exit status

Error compiling.

This report would have more information with

"Show verbose output during compilation"

enabled in File > Preferences.

0
None
SyedA58SyedA58

Reply 2 years ago

Seril,println( distance );

lcd.print(distance);

0
None
blockswitch

2 years ago

outputs this

Ping: 22cm

7cm

Ping: 7cm

Ping: 7cm

Ping: 7cm

Ping: 8cm

ing: 7cm

Ping: 8cm

Ping: 7cm

Ping: 8cm

Ping: 8cm

Ping: 8cm

Ping: 22cm

Ping: 8cm

ò²²WW£ððàôšñàõ²æðšóæ²ææò£óñ²ððñô²›šššñ’²ðæòg’ððôòçg›šò’²’ç›ð²²²ðšš

0
None
JurieP

3 years ago on Introduction

I also had a problem with it reporting 0cm all the time. I finally figured out that the echo input pin for some reason was stuck on high. Adding the following line of code before conducting a sonar ping solved it: digitalWrite(ECHO_PIN, LOW);