The trigger pin is connected to digital pin 2 and the echo pin to digital pin 3 on the Arduino. Print the distance on the Serial Monitor (Ctrl+Shift+M):įirst, the trigger pin and the echo pin are defined. Read the echoPin, pulseIn() returns the duration (length of the pulse) in microseconds: Trigger the sensor by setting the trigPin high for 10 microseconds: Begin Serial communication at a baudrate of 9600: * Example code for HC-SR04 ultrasonic distance sensor with Arduino. Next, I will explain to you how the code works. You can upload the following example code to your Arduino using the Arduino IDE. Now that you have wired up the sensor it is time to connect the Arduino to the computer and upload some code. The code examples below use digital pin 2 and 3 for the trigger and echo pin respectively, but of course you can change this to any digital pin you want. HC-SR04 Specifications Operating voltageįor more information you can check out the datasheet below: After that, you can use the formula mentioned above to calculate the distance between the sensor and the object. You can use the pulseIn() function in the Arduino code to read the length of the pulse from the Echo pin. The Echo pin then outputs the time that the sound waves traveled in microseconds. This sonic burst travels at the speed of sound and bounces back and gets received by the receiver of the sensor. The sensor then creates an 8 cycle burst of ultrasound at 40 kHz. To let the sensor generate a sonic burst, you need to set the Trig pin high for at least 10 µs. This formula doesn’t include the humidity since its effect on the speed of sound is only very small.īelow you can find a tutorial on how to use a DHT11 temperature and humidity sensor to calibrate the speed of sound and get a more accurate distance reading with the HC-SR04.Īt the front of the HC-SR04 sensor, you can find two silver cylinders (ultrasonic transducers), one is the transmitter of the sound waves and the other is the receiver. For most cases at 20☌ you can just use 343 m/s but if you want to get more accurate readings, you can calculate the speed of sound with the following formula: Wikipedia states that the speed of sound increases with roughly 0.6 m/s per degree Celsius. The speed of sound actually depends strongly on temperature and to a far lesser degree on the humidity of the air. Temperature dependence of the speed of sound So you need to divide the result by two.ĭistance (cm) = Speed of sound (cm/µs) × Time (µs) / 2ĭistance (cm) = 0.0343 (cm/µs) × 2000 (µs) / 2 = 34.3 cm The distance between the sensor and the object is actually only half this distance because the sound waves traveled from the sensor to the object and back from the object to the sensor. If you multiply the speed of sound by the time the sound waves traveled, you get the distance that the sound waves traveled.īut that is not the result we are looking for. Let’s say that the time between sending and receiving the sound waves is 2000 microseconds. Source: Īt 20☌, the speed of sound is roughly 343 m/s or 0.034 cm/µs. Ultrasonic distance sensors working principle. By measuring how much time passed between sending and receiving the sound waves, the distance between the sensor and the object can be calculated. If there is an object in front of the sensor, the sound waves get reflected back and the receiver of the ultrasonic sensor detects them. These sound waves travel through the air with the speed of sound, roughly 343 m/s. Ultrasonic sensors work by emitting sound waves with a frequency that is too high for a human to hear. How does an ultrasonic distance sensor work? As an Amazon Associate we earn from qualifying purchases. is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to products on. Other Useful Links From Around The Web:.Example code HC-SR04 with DHT11 and I2C LCD.Example code HC-SR04 with DHT11 temperature sensor and Arduino.Example code HC-SR04 with I2C LCD and Arduino.How to use ping_median() digital filter.Interfacing ultrasonic sensors in 3 pin mode.Example code HC-SR04 with Arduino and NewPing library. Wiring – Connecting HC-SR04 to Arduino UNO.Temperature dependence of the speed of sound.How does an ultrasonic distance sensor work?.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |