How to Build a 4WD Arduino Robot for Beginners | Dramel Notes

Posted On // Leave a Comment

Remote controlled cars are fun, sure, but self-driving robotic cars are even more fun. In this tutorial, we’ll build a four-wheeled robot that can drive around and avoid obstacles. I purchased this complete 4WD kit from AliExpress, but you could easily buy most of these components from an electronics store and put it together yourself.

I recommend reading through all of the instructions before you start, as that will make some things clear that might be confusing on the first time through. Also, this may look like a very long, advanced project due to the length of the instructions, but it’s actually pretty simple. No need to be intimidated – this is a beginner level project that you can get some satisfying results with, then build upon as you learn more. Don’t like this style of robot? Here’s some more Arduino robots you could easily build instead.

Here’s what we have, after taking everything out of the packaging:

To get started, we’ll attach the motors and the H bridge (the card that delivers power to the motors) to the lower part of the chassis. First, attach the four metal brackets (they’re rectangular, drilled blocks of metal) to each motor using two long bolts and two nuts.

You’ll need to make sure that they’re attached correctly, so check out the image below to make sure that the side of the block with two drilled holes will face downward. Note that the wires on each motor are pointing toward the center of the chassis.

Now each motor can be attached to the chassis by using two short bolts in the bottom of each metal bracket. Here’s a view of the bottom of the chassis so you can see where the bolts need to be:

The next step is to secure the H bridge (that’s the red board, in my kit) to the chassis. You may want to wait until all of the wires are attached to the H bridge before doing this, but that’s up to you (I found it to be easier). A quick note: my kit was missing a number of fasteners, so I used electrical tape to secure the bridge. However, you can see here where the bolts and nuts would have gone:

Now that the H bridge has been attached, you can start wiring up the power supply. Because the six-AA battery holder comes with a DC adapter, you’ll need to either cut off the end (which I did) or run jumper wires to the batteries themselves.

No matter how you decide to do it, you’ll run the positive wire to the port labelled “VMS” and the negative wire to the one labelled “GND” on the bridge. Screw down the fasteners and make sure they’re secure. Then, you’ll connect the motor wires. On both sides, there’s a set of two ports; one is labelled “MOTORA” and the other “MOTORB.” Both red wires on each side will go into the centermost green port, and both black wires will go into the outermost. This picture should make it more clear:

I found that I had to strip some of the housing off of the motor wires to get this to work. Now that you have the motors and the power supply all wired up, slide the wheels onto the motor drive shafts, and attach the four copper shafts in the locations show in the picture below (each copper shaft needs one small bolt). This robot is starting to take shape!

Now, set that part of the chassis aside and grab the other one which will sit on top. The next step is to attach the Arduino—again, I had to use electrical tape, but you should be able to better secure yours with some bolts and nuts.

The next step requires the micro servo, the black crosspiece, the servo holder (which consists of three black plastic pieces), and some small screws. Use one of the larger sharp screws in the kit to attach the black crosspiece to the micro servo:

Then flip the servo upside down into the black plastic ring of the holder. Make sure that the wires coming out of the servo are facing in the same direction as the longer part of the holder (again, see the image below), and use four tiny screws to secure the crossbar (there are four holes in the holder that align with the holes on the crossbar).

Here’s what it looks like after it’s attached:

Finally, take the other two pieces of the servo holder and snap them onto the servo (there are grooves in the side pieces that match the plastic tab on the servo).

Now that the servo holder is complete, it can be mounted to the chassis.

Here’s where the bolts go:

It’s time to give our robot some eyes. Attach the ultrasonic sensor to the servo holder using two zip ties.

If you’re working from the same kit as I am, you’ll have received an Arduino sensor shield. We won’t be using it in this build, but you can pop it on top of the UNO now if you want (as I have in the image below). Just align the pins on the bottom of the shield with the I/O ports on the Arduino and press down to connect them. You don’t need it at the moment, but shields can come in handy.

Whether you connect a sensor shield or not, you’ll now need four wires to connect the ultrasonic sensor to the Arduino. There are four pins on the sensor, VCC, GND, TRIG, and ECHO. Connect VCC to the 5V pin on the Arduino, GND to GND, and TRIG and ECHO to I/O pins 12 and 13.

Now grab the lower part of the chassis, and connect six jumper wires to the I/O pins of the H bridge (they’re marked ENA, IN1, IN2, IN3, IN4, and ENB). Take note of which color wires are connected to which ports, as you’ll need to know later.

Now it’s time to start putting this thing together. Grab the upper part of the chassis and set it on top of the copper shafts connected to the lower part, and pull the wires attached to the H bridge through the hole in the center of the chassis. Connect the six wires to I/O ports as follows:

  • ENA to I/O port 11
  • ENB to I/O port 10
  • A1 to I/O port 5
  • A2 to I/O port 6
  • B1 to I/O port 4
  • B2 to I/O port 3

Now, use four short screws to attach the upper part of the chassis to the copper shafts. Set the six-AA battery holder on top of the chassis (screw it down if you can), attach the 9V cell holder to the Arduino, and this bot is ready to rock!

Well, almost ready to rock. It doesn’t have quite enough personality yet.

There we go. Now to give it a brain. Let’s do some programming.

The first thing we’ll do is test to make sure that the bridge and motors are hooked up correctly. Here’s a quick sketch that will tell the bot to drive forward for half a second, drive backward for half a second, then turn left and right:

That’s a lot of code for a simple test, but defining all of those functions makes it easier to tweak later. (Big thanks to Billwaa for his blog post on using the H-bridge for defining these functions.) If something went wrong, check all of your connections and that the wires are connected to the correct pins. If everything worked, it’s time to move onto the sensor test. To use the ultrasonic sensor, you’ll want to download the NewPing library, and then use Sketch > Include Library > Add .ZIP Library… to load the library.

Make sure that you see the include statement at the top of your sketch; if you don’t, hit Sketch > Include Library > NewPing. Once you’ve done that, load up the following sketch:

Upload the sketch, and open up the serial monitor using Tools > Serial Monitor. You should see a rapidly updating sequence of numbers. Hold your hand in front of the sensor and see if that number changes. Move your hand in and out, and you should get a measurement of how far away your hand is from the sensor.

If everything worked correctly, it’s time to put it all together and let this thing run! Here’s the code for the robot now. As you can probably tell, this is basically the two test sketches put together with an added if statement to control the robot’s behavior. We’ve given it a very simple obstacle-avoidance behavior: if it detects something less than four inches away, it will backup, turn left, and start moving again. Here’s a video of the bot in action.

Give Your Robot Some Life

Once you’ve gotten this behavior working correctly, you can add more complex behavior; make the robot alternate between turning left and right, or choose randomly; sound a buzzer if it gets close to something; just turn, instead of backing up; you’re really only limited by your imagination. You could use just about anything in your Arduino starter kit to add more functionality. You’ll notice also that we haven’t coded anything for the servo yet: you can actually makes your robot’s “eyes” move back and forth. perhaps using them to seek out a path instead of just backing up whenever it finds an obstacle directly in front.

Let us know if you decide to build this robot or another one, and tell us how you decide to customize its behavior or looks. If you have any questions about this robot, post them in the comments below, and I’ll see if I can help!


Post a Comment