it's the first part of my project to make a car controlled by ultrasonic sensor, as it will be moving forward till it faces any type of obstacle, the ultrasonic sensor then will turn it in a circle to avoid the obstacle, and in the same time the IR sensor will turn the buzzer on when the car is so close to the obstacle.
And attached to all of this a pen with a servo as I was trying to make it draw it's moving Pattern on floor.
to build and simulate my circuit before making it IRL
It will be our brain for the curcit.
to control the 2 DC motors I'll be using for the car
to wire it all up
I'll be using on DC motor to move the car forward and the other to move car in a circle so that it avoid obstacles
I'll use it to move the flowmaster pen
for building temporary circuits
it will be stick to servo shaft so that the car will be drawing while it moves
as i used it to make the encloser for the car
Obstacle detector
I'll be using the buzzer as my second output, as it will buzz when the car comes to an obstacle, and its input will be the IR sensor.
first things first we will build and simulate our curcit in Tinker CAD✨
start by listing all of your components and searching about how to wire it all up, then start connecting it to see the final result
p.s. I need a Motor driver l298n to control the 2 DC motors I'll be using for the car, but this module isn't available on tinker CAD so I used the "l2930" instead, sme with the IR sensor, as i used PIR sensor to replace
start by including libraries that you need for coding in my case it was the servo motor, so I include it in the first line then i defined its place on Arduino in the third line.
2. I started defining the variables I need for ultrasonic sensor, and its pins on the Arduino.
3. I defined every and each pin of the two motors, and by setting every value of these pins I can control the direction of the motor, and that's the coolest part of the Motor driver l298n module
4. finally the i set the integrs for buzzer and IR sensor.
I set every pin mode as I'll be using 2 inputs which is IR sensor and Ultrasonic sensor, and 3 outputs which are buzzer, 2 DC motors and a servo motor.
the void loop which will have my Ultrasonic sensor loop, as i need it to be done at every start of the code.
Ultrasonic sensor If condition:
which will be running the motor to move the car forward, and once the Ultrasonic sensor detect an obstacle, the code will run the other motor with the front wheel to make the car turn.
IR sensor If condition:
It will be buzzing the buzzer when a obstacle comes too close.
It gives false distance out of no where every some accurate distances, and I'll be suffering with this when i get to the final check ✨
finally I but it all in a cardboard encloser, and started testing it in real life.
there was a little bit of delay in ultrasonic sensor part as it showed before for the out of no where false reading but overall it was functioning.
so this was my ultrasonic code reference: ultrasonic code reference.
and i used my pervious week IR sensor coding which is Here, and i just replaced servo with Buzzer.
and the Motor driver l298n module reference for wiring, and coding was THIS,
except that i didn't use the ENA and ENB part as i didn't want to control the speed of the motors for now.