1

Your cart is empty.

Future Electronics Egypt

08 Aug '20

Ultrasonic Sensor with Arduino Uno Tutorial

Posted by Saleh Osman in Arduino UNO, Ultrasonic

Overview

The ultrasonic sensor works on the principle of SONAR and RADAR system which is used to determine the distance to an object.
An ultrasonic sensor generates the high-frequency sound (ultrasound) waves. When this ultrasound hits the object, it reflects as echo which is sensed by the receiver as shown in below figure.

By measuring the time required for the echo to reach to the receiver, we can calculate the distance. This is the basic working principle of Ultrasonic module to measure distance.

In this Tutorial we will learn how the Ultrasonic Sensor works and how to use it with the Arduino Board.

---------------

Components needed for this

 

---------------

Wiring – Connecting Ultrasonic Sensor Module to Arduino Uno.

Connecting the ultrasonic sensor to the Arduino is pretty easy. Start by placing the sensor on to your breadboard.
Connect VCC pin to the 5V pin on the Arduino and connect GND pin to the Ground pin on the Arduino.
Connect Trig pin to the pin 11 on the Arduino.
Connect Echo pin to the pin 12 on the Arduino.

When you’re done you should have something that looks similar to the illustration shown below.

---------------

Sketch

Open the Arduino IDE software on your computer.
Open a new sketch File by clicking New.

 

Arduino Code

Upload the following sketch to your Arduino.

/*

* Ultrasonic Sensor HC-SR04 and Arduino Tutorial

*

* by Future Electronics Egypt

* www.fut-electronics.com

*

*/

const int pingPin = 11; // Trigger Pin of Ultrasonic Sensor

const int echoPin = 12; // Echo Pin of Ultrasonic Sensor



void setup() {

   Serial.begin(9600); // Starting Serial Terminal

}



void loop() {

   long duration, inches, cm;

   pinMode(pingPin, OUTPUT);

   digitalWrite(pingPin, LOW);

   delayMicroseconds(2);

   digitalWrite(pingPin, HIGH);

   delayMicroseconds(10);

   digitalWrite(pingPin, LOW);

   pinMode(echoPin, INPUT);

   duration = pulseIn(echoPin, HIGH);

   cm = microsecondsToCentimeters(duration);

   Serial.print(cm);

   Serial.print("cm");

   Serial.println();

   delay(100);

}

long microsecondsToCentimeters(long microseconds) {

   return microseconds / 29 / 2;

}

​

 

After you have uploaded this sketch onto your Arduino, click on the right-most button on the toolbar in the Arduino IDE.

 

---------------

Result

Put any object front the sensor and you will see the distance measured by sensor in cm on Arduino serial monitor.

Time to start experimenting!

 

 

 




Sale

Unavailable

Sold Out