Ultrasonic Distance Sensor - HC-SR04

This is the HC-SR04 ultrasonic distance sensor. This economical sensor provides 2cm to 400cm of non-contact measurement functionality with a ranging accuracy that can reach up to 3mm. Each HC-SR04 module includes an ultrasonic transmitter, a receiver and a control circuit.

There are only four pins that you need to worry about on the HC-SR04: VCC (Power), Trig (Trigger), Echo (Receive), and GND (Ground). You will find this sensor very easy to set up and use for your next range-finding project!

  • Operating Voltage: 5V DC
  • Operating Current: 15mA
  • Measure Angle: 15°
  • Ranging Distance: 2cm - 4m

Ultrasonic Distance Sensor - HC-SR04 Product Help and Resources

Light-Seeking Robot

November 28, 2017

We use parts from the SparkFun Inventor's Kit v4.0 to create a light-seeking robot that mimics the behavior of single-celled organisms.

Single Supply Logic Level Converter Hookup Guide

August 9, 2018

The Single Supply Logic Converter allows you to bi-directionally translate signals from a 5V or 3.3V microcontroller without the need for a second power supply! The board provides an output for both 5V and 3.3V to power your sensors. It is equipped with a PTH resistor footprint for the option to adjust the voltage regulator on the low side of the TXB0104 for 2.5V or 1.8V devices.

Red Box Robot Hookup Guide

November 23, 2016

Turn an iconic SparkFun red box into an obstacle-avoiding robot.

SparkFun Inventor's Kit Experiment Guide - v4.0

November 15, 2017

The SparkFun Inventor's Kit (SIK) Experiment Guide contains all of the information needed to build all five projects, encompassing 16 circuits, in the latest version of the kit, v4.0a.

Resources & Going Further

For more project ideas about the ultrasonic sensor? Check out the following blog posts:


Acoustic Interference

Just like any ultrasonic sensor, this product can be sensitive to other ultrasonic sensors and acoustic noise in the area. If your readings are jumping down to 0 inches even though you have an object placed a few inches away from the sensor, it could be due to interference caused by other ultrasonic sensors and your environment. If you have more than one ultrasonic sensor, try to turn each one on individually after a certain amount of time. If there is a lot of noise and and an HVAC in your area, it's possible that it is tuned just enough where it interferes with the HC-SR04 ultrasonic range finder.

