Iot Exp2
Iot Exp2
An Ultrasonic Sensor is a device that measures distance to an object using Sound Waves. It
works by sending out a sound wave at ultrasonic frequency and waits for it to bounce back from
the object. Then, the time delay between transmission of sound and receiving of the sound is
used to calculate the distance.
We divide the distance formula by 2 because the sound waves travel a round trip i.e from the
sensor and back to the sensor which doubles the actual distance.
The HC-SR04 is a typical ultrasonic sensor which is used in many projects such as obstacle
detector and electronic distance measurement tapes. In this Instructable I'll teach you how to
interface the HC-SC04 with an Arduino Uno.
To interface an Ultrasonic Sensor with an Arduino and view the distance on the serial monitor
you'll need:
Arduino Uno
HC-SR04 Module
BreadBorad
Jumper wires
You'll need a laptop or a PC to upload code to the Arduino and view the Distance on the Serial
Monitor.
You can send the commands by typing in the window on the top and pressing 'Enter' or clicking
'Send'. The data from the board is displayed below that.
This is very useful when debugging the code, or if you need to give inputs to the board, This is
probably the most useful tool in the IDE. The more you use it, the better you get at testing
complex projects that takes inputs and provides consequent outputs.
Step 4: The Circuit
The connections are as follows:
Vcc to 5V Pin of the Arduino.
Gnd to Gnd Pin of the Arduino.
Trig to Digital Pin 9 .
Echo to Digital Pin 10.
Refer the schematics for more clarity on the connections.
Few things to remember while building the circuit
Avoid placing the sensor on metal surfaces to avoid short circuits which might burn the sensor.
It is recommended to put electrical tape on the back side of the sensor.
You can also directly connect the Ultrasonic sensor to the Arduino with jumper wires directly.
/*
* Ultrasonic Sensor HC-SR04 interfacing with Arduino.
*/
// defining variables
long duration;
int distance;
void setup() {
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
distance= duration*0.034/2;
Serial.print("Distance: ");
Serial.println(distance);