Using a Distant Sensor

Resource found from:

– – – – – – –

HC-SR04 Ultrasonic Ping Sensor Arduino Tutorial

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.


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.


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

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s