![]() ![]() To view the period of each pulse, open the Serial Monitor (Ctrl+Shift+M). The period is the amount of time between the sensor sending out and receiving a pulse. ![]() Another term for this duration is the period of each pulse. Look through the following code labelled ultrasonic_callibration.ino to see how to print the duration of your ultrasonic sensor's pulses. Otherwise, you may see some missing spaces. Serial.Note: Due to a bug in Instructables, please click on the external link to the code in order to copy it correctly. ![]() Within the setup function, all we need to do is initialize it for serial communication: HCSR04 hc(trig, echo) //HCSR04 Initialization (trig pin, echo pin) Here we initialize the class of the ultrasonic sensor module and as you can see, we add the pins that it is going to use: Int i //Random variable used to control the loop Int tankFinal //Final percentage value for how full the tank isįloat allDist //The sum of all the distances in allDistįloat avgDist //The average of all the distances in allDist This section is split into two parts: pin configuration constants and the variables associated with storing the data:įloat distBuff //Buffer for the raw distance taken by the sensor For this we only need one: #include “HCSR04.h”Īfter this, we must declare all the variables and constants that we are going to use. Regarding the code, the first thing to consider is the libraries that must be included. The Arduino then waits for an incoming pulse from the module and uses the time it took to get the distance.īut you don’t have to think about this since the library you’re going to use includes everything needed. The HC-SR04 module needs a 10 µs pulse from the Arduino to trigger the sound wave being emitted from the module. Since the signal first traveled to the object, then back, you can get the distance to the object by dividing by 2. So by multiplying this with the time the signal took to return, you get the distance that the signal traveled. The sensor uses a microphone to detect the echo. When the sound hits an object, this creates an echo that bounces back to the sensor. The basic principle of an ultrasonic sensor is pretty basic: It sends out a sound wave from its speaker. ![]() So to follow these instructions, your board must at least be compatible with that. Keep in mind that, in general, any kind of microcontroller should be able to run this type of project, but we’ll specifically be working with Arduino IDE. HCSR04 ultrasonic sensor lib by gamegine.To build this project you’ll need the following components You’ll understand this part better when I get into the workings of it later on. Secondly, the module makes the functionality of the sensor relatively simple. Firstly, connecting it is very straightforward as it only requires direct connections between the module and microcontroller. There are two things that make this type of sensor easy to use. And for those looking to get even more into the technicalities of the sensor, the GitHub repositories are open and available to all. This is especially true when talking about an Arduino, as there are a couple of very reliable libraries out there. But the fact is, it’s one of the most accessible and fun accessories for those who like to dabble in microcontrollers. Specifically, you’ll learn how to use the HC-SR04 module with an Arduino to measure the depth of a water tank.Īn ultrasonic sensor is one of those things that some people don’t like getting into just because it sounds complex to use and understand. In this tutorial, you’ll learn how to use an ultrasonic sensor. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |