0:09
hello and welcome to controllers tech
0:12
today we will see how to interface GP2
0:17
SK0F an infrared sharp sensor with SDM32
0:22
this IRS sharp sensor uses the infrared
0:25
to measure the distance of an object the
0:28
sensor is a combination of position
0:30
sensitive detector an infrared emitting
0:32
diode and a signal processing circuit
0:36
the device outputs the voltage
0:37
corresponding to the detection distance
0:40
so it can also be used as a proximity
0:42
sensor it can measure distances from 4
0:49
the sensor is an analog output type so
0:52
we can measure the voltage corresponding
0:54
to the distance by using the ADC of the
0:57
MCU the supply voltage needs to be 5
1:00
volts but I have tested it with 3.3
1:03
volts and it works just fine we will
1:07
check the other things in this data
1:09
sheet in a while let's take a look at
1:11
the connection first the sensor is
1:14
powered with 3.3 volts from the MCU
1:18
itself the signal pin is connected to
1:21
PA0 which is basically the ADC1 channel
1:24
zero pin that is all for the connection
1:28
let's create a new project in cube IDE i
1:36
let's give some name to the project and
1:38
click finish i am starting with the
1:41
clock setup first i am selecting an
1:44
external crystal for the clock the board
1:47
has 8 MHz crystal on it and we will run
1:50
the system at maximum 72 MGHertz
1:54
now go to the ADC1 and enable channel
1:57
zero you can see the pin PA0 got
2:01
configured as the ADC pin i am leaving
2:04
the configuration to default so I will
2:07
use the ADC single channel in the
2:09
blocking mode you can check the video on
2:12
the top right corner to understand ADC
2:15
modes in detail there is some issue with
2:18
the clock let's reduce the ADC clock to
2:22
a reasonable frequency
2:24
all right generate the project again
2:27
let's define a variable to store the
2:30
calculated distance value now create a
2:33
function to measure the distance
2:36
here we will first measure the ADC value
2:39
and then convert it to the distance i am
2:42
using the ADC in the polling mode the
2:45
F103 has 12 bit ADC therefore we will
2:49
use the 16 bit variable to store the ADC
2:52
value the default ADC reference voltage
2:56
is 3.3 volts we will use this 3.3 volts
3:01
to convert the ADC value to the voltage
3:03
format now this converted voltage can be
3:07
used to calculate the distance of the
3:09
object from the sensor but there is no
3:12
formula provided in the data sheet for
3:14
the same we can use the graph to figure
3:17
out a relation between the voltage and
3:19
the distance this is the graph of
3:22
equation y= ax + b where the y-axis
3:25
represents the output voltage and the
3:28
x-axis represents the distance of the
3:31
we can use two different points on the
3:33
graph to calculate the constants A and B
3:37
and then use the constants along with
3:40
the output voltage to calculate the
3:42
distance of the object
3:44
there is one more graph which is more
3:46
like a straight line the graph is
3:49
plotted between the output voltage on
3:51
the y-axis and the x-axis represents the
3:54
inverse of the distance along with some
3:56
offset i will use this particular graph
4:00
to find the relation between the output
4:02
voltage and the distance
4:05
assuming that this is indeed a straight
4:07
line let's calculate the slope of this
4:11
we need to take two sets of coordinates
4:14
on this line the first coordinate will
4:17
be 0.15 on the x-axis and 0.65 on the
4:23
similarly the second coordinate will be
4:26
0.15 on the x-axis and 1.95 on the
4:32
let's use these two coordinates to
4:34
calculate the slope of this line it
4:40
now you can calculate the value of the
4:42
constant in the equation of line y = mx
4:45
+ c the constant will be zero so we have
4:50
the line equation as y = 13x
4:54
although this line remains straight for
4:56
most of the distances it is not quite
5:02
but that is all right we can have a
5:04
small error in that part the x-axis here
5:07
is basically the inverse of the distance
5:11
so we need to rearrange the formula to
5:13
calculate the distance L as shown in the
5:18
let's test it suppose we got the output
5:21
voltage of 1.2 volts from the ADC
5:25
now we will use the rearranged formula
5:28
to calculate the distance
5:30
first divide the value 13 by the voltage
5:35
now subtract the constant value 0.42
5:39
we got the distance around 10.4
5:43
if you check the graph this is
5:45
approximately the same as expected
5:48
so we can use this rearranged formula to
5:51
calculate the distance
5:53
let's use the formula to calculate the
5:56
distance inside the measure distance
5:58
function the calculated value will be
6:01
stored in the distance variable now
6:04
inside the main function call the ADC
6:06
calibration function once inside the
6:10
while loop we will measure the distance
6:12
every 500 milliseconds
6:15
let's build and debug the project now
6:20
add the distance variable in the live
6:22
expression and run the debugger you can
6:26
see the device on the bottom right
6:27
corner i am placing the object at 4 cm
6:31
and you can see the distance being shown
6:36
as I mentioned there is some error below
6:39
5 cm because the graph is not actually a
6:42
straight line in this region now the
6:45
object is at 5 cm and the distance being
6:52
i have placed the object at 6 cm now the
6:56
object is at 7 cm now you can see the
7:00
measured distance is approximately the
7:02
same as the object distance
7:07
the object is now placed at 14 cm from
7:15
it is 17 cm away right now
7:27
the object is now 26 cm away and we are
7:30
still getting a pretty good measurement
7:33
so the sensor works pretty well even at
7:36
higher ranges although it seems
7:38
unreliable around 29 to 30 cm but it
7:42
works fine for the rest of the distance
7:44
range there are other sharp sensors that
7:48
work for higher distance ranges so you
7:50
can check them if you need this
7:53
particular sensor works between 4 to 30
7:55
cm and it does the job well that is it
7:59
for the video i hope you understood how
8:02
to interface the IR sharp sensor with
8:07
you can download the project from the
8:09
link in the description leave comments
8:12
in case of any doubt keep watching and
8:15
have a nice day ahead