Arduino Ultrasonic Sensor

I have just received a very affordable fun toy from China AliExpress Market Place, the ultrasonic sensor, it is about RMB15 (USD2.50) excluding the shipping cost or you can get this Ultrasonic Sensor for a very good price 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.



Sponser Links


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()
{
  Serial.begin(9600);
  pinMode(inputPin, INPUT);
  pinMode(outputPin, OUTPUT);
}
void loop()
{
   digitalWrite(outputPin, LOW);  // send low pulse for 2μs
   delayMicroseconds(2);
   digitalWrite(outputPin, HIGH); // send high pulse for 10μs
   delayMicroseconds(10);
   digitalWrite(outputPin, LOW);  // back to low pulse
   int distance = pulseIn(inputPin, HIGH);  // read echo value
   int distance1= distance/29/2;  // in cm
   Serial.println(distance1); 
   int distance2= distance/74/2;  // in inches
   Serial.println(distance2);
   delay(50);  
}

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.

< BACK

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 *

This site uses Akismet to reduce spam. Learn how your comment data is processed.