HC-SR04 Ultrasonic Ping Sensor Arduino Tutorial

Hi everyone,

Just the word ultrasonic sounds cool to me! It makes me think of something fast, hi-tech, and hard to understand. Fact is, I think I’m right about everything but the hard to understand part, which I realized after purchasing the HC-SR04 ultrasonic distance sensor and hooking it up for a test run. The uncanny resemblance to Wall-E just makes it even better. I can think of a whole list of applications of this technology for my future projects; particularly for autonomous robots and UAVs (Unmanned Air Vehicles). Hopefully you have some ideas for your own projects too!

HC-SR04 Ultrasonic Ping Sensor – Arduino Tutorial from Treehouse Projects on Vimeo.

The concept of ultrasonic technology is quite simple. By emitting high frequency pulses of sound forwards, an obtrusive object would allow the waves to bounce right back to the source. If the pulse is in fact received after emitting it, we can assume that there is something in the way. Another way to picture this phenomenon is with a ordinary ball. If you throw it horizontally and it comes back to you, presumably something in the direction of your toss got in the way of the ball; thus, allowing it to bounce back. The difference between a ball and a sound wave is that the ball looses a lot more energy along its journey to and from you, but a sound wave loses a relatively negligible amount. In other words, the sound wave will leave and come back to you at more or less the exact same speed, but a ball wouldn’t due to gravity, air resistance, etc. Since we know the speed of sound (340.29 m/s) and the time it took for the pulse to be emitted and received, we can quite simply deduce the distance the sound wave traveled and therefore the distance to the object which obstructed its path. Bats and submarines use this exact same technology to navigate because visibility under the ocean is extremely minimal, and bats can’t see the way most animals can.

Resources Required

  1. HC-SR04 (you can get these dirt cheap, for as little as $3)
  2. Arduino Microcontroller (I use an Arduino clone)
  3. Breadboard
  4. LEDs (I used two, one green and one red)

If you have all the parts, you could get this up and running in less than 15 minutes.

Process

The process to set this up is very simple, and best understood by watching the video. However, I will outline it textually as well:

  1. Attach your HC-SR04 and LEDs to your breadboard
  2. Connect the ground pins to your LEDs, and the sensor (GND)
  3. Attach the green LED to Pin 3 on the Arduino
  4. Attach the red LED to Pin 2 on the Arduino
  5. Attach the VCC pin on the sensor to 5V
  6. Attach the trig pin on the sensor to Pin 13 on the Arduino
  7. Attach the echo pin on the sensor to Pin 12 on the Arduino
  8. Connect the Arduino to your computer, copy the code below, and upload it

If everything goes right, you should see the green LED stay on when you are more than 5 cm away from the HC-SR04, and the red LED otherwise.

Resources

Below, find the code I used for this tutorial. Feel free to copy it, edit it, and tear it apart!

References

  • Fritzing – Thanks to the folks at Fritzing.org for getting me inspired to try out this technology, and guiding me with the setup.
