Arduino Ultrasonic Sensor

I have just received a very affordable new toy from Taobao Market Place, the ultrasonic sensor, it is about RMB15 (USD2.50) excluding the shipping cost or you can get it for USD5.88 from Amazon.

It is very small size, 4.5cm x 2.0cm and about 1.5cm thick.

Arduino Ultrasonic Sensor

Arduino Ultrasonic Sensor









This small device comes with 5 pins, start from the left Vcc, Tx, Rx and the last 2 pins are for the ground. Normally it needs 5v operating power supply to the Vcc pin. It working angle is less than 15° with the working distance of 2 – 450cm. This small device is normally use to detect any blocking object, or measure the distance from the nearest blocking object.

How does this small device work? It sends out a burst of ultrasound waveform  and listening for the echo when it bounces off from an object.

Below is a simple code to be use to measure the distance of an blocking object in metric and inches. Simple connect the 5v to the Vcc, Trig/Tx to Digital I/O 5 on the Arduino board, Echo/Rx to Digital I/O 4 on the Arduino board and unsure that you have ground it.

int inputPin=4;  // connect digital I/O 4 to the ECHO/Rx Pin
int outputPin=5; // connect digital I/O 5 to the TRIG/TX Pin

void setup()
  pinMode(inputPin, INPUT);
  pinMode(outputPin, OUTPUT);
void loop()
   digitalWrite(outputPin, LOW);  // send low pulse for 2μs
   digitalWrite(outputPin, HIGH); // send high pulse for 10μs
   digitalWrite(outputPin, LOW);  // back to low pulse
   int distance = pulseIn(inputPin, HIGH);  // read echo value
   int distance1= distance/29/2;  // in cm
   int distance2= distance/74/2;  // in inches

How the formula works? The pulseIn function return you time in μs for the sound wave to travel, hit an object and bounce back. Since sound travel at 340 meter per sec (29μs per cm), we need to divide it by 29, and for a round trip you need to divide them by 2 again.



This entry was posted in Hardware and tagged , . Bookmark the permalink.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>