Hi, welcome to nor doing a tutorial for Robojax.
my name is Ahmad Shamshiri. and this little we're going to learn how to use this
sharp infrared 0A51Sk distance sensor that measures up to 15 centimeter using
Arduino as you can see we are at around 7 centimeter and we are reading that 7
centimeter you can get the code for this by clicking at the link below the video
in the description which will take you to www.robojax.com/learn/arduno
let's get started with this
Presenting this tutorial from Canada.
you can use this with our doing a nano annual donor that you have the code that
I've written we'll work with 3.3 volts and the five volts Arduino version.
the code that I've written for this is for multiple sensor because we knew in
robotics you will need more than one of this so the code is accommodating if you
have multiple of this as long as the input of your Arduino for example in
this one this one allows six inputs but if you are using larger Arduno with
more analog inputs you can do so. this module is widely used in many robotics
application and for shorter distances there are different version of this and
I have separate video explaining those. but this version particularly is used
between 2 centimeters to 15 centimeter and it can be used in robotic arms small
moving robots that you need to measure the distance it can be attached for of
this we can attach one here and one or two maybe on this side two on this side
and one at the back so your robot will be aware of the surrounding distance. you
can use it for many other application so how this function is that we have an
infrared emitter and infrared receiver. so the signal emits and hits the object is received back
and proportionally (to the reflected signal) we have a voltage at the output and this can operate operate
this operate with 5 volts and we have only three wires the red will go to the
VCC this goes to the ground and this is a signal which we are reading the
voltage. and because we have analog inputs with Arduino we can read the
voltage and proportionally we can calculate the distance. and I've
experimented with the values have measured from 2 centimeter up to 15
centimeter and then I measure the voltage from these have created this
plot the blue is the value that I've done and then the red is equation of the
line the fitting curve that is set on this equation and here we got the
equation. We use this equation in the calculation that I've done. I will
also provide you in this table in case if you need it. Length of the 27.1
millimeters with is 10.8 millimeters depth of this is 11.8mm
s but of course there is a connector. but if you want to
create a space for this to fit it in something the length is 19.1 millimeters
the with is 7.9 limiters.
Datasheet viewed
and here's a data sheet for GP2Y0A51SK0F this is the most important part A51SK0F
from sharp so this is a block diagram
how it works we have a emitter that emits infrared rays. this is a emitter and that is the
receiver which receives it and the voltage will be proportionally outputted.
the follow specification is here the operating voltages from 4 4 point 5 to 5
point 5 volts here at 15 centimeters to 2 centimeter we will be getting one
point three five to one point nine five volts and the current that it needs says
12 mA so keep that in mind. maximum will be 22 milli ampere in case
the battery matters to you. I will provide you this datasheet this plot is
here and because I was started with 2 centimeter I was not worried before that
so I've started from here and I got almost the same type of plot but mine is
very accurate because I did it practically. here I'm measuring every
step from 2 centimeters to 15 centimeters and recording them here so I
can create the formula and get the equation of the plot from here.
Wiring Explained
now let me explain the wiring. we have here three wires this will be connected
to five volts this is connected to ground so fortunately they follow the
convention by putting red for VCC and black for ground and this you might get
it yellow or white doesn't matter but this is a signal the voltage output. they
did not have this pin header but I solder that. here is how I've connected it.
the red is connected to five volts the middle the black is connected to the
ground and the white is connected to A0 of course you can select any
on any other analog pin that you want. if you want to learn Arduino I have a best
seller course on udemy where you can learn Arduino from ground up with
hundreds of projects all course material library codes wiring diagram everything
available in one place the link for module new courses below this video in
the description.
Code explained
now let me explain the code I've kept in mind that you can use multiple sensors
and here we are defining an array for pins
I used analog 0 if you're using and if you're using more than that then define
the pins here for example A0, A1, A2 and so forth. but this is the distance
that is holding this variable this array is holding the distance for each sensor
so because I have only here because I had only one I put here 1 but if you
have here 2 for example if you have analog 1 then because we have 2 elements
you must put here 2 otherwise you will get errors. because they must match. this
line is average of 50 measurement to get accurate value and this is a
microcontroller unit voltage for if you are using Arduino nano or other
version that are 3.3 volts please enter here 3.3 volts. because I'm using Arduino
Uno it is 5 volts I put 5 volts.
The voltage is for measurement calculation. For supply (operating) voltage of the sensor please connect it to 5V.
if you are Arduino operates with 3.3V set 3.3V .
inside the setup we initialize a serial monitor with 9600 baud and in here if
you come here to the right to the lower section you will see 9600 this so
this must much otherwise you will not be able to read it.
inside the loop() this is a function that is going to measure the distance and
this zero means the first sensor if you have a second sensor put one two three
and but start from zero as much as many as you have because now I have only one
so I put zero here so keep that in mind because it starts with zero and here
this line is printing the text d1 with equal sign and then this is printing the
distance because we are reading distance zero so that is for the sensor zero and
this is just printing this text from here and at the end I've just added this
centimeter inside the double quotation. this function is in here and it is
calculating the average and returning the value and you say the distance and
you pass a value zero one or two so this number the number that you pass will
become sensor voltage_temporary_average so this is a variable that we used to
hold the average values and here use we use analog read sensorPin and we get
this value that we pass here this sensor comes here and it goes and picks up the
value from here zero means get this one one means get the other one and so forth.
so this gets the sensor pin and this reads from that sensor pin the value for
this will be stored here we call it sensor value and the value will be
between 0 to 1023 and 10 bit system and we have one millisecond delay so give it
enough time to read and then we get this value which is this one and we
applied by five/1023 this five is a five volts of operating voltage of Arduino
and 1023 is a number it is two to the power 10 which is 1,024 minus
1 so it becomes 1023 steps. so we are dividing the 5 volts by this many steps
and we get a factor when we multiply it by this sensorValue you get a voltage.
and then we are adding that calculated voltage adding it to this variable so we
are adding by this number average_of is a variable that we defined here 50
average_of so we are getting 50 calculations and adding it now it will
be a huge number but in this one we are getting that average and then dividing
it by average of dividing it by number of iteration which is divided by 50 so
we get the actual average and using this value and then we use the average
calculated value to insert it into this equation of the line to the fitting
equation and then this value will be stored in the variable called distance
at this location if this is 0 it will be for the same cell 0 if it is 1 4 it will
be for sensor 1 and so forth. so this is just updating this value which is in
here.
you
To use distance sensor 0 to do something do like this:
Demonstration
I'm attaching the sensor here on this box so it stays stable
and at this point so this is the sensor and it's exactly at zero millimeter this
is a millimeter range and here this is sectional six centimeter at this point
at the edge and we are reading six centimeter let me go to three
centimeters
and here we are reading 3.1 so two centimeter we are reading 1.89 let's go
ten centimeters and we are reading ten point one eight let's go further so 14
centimeter at 14 we are reading fourteen point one fourteen point three and this
is the edge where after this you will get incorrect reading reading fifteen
point three which is correct but fourteen point five is not fluctuating
now so if I go to 18 now this is unreliable but if I go with 11
centimeter
7.20 centimetre it's very accurate. but it has some deviation because it's infrared and
also depends on the surface if you use a very shiny surface like this let's see
what we did at 11 centimeter actually we are reading not bad
9 centimeter less 9.5 9.3 though when I put a glass it disturbs the reading
because it has magnifier and let me put a mobile phone with very shiny surface
so we are reading 14 even though we are at 11 so you need a very non reflective
surface to get better measurement
I would like to thank my Patrons from Patreon.com
Subtitel typed and corrected by Ahmad Shamshiri in Candad on July 13, 2020
I did my part to help you learn. Now it's your turn.
You can subscribe to my channel, thumb up 👍 the video and say "thank you" in the comment section.
The link to my PayPal account is below this video if you want to send me chase donation. Thank you.
thanks for watching this was how to use this short infrared distance meter with
Arduino if you learned something and found it useful please thumb up 👍 as this
will help my video in the search algorithm of YouTube. if you have comment
or question please post a comment section below I try to answer I reply.
and to get updates of my upcoming videos make sure to subscribe
Related Videos
in this tutorial we are going to learn and test this VL 53 l1x time-of-flight
laser sensor with range of 4 meter this modulus offered in this shape and so we
are reading 3 meter and let me point it to the further away on the world