
- #Ultrasonic sensor arduino 4 pin how to
- #Ultrasonic sensor arduino 4 pin code
Int trigPin=13 //Sensor Trigger pin connected to Arduino pin 13 Int trigPin = 13 //Sensor Trigger pin connected to Arduino pin 13 int echoPin = 11 //Sensor Echo pin connected to Arduino pin 11 float pingTime //time for ping to travel from sensor to target and return float distance //Distance to Target in inches float speedOfSound = 776.5 //Speed of sound in miles per hour void setup ( ) Now, we need to calculate the distance using a formula: Distance = Speed X Time.Here, you know the distance, you will get traveling time from the sensor, now you need to use a formula to calculate the speed. The speed of ultrasonic wave at room temperature is approximately 776.5 miles per hour (or you can calculate exact speed of waves by keeping the object at a known distance.We will get the time required for ultrasonic waves to fly back from the targeted object by reading the Echo pin of the ultrasonic sensor.
#Ultrasonic sensor arduino 4 pin code
Write code to measure the distance of targeted object: Look at the above figure 3 and wiring details to interface ultrasonic sensor with Arduino Uno.
Connect GND pin of a sensor to GND pin of Arduino. Connect Vcc pin of a sensor to Vcc pin of Arduino. Connect Echo pin of a sensor to pin 11 of Arduino. Connect Trigger pin of a sensor to pin 13 of Arduino. Interfacing of Ultrasonic Sensor with Arduino UNO: Fig.3 Interfacing of Ultrasonic Sensor with Arduino Uno
GND – This pin is connected to the Ground of the system. This pin goes high for a period of time which will be equal to the time taken for the ultrasonic wave to return back to the sensor. To initialize the measurement by sending ultrasonic sound waves, you need to provide a high signal for 10 microseconds on this pin. Vcc – Vcc pin powers the sensor with +5V. To trigger the ultrasonic sensor, you need to generate a HIGH pulse for at least 10 microseconds on the trigger pin. So, from the width of the echo pulse, we can calculate the traveling time of transmitted and reflected wave. When sensor gets a trigger, it transmits the waves and set the echo pin to HIGH and the status of the Echo pin will be HIGH until it will not receive the reflected wave. Note: Here, you need to divide the time/distance by 2 as the calculated time is the time required for round trip.įor the ultrasonic sensor, when we send a trigger pulse/signal on trigger pin, a sensor sends waves towards a targeted object and we start looking at Echo pin to get the response of reflected wave. So, we can calculate the distance of an object using the standard distance formula. And the speed of the sound wave in the same medium is constant. So, here SONAR system calculates the time required for the sound wave to travel from a transmitter to object and from object to a receiver.
The reflected sound wave travels back towards receiver and receiver will receive the waves. Sound waves travel from a transmitter toward an object and reflect back from an object. In SONAR, the transmitter (Tx) sends a sound wave towards the targeted object. We bought for our engineers this Arduino super-starter kit. Note: You can get these components fairly easily at many places (including online). This sensor works based on the working principle of SONAR ( SOund Navigation And Ranging) and you can look at the datasheet here for more specifications.
Ultrasonic sensors are very helpful in detecting objects and measuring the distance of an object.
Any Object as a target (Book, mobile, human hand, etc)įor this tutorial, we are using HC-SR04 ultrasonic sensor which has 4 pins as shown in the above figure 1. So, if you are the kind that learns by doing, follow along! We are presenting some of the most commonly used hardware components along with some basics of electronics theory. This is the sixth tutorial of our ‘Arduino tutorials for testers’ series. #Ultrasonic sensor arduino 4 pin how to
In this tutorial, you will learn about how to interface ultrasonic sensor with Arduino and how to measure the distance of an object using ultrasonic sensor.