Part IV of Build Electronics: The Final Project
Hello friends,
Today I am going to teach how to build an obstacle avoiding car. For this project, you will need:
- An ultrasonic sensor (HC-SR04)
- Ultrasonic sensor mount
- An arduino/genuino Uno
- These Motors
- This Motor Shield
- A Piece of rectangular or circular plastic to use as a chassis (Bought Pre-made on amazon is fine too)
- Jumper Wires
- A 9V Battery
- Arduino IDE - download here
- Adafruit Library Installed - https://github.com/adafruit/Adafruit-Motor-Shield-library
- Some tape for wire management
- Hot glue gun
Alright, lets get started!
First thing you want to do is attach the motors to the chassis. If you bought pre-made on Amazon, follow the instructions that came with your kit. If not, use some hot glue to glue the motors to the chassis.
I used a 3d printed chassis - feel free to be creative and make your own designs.
Do this for all four motors.
Now, attach the wheels to the motors.
Next thing you want to do is attach the jumper wires to your motors. On your motor you should see small metal pieces with holes in them.
Thread your jumper through this and then bend it down so it doesnt come out. (Or you can use hot glue, but I find bending it gets a better connection. Dont solder until the end when you have a working robot.)
Thread 8 wires into the holes on your motors, 2 for each motor, making sure the connections on each are strong.
Flip the chassis over.
Now for the arduino. Your arduino and motor shield should snap together like so:
Put this on top of the chassis.
Now the next thing to do is to attach the HCSR04 to the mount. Use the mounting screws to secure this.
Use hot glue or a lot of tape to secure this to your chassis (I personally recommend hot glue)
Alright, your car body is built! Time to wire it up.
Each motor has by now 2 individual wires coming from it. Tape these together so that its easier to keep track of. Designate the front right motor at fr1, front left as fl1, bottom left as bl1 and bottom right as br1.
Plug in front right to M3, back right to M4, front left to M2, and back left to M1.
Make sure the positive wire is on the outsides, and the ground on the inside. Like this:
In this case, red are positive and blue and white are ground. Wire your board in the same way
Good, the motors are now attached.
Almost done! Wire the ultrasonic sensor to the motor board, with vcc going to the 5v pin, GND to GND, trig to A1 and echo to A0.
Alright! Everything is wired up. Time to upload the code and get moving.
Upload this code to your arduino device.
Great! Your finished. Not to hard is it? You've just built a robot!
Plug in the 9V battery and watch him go. When he runs into an object, he should stop, back up, and turn away another direction. Feel free to play with the code and see if you can develop something better.
This may have been a simple project, but remember - if you can dream it - you can build it. All that lies between you and your imagination is just knowlege and time. Learn what you need to learn, and spend time developing your project - and you can build anything!
I hope you enjoyed this tutorial, and remember as always if you ever have any questions or comments dont hesitate to ask.
I hope you have a great day!
This is very easy to follow @buildingrobotics! I never used an ultrasonic sensor before; encouraged me to look into how it works.
So do you just adjust that if condition for distance up if you want the robot to turn before bumping into something? Is the sensor that good?
Thanks for sharing!
Glad it was easy to understand and follow - thats my goal :D
By turning 'before' bumping into it, are you asking if the sensor is good enough to detect things soon enough to stop before the robot hits something?
In that case - if you were to adjust the condition up like you said, the robot would turn further away from the object. So for example, my current code makes the robot stop, back up, and turn if an object is detected less than 25 cm away from it. If you were to up this to, say, 50cm - the robot would turn much sooner and much further away.
I guess the only way to make sure it doesn't bump into things is to make sure your stopping within the reaction distance. There is a brief moment in time between object detection and the robot stopping. During this time, the robot is still moving forward for a brief moment, even though an object is close enough to meet the condition for stopping and turning. You have to make sure that the distance the robot travels during this time does not exceed the condition threshold, or else the robot will crash.
That may sound complicated, but let me give an example. Lets say it takes the robot 1 second to stop after detecting an object. During this 1 second, the robot travels 10cm. If your conditional statement that tells the robot to react to the object is less than this, your robot will crash because it doesn't have time to stop - by the time the signal is processed to stop the robot has already hit a wall. So make sure your robot's conditional to stop and turn is a greater distance than that it will travel before it can react.
The sensor is pretty good - and very fun - as long as the connections are good. It is good enough that if you wanted it to turn before hitting something, it could - But as I said, there is a little delay between detection and reaction that you have to account for, and yes, you could account for this by adjusting the if condition.
I hope that answers your question, sorry that must have been a long read :)
But yeah definitely experiment with ultrasonics, its really fun and has a lot of practicality.
so cool, it's time to go deeper down the field.
You're a genius. I wouldn't even know where to begin.
It's like a micro-version of what Tesla uses on the road.
Haha thanks. Its really not as hard as you would think, plenty of people build things like this.
Tesla uses much more advanced sensors and a more sophisticated algorithm, but yeah at the most fundamental level the tech is the same.
I'm glad you liked it! :)