this is the ultrasonic sensor that we use for Arduino this is one of the most famous component
that is used for measuring the distance and it's being used in cars to avoid obstacle it's being
used a robotic arm these are two speaker type modules so this is a transmitter that acts as
a speaker and it transmits ultrasound ultrasonic mean this is above the hearing frequency of human
uh which is above 25 kilohertz so this will be around 40 kilohertz so this transmits and this
piece acts like a receiver they put r here and it receives it here is how it operates I’ve drawn
this diagram to illustrate it this is the ultrasonic sensor and this is a transmitter
usually shown with TX this since ultrasonic wave and when it goes and hits for example this object
this is a wall it hits and returns or bounces back the signal that travels from here to
the wall and coming back will take some time and the phase of the signal at this point is compared
with the phase at this point and we will see what is the delay and by calculating the delay of
the phase because we know the speed of sound we can determine the distance so
with Arduino you can get the distance of the object from this device from this point
and one of the application would be that you may build a smart car and install this at the front
of the smart car and program your Arduino that whenever this approaches to an object
for example when it reaches 30 centimeter you can stop the car or when it reaches at
a certain distance you can use the steering and turn the car left or right to another direction
until it finds a clear path and then proceeds so this can be used in so many ways the module
has four pins this is the vcc will be connected to five volts this is ground and then we have
trig and echo and the chips that are used here with conjunction to the calculation from Arduino
the proper wave signal is sent here and the wave phase is measured and when it receives the signal
compares it and from the delay it calculates and the value will be available within the Arduino
here is a wiring diagram and I’ve used first thing this is a free software from flipsing.org
you can draw the schematic and wiring and this is the ultrasonic sensor from left this is vcc
this has been connected to 5 volts to the right we have ground have been connected to the ground
of Arduino and then the track is connected to pin 12 and eco is connected to pin 11.
in order to use this module you need extra set of codes that have been written specifically for
this device to work and it's called library so we are going to download the library plus
some code that is within the Arduino ide together they will do the calculation for us so let's first
get the library and then I’m going to explain the code for this first let's go to this address that
I will provide you googlejacks.com and you will see this page and download the library
this is the official library that is there but this is a direct link
click and then save it somewhere let's say in downloads
then go to sketch include library and click add zip library
from here point to your downloads and that is ultrasonic library
once you click and open it will be included in your library
click on file examples and scroll down until you see new ping and from here ping example
this include newping.h is part of the library that you just downloaded
and installed we're using the define as a constant we are setting a constant this is a text trig
pin trigger pin and here is pin 12 and then echo pin which is pin 11 and then the maximum distance
this is in centimeters 200 centimeter because this device cannot read more than that
you will get errors but you can play with this and reduce it for more precision and after that
we are using this class this is a class name the same name and then sauna and you can put
different name if you wish but after that this needs three arguments the three arguments that
have been defined here in order so first the track pin then then equipping and then max distance pin
if you do not define this this you also you can put here for example that 12 and
equip and 11 and you can put 200 as an integer after that we go to the setup and serial begin
I’ve said change it to 9600 baht for the serial monitor to display the text for us and here
inside the loop we just wait 50 milliseconds after that it prints this ping and then
this is the one that is giving us the value sooner dot ping under score cm this is in centimeter
so this is actual value and then print ln so this prints this text and then
this prints after that it prints this and after the value we print cm centimeter and
ln this means new line so after that there will be an enter that goes to the next line and loops
and prints all these three portion on the screen and the loop continues like that
here I have connected yellow the last pin to five volts this brown to ground and then the trig
the orange is connected to pin 12 and then eco is connected to pin 11. let me just upload the code
and as you can see it continuously shows when it's far beyond but if I put my hand you will see that
it shows five centimeters or six centimeter and then the distance increases and it is directional
put the phone here at 10 centimeter
and flatten it as you can see it shows 10.
let me go to 20
it shows 20 centimeters and as I move
continuously it displays the value now I’m putting it on a surface and three four five centimeters
here this is of the value and you can do some action based on this let's say you're
building a smart car and you want to stop the car the motor or turn off the motor when the
distance to the object reaches let's say 8 centimeter or 10 centimeters so you can
say like this if this is the distance if distance is smaller than 8 centimeters
so this is a condition the distance is
smaller than 8 centimeters from here to here this is the action you can say
digital right and your motor is connected let's say 2.5
and low mean turn off it will be turned off or maybe the light will be turned on
or something else so based on the distance you can take some action
now let's have a look at the other example here if we go to the ping
and here is the three sensor and if you're using multiple sensors remember from our previous code
this is a track pin the second one is the echo pin so this is the track pin and this is the eco pin
you can put multiple one and this is a trick pin for the second
sensor and this is for the third one
these are the maximum distance so this way you can use three sonars. sonars and this will go
continuously read the value and this this eye is referring to each sensor to read sonar number one
you will just type zero that is referring to sonar number one that is distance in centimeter
for you and if you want number two just put it one and if you want sooner number three put two
and you can have more sensors for a smart car if you have
if you need four or five six or even ten you can do that