hi welcome to Arduino tutorial from Robojax in this video we are going to measure the distance
using ultrasonic sensor and displayed as a bar graph on the LCD screen you can get the code
for this by clicking at the link below the video in the description which will take you
to Robojax.com/learn/Arduino let's get started with this
there are many instances that you need to display the distance measured using this ultrasonic sensor
as a bar graph the advantage of displaying of the bar graph is that with repelling to number
bar graphs can easily be understood for example right now the maximum is set at 20 centimeters
and immediately we know at the middle is half without even reading this from further distance
you can read it and we can even go to 20 and it will be full and the video I'm going to
explain how to get the bar graph have separate video explaining how to use this display with
complete explanation the link will be provided also another video that explains this ultrasonic
sensor I'm not going to do the detail of this our concern is to just display the bar graph
now let me explain the wiring I will show you the wiring here but that's hard that we have four pins
SEL that is a clock is the SCL and SDA we have two pins on Arduino here if CL and if me if you're not
doing it doesn't have it alternatively also you can use analog for analog 5 analog for is SDA
and analog 5 is SEL you can connect it in here if you're using other devices for example you know
Ethernet if the if a 4 a 5 is ECL Arduino mega xx is D and 21 is ECL and northern do there have
been all mentioned I will provide you the link for this as well then we have VCC blue have been
connected to 5 volts of Arduino and then G and D or ground the green have been connected to ground
of Arduino for ultrasonic sensor we have VCC this purple have been connected to pin 2 which have
been defined as additional 5 volts and then drag and a coat reg is gray have been connected to pin
11 and eco is white have been connected to pin 12 of Arduino the last one is ground black have been
connected in here because we additional ground available here I've spent quite some time writing
the code testing it and making sure it works and providing you the code the least that you can do
for me please subscribe you don't lose anything and it will be a great help and to be appreciated
let me now explain the code the first thing that you need to do is we need to download
two libraries while library is the liquid crystal under school I to see that is the ITC module that
is here and behind us and they have to include LCD bar graph Robo jacks which is the updated
version I've taken the original and I bloody from this address from the playground and it was not
working with this so I have updated and worked and changed many places and customize it with adding
some feature so it can work easily with this now you have to download it I will provide you the
link this can be used without using the ultrasonic or the graphs odessa's generally you need this for
this purpose so this library is needed but for bar graph we are adding this on top of that so
if you have this do not worry and then just add this in both cases the link will be provided so
you have to download it a zip file and save it in your computer so you have two separate file
that you have to download a zip after that click on sketch include library and then add dot set a
library when we click on that point it to area of saved it says Robo Jack's USB related that this
is another example you just click to the zip that you have download it and click open this way there
everybody will be added and do the same thing for build once that is done then the examples will be
routed with this that I have written click on file examples and then scroll down until
you see our Robo Jack's bar graph and then go to Ultrasonic once you click this is a file that will
be opened now let me now explain it this wire dot each is part of like Arduino IDE you don't
need to do anything if you just downloaded these two and include it from hell you set the number
of characters this is 16 characters and to line this to means number of lines and then we use a
class create an instance of it and we call it LCD and then it has three parameters here the first
one is a I square C address for this tiny device that is communicating instead of multiple lines
we are sending data here via serial communication and the number of columns that we have and then I
will see the line number of lines we have to pass these three to the class to find this address you
need to run I square feet scanner and I score C scanner the link will be provided once you added
let me open eyes cursive scanner once that open do not worry what is inside it the only thing
that you need to worry is to make sure that write comport have been selected and uploaded now the
code will be uploaded to toward we know after that click on the serial monitor and it will print the
address for you it says 0 X 3 F and then we use this address let me upload the code back and then
we use that address and here after that we use the LCD bar graph logo Jack's class and we created an
instance of it we call it our GX and when we pass this LCD with the Sam person here and then number
of columns of the LCD and then that position character 0 9 0 which is displayed in here
I think loot tank new pang is for this device for this ultrasonic sensor it is part of our duty
library you don't need to do anything it will just work fine and then we define a trigger pin number
11 echo pin pal number 12 and maximum distance after did succeed depending on your application
you have to put that so the calculation can be done properly to display the maximum if you said
this sixty the sixty maximum will be here and the thirty will be at the middle and so forth so make
sure to put the amount that you are using so you can get the proper proportion graph and then you
paint sonar this is an instance of this class and we are passing the trigger pin echo pin and
maximum distance for the class to work because we are connecting these two just needs five
volts there's needs five volts and some Arduino is you do not have five volts even though I had
here 5 volts extra 5 volts but I have to find pin 2 as 5 volts so we can have 2 5 volts one
for this one for this one is connected directly to 5 volts and the other will be connected to pin 2
as extra VCC and here we have defined VCC to pin number 2 and side the setup using pin mode VCC 2
we said this pin has output so this is outputting the voltage acting as an output and using digital
right 2 and 2 as high this VCC 2 is equal to 2 so this is as fi type 2 when we said this as
high the pin this pin will always have 5 volts but with letter current this is not suitable for
everything I've separate video explaining that the link will be provided and this portion is
for LCD as he did not begin initialize with the class for the LCD dot clear clears the value the
previous value in case if it exists and then LCD print inside this double quotation reporter text
Robo jacks which will be displayed on the first line and then we said zero-one we go to the line
one character zero to the next line and we print bar graph now let me reset this so you can see
it reset it will be quick Robo jacks bar graph so this is the text and then we give the user
for two seconds to see it after that we clear the screen then you go inside the loop whatever code
we have inside the loop will run continuously the first line with sonar dot Peng cm this will
get the a distance and centimeter and store it in here so this is the value and we say equal
to this and we put a distance cm then we also get the inch and we put a distance I and I’m
using centimeter in case if you are using inch inside the code wherever we see this I replace
it with us and then here Robo jacks clear line one we are clearing line 0 line 1 continuously
so it can read new value for us and we don't care this one because a class is taking care of that
the graph class and then we set LCD cursor to character 0 line 1 and we print this the word
distance with colon and then we set the cursor at character 10 if we count death you will see
that 0 1 2 3 4 5 6 7 8 9 at 10 we are putting it and we say distance cm it will print the distance
for a centimeter and then we go to character 14 this is 10 14 so 10 11 12 13 14 at 14 we print
cm again inside double quotation because it's a text just doesn't have any quotation because
it is a variable that has value then we read hundred millisecond and loop continuous like that
and here is a demonstration at the moment it is set to 20 centimetres and it perfectly shows the
distance as bar graph if I go further than a 20 centimeter because the ultrasonic sensor is
automatically disabled beyond that distance you will get 0 that's a maximum distance that shows
20 if I make this for example 60 you will see that that the whole span of this bar graph from here to
here will be 4 60 and for example 15 centimeter will show somewhere in here so 4 of this have
been taken and then 8 is 30 and then 16 is 60 so if I move this you will see that 30 is just there
and if I point it to the laptop screen well right now it shows around 60
now let's say you want to have the bar graph instead of starting from here you want it to
start from the middle if a character age and if you pay attention the LCD call number columns
so this is number of column because we wanted to fill up this it was 16 now let's change it
to 8 character and then we want to start from 8 so this is a length this is a start and this is
a line 0 if you want to move it to the next one changes to 1 so let's see inch for 20 centimeter
so now this is 16 centimeter and if I go 20 it will fill it up
now I've inverted this larger faith it will not work because the address for
this is not 3f so I have to change the address the address is 0 X - 7 and then
also the number of characters is 20 and number of line is 4
now the bar graph is being calculated for 20 characters which is this would
with compare it to this the 16 is here so we have four extra characters
I change that to 30 the spreader bar graph this is a death line at 9:40 is where you change the
location of the bar graph to be displayed so zero zero means it goes the line zero
character zero and if I want to display the bar graph and here it will be line 0 1 2 so
I will put 0 2 let me change this to that zero just change this to two and upload the code
and you will see that bar graph have been moved to the next line in the same way you can change
those as well thank you for watching this was how to display the distance using ultrasonic
sensor on the LCD screen I hope you have learned something please don't forget to
thumb up the video if you have comment a question posted I try to answer and
reply don't forget to subscribe and leave the notification on thank you
you