loading
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

<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>
<p>hello friend !!</p><p>Do you have any idea how to connect water Proofed Ultrasonic sensor with arduino and coding ,,,i have try but i just failed ..if you have any idea please share with me </p><p>thank you</p><p>Link:</p><p>http://www.google.lk/imgres?imgurl=http://g01.a.alicdn.com/kf/HTB1a12XJpXXXXX9aXXXq6xXFXXXE/-font-b-Ultrasonic-b-font-font-b-Module-b-font-Distance-Measuring-Transducer-font-b.jpg&amp;imgrefurl=http://www.aliexpress.com/w/wholesale-waterproof-ultrasonic-sensor-module.html&amp;h=800&amp;w=800&amp;tbnid=it82jjrdO0G8zM:&amp;docid=AoqmQkcV5JgGRM&amp;ei=YSEFVuPvKoKIuATQsq2QBQ&amp;tbm=isch&amp;ved=0CHsQMyhWMFZqFQoTCKOk9MT8kcgCFQIEjgodUFkLUg</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>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>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>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>
<p>can anyone tell me what is the maximum range of this ultrasonic sensor??</p><p>i need a sensor with a range of atleast 4-5 mts,should i go for this??</p>
<p>Hi, I love doing new things with the Arduino Uno I have. I have connected the wires correctly and have downloaded the NewPing file and added it to the arduino library. Once I upload the sketch and open the serial monitor I don't recieve any data. Please help!</p>
<p>Your Power and Ground are the opposite of my board...mine is working. IF this was connected backwards, you may have bigger problems. Best of Luck!</p><p>-fab</p>
<p>hello sir can im doing obstacle avoidance robot with vacuum cleaning robot.im need to use 3 ultrasonic HC-SR04 sensors in front of the robot.can i connect VCC and Gnd pin directly to the regulated 5V from IC7805 will affect the working of sensors.if not is there any way to connect 3 sensors.</p>
<p>hello sir can im doing obstacle avoidance robot with vacuum cleaning robot.im need to use 3 ultrasonic HC-SR04 sensors in front of the robot.can i connect VCC and Gnd pin directly to the regulated 5V from IC7805 will affect the working of sensors.if not is there any way to connect 3 sensors</p>
<p>hello sir can im doing obstacle avoidance robot with vacuum cleaning robot.im need to use 3 ultrasonic HC-SR04 sensors in front of the robot.can i connect VCC and Gnd pin directly to the regulated 5V from IC7805 will affect the working of sensors.if not is there any way to connect 3 sensors</p>
<p>i want the ultrasonic sensor to measure a constant distance(eg 5cm), i want to use it under water to do wall following..can any one help.</p>
<p>Hi Do you any idea how to activate a relay when the ultrasonic sensor detects a presence?</p>
<p>Hi Do you any idea how to activate a relay when the ultrasonic sensor detects a presence?</p>
<p>Cool...............</p>
can i use more than one ultrasonic sensor with arduino uno <br>and if not which arduino should I buy to use several ultrasonic
<p>Hi, I am very new in Arduino and programming. I have recently purchased a SR04 and got it working in detecting distance.<br><br>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.<br><br>Thanks.</p>
<p>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.</p>
<p>For anyone wondering how to use this with servos, as in; &quot;if a certain distance is measured stop servo motors&quot; I have managed to change the code slightly allowing for this, Enjoy!</p><p>// ---------------------------------------------------------------------------</p><p>// Example NewPing library sketch that does a ping about 20 times per second.</p><p>// ---------------------------------------------------------------------------</p><p>#include &lt;NewPing.h&gt;</p><p>#include &lt;Servo.h&gt;</p><p>Servo myservo;</p><p>#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.</p><p>#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.</p><p>#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.</p><p>NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.</p><p>void setup() {</p><p> Serial.begin(9600); // Open serial monitor at 9600 baud to see ping results</p><p> myservo.attach(9);</p><p>}</p><p>void loop() {</p><p> delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.</p><p> unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).</p><p> Serial.print(&quot;Ping: &quot;);</p><p> Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)</p><p> Serial.println(&quot;cm&quot;);</p><p> if(uS / US_ROUNDTRIP_CM &lt; 10) {myservo.write(82);}</p><p> if(uS / US_ROUNDTRIP_CM &gt; 10) {myservo.write(180);}</p><p>}</p>
<p>Cool project! :D</p>
<p style="margin-left: 40.0px;">I am extremely interested in the many applications of this type of sensor I find on &quot;Instructibles.</p><p style="margin-left: 40.0px;">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. </p><p style="margin-left: 40.0px;">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?</p><p style="margin-left: 40.0px;">I will certainly post my project if I can get throught this type of hurdle.</p><p style="margin-left: 40.0px;"> </p><p style="margin-left: 40.0px;">John Johnson</p><p style="margin-left: 40.0px;"> </p>
<p>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 &quot;outside set distance range.&quot; What does this mean and how can I fix it?</p>
<p>Hi, really straightforward, easy and useful instructable. Thanks! <br>I wonder if the New Ping library supports analog input pins, or can this even be done with these sensors?</p>

About This Instructable

220,266 views

123 favorites

License:

More by johnag:  Arduino  DEMO series Make an Oscilloscope Using the SainSmart Mega2560 with the TFT LCD shield and the 3.5 "color touch screen Make a Mini Arduino programmable  4 channel DC-DVM
Add instructable to: