Obstacle avoiding robot
2 DC motors make the robot move forward until there is an abject 20 cm or less away then the motors will stop and turn right (left motor is on, the right one is off).
If there is an object/ wall on the left the car will turn right.
Software:
TinkerCAD
Components:
1- Motor driver
2- Breadboard
3- Arduino Uno
4- Jumper wires
5- Ultrasonic sensor
6- 2 DC motors
7- IR sensor
1- Attach the 2 DC motors to the motor driver
2- Attach Arduino pins (2,3,4,5) to motor driver input pins
3- Attach 6V adaptor to the right terminal
4- GND for the Arduino, power adaptor & Motor driver in the breadboard
6- 5V Arduino pin to the breadboard to connect to the Motor driver & the sensor VCC
7- Enable pins to 5V
8- Wire the ultrasonic sensor
Trigger pin >> 9
Echo pin >> 10
GND >> GND
VCC >> 5V
9- Wire the IR sensor
Signal pin >> 6
GND >> GND
VCC >> 5V
10-Copy the code from Arduino IDE
Schematic Diagram
A) Circuit wiring
1- Attach the 2 DC motors to the motor driver
2- Attach Arduino pins (2,3,4,5) to motor driver input pins
3- Attach 6V adaptor to the right terminal
4- GND for the Arduino, power adaptor & Motor driver in the breadboard
6- 5V Arduino pin to the breadboard to connect to the Motor driver & the sensor VCC
7- Check the code & the wiring (move forward function at first)
8- Wire the ultrasonic sensor
Trigger pin >> 9
Echo pin >> 10
GND >> GND
VCC >> 5V
9- Test all the components and the code together
B) Coding
1- define variables (Motor pins ) 2,3,4 and 5
2- Set them as OUTPUT
3- Write the moveForward function to make the 2 motors move in the same direction
4- call it in the loop
5- test the code & the circuit together, and it worked successfully
6- Create stopMotors fuction then test it too
7- Create turnRight function and test
8- create ultrasonic sensor variables pins (trig = 9, echo = 10)
9- create ultrasonic sensor function (calcDistance), it returns int variable (distance)
10- call it in the loop and put its value in distance variable
11- print the distance to make sure it works well
12- remove the print instruction
13- the defualt is the robot car will move forward until it finds an objwct
14- make a condition if the there is a object 20cm away or less
stop motors
turn right alittle
move forward again (because of the loop) or stop again and turn until there is an object to move forward
15- Create IR sensor pin =6
16- In the loop function, if the IR reads 0 (there is no light/ there is an object or wall), then turn right to avoid it.
Someone asked me about a servo motor problem, the problem was that it needs to connect directly to the VCC from the adaptor not the Arduino 5V with other components that consume power.
DHT sensor library isn't recognized by Arduino IDE after installing it many times using manage library and zip file.
The error :DHT_sensor_library-1.4.1\DHT_U.h:36:10: fatal error: Adafruit_Sensor.h: No such file or directory
Solution: install Adafruit unified library, more details in this link
https://forum.arduino.cc/index.php?topic=471001.0
Sensing, Arduino, integrating multiple components together