Especially in today's age, robot and radar applications have developed at a very rapid pace. Thanks to these important developments, it has become much more possible for people to access the data they want and to implement the projects they want. Of course, today's article will also mention the Ultrasonic Distance Sensor that has entered our lives in this process.
What is ultrasonic distance sensor?
Distance sensors have been incorporated into our lives for the purpose of detecting and measuring distances. There are two types of distance sensors, infrared and ultrasonic, which are widely used today. Today, we will process the ultrasonic distance sensor with you by descending further.
When we look at the ultrasonic distance sensor in detail, there are two sections on it. One of these sections is the receiver and the other is the transmitter. The sound wave coming out of the transmitter is reflected back by hitting it as soon as it reaches the object opposite. This reflective sound wave is captured again by the receiver. The distance is calculated by keeping the time elapsed during this process (X=Vxt). In today's article, we will use the HC-SR04 Ultrasonic Distance Sensor. The distance measurement range of this sensor is between 0 Meters and 4 Meters. In other words, it sees a maximum value of 400.
- VCC = 5Volt
- TRIG= Ultrasonic sound dissemination
- ECHO= Holding back the returning ultrasonic sound
- GND= Grounding
In today's application, we will again use our mBlock in interactive mode. The reason is that after placing the code blocks, we will print the values we have received thanks to the ultrasonic distance sensor in our scene and we will examine the value change with you. Of course, what materials do we need for this?
Bill of Materials
- Jumper Cable
As you can see from the section I mentioned in the pin inputs, we have 4 initiatives in our sensor. Two of them are echo and trig pins, where we control the sound wave, and the other two are GND, 5V connectivity, which will provide the grounding and power of our sensor. We make our connections as you can see in our diagram below (Trig-8, Echo-9). Of course you can change the pin values, but in this case you will need to make changes accordingly when writing your code.
- GND= GND
- VCC= 5V
- TRIG= Pin #8
- ECHO= Pin #9
Code Block Structure
Since we will first use it in interactive mode, our code block that we must first drag is the block of clicks on the Green flag. The reason is that our code will be activated and start working as soon as I click on the green flag by the stage. Then, as we know, thanks to our sensor, we have to assign our measured value into a variable in order to show it on our stage. Thanks to this variable, we will see the value measured by the sensor on stage. That's why I'm calling our variable "Distance." The value that the sensor will now calculate will be indicated as distance in the scene. In the aftermath, as always, we drag our block called repeat from the "Control" sequence to ensure the continuous operation of our code block.
As we now know, it occurred in our variable, so how do we transfer the value we get from the sensor into our variable? The way to do this is to drag the code "Distance, get 0" from the "data&block" array into our loop. Then we need to assign another block of code to the field that says "0", so that we can assign the sensor value there. This code block is called "ultrasonic 8 trigger pin 9 reading pin" in the array of "robots". Thanks to this code, we will be able to assign the value we obtained to our variable called Distance. Finally, we drag our code block "Say hello" from the "view" sequence to the hello section, and we drag our variable called Distance in the "data & block" sequence. So we'll see the value we get on stage. Thus, it will continue in the form of a continuous cycle. After we finish all the code, we check our re-entered pins so that it doesn't cause any mistakes. As you can see, I use pins 8 and 9, but you can change it any way you want. We don't just use pins 0 and 1. The reason is that our pins "0 and 1" are used for serial communication purposes.
Our distance is seen in cm in our stage and thanks to our sensor we have used, our value can go up to 400.