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.

<p>I hooked up two sensors using the same pins. The serial monitor shows the closest distance of the two.</p>
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!
<p>boss the program is working but display in serial monitor irrelevant</p>
<p>error compiling on the board</p>
<p>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.</p>
<p>hi, how can i use one ultrasonic sensor to obtain two readings and compute area</p>
<p>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</p>
<p>hey guys i got the 0cm all the time problem too, and then some weird characters.</p><p>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</p>
<p>Thanks a lot, Rohan. I had not realized it, but got it working now...</p>
<p>You might have the wires reversed</p><p>Try swapping the wires or swapping the pin numbers in the code</p>
<p>The link you have listed in step 4 no longer works. The new one:</p><p>https://bitbucket.org/teckel12/arduino-new-ping/wiki/Home</p>
<p>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.</p>
<p>Arduino: 1.7.10 (Windows 8.1), Board: &quot;Arduino Uno&quot;</p><p>C:\Users\DELL\AppData\Local\Temp\build6771404923751407265.tmp/core.a(wiring.c.o): In function `delay':</p><p>C:\Users\DELL\Desktop\Saraswati Diksha\Arduino\hardware\arduino\avr\cores\arduino/wiring.c:114: undefined reference to `yield'</p><p>collect2.exe: error: ld returned 1 exit status</p><p>Error compiling.</p><p> This report would have more information with</p><p> &quot;Show verbose output during compilation&quot;</p><p> enabled in File &gt; Preferences.</p>
<p>can I use both serial monitor and lcd in this project </p>
<p>yes u can..</p>
<p>Seril,println( distance );</p><p>lcd.print(distance);</p>
<p>outputs this</p><p>Ping: 22cm</p><p> 7cm</p><p>Ping: 7cm</p><p>Ping: 7cm</p><p>Ping: 7cm</p><p>Ping: 8cm</p><p>ing: 7cm</p><p>Ping: 8cm</p><p>Ping: 7cm</p><p>Ping: 8cm</p><p>Ping: 8cm</p><p>Ping: 8cm</p><p>Ping: 22cm</p><p>Ping: 8cm</p><p>&ograve;&sup2;&sup2;WW&pound;&eth;&eth;&agrave;&ocirc;š&ntilde;&agrave;&otilde;&sup2;&aelig;&eth;š&oacute;&aelig;&sup2;&aelig;&aelig;&ograve;&pound;&oacute;&ntilde;&sup2;&eth;&eth;&ntilde;&ocirc;&sup2;&rsaquo;ššš&ntilde;&rsquo;&sup2;&eth;&aelig;&ograve;g&rsquo;&eth;&eth;&ocirc;&ograve;&ccedil;g&rsaquo;š&ograve;&rsquo;&sup2;&rsquo;&ccedil;&rsaquo;&eth;&sup2;&sup2;&sup2;&eth;šš</p>
<p>Success! I had problems with it showing 0cm but fixed it by using different breadboard connections. Did a tutorial mashup and learned how to use the &quot;Processing&quot; app to graph it, I also made it change color if it was out of range.</p>
<p>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);</p>
<p>Aim the sensor at a wall or other large solid object within 30cm. Just mentioning this on the off chanch that you haven't tried this. Good luck!</p>
<p>I am having a problem of getting 0cm all the time. I tried using digitalWrite(ECHO_PIN,LOW) but its still giving 0cm</p>
<p>I changed the echo pin from 11 to 13 and it started working.However if the target is moved or I move the breadboard around. Sometime it resets to 0 and then doesn't recover :-(</p>
<p>after click serial port monitor showed me this type of data stream </p><p>plz what i should do to fix </p>
<p>According to the example code(that i got from the library anyway), the baud rate is at 115200. Your picture shows you are reading the data with 9600 baud, the default setting. Try switching to different baud rates(just click on it 115200 is part of 'em) or change the line in the code that chooses the baud rate aka Serial.begin(whatevernumberyouwant); Hopes this fixes your problem.</p>
<p>Haha i had the same issue. Thanks!</p>
<p>Np glad i could help :)</p>
<p>Great project!</p><p>I made <a href="http://www.thingiverse.com/thing:1241706" rel="nofollow">this game</a> for drones with the same components!</p>
<p>great tutorial !! thanks :-)</p>
<p>Hi I followed every step correctly and when I go to upload the program to the Arduino I get a &quot; Exit Status 1 Error Compiling&quot; What can this be, is there a solution to this?</p>
<p>Hi do you know about signal in dangrees </p><p>road bend,project?</p>
<p>I set the baud rate but my sensor is continuously receiving the data....the data on serial monitor is coming very fast.It is not stopping.Why?</p><p>Thanks in adavance for any help.</p>
<p>i think u hav to put a delay in the loop to make a time gap b/w 2 successive fetches...</p>
Awesome tutorial. I tried it and it worked for me. thank you.
<p>please i want help </p><p>i build quad copter detect fish in water </p><p>how to use Ultrasonic Range detector to detect fish in water please help me </p>
<p>Water reflects ultrasonic waves (when using the module above) which means you cant detect the fish. There are ultrasonic sensors which are placed INTO the water. But thats not what you are looking for when operating from a quadcopter. </p>
<p>pi&ntilde;as onjde descargo er minecr&aacute;?</p>
<p>aka tdejo la link http://rulateelporrotete.com</p>
<p>hay ya entiendo estan jugando maincr&aacute;</p>
<p>no seras tu aki el mas pussydestroyer?</p>
<p>onde vas loko yo tengob espada de diamont of cours</p>
<p>Just done, it's my first try with this sensor. I only change Serial.begin to 9600 and monitor also works. </p><p>Thanks. </p>
<p>hii, when i am uploading it to my arduino uno. i am getting error like:</p><p>avrdude: ser_open(): can't set com-state for &quot;\\.\COM13&quot;.</p><p>please help me. thank you</p>
<p>Hi guys, I would like to ask on how to stabilize my ultrasonic sensor mounted in a servo motor. It is because everytime an &quot;Out of Range&quot; displays in the serial monitor it gives current to the servo motor causing it to sweep. The possibility of sweeping the servo left or right only is when there is an object being detected. BTW, the ultrasonic sensor is mounted above the servo mtor. </p><p><br>Here is my code://<br></p><p>Code: <a rel="nofollow">[Select]</a></p><p><br><br>#define trigPin 13<br>#define echoPin 12<br>#define led 11<br>#define led2 10<br>#include &lt;Servo.h&gt;<br>int pos = 0; // variable to store the servo position <br>int defPOS = 0; <br><br>Servo myservo;<br>void setup() {<br>Serial.begin (9600);<br>pinMode(trigPin, OUTPUT);<br>pinMode(echoPin, INPUT);<br>pinMode(led, OUTPUT);<br>pinMode(led2, OUTPUT);<br>myservo.attach(9);<br>myservo.write(0);<br><br>}<br><br>void loop() {<br>long duration, distance;<br>digitalWrite(trigPin, LOW); // Added this line<br>delayMicroseconds(2); // Added this line<br>digitalWrite(trigPin, HIGH);<br>// delayMicroseconds(1000); - Removed this line<br>delayMicroseconds(10); // Added this line<br>digitalWrite(trigPin, LOW);<br>duration = pulseIn(echoPin, HIGH);<br>distance = (duration/2) / 29.1;<br>//0 Left<br>//1 Center<br>//2 Right<br>int buttonState1 = digitalRead(led); //red<br>int buttonState2 = digitalRead(led2); //green<br>if( (buttonState1 == 0 &amp;&amp; buttonState2 == 1) &amp;&amp; (defPOS != 1))<br>{<br> delay(2000);<br> if(defPOS == 2)<br> {<br> for(pos = 0; pos &lt;= 90; pos += 1) // goes from 0 degrees to 180 degrees <br> { // in steps of 1 degree <br> myservo.write(pos); // tell servo to go to position in variable 'pos' <br> delay(2); // waits 15ms for the servo to reach the position <br> }<br> }<br> else if(defPOS == 0)<br> {<br> for(pos = 0; pos &lt;= 90; pos += 1) // goes from 0 degrees to 180 degrees <br> { // in steps of 1 degree <br> myservo.write(pos); // tell servo to go to position in variable 'pos' <br> delay(2); // waits 15ms for the servo to reach the position <br> }<br> }<br> defPOS = 1;<br>}<br><br>if (distance &lt; 30) { // This is where the LED On/Off happens<br> digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off<br>digitalWrite(led2,LOW);<br> if(defPOS == 0)<br> {<br> for(pos = 90; pos&gt;=0; pos-=1) // goes from 180 degrees to 0 degrees <br> { <br> myservo.write(pos); // tell servo to go to position in variable 'pos' <br> delay(2); // waits 15ms for the servo to reach the position <br> }<br> defPOS = 2;<br> }<br> else if(defPOS == 2)<br> {<br> for(pos = 0; pos &lt;= 90; pos += 1) // goes from 0 degrees to 180 degrees <br> { // in steps of 1 degree <br> myservo.write(pos); // tell servo to go to position in variable 'pos' <br> delay(2); // waits 15ms for the servo to reach the position <br> }<br> defPOS = 1; <br> }<br> else if(defPOS == 1)<br> {<br> for(pos = 0; pos &lt;= 180; pos += 1) // goes from 0 degrees to 180 degrees <br> { // in steps of 1 degree <br> myservo.write(pos); // tell servo to go to position in variable 'pos' <br> delay(2); // waits 15ms for the servo to reach the position <br> }<br> defPOS = 0; <br> }<br> //myservo.write(180);<br> // delay(1000);<br> //myservo.write(0);<br>}<br>else {<br> digitalWrite(led,LOW);<br> digitalWrite(led2,HIGH);<br>}<br><br>if (distance &gt;= 400 || distance &lt;= 0){<br> Serial.println(&quot;Out of range&quot;);<br>}<br>else {<br> Serial.print(distance);<br> Serial.println(&quot; cm&quot;);<br>}<br>delay(500);<br>}</p>
<p>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. </p>
<p>wow man great work i want to know about connections between LCD and arduino</p>
<p>How do you use the LCD display with an arduino uno. Please send me the instructions for this. I wanted to make this for a long time</p>
<p>This is the sketch I use to add an LCD display to a project.<br>It uses 2 libraries. Wire and LiquidCrystal_i2C.<br></p><p>//DFRobot.com</p><p>//Compatible with the Arduino IDE 1.0</p><p>//Library version:1.1</p><p>#include &lt;Wire.h&gt; </p><p>#include &lt;LiquidCrystal_I2C.h&gt;</p><p>LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display</p><p>void setup()</p><p>{</p><p> lcd.init(); // initialize the lcd </p><p> // Print a message to the LCD.</p><p> lcd.backlight();</p><p> lcd.print(&quot;Hello, world!&quot;); //display top line </p><p> lcd.setCursor(0,1); //set cursor to first character of line 2 </p><p> lcd.print(&quot;Hello Mars too&quot;); //display to line 2</p><p>}</p><p>void loop()</p><p>{</p><p>}</p>
<p>There are 2 ways to connect the LCD display to the Arduino so it depends which display you have bought. Essentially there is the parallel way which has lots of wires for each data line or the serial way (i2C) which uses only 4 wires (including the Vcc &amp; Gnd). Once you have the right display (which has a serial converter board on the back) life becomes much easier. So lets find out which display you've bought please ?</p>
<p>cool project, just try not to burn too many componemts! </p>

About This Instructable




More by johnag:Arduino Color LCD Message Clock  Arduino  DEMO series  Make an Oscilloscope Using the SainSmart Mega2560 with the TFT LCD shield and the 3.5 "color touch screen 
Add instructable to: