Sure, remote controlled cars are fun, and self-service robotic cars are even more fun. In this tutorial, we will create a four-wheeled robot that can move around and avoid obstacles. I bought this complete 4WD kit from AliExpress, but you can easily buy most of these components from an electronics store and assemble them yourself.

I recommend reading all the instructions before you get started, as it will clear up some things that might be confusing the first time around. Also, this may look like a very long, advanced project due to the length of the instructions, but it’s actually quite simple. No need to be intimidated — this is an entry-level project that you can get some satisfying results with and then build on as you learn more. Don’t like this robot style? Here are some more Arduino robots you could easily build instead.

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

arduino-bot-parts

First, we will attach the motors and the H bridge (the board that supplies power to the motors) to the bottom of the chassis. First, attach four metal brackets (they are rectangular, drilled metal blocks) to each motor using two long bolts and two nuts.

arduino-bot-bracket

You need to make sure they are attached correctly, so look at the image below to make sure the side of the block with the 2 holes drilled will be facing down. Note that the wires on each motor are directed towards the center of the chassis.

arduino-bot-motors

Each motor can now be attached to the chassis using two short bolts at the bottom of each metal bracket. Here is a view of the underside of the chassis so you can see where the bolts should be:

Motor-screws-bottom view

The next step is to attach the H-bridge (that’s the red board in my kit) to the chassis. You can wait until all wires have joined the H-bridge before doing this, but that’s up to you (I found it easier). Quick note: my kit was missing a few fasteners, so I used duct tape to secure the bridge. However, you can see here where the bolts and nuts would go:

H-bridge bolts

Now that the H-bridge is connected, you can start connecting the power supply. Since the six AA battery holder comes with a DC adapter, you either need to cut off the end (which I did) or run jumpers to the batteries themselves.

Battery holder-DC-remote

Regardless of how you choose to do this, you will run the positive wire to the port labeled «VMS» and the negative wire to the port labeled «GND» on the bridge. Tighten the fasteners and make sure they are securely fastened. Then connect the motor wires. There is a set of two ports on both sides; one is labeled «MOTORA» and the other «MOTORB». Both The red wires on each side will go into the center green port, and both black wires will go into the outer one. This picture should make it more clear:

H-bridge-motors-wired up

I found that I had to remove part of the housing from the motor wires to get this to work. Now that all the motors and power supply are connected, slide the wheels onto the motor drive shafts and attach the four copper shafts at the locations shown in the picture below (one small bolt is required for each copper shaft). This robot is starting to take shape!

wheel-on-drive shafts

Now set aside this part of the body and take another, which will be located on top. The next step is to attach the Arduino — again, I had to use duct tape, but you should be able to better secure yours with some bolts and nuts.

Arduino chassis

The next step requires a micro servo, a black D-pad, a servo (which consists of three black plastic pieces) and a few small screws. Use one of the large, pointed screws in the kit to attach the black cross to the micro servo:

black-crossbar-micro-servo

Then turn the servo upside down into the black plastic holder ring. Make sure the wires coming out of the servo are facing 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 crossbar holes).

servo-black-ring

Here’s what it looks like after attaching:

circuit board bottom ring

Finally, take the other two parts of the servo holder and snap them onto the servo (there are indentations on the sides that match the plastic tab on the servo).

completed follower holder

Now that the servo is ready, it can be mounted on the chassis.

servo holder mounting

Here’s where the bolts go:

servo-holder-chassis bolts

It’s time to take a look at our robot. Attach the ultrasonic transducer to the servo with two cable ties.

Ultrasonic Zip Link Sensor

If you’re working with the same kit as me, you’ll get an Arduino touchscreen. We won’t be using it in this build, but you can place it on top of UNO now if you like (as shown in the image below). Just line up the pins at the bottom of the screen with the I/O ports on the Arduino and press down to connect them. You don’t need it at the moment, but shields might come in handy .

Arduino sensor shield

Whether you’re connecting a touch screen or not, you now need four wires to connect the ultrasonic sensor to the Arduino. The sensor has four pins: 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 take the bottom of the chassis and connect six jumpers to the I/O pins of the H bridge (they are labeled ENA, IN1, IN2, IN3, IN4 and ENB). Note which colored wires are connected to which ports, as you will need to know later.

H-bridge-wire

Now it’s time to start putting this thing together. Grab the top of the case and place it on top of the copper shafts connected to the bottom, and pull the wires attached to the H bridge through the hole in the center of the case. Connect six wires to the 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

arduino-bot-wiring

Now use four short screws to attach the top of the housing to the copper shafts. Install a battery holder with six AA batteries on top of the case (screw it on if you can), attach the 9V cell holder to the Arduino and this bot is ready to go!

arduino-larva-ultimate

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

arduino-bad-bot

Here we are. Now to give him brains. Let’s do some programming.

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

// Motor A pins (enableA = enable motor, pinA1 = forward, pinA2 = backward)
int enableA= eleven;
int pinA1 = 6;
int pinA2 = 5;
//Motor B pins (enabledB = enable motor, pinB2 = forward, pinB2 = backward)
int enableB= ten;
int pinB1 = four;
int pinB2= 3;
//This lets you run the loop a single time for testing
boolean run = true;
void setup() {
pinMode(enableA, OUTPUT);
pinMode(pinA1, OUTPUT);
pinMode(pinA2, OUTPUT);
pinMode(enableB, OUTPUT);
pinMode(pinB1, OUTPUT);
pinMode(pinB2, OUTPUT);
}
void loop() {
if (run) {
delay(2000);
enableMotors();
//go ahead
forward(200);
//go backward
backward(200);
//turn left
turnLeft(400);
coast(200);
//turn right
turnRight(400);
coast(200);
//This stops the loop
run = false;
}
}
//Define high-level H-bridge commands
void enableMotors()
{
motorAOn();
motorBOn();
}
void disableMotors()
{
motorAOff();
motorBOff();
}
void forward(int time)
{
motorAForward();
motorBForward();
delay(time);
}
void backward(int time)
{
motorABackward();
motorBBackward();
delay(time);
}
void turnLeft(int time)
{
motorABackward();
motorBForward();
delay(time);
}
void turnRight(int time)
{
motorAForward();
motorBBackward();
delay(time);
}
void coast(int time)
{
motorACoast();
motorBCoast();
delay(time);
}
void brake(int time)
{
motorABrake();
motorBBrake();
delay(time);
}
//Define low-level H-bridge commands
//enable motors
void motorAOn()
{
digitalWrite(enableA, HIGH);
}
void motorBOn()
{
digitalWrite(enableB, HIGH);
}
//disable motors
void motorAOff()
{
digitalWrite(enableB, LOW);
}
void motorBOff()
{
digitalWrite(enableA, LOW);
}
//motor A controls
void motorAForward()
{
digitalWrite(pinA1, HIGH);
digitalWrite(pinA2, LOW);
}
void motorABackward()
{
digitalWrite(pinA1, LOW);
digitalWrite(pinA2, HIGH);
}
//motor B controls
void motorBForward()
{
digitalWrite(pinB1, HIGH);
digitalWrite(pinB2, LOW);
}
void motorBBackward()
{
digitalWrite(pinB1, LOW);
digitalWrite(pinB2, HIGH);
}
//coasting and braking
void motorACoast()
{
digitalWrite(pinA1, LOW);
digitalWrite(pinA2, LOW);
}
void motorABrake()
{
digitalWrite(pinA1, HIGH);
digitalWrite(pinA2, HIGH);
}
void motorBCoast()
{
digitalWrite(pinB1, LOW);
digitalWrite(pinB2, LOW);
}
void motorBBrake()
{
digitalWrite(pinB1, HIGH);
digitalWrite(pinB2, HIGH);
}

view raw
4wd-robot-test.ino
hosted with ❤ by GitHub

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

add-in zip library

Make sure you see the include operator at the top of the sketch; if you don’t, click Sketch > Include Library > NewPing . Once done, upload the following sketch:

#include <NewPing.h>
//Tell the Arduino where the sensor is hooked up
NewPing sonar(12, 13);
long inches;
void setup() {
//Activate the serial monitor so you can see the output of the sensor
Serial.begin(9600);
}
void loop() {
delay(fifty);
//Ping the sensor to determine distance in inches
inches = sonar.ping_in();
//Print the distance in inches to the serial monitor
Serial.print(inch);
Serial.print(« in.«);
Serial.print(«\n«);
}

view raw
newping_demo.ino
hosted with ❤ by GitHub

Upload the sketch and open 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 this number changes. Move your hand in and out and you should measure how far your hand is from the sensor.

sensor_test

If everything worked correctly, it’s time to put everything together and run! Here is the code for the robot now. As you can probably tell, it’s basically two test sketches along with an added if statement to control the behavior of the robot. We gave it a very simple behavior to avoid obstacles: if it detects something less than four inches away, it will back up, turn left, and start moving again. Here is a video of the bot in action.

Give a robot life

Once you have this behavior set up correctly, you can add more complex behavior; make the robot alternate with turning left and right, or choose randomly; give a sound signal if he approaches something; just turn instead of retreating; you are really only limited by your imagination. You can use anything in your Arduino starter kit. included in your starter kit to add more functionality. You’ll also notice that we haven’t coded anything for the servo yet: you can actually make your robot’s «eyes» move back and forth. perhaps using them to find a path instead of simply backing away when he finds an obstacle directly in front of him.

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

Похожие записи