Doing a quick search online, there is indicating that acoustic noise can cause problems with an ultrasonic sensor. Further research indicates that an HVAC can cause interference for 25-40kHz ultrasonic sensors [pg 10 - http://www.lrc.rpi.edu/programs/NLPIP/PDF/VIEW/SROS2.pdf ]:

"For the open-office testing, the room’s HVAC system was turned off for testing the ultrasonic occupancy sensors since these products can be affected by the HVAC system..."

The HVAC used in our custom made building was actually tuned just enough to interfere with the sensor's reading in certain parts of our building. We had issues getting it to work reliably in our education department wherever we heard the acoustic noise.


Core Skill: Programming

If a board needs code or communicates somehow, you're going to need to know how to program or interface with it. The programming skill is all about communication and code.

2 Programming

Skill Level: Rookie - You will need a better fundamental understand of what code is, and how it works. You will be using beginner-level software and development tools like Arduino. You will be dealing directly with code, but numerous examples and libraries are available. Sensors or shields will communicate with serial or TTL.
See all skill levels


Core Skill: Electrical Prototyping

If it requires power, you need to know how much, what all the pins do, and how to hook it up. You may need to reference datasheets, schematics, and know the ins and outs of electronics.

2 Electrical Prototyping

Skill Level: Rookie - You may be required to know a bit more about the component, such as orientation, or how to hook it up, in addition to power requirements. You will need to understand polarized components.
See all skill levels


Comments

Looking for answers to technical questions?

We welcome your comments and suggestions below. However, if you are looking for solutions to technical questions please see our Technical Assistance page.

  • Member #484945 / about 8 years ago * / 4

    Pro tip: you can tie the trigger and receive pins together and save yourself a pin. They're never active at the same time. The receiver is locked out for a very brief period after the pulse is sent (if I had to guess, I'd say about how long sound takes to travel 2cm and back.) Search for SRF-04 if you're trying to find a good datasheet.

  • Member #187451 / about 8 years ago * / 2

    I haven't used this particular version before, but I've used a similar version and it works ok. A capacitor across VCC-GND did wonders stabilizing readings (1uF - 3.3uF). Not sure how much range you will get without the MAX232 (that was used as a voltage stepper which "rang" the ultrasonic speaker at +12/-12). The PING is significantly more robust, but it is more expensive no doubt.

    Following is some useful information. Hopefully it helps someone.

  • Valen / about 8 years ago / 2

    This one seems significantly different from other HC-SR04 modules. It seems to lack the 3rd chip, a MAX232-like chip (TTL to RS232 chargepump converter) to boost the transmitter voltage. Not sure if this means less performance though, since I've read that it could also be the source of internal ringing noise in the detection circuit. Could be interesting to test and compare.

  • Member #360077 / about 8 years ago / 2

    This device operates with an audio signal at 40KHz. That is within the hearing range of cats and dogs. I would never use this in a project that would operate in or around my home.

    • 172pilot / about 8 years ago / 1

      So.. that's interesting, because I was actually coming here to post a question about it's ability to DETECT a cat.. The video says it works best with a flat surface (of course) but if I just need to detect the existance of a fluffy cat getting ready to jump up on a birdcage, would it work... OR, would the 40khz be annoying enough to keep the cat away, and/or should I NOT do it because it will annoy/hurt the canary?!

      • Member #501584 / about 7 years ago / 5

        We have a cat that would sit outside our bedroom door and cry incessantly for food promptly at 4:00 am. This was obviously driving us insane. In a sleep deprived stupor, I purchased a few items from Sparkfun (Ultrasonic sensor / 16x2 LCD / Voltage regulator / Power supply) and acquired a Particle Photon, 10V solenoid, a toilet flange from Home Depot and an air can. I can say that this sensor will most definitely detect cats. When triggered by the sensor, the solenoid pumps the air can for a quick second of PSSSSSSSTTT!!! followed by sweet silence. Thank you Sparkfun for a restful sleep!

      • Member #5515 / about 8 years ago * / 1

        I have not used this particular sensor, but, based on my past experience with similar ultrasonic sensors, you will not detect a fluffy cat with it. (The sensors I have used before even struggled to detect clothed humans...naked humans (or shaved cats?) do seem to be reflective enough. YMMV).

        Of course, if you can shoot at a wall or other hard surface in a no-cat state and then look for a major change in that reading as the cat absorbs the sound, you might be in business...

    • Chiel / about 8 years ago / 1

      Many sonar sensors use 40khz as it's working frequency and yeah that is in within hearing range of animals, But the pulses created by sonar sensors are very short and not at all powerfull. It is not harmfull and none of my cats have ever responded even remotely to any of my sonar sensors, not even a twitch in the ear.

  • Some interesting piece of information for people regarding this sensor (source), practical measurement of the sensor characteristics:

    • FOV (full cone): horizontal ~20º, vertical ~13º
    • Spatial resolution (full cone): ~0.6-1.4º
    • Range: tested from 5 to 200 cm
    • Accuracy: relative error ~0/5% (-1.3 ±4.6 %); absolute error ~-0.5/-1.5 cm (-0.4 ±1.2 cm).
    • Precision: standard deviation ~0.1/0.5 cm (0.3 ±0.6 cm)

    Hope this helps, especially having a sense of the accuracy of the sensor.

  • Member #1500382 / about 5 years ago / 1

    These sensors are horrible! I ordered 12 Inventor's Kits for my classroom and half of the sensors are bad. This makes it really difficult to teach classes when students need to share parts. To make it worse, Sparkfun customer support has been completely unresponsive when I contact them. I will not be purchasing anything else from them, which is unfortunate because they offer some really great products.

    • M-Short / about 5 years ago / 1

      I'm sorry about that. Please double check your spam folder (sometimes emails responses from techsupport end up there). They usually should get back to you in a day or 2, but I'll make sure they got your email.

  • Member #139104 / about 6 years ago / 1

    I discovered this particular version to be totally useless within a new school building where it was to be used. It it terribly susceptible to electrical noise in a modern building. I eventually bought three thinking I had a dud after buying the first one. The device worked perfectly at home where I developed at least four different physical designs and countless software tricks trying to discover and mitigate the problem. In the final attempt the sensor and Arduino controlling it were next to each other all within a metal enclosure.

    I rolled the device in its finished form on a cart through the new building to observe its behavior. In one remote egress stairwell the device worked well enough to possibly be functional for its intended purpose, which was to be in a classroom situation. The only location where it worked perfectly just like at home was within an elevator cab with the door closed. Oddly, there was one particular location in a hallway where it functioned to some degree better than the nonsense it showed in most locations. Everywhere else it would read either very low distances, i.e. the echo signal is coming back too soon, or there would be random zinger interspersed with a very rare correct distance reading.

    Keep in mind the unit worked perfectly at home and in the elevator cab. The project design included capacitor filters mounted close to the device. The capacitors made a performance improvement at home.

    Needless to say I am terribly disappointed. I am guessing the electrical noise is from all the HVAC motor circuits on frequency drive controllers.

  • Member #680509 / about 6 years ago / 1

    Will I be able to measure liquid levels with this?

  • Member #763187 / about 6 years ago / 1

    At first I could not get this device to work -- the SparkFun Inventor's kit sample code always came back as 0.11 inches no matter was in front of it. I contacted customer support and they asked if I was in an office setting with fluorescent lights and/or HVAC nearby. I had both overhead. I took everything to another room without LED lights and no HVAC, and the device worked perfectly. Apparently, things can interfere with the ultrasonic sensing.

  • Jorge P. / about 6 years ago / 1

    Code for Parallax Propeller is on the Parallax Forums use at least a 1K resistor between the propeller (3.3V) and the Echo Pin(5v). Measurements are accurate from the surface of the PCB! My code can be cleaned up a bit, but it works great. Sparkfun can use the code to make available on GitHub at their discretion.

  • Member #909211 / about 6 years ago / 1

    Would this board be suitable for measuring speed of a 3.5cm (1-3/8") ball coming at it? Working range would need to be from 0 to ~15-20cm (6-8") and an horizonal angle of about 90* and same vertical plane.

  • Jordan Tallent / about 6 years ago / 1

    What size are the mounting holes?

  • Member #1148635 / about 7 years ago / 1

    Hello, I have this Ultrasonic Sensor - HC-SR04 and SparkFun RedBot Mainboard. How does the sensor assembled?

    Thanks

    Andrea

  • Member #959091 / about 7 years ago / 1

    Can this detect the distance to a surface of water? Thinking of mounting it above a fish tank to measure the water level.

    • Member #845958 / about 7 years ago / 1

      Measuring tanks, grain silos and the like is a major use of ultrasonic sensors. The ones used in industry cost about $30 (just for the transducer) and have longer range but this hobby grade device would work for smaller tank. these have a wide angle of vie and might detect the tank rim. They trigger on the closest object. Somount it low and near the tank's center might

  • Member #149813 / about 7 years ago / 1

    possible to generally detect rocks couple inches under the ground?

  • OHoilett / about 7 years ago / 1

    Not surprised these things sold out so soon. They're incredibly popular and useful. Good job SparkFun on making these available in your store.

  • Member #829327 / about 8 years ago / 1

    Works well, but is inaccurate below 20 cm and on non-flat surfaces (such as hands), if powered with 3V3. I suggest to take the average of several values to get a more precise measurement The example code didn't work on a teensy, as the echo pin is not explicitly declared as input. Hope this may help someone else

  • Member #367420 / about 8 years ago / 1

    FYI, at 5v I get garbage readings and lots of OUT OF RANGE from the sample code, but at 3.3v all is smooth and accurate.

  • tpobrienjr / about 8 years ago / 1

    I just read over the datasheet. Somebody please get the maker to correct the errors! Lots of technical errors and even more language errors. That said, this is sure cheap - hope it works.

  • Member #367806 / about 8 years ago / 1

    Sort of the opposite of member #114323's comment - I noticed this is about 1/10 the price of the Parallax PING Ultrasonic sensor which I usually see retail for around $30 (e.g. Jameco Electronics p/n 282861). Does Parallax just have an insane markup, or is there some quality/accuracy difference that explains the huge price gap?

    • Chiel / about 8 years ago / 1

      Sonar sensors have gotten pretty cheap over the past decade. I remember each costing atleast 15$ as a minimum and the Ping))) costing 30$ back then aswell. Now you can get sensors for a few bucks a piece. So i would guess that the Ping is just a older more expensive sensor.

      To be fair tough looking at the back of the ping it is a little more complicated in design and probably a little bit more complicated. The SR04 is pretty simplistic. can't say for certain tough.

    • bfesser / about 8 years ago / 0

      Parallax makes (or made?) huge profits by selling to schools. Hence, product names like "Board of Education". They don't really focus on individual customers (makers). Because of this, their prices have always been ridiculously high. I remember buying a BS2 rev. B from them back in the day for $50! I don't know if Arduino existed yet or if I just wasn't aware of it.

      • Hi, I work at Parallax and yes, we do sell the PING))) for much more than the HC-SR04. Quite a bit is happening in the micro on the PING))), allowing it to work in noisier environments more accurately with high resolution.

        As much as I wish we did, we don't make huge profits - We cater to schools, and schools need durable, reliable parts and in-depth, thorough documentation and support. All of our revenue gets reinvested in making open-source curriculum materials for schools and R&D on future products that support maker education. Hobbyists are comfortable digging around the web to find what they need to get a project of the ground, but students and teachers don't usually have the patience or the time, so we work hard to get everything they need in one place and as user-friendly as possible.

        I know Sparkfun is working on supporting maker education too, and I know that when you purchase from Sparkfun, Adafruit, and Parallax, you're supporting American manufacturing, jobs, and the work we all do with and for schools.

        • Member #763187 / about 6 years ago / 2

          I had a project where I really needed a reliable sensor, and I found that the HC-SR04 had too many bad readings. I switched to the PING))) sensor, which I agree was significantly more expensive, but it worked consistently. I never once had a problem with it.

        • bfesser / about 8 years ago / 0

          Thank you for that explanation, Matix. I stand corrected. :)

  • Alwyn Smith / about 8 years ago / 1

    I'm sincerely curious about how business for this module (and similar components) works. You can get one of these on ebay for a dollar, if you need one. And you can get 10 of them for about 78 cents each. They work great.

    I've got faith in the quality of the design and manufacturing of SparkFun's products, but this costs about 4 times as much. Why go through the effort of offering this, when you can't compete on price?

    And lest anyone think I'm trolling or being rude, I mean no disrespect. I've got a side-hustle as a freelance electrical engineer doing embedded systems and PCB layouts and would like to learn more about how U.S. & European businesses can compete with cheap Chinese manufacturing in this space.

    • Jess2 / about 8 years ago / 3

      I understand and agree what you are saying about the price disparity to a degree; with that being said much of the time the quality of the 78 cent sensor can certainly be suspect. I ordered 2 last month, both came in crushed after waiting 3 weeks so I can't comment on the quality of those. One of my friends gave me one purchased from amazon for a couple dollars last weekend to play with, turns out it was DOA (the receiver side operated intermittently.) I ordered 5 more early this week from amazon with the hopes that at least a couple will turn out problem free. If I would have been able to purchase one from Sparkfun originally, there would have been a working distance sensor on my quad 3 weeks ago; also I would have enjoyed the added benefit of less lost hair. To summarize, I have found Sparkfuns quality to far exceed the quality of their cheap Chinese competitors on more than one occasion; if you have a pressing project in mind you just want to work, it's certainly (to me at least) worth the few extra bucks. If you just want to play with a sensor and don't mind the wait or potential problems, get the 88 cent one; in fact there is valuable learning experience available troubleshooting suspect parts. Please don't take this the wrong way though, there are plenty of perfectly functional, high value parts available on the extreme cheap that certainly represent an excellent value! Who knows, if the original box didn't come in crushed, I may have been able to crush them on my quad-copter and would be saying "Why would I pay 5x as much for a sensor I'm just going to destroy in a rough landing anyway."

    • Member #623105 / about 8 years ago / 2

      I have young kids in my house, so I try to buy lead-free whenever possible. I love that SparkFun marks so many components as RoHS (and I'm bummed that Adafruit has been doing it less).

      The cheap sensors and Arduino knock-offs you get on eBay are never lead-free, as far as I can tell. It's worth the extra few bucks for me.

    • kg6hxm / about 8 years ago / 2

      Sparkfun is relying on the community and support they offer to keep you coming back for more parts and assistance. Sure you can buy 10 of them for super cheap on Ebay, but that comes with no support. Sparkfun has guides and people to help you learn and when you have issues. Very few places have that.

      For me, they are just down the road, so I get my parts reliably, and much quicker than I would with Ebay.

      • Sparkfun support of their products is great, no doubt. But this is not a Sparkfun product. There's no schematic and the datasheet is from an eBay version of the HC-SR04. I'm guessing this is not a Sparkfun engineered or manufactured product, rather it's a cheap Chinese import with a beefy markup by Sparkfun. Like maybe this one? https://korean.alibaba.com/product-detail/ultrasonic-transmitter-and-receiver-sensor-hc-sr04-40khz-ultrasonic-sensor-60126024326.html

    • 172pilot / about 8 years ago / 1

      For me, it's the reliability and consistent quality, the support site, and often times the speed to get it to me.. I buy my fair share of "cheap stuff" off of the internet when I see something interesting, but dont have a particlar project in mind, but if I know I need something specific, I try to go to sparkfun.

  • Just curious, is there any reason this wasn't designed with copper pours?

Customer Reviews

No reviews yet.