in this lesson we are going to use this infrared obstacle avoidance and we will
see how we can detect an obstacle from infrared Ray that hits the object we
are going to modify the code as well as adding a buffer so when the obstacle is
detected the buzzer will turn on let's see if we can adjust it and increase the sensitivity
this course is based on SunFounder three in one Arduino kit sold on Amazon everything
that you learn in this course is included in this kit the course comes with tons of
components and projects all with instruction and video for smart car and other projects that
you can use for school or home automation you can purchase it from sunfounder.com
the kit also comes with full documentation code download learning projects with full
explanation wiring diagram and a code that you can download a link to purchase the
kit and documentation is below the video in the description welcome to Arduino course by Robojax
in this lesson we are going to learn about this infrared
obstacle avoidance sensor where we will detect an object if it is on the path
here is how it works we have infrared LED or transmitter which transmit transmits infrared
Ray that hits the object and reflects back then with that we have photodiode or infrared receiver
where it receives a signal and detects it and it will we will amplify it using an op-amp or
operational amplifier and we detect it this is the power when you connect it to power regardless
this will be always on and this led will turn on when the obstacle is detected this will output
always HIGH or one when there is nothing when the obstacle is detected this is turned on and the
output goes to zero or LOW so 0 means an object and one mean nothing so right now there is nothing
but the light is off when light is off we get one when light is on we get zero this is a transmitter
it emits infrared light and when this black is the receiver and the receiver will receive
it and we will amplify it we will also set the sensitivity using this variable resistor and that
determines if you want to make it very short or the maximum a long range that it can detect here
is the lesson we can read it as obstacle avoidance at learning project 3 and 3.3 here is the wiring
picture V out is connected to pin two ground is to ground and VCC we will connect it to 5 volts and
here the actual wiring diagram again the pen that is on the other side this will be connected using
green wire to pin two the middle one is ground using black wire connected to ground and the
right pin is VCC we will connect it using red wire to 5 volts and here is the actual wiring
right pin is connected to pin 2 using green wire the middle is
ground if it's connected to ground the right pin is red connected to pin five
and let's open The Code by clicking file open
go to the folder that you have downloaded click three in one kit
learning project and come to 3.3 detect up the obstacle and detect the obstacle
and then open it so this is our code the code is very simple we are defining pin 2 as IR obstacle
pen of type integer inside the setup we are initializing the serial monitor with 9600 bot
and this line is setting that pin 2 as input so we are receiving input and because you have we
have used pen mode on pin 2 this is just digital inside the loop we are reading the Thunder pin
2 and here using digital read the result of this which is either the result of this is one or zero
will be detected the value will be detected and will be printed using the serial print Ln on the
serial Monitor and we have 10 millisecond delay and the loop will continuously does the same thing
now let's open the serial monitor
as you can see, we are reading one okay so here is a Serial Monitor and if I bring my hand away you
can see that we are reading here one as soon as I bring my hand you will see that we are reading
zero if I go back let's see up to what distance it can read so more than this it cannot read but here
read yeah now it cannot read and then read I'm gonna put the ruler
in terms of centimeter so we can see it and here I'm going to put this Laura module here exactly at
about one millimeter further away but that's my obstacle
so as you can see, we are detecting here zero the light is on it has detected let's go backwards
so, I have to zoom out
and here again let's go back hmm how much I should go backwards so I'm gonna put the
rulers back because it was not sitting on the screen so you could see it now,
we are at 12 centimeter as you can see 120 millimeters and
we are now at 13 we are going back so almost 15 as you can see at 15 it's turning on and
off but keep in mind that this is very shiny if it's a dull object it might get better result,
I'm going to put the SunFounder Arduino cat box
we are at exactly 13 so 13 and 14 it just turns off so maximum 12 centimeter is very reliable
now let's try to adjust it you see at 12 and a half or 13 it's on and this then it's off
let's see if we can adjust it and increase the sensitivity maybe it's being decreased
if the light on turns on not my mother I went all the way to
clockwise counterclockwise it stopped going back
so now I increase it to 15. as you can see
so now we are at 15 centimeters let's go back further 18 and see if it works
now
at 20 it doesn't work but 18 centimeter it works amazing so 20 centimeters exactly
20 centimeter it works but if I put the angle if it's straight it's
the surface the angle of incident is 90 degrees, we are getting but if I put it a little up,
we are not reading oh we are reading so as you can see, we can increase or decrease the sensitivity
now we are going to modify the code as well as adding a buffer so when the obstacle is detected
the buzzer will turn on and here is the code to detect obstacle this was part of the code,
I have defined alarm pen as pen 3 and I've defined a sense of value where we will hold the value for
the sensor inside the setup, I have added this line where the alarm fan is defined as an input
sorry it should be output so we are turning the buzzer the buzzer on and inside the loop we are
reading the pen the sensor value and saving it in a sensor after that we are using conditional
statement using F and we check if since the value is equal LOW these two equal sign mean compare it
if it is LOW or zero this LOW means 0 then we print this on the screen which says obstacle
and then also we use digital right the alarm pen we send HIGH signal or 5 volts to this pin
so the buzzer starts buzzing else mean otherwise which is not LOW which means it is HIGH then we
send the spend too LOW so the buzzer is off and then the loop will continue to do the same thing
and this is the wiring for the buzzer the buzzer's longer Pennies on this side I'm inserting it to
the breadboard the first wire is from the positive this is coming to pin 3 of Arduino
the ground the left pin on the other side will be connected to ground and here
and here is a project demonstration the light is off there is nothing printed
on the serial monitor when I bring my hand you can see the buzzer is running
and also the information is printed if you want it, you can omit it but
so perfectly working
to just remind you that you don't need to connect to USB or to computer you just
need to connect your battery as well and the project well perfectly work fine with battery