![]() We need to divide the travel time by 2 because we have to consider that the wave was sent, hit the object, and then returned to the sensor. distance = (traveltime/2) x speed of sound Then, we calculate the distance to an object, taking into account the sound speed. The pulse length corresponds to the time it took to travel to the object plus the time traveled on the way back. It returns the length of the pulse in microseconds. It accepts as arguments the pin and the state of the pulse (either HIGH or LOW). The pulseIn() function reads a HIGH or a LOW pulse on a pin. We use the pulseIn() function to get the sound wave travel time: duration = pulseIn(echoPin, HIGH) ![]() But, before that, give a short LOW pulse to ensure you’ll get a clean HIGH pulse: digitalWrite(trigPin, LOW) In the loop(), trigger the sensor by sending a HIGH pulse of 10 microseconds. In the setup(), initialize the serial port at a baud rate of 9600, and set the trigger pin as an OUTPUT and the echo pin as an INPUT. The cm variable will save the distance in centimeters, and the inches variable will save the distance in inches. The duration variable saves the time between the emission and reception of the signal. You also create three variables of type long: duration and inches. The trigger pin is connected to digital Pin 11, and the echo pin is connected to Pin 12: int trigPin = 11 Inches = (duration/2) / 74 // Divide by 74 or multiply by 0.0135įirst, you create variables for the trigger and echo pin called trigPin and echoPin, respectively. of the ping to the reception of its echo off of an object.Ĭm = (duration/2) / 29.1 // Divide by 29.1 or multiply by 0.0343 ![]() ![]() duration is the time (in microseconds) from the sending Read the signal from the sensor: a HIGH pulse whose Give a short LOW pulse beforehand to ensure a clean HIGH pulse: The sensor is triggered by a HIGH pulse of 10 or more microseconds. * Complete Guide for Ultrasonic Sensor HC-SR04 Upload the following code to your Arduino IDE. The following table shows the connections you need to make: Ultrasonic Sensor HC-SR04 ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |