in this lesson we are going to use the ultrasonic sensor to detect an obstacle and the distance take
action so our core can proceed better with us understanding the surrounding
welcome to Arduino course by Robojax 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 the code that you can download a link
to purchase the kit and documentation is below the video in the description
this is the fourth project for smart car we are using this ultrasonic distance sensor to
measure the distance this way we can detect the obstacle it also we have employed also
two or infrared obstacle avoidance but with this we can also detect two things we can
detect the distance how far something is an object and also we know the existence of an
object in this area and a little wider we had an introduction ultrasonic sensor and
also how to use this motor driver I expect that you have already watched those videos otherwise
please watch them before proceeding with this the link for those videos are below this video
and here is a documentation page from Sun founder if you go to car project and number
six play with ultrasonic module this is a page where you see the wiring the code and explanation
and let's have a look at the wiring this is from Sun founder so how it works is that we
have positive to A breadboard and negative to the breadboard and we are connecting one wire for the
positive the other for the negative and the Ping and Echo wires are connected to pin three and four
and three and pin four on our system the positive is connected from this pin from here to this point
where we have positive and then from here, we have ground, so these are all our ground
and this is our ultrasonic sensor we are just putting it inverting
it on the breadboard somewhere at the middle, so it faces this way and here
this is exactly at the middle
one whole left or right doesn't matter but this is adapted
right the first one positive goes to positive
I'm moving my wires so we can have one more positive so that is my positive and here
and the negative the last 10 is negative so this is one line I can connect anywhere
and I'm connecting it to this point so that is negative, and the right pin is green second pin
I've connected to the second pin, and this goes to pin three of Arduino and here pin three
and that second pin from the left
here this will go to pin 4.
so, the wiring is completed
now let's open the project click on file open go to the sun founder download page
click on car project then number six ultrasonic module click and open it
we explain the code we have already seen this code in previous lesson these are for the motor control
and then we have trigger pin and Echo pin which we have studied and the introduction to ultrasonic
sensor that we are introducing we are initializing the serial monitor with 9600 bot and then the two
Motors this is a for the first motor for the right that's for the left and this is for Ultrasonic for
Ping pin and Trigger pin one is as an input the other is an output we already have seen
this and inside the loop this is where the action takes place this function reads sensor data is
a function at the bottom of the code which reads the distance and returns it and the result of this
whatever it is we are storing it in this variable called distance of type float and this line is a
conditional statement we say F this distance is greater than 25 that is centimeter then which
means the obstacle is further away so move forward we go forward with the speed 200 that remember the
speed is from 0 to 255 so 255 mean 100 percent we already done it in the introduction I spoke about
it so this is just sending the car with that speed else F otherwise if the distance is less than 10
okay or greater than 2 which means between 2 and 10 then move backwards
else stop so the otherwise it will stop this is a function this is another function
forward backward move stop move so this is a function that reads the data
and the result is returned using this variable we have already spoken about this so we are setting
the Trek pin as LOW for 2 microseconds and then setting it to HIGH the track pin for 10 seconds
and then track pin LOW then we read pulse n we pass that track pin with HIGH so this is
reading and dividing it by 58 which is getting the based on speed of light the speed of sound we are
getting the distance and distance is returned move forward is when we pass that speed it comes here
so we are controlling this is for one motor this is for another motor
that's forward for backward these two at the middle which have speed will be zero and the
top and bottom which was zero and now they are in speed so the motor will rotate in the other
direction and for stop you are just sending 0 0 to all four pins which I mentioned these two is
for right side and this is for the left side for motor a and motor beta motor will stop on each
function I have added this text so it will tell us if the function is running for when it goes to
backward we see reverse when it moves forward we see forward and stopped now I'm uploading the code
and just disconnecting negative uh ground pin from this motor driver and then programming it
the code is loaded let me open the serial monitor as you can see it says forward or
stopped forward stopped because there is certain distance, I have to make sure
now if I bring my hand it says reverse otherwise it goes forward and Y forward and stop when you
run the program and if you see this error which says move forward was not declared in this scoop
for example in this line it's stopped or it stops at this I have added this
each function this is called prototype so before the setup we have to add each of
these with open close and that semicolon like this so these are called prototype
we have already spoken when we declared or learned about functions so the program will
know that this function exists below the code now let's upload the code
the error disappeared
now let's open the serial monitor
so it reads the distance 0 and applies the stop y
and when I run this code pay attention here we are reading forward and then stop
let me stop this by clicking on how to scroll you see it reads zero that's why it has to apply the
stop and then it reads 36 forward and then 0 even though this is not being moved and there
is an object in front of it so it's reading and let's solve this the reason that we are getting
that error is when we are reading this in the loop we are doing it very quickly the loop ends
and it goes back an ultrasonic sensor does not have enough time to read it for that reason we
need to have a delay let's say delay of 100 millisecond and it will resolve the problem
now let me upload it and show you
now look at the serial monitor now this is hitting an object and the distance is
coming it's more than 36 greater than 36 25 centimeter and as you can see, we don't get
we don't get any stop, so it consistently works perfectly
and now here is the demonstration as you can see because the distance is
greater than 25 centimeter now if I put my hand and it's less than 25 so it's stopped
let me bring it goes reverse because it's less than 10 and here it stops
16 17. and less than that it goes reverse and now let's demonstrate it this is more
than 25 centimeter and it's facing the obstacle let me connect the power
ground wire [Music] so that that's connected and let's power up
oh, you should okay so now it's facing and stopped because it's less than 25
and if I go further, it goes further but if I bring it closer it goes backwards
and
as you can see the code is working perfectly and our car is really smart at the moment
[Music]