Posted in Tutorials
43 comments on “HC-SR04 Ultrasonic Ping Sensor Arduino Tutorial
  1. rafael says:

    I’m working on a project for school and I wanted to know how you would change the code to use 2 sensors. We’re making an automated garbage can. One sensor would be the “walk up” sensor and the other would be a manual. I’m playing with it now but i have no prior experience so any help would be great. Thanks

    • Sounds like a great project! The first thing I need to know is what both sensors are doing. So I understand that the first one detects if someone has approached the can, but the second sensor does what exactly?

  2. rafael says:

    Thanks for the quick response! And you’re correct about the first sensor. The second would be used as a manual switch. So if you wanted to peel vegetables and you needed the can open for a while, you would swipe over it to open, and when done swipe again to close. I just can’t get both sensors working at the same time.

    • Cool. So normally, you can’t have two things running at the same time. What you need to do is work with the internal timers of the ATmega chip running the Arduino bootloader. It’s not as hard as it may sound. Essentially, interrupts are just like blocks of code running on separate clocks. So it would be like having two programs running at the same time if the micrcotroller had two clocks (more specifically, two timers). What you would need to do is setup an ISR (interrupt service routine). A great link to walk you through the process is this one: http://letsmakerobots.com/node/28278

      The other thing you can do is simply setup another inPin and pingPin for your second sensor. Initialize them the same way I did for the first sensor in the code, then in the loop, run the same procedure twice. The first run would be for sensor number one, the second run would be for sensor number two. Now, the drawback is that the two sensors aren’t actually running at the same time. But if you look at the code, the whole procedure itself takes only 7 microseconds, and then a 100 millisecond delay is added just to make sure the ‘field is clear’ of any noise from the last run. So you can just add the same code twice, one after another, totaling to 7 microseconds for the first run, 7 microseconds again for the second, and then the original 100 millisecond delay. The difference will be pretty much unnoticeable to you because 7 microseconds are 7 millionths of a second!

      So that’s the easy way. Try it, and if it works fine, then you don’t need to mess around with interrupts. Even if you eventually do, they are very useful, and not too difficult to grasp.

      Finally, make sure both your sensors are not facing the same direction. Although its unlikely if you decide to operate them separately, there could be some interference if you install them in parallel.

      Oh ya, I have a question. Why do you need two sensors anyway? Why don’t you just use one which can detect if someone approaches, and use it also in the event that someone waves their hand in front of it? Perhaps you plan on placing them at different locations.

      Good luck, let me know if you need more help!

  3. rafael says:

    What you described is what i was trying to do, but the second sensor doesn’t give a reading and the led doesn’t turn on. The code is below. I currently have both sensors facing away from each other on a breadboard. I switched the sensors to see if the one wasn’t working but it worked when switched. And again thank you so much for your help!

    • I just tried it myself, and it works great for me. Here is the code I wrote up for it:

      Note that I omitted the LED stuff to keep things simple, but you can see both sensors working perfectly in the serial monitor. Implementing the LED stuff should be easy anyway. I think your problem may have been the ordering of your code. You should read the echo pin right after you run the triggering sequence, not perform both triggering sequences, and then read from both echo pins. That defeats the whole purpose of the code. Using the above code, the serial monitor will alternatively display the distances read by each sensor.

      I hope this helps. I’ll keep this setup on my desk for a while in case you need me to test anything else. Just make doubly sure that your wires are connected to the correct pins as well.

  4. rafael says:

    I hate to be annoying but I wanted to know if you could help me one more time. I took the code you posted last time(thanks a million) and modified it to control the servo. I’m able to get the servo to rotate but then it comes back. I’m sure i’m doing something stupid but i can’t figure it out. I want to be able to get the servo to an angle and hold it until a condition is met. This would be either a time delay, or in the case of the manual switch, another reading from it.

    • Helping you is truly my pleasure, so ask as many questions as you need to.

      The problem in your code is quite clear. I believe you are using the for loop for(pos = 0; pos safeZone1) incorrectly. Essentially, it doesn’t seem like your code ever sets a value for the variable pos to anything but zero. That’s why your servo always retreats to its zero position. Now, since your code for that for loop does not have any comments, I cannot understand what you are trying to do with the servo, or why you even need the for loop. If you’re sure you need the for loop, review this http://arduino.cc/en/Reference/For to make sure you are doing it right.

      Your problem has been identified, and once you tell me exactly what you need the servo to do, I can help you further.

      Good luck!

  5. rafael says:

    I think I see what you mean. I also see your point about not having comments. The code I used was the sweep code from the arduino site. It was to verify I could trigger the servo in the code. Ultimately with the first sensor I want to be able to trigger the servo to go to an angle I set and hold there for a time that I could also set, then return to zero. With the second sensor I want the servo to go to the same angle but stay there until it got another trigger from the sensor to return to zero. The sensors would trigger the servo when they got a reading of a given distance. Also I’m very glad to hear you don’t mind the harrassment. Thanks again!

    • Great! Well, it sounds like an easy enough task. Why don’t you give it a shot first, and if you don’t figure it out, I’ll help you more. It will also help you learn.

      To get you started, I’ll tell you how I would approach it. When the first sensor is triggered the way you want, set the position variable for the servo to whatever amount you want. Since you want it to wait, and then close, simply add a delay, then set the position of the servo to 0. For the second sensor, when it is activated the way you want, create an if statement to test the position of servo. If the position of the servo is at zero, set it to whatever position you want, and continue with your code. If the position is not at zero, that means someone previously activated your system, and perhaps the lid of the garbage can is currently open. So the second part of your if statement will check if the servo’s position is something other than zero. If it is, return it to its zero position. You’ll have to ensure that the position variable of the servo is declared as a global variable (the way you have it is fine), instead of local.

      I hope that makes sense! Feel free to ask more questions!

  6. rafael says:

    I understand what you were saying about never setting the angle so below was my attempt to address that. I wanted to make pos a variable, and then set the angle to either 0 or 45 as part of an if/else statement. However, the servo stills spins and never stays at a given angle.

  7. rafael says:

    I’m using writeMicroseconds example and now i can actually control the servo pretty well. I’m going to try and work it into the code and see what happens.

    • Sounds great buddy! Please excuse my delayed response. Feel free to let me know if you need more help, and don’t hesitate to contact me directly via the contact form on my website. We can discuss your project in depth if you like.

      If you get a chance, send me a picture of your project. I’d be thrilled to see the final result!

      Best wishes!

  8. Larry says:

    How does this circuit not need resistors for the LEDs? It does work fine by the way… Thanks :)

    • Hey Larry,

      You know what? That’s a really good question!

      I have absolutely no idea! I think I probably judged that the LEDs wouldn’t be bright enough with ~2V. And perhaps those LEDs were rated for a higher voltage.

      Thanks for bringing that up though, I too wonder what my reason could have been for not using resistors!

      • Larry says:

        Thanks for the quick reply! I’m just learning this technology and find it fascinating and a bit overwhelming at times… I’ve seen projects with or without resistors, resistors on the LED anode or cathode side. I’m just trying to absorb as much information as I can at this stage. Thanks again!

      • The pleasure is mine. School’s keeping me busy right now, but I hope to get some cool projects up soon!

        Reading is the absolute best way to learn. If you have specific questions, I recommend asking them at http://www.electronics.stackexchange.com. If you want to see the kinds of things people do with tech, engineering, and science, check out http://www.hackaday.com. Those are two of my favorite websites.

      • Eric Stark says:

        You really should use resistors in series with your LEDs. There are special cases when you don’t need them (e.g., PWM control), but most static cases like this project really *should* use them. Resistors are used to limit current – both through the LED and through the MCU. LEDs are current-based devices. Most hobby grade LEDs should be operated at no more than 15ma and will glow just fine at lower currents. For example, unless they need to be visible in bright daylight, I’ll run them as low as 5ma and they are quite visible in most indoor applications.

        One important aspect: LEDs have what is known as a “forward voltage” or “(series) voltage drop”. This is typically abbreviated as “Vf”. It represents how much voltage is “dropped” when the LED is being operated and is forward biased (anode voltage is higher the the cathode voltage). Depending on the color and style of LED, the Vf can be in the range of about 2 (typical of red, yellow, or green) to 3.5 (typical of blue or white LEDs) volts. Most hobby projects use red, green, or yellow LEDs making 2 volts a fair rule of thumb. If using other LEDs such as white, blue, or infared, please look at the part’s datasheet.

        To find what sort of resistor you need, first calculate the effective series voltage of the LED. In most cases, this is the supply voltage less the Vf of the LED. If using a 3.3v supply with a red/green/yellow LED, the series voltage is 3.3v – 2.0v = 1.3v. At 5v, the series voltage would be 5.0v – 2.0v = 3.0v. Using 10ma as a crude target current, find the resistance by dividing the series voltage by the desired LED current. Remember that unit for current in the E=IR equation is in AMPS and we have been referring to milliamps. So, for the first example (3.3v supply and 1.3v series voltage) with a target current of 10ma (or 0.010amps), we use the equation R = 1.3v / 0.010a = 130ohms. Being a rough target, a resistor between 100ohms and 200ohms would be fine. For the 5v supply (and 3.0v series voltage after the LED’s Vf), we use the equation R = 3.0v / 0.010a = 300ohms. Again, a value of 200 to 400 ohms would be fine. Older 5v TTL circuits frequently used the common values 220ohm or 330ohm for the current limiting resistor.

        So, why aren’t the LEDs burning up in this example? Excellent question indeed! If this were an ideal circuit, they would. However, this is far from an ideal circuit. First, there is lots of resistance here. There are a lot of interconnects with jumpers, headers, breadboards, etc. and relatively long leads. Adding them up, I wouldn’t be surprised if there weren’t over 25 ohms of resistance in total. Second, the MCU in the Arduino isn’t a perfect switch. While I didn’t account for it above, the gates in its construction also have Vf drops making the series voltage drop even greater than the LED itself. Additionally, as the LEDs heat up (something that happens quickly when being driven with high currents), their Vf increases as does a small parasitic series resistance. Finally, you are also likely experiencing a voltage sag on your supply line. Even a little sag will greatly reduce your series voltage through the LEDs and make the series current much smaller. All of these factors combine to provide a sort of safety net. While you may be shortening the life of your LEDs and perhaps the Arduino itself, it isn’t likely a big problem. Another safety net is that the LEDs probably have maximum currents in the range of 30ma or so. Exceeding that by a small margin will lead to shorter lives (days instead of years) but probably not catastrophic failure.

        Considering all of the above: If your true series voltage is about 1.0v and my assumption of about 25 ohms of true series resistance is viable, then you probably aren’t experiencing more than 40ma of current through each LED. They will probably hold up to that abuse for a long time. That isn’t a promise though! You really should be using a current limiting resistor. Even something like 100ohms can go a long way to provide basic protection.

        One good precaution in cases like this: measure the temperature of the MCU (the Atmel controller) in the Arduino. If it is getting very warm or hot to the touch (above 50 deg. C), you’ll want to be careful. Running it too hot by dumping lots of current through it GPIO pins will shorten its life or cause sporadic failures (something very odd things like random program resets).

        All of the above is through out the window if you are doing more advanced control such as using PWM or constant current sources. Of course, if you are using those techniques, then you are well above the elementary information I’ve disclosed herein.

        Good luck!

      • Treehouse Projects says:

        Yes, all your points definitely make sense. I think those LEDs were actually rated for up to 5 V though. Also, at the time I created this tutorial, I was not so educated about the basics. I think I have come quite a long way in less than a year though (and if you look at my most recent projects, you will notice that all LEDs are coupled with appropriately chosen resistors :) ). Your comment itself is amazing information for newbies! Thanks for a refreshing read.

  9. colin says:

    hi, unfortunately i think my school project will be the most difficult yet but i’m building a robot and i need this robot to sense other robots using the ultrasonic sensors, once the sensors recognize the other robot i have to program to tell the speed controllers for the motor to move in the direction of the other robot that was just sensed by the sensors. i was planning on using 8 sensors but im not sure how much i can actually use with the arduino mega 2560 rev 3.

    ANY HELP WOULD BE SO GREATLY APPRECIATED!!

    • Hi Colin,

      Forgive me for my delayed response.

      First off, sure your project may be difficult, but I’m telling you it will be a superb experience. If you accomplish something difficult, not only does it feel great, but you gain some really useful skills.

      So it depends on what kind of distance sensors you are interested in using. If you want to know distance (i.e. a range) then you need analog sensors. If you want to know if something is in the way, but don’t care how far it is, then you can use digital sensors. Your Arduino has many digital ports, so that shouldn’t be a problem.

      Anyhow, I think you want to measure distance, so you need analog readings. Your Arduino has 16 analog input pins (refer to the summary here http://arduino.cc/en/Main/arduinoBoardMega2560), twice as much as what you need. So you’re good :)

      Now, suppose you needed to use more than 16 sensors with your board, then you can try something called multiplexing. You can read more about multiplexing here: http://playground.arduino.cc/learning/4051

      Let me know if you need anymore help!

  10. Rafael says:

    Good afternoon,

    I’m hoping once again you can help me out. What I’m trying to do is use 2 ping sensors to control the flashing frequency of 2 LED’s. The idea is to have the frequency increase as the distance reading decreases. I figured i could set 2 or 3 thresholds that would change the rate on the LED’s. The code i have below is what i came up with for my last project(i got a lot of help as you can read above). I figured i can take out the servo stuff and use it to run either the blink without delay or the blink example code, I’m just not sure how to put it together. If you could give a couple of tips I would really appreciate it. Thanks.

    /*
    Treehouse Projects – http://www.treehouseprojects.ca
    Ping HC-SR04 Tutorial
    This program is adapted from the example Ping code from Arduino.
    It is extended to light a red LED when an object is a certain distance
    from the sensor, and green otherwise.
    Jun. 2012
    */

    //pin which triggers ultrasonic sound
    const int pingPin1 = 13;
    const int pingPin2 = 12;

    //pin which delivers time to receive echo using pulseIn()
    int inPin1 = 11;
    int inPin2 = 10;

    void setup() {
    // initialize serial communication
    Serial.begin(9600);
    }

    void loop()
    {
    //raw duration in milliseconds, cm is the
    //converted amount into a distance
    long duration, cm;

    //initializing the pin states
    pinMode(pingPin1, OUTPUT);
    pinMode(pingPin2, OUTPUT);

    //sending the signal, starting with LOW for a clean signal
    digitalWrite(pingPin1, LOW);
    delayMicroseconds(2);
    digitalWrite(pingPin1, HIGH);
    delayMicroseconds(5);
    digitalWrite(pingPin1, LOW);

    //setting up the input pin, and receiving the duration in
    //microseconds for the sound to bounce off the object infront
    pinMode(inPin1, INPUT);
    duration = pulseIn(inPin1, HIGH);

    // convert the time into a distance
    cm = microsecondsToCentimeters(duration);

    //printing the current readings to ther serial display
    Serial.print(cm);
    Serial.print(“cm”);
    Serial.println();
    delay(100);

    //run 2

    //sending the signal, starting with LOW for a clean signal
    digitalWrite(pingPin2, LOW);
    delayMicroseconds(2);
    digitalWrite(pingPin2, HIGH);
    delayMicroseconds(5);
    digitalWrite(pingPin2, LOW);

    //setting up the input pin, and receiving the duration in
    //microseconds for the sound to bounce off the object infront
    pinMode(inPin2, INPUT);
    duration = pulseIn(inPin2, HIGH);

    // convert the time into a distance
    cm = microsecondsToCentimeters(duration);

    //printing the current readings to ther serial display
    Serial.print(cm);
    Serial.print(“cm”);
    Serial.println();

    delay(100);
    }

    long microsecondsToCentimeters(long microseconds)
    {
    // The speed of sound is 340 m/s or 29 microseconds per centimeter.
    // The ping travels out and back, so to find the distance of the
    // object we take half of the distance travelled.
    return microseconds / 29 / 2;
    }

    • Well, you raise an interesting problem. Since these micrcontrollers only have one processor, you can really only run one process at a time. But since they compute at such high speeds, you can process things in serial without the user noticing. So what I am trying to say is this:

      In order to get the LED to blink at a specific rate, use a counter (called timers, actually, in the microcontroller world). So you could use things like timer interrupts.

      What I would suggest is to use the PWM pins on your Arduino. Search the net for how to use PWM (it is extremely easy, don’t worry). So connect your LEDs to PWM pins, and in your loop, set a different PWM frequency depending on the distance or whatever your requirements may be.

      Look into that, and let me know if you need more help.

      Learning how to use PWM will also be really helpful to control things like LED brightness, motor speeds, etc.

      Cheers!

  11. Ben Moss says:

    Hi, thanks for this tutorial. I’m getting my electronics club into Arduino now and we have an ambitious project for a robot that does several things based on a distance sensor. One of the things I’m hoping to be able to make work is to have an arm with a STOP sign attached to a servo that has LED lights around it that blink. As you approach the arm comes up and shows the STOP sign. If you come closer the LEDs would blink and as you approach they blink faster. I was thinking about starting with your code as a basis but maybe you have another suggestion? We’re really new to arduino coding, this will be our first attempt at doing anything other than the most basic copy/paste from the sample code. Thanks again!

    • Hiya Ben,

      Apologies for the late reply. Been wrapped up with school and finishing off the semester.

      Your project and club sounds great! The Arduino is a great way to get your feet wet in robotics and simple control systems. It bridges the electro-mechanical gap quite well. Eventually you will realize that you do not need the Arduino in the form you see it, and will switch over to standalone micrcontrollers/processors. Same thing as the Arduino, but without all the extra paraphernalia.

      Anyhow, specifically about your project: it seems like a very doable weekend project for your club. You can start with my code, but there really isn’t much to it. I’ll outline some pseudocode for you below:

      Setup:
      Attach your servo
      Attach your LED pins (pinMode as Output)
      Loop:
      Continuously read the analog value from the distance sensor (you can use my code for this). As soon as you detect the distance you want, write the position to the servo which lifts the sign. Create a small function which takes the distance as the input, and a value representing the rate at which you would like the LEDs to flicker at as a function of the distance. Simply create a delay that long for your LEDs to flicker (maybe use a counter).

      If you need any more detailed pseudocode, let me know.

      My best advice would be to do this step by step. So open the examples that build up your code (my code above, the example Blink code, and the example Servo code). Mix and match elements from each to achieve what you want. So instead of flashing LEDs the way you see in my video above, replace or add to that part of the code a command to change the position of a servo as well. Simple.

      If you need any assistance, please do let me know!

      Thanks Ben. I’d love to see what your club comes up with, so please share if you can :)

      • Ben Moss says:

        We went through and made your project as-is last night and talked about how to expand on it. Good progress! Thanks for your response and I will post when we’ve got something to share. Best, Ben

      • Awesome! Great first step. Looking forward to seeing what you all come up with!

  12. Robotics says:

    It could be much better if you expand the tutorial in order to build robotics applications

  13. Thursday Club says:

    Hi i want to retrieve the data from this sensor and have it posted in a data web server (i want to pass the data if it detects something and show it in a website). Do you have any idea on how I do it? Thanks in advance!

    • Treehouse Projects says:

      Hey,

      Shouldn’t be too hard. So what I would do is either get a module or build one yourself which allows you to connect your microcontroller to the internet. Then it’s just a matter of sending requests to your server with your data, tonnes of libraries out there which can do that.

      The other option (which may be easier, no need for extra hardware) is connecting your microcontroller to your computer over, say, a serial connection just like you normally do. Write a simple application on your PC which connects to a database (maybe a SQL databse) or writes to a file on your server. You can write this code in any language, all you have to do is read from the serial port of your computer to get your sensor’s values which you are reading and transmitting over serial.

      On the server side, like I said you can set up a SQL database, or you can just write to a file, say over FTP. If you don’t have your own server, you could either run your own simple stack on a local machine, purchase some server space (pretty cheap, some places even free), or use something like Dropbox’s API to write to a file in your Dropbox folder which can then be read from anywhere.

      See my post about the internet based temperature sensor, I kind of did what you want and the code is there too. In retrospect, I would have done things much more simply, but I learned a lot during that process.

      Let me know if you need more help!

      • Thursday Club says:

        Im lost at your internet based temperature :( do you have any other guide? or could you guide us? Like some pointers if it wouldn’t be too much. I have the ultrasonic sensor working and a Ethernet Module ENC28J60 but I don’t know what to do. Thanks!

  14. Thursday Club says:

    Hi again, do you have any link that might help us? As of now I have Ethernet Module ENC28J60 but then I don’t have the guide on how to do it. Oh first Im not an ECE student Im a software engineering student so I have a little background on how to do it.
    Thanks in advance TREEHOUSE! :)

  15. Satoro DK says:

    Hi, I’m an Indonesian and not proficient in English, so please understand my writing ability :)

    I’m studying physics and concentrated in subject of instrumentational physics. By using two ultrasonic sensors (HC-SR04) and Arduino Uno R3 board, I want to make an instrument that will work as a timer which can count the lapse of time needed by an object to pass through a given distance. The counted time will be displayed with a 4-digit seven-segment to give a precision in milisecond.

    Since I have insufficient knowledge in programming the Arduino, so I ask your help to solve this project.

    Here is my project:
    Suppose that the 2 sensors are Sensor-A and Sensor-B and placed in different positions. When the Object pass the Sensor-A, the sensor will detect it and starts the time counting. And when the Object pass the Sensor-B, the time counting will stop.

    I hope you can help me to apply the concept above. Any advice from you would be very beneficial for me. Please respond as soon as possible. Thank you :)

    • Treehouse Projects says:

      Sounds like a really cool project! How much time do you expect to pass (roughly) between the two sensors being triggered? This may be of help to you: http://playground.arduino.cc/Code/Time

      • Satoro DK says:

        Hei, thanks for your response. Actually, I’ve found some problem while I’m trying to attach the two sensors to the Arduino’s pins. I’m so confused with its coding. Can you give me some coding modification for my project?

  16. Tony says:

    Hi. Hope you can help…I’m a beginner too. I copied your code, and it works fine on my Mega ADK as long as the USB cord is plugged in, but as soon as I use the external DC power supply (5v) without the USB connected, the code no longer works…only one LED stays on continuously. Thank.

    • Treehouse Projects says:

      I haven’t used Arduino boards in a while now (I use other microcontrollers), but if I recall correctly, you need between 9V – 12V for your DC adapter. The on board voltage regulator to regulate a DC power supply won’t work properly if you use a 5V adapter. Let me know if that works.

  17. Robofest says:

    Nice job thanks bye.

  18. can i use arduino uno for this project??

  19. i finish it and it work perfectly!!!


Leave a Reply to Treehouse Projects Cancel reply

Your email address will not be published. Required fields are marked *

4 Pings/Trackbacks for "HC-SR04 Ultrasonic Ping Sensor Arduino Tutorial"
  1. [...] Ultrasonic Ping Sensor Arduino Tutorial Posted on June 27, 2012 by Treehouse Projects — 13 Comments [...]

  2. [...] HC-SR04 Ultrasonic Ping Sensor Arduino Tutorial – if you don’t work until now with HC-SR04 ultrasonic sensor, this guide teach you hot to interface the sensor with Arduino board and run a test; [...]

  3. [...] Todo sobre sensores de ultrasonidos My Robot Lab(ENG): Use HC SR04 sensors Three House Projects: HC-SR04 Ultrasonic Tutorial Instructables(ENG): Simple Arduino and HC-SR04 example Instructables (ENG): Ultrasonic distance [...]