Sie sind auf Seite 1von 18

15/5/2018 How to Build a 4WD Arduino Robot for Beginners

Arduino Robot for


Beginners
By Dann Albright / December 28, 2015 28-12-2015 / 8 minutes

Dann Albright
610 articles

Dann is a freelance
business and technology
Facebook Twitter Pinterest Stumbleupon writer. When he's not on
Email his bike, he's looking for
new ways to help people
Advertisement be productive, meet their
goals, and maintain a
healthy relationship with
technology.

Latest
Giveaways!
Remote controlled cars are fun, sure, but self-driving robotic Oculus Go: Is This
cars are even more fun. In this tutorial, we’ll build a four-
The Best Mobile VR
Yet? (Review and
wheeled robot that can drive around and avoid obstacles. I Giveaway)
purchased this complete 4WD kit from AliExpress, but you
could easily buy most of these components from an Fitbit Versa Review:
electronics store and put it together yourself. Fitbit’s Best
Wearable Yet?
I recommend reading through all of the instructions before
you start, as that will make some things clear that might be This USB-C Hub
confusing on the rst time through. Also, this may look like a Does Everything:
QacQoc GN30H
very long, advanced project due to the length of the Review
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:

https://www.makeuseof.com/tag/build-4wd-arduino-robot-beginners/ 1/18
15/5/2018 How to Build a 4WD Arduino Robot for Beginners

Trending
TECH NEWS

Google Duplex Will


Identify Itself as an
AI

INTERNET , ANDROID

6 Google Photos
Alternatives to Use
If You’re Tired of…

WINDOWS

What Is Windows 10
To get started, we’ll attach the motors and the H bridge (the Timeline? Why It’s
Great and How…
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.
Related Articles

PRODUCT REVIEWS

iLife V8s: The Best


Budget Robot
Cleaner Just Got
Better (Review &
Giveaway!)

INTERNET

Learn to Build Cool


Techy Gadgets With
These Step-by-Step
Video Guides

DIY

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

https://www.makeuseof.com/tag/build-4wd-arduino-robot-beginners/ 2/18
15/5/2018 How to Build a 4WD Arduino Robot for Beginners

Latest Articles
INTERNET , ANDROID

No Gym
Membership? 5
Workout Apps to
Get Fit for Free
Anywhere

IPHONE AND IPAD


, SECURITY

The Best VPN for


iPhone Users

ENTERTAINMENT
Now each motor can be attached to the chassis by using two The 15 Best Shows
short bolts in the bottom of each metal bracket. Here’s a view on Amazon Prime
You Need to Watch
of the bottom of the chassis so you can see where the bolts
need to be:

Latest Free PDFs

Affiliate Disclosure: By buying the


products we recommend, you help keep
the lights on at MakeUseOf. Read more.

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:

https://www.makeuseof.com/tag/build-4wd-arduino-robot-beginners/ 3/18
15/5/2018 How to Build a 4WD Arduino Robot for Beginners

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:

https://www.makeuseof.com/tag/build-4wd-arduino-robot-beginners/ 4/18
15/5/2018 How to Build a 4WD Arduino Robot for Beginners

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.

https://www.makeuseof.com/tag/build-4wd-arduino-robot-beginners/ 5/18
15/5/2018 How to Build a 4WD Arduino Robot for Beginners

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 ip 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).

https://www.makeuseof.com/tag/build-4wd-arduino-robot-beginners/ 6/18
15/5/2018 How to Build a 4WD Arduino Robot for Beginners

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).

https://www.makeuseof.com/tag/build-4wd-arduino-robot-beginners/ 7/18
15/5/2018 How to Build a 4WD Arduino Robot for Beginners

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


the chassis.

Here’s where the bolts go:

https://www.makeuseof.com/tag/build-4wd-arduino-robot-beginners/ 8/18
15/5/2018 How to Build a 4WD Arduino Robot for Beginners

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.

https://www.makeuseof.com/tag/build-4wd-arduino-robot-beginners/ 9/18
15/5/2018 How to Build a 4WD Arduino Robot for Beginners

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:

https://www.makeuseof.com/tag/build-4wd-arduino-robot-beginners/ 10/18
15/5/2018 How to Build a 4WD Arduino Robot for Beginners

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.

https://www.makeuseof.com/tag/build-4wd-arduino-robot-beginners/ 11/18
15/5/2018 How to Build a 4WD Arduino Robot for Beginners

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


programming.

The rst 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:

1 // Motor A pins (enableA = enable motor, pinA1 = forward, pinA


2 int enableA = 11;
3 int pinA1 = 6;
4 int pinA2 = 5;
5
6 //Motor B pins (enabledB = enable motor, pinB2 = forward, pinB
7 int enableB = 10;
8 int pinB1 = 4;
9 int pinB2 = 3;
10
11 //This lets you run the loop a single time for testing
12 boolean run = true;
13
14 void setup() {
15 pinMode(enableA, OUTPUT);
16 pinMode(pinA1, OUTPUT);
17 pinMode(pinA2, OUTPUT);
18
19 pinMode(enableB, OUTPUT);
20 pinMode(pinB1, OUTPUT);
21 pinMode(pinB2, OUTPUT);
22 }
23 void loop() {
24 if (run) {
25 delay(2000);
26 enableMotors();
27 //Go forward

https://www.makeuseof.com/tag/build-4wd-arduino-robot-beginners/ 12/18
15/5/2018 How to Build a 4WD Arduino Robot for Beginners

28 forward(200);
29 //Go backward
30 backward(200);
31 //Turn left
32 turnLeft(400);
33 coast(200);
34 //Turn right
35 turnRight(400);
36 coast(200);
37 //This stops the loop
38 run = false;
39 }
40 }
41
42 //Define high-level H-bridge commands
43
44 void enableMotors()
45 {
46 motorAOn();
47 motorBOn();
48 }
49
50 void disableMotors()
51 {
52 motorAOff();
53 motorBOff();
54 }
55
56 void forward(int time)
57 {
58 motorAForward();
59 motorBForward();
60 delay(time);
61 }
62
63 void backward(int time)
64 {
65 motorABackward();
66 motorBBackward();
67 delay(time);
68 }
69
70 void turnLeft(int time)
71 {
72 motorABackward();
73 motorBForward();
74 delay(time);
75 }
76
77 void turnRight(int time)
78 {
79 motorAForward();
80 motorBBackward();

https://www.makeuseof.com/tag/build-4wd-arduino-robot-beginners/ 13/18
15/5/2018 How to Build a 4WD Arduino Robot for Beginners

81 delay(time);
82 }
83
84 void coast(int time)
85 {
86 motorACoast();
87 motorBCoast();
88 delay(time);
89 }
90
91 void brake(int time)
92 {
93 motorABrake();
94 motorBBrake();
95 delay(time);
96 }
97 //Define low-level H-bridge commands
98
99 //enable motors
100 void motorAOn()
101 {
102 digitalWrite(enableA, HIGH);
103 }
104
105 void motorBOn()
106 {
107 digitalWrite(enableB, HIGH);
108 }
109
110 //disable motors
111 void motorAOff()
112 {
113 digitalWrite(enableB, LOW);
114 }
115
116 void motorBOff()
117 {
118 digitalWrite(enableA, LOW);
119 }
120
121 //motor A controls
122 void motorAForward()
123 {
124 digitalWrite(pinA1, HIGH);
125 digitalWrite(pinA2, LOW);
126 }
127
128 void motorABackward()
129 {
130 digitalWrite(pinA1, LOW);
131 digitalWrite(pinA2, HIGH);
132 }
133

https://www.makeuseof.com/tag/build-4wd-arduino-robot-beginners/ 14/18
15/5/2018 How to Build a 4WD Arduino Robot for Beginners

134 //motor B controls


135 void motorBForward()
136 {
137 digitalWrite(pinB1, HIGH);
138 digitalWrite(pinB2, LOW);
139 }
140
141 void motorBBackward()
142 {
143 digitalWrite(pinB1, LOW);
144 digitalWrite(pinB2, HIGH);
145 }
146
147 //coasting and braking
148 void motorACoast()
149 {
150 digitalWrite(pinA1, LOW);
151 digitalWrite(pinA2, LOW);
152 }
153
154 void motorABrake()
155 {
156 digitalWrite(pinA1, HIGH);
157 digitalWrite(pinA2, HIGH);
158 }
159
160 void motorBCoast()
161 {
162 digitalWrite(pinB1, LOW);
163 digitalWrite(pinB2, LOW);
164 }
165
166 void motorBBrake()
167 {
168 digitalWrite(pinB1, HIGH);
169 digitalWrite(pinB2, HIGH);
170 }

4wd-robot-test.ino hosted with ❤


PC by
& GitHub
MOBILE LIFESTYLE view raw

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.

https://www.makeuseof.com/tag/build-4wd-arduino-robot-beginners/ 15/18
15/5/2018 How to Build a 4WD Arduino Robot for Beginners

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:

1 #include <NewPing.h>
2
3 //Tell the Arduino where the sensor is hooked up
4 NewPing sonar(12, 13);
5
6 long inches;
7
8 void setup() {
9 //Activate the serial monitor so you can see the output of the
10 Serial.begin(9600);
11 }
12
13 void loop() {
14 delay(50);
15 //Ping the sensor to determine distance in inches
16 inches = sonar.ping_in();
17 //Print the distance in inches to the serial monitor
18 Serial.print(inches);
19 Serial.print(" in.");
20 Serial.print("\n");
21
22 }

newping_demo.ino hosted with ❤ by GitHub view raw

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.

https://www.makeuseof.com/tag/build-4wd-arduino-robot-beginners/ 16/18
15/5/2018 How to Build a 4WD Arduino Robot for Beginners

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

https://www.makeuseof.com/tag/build-4wd-arduino-robot-beginners/ 17/18
15/5/2018 How to Build a 4WD Arduino Robot for Beginners

robot’s “eyes” move back and forth. perhaps using them to


seek out a path instead of just backing up whenever it nds 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!

Enjoyed this article? Stay informed


by joining our newsletter!

your@email.com Submit

Read our privacy


policy

24 COMMENTS WRITE A COMMENT

© 2018 MakeUseOf. All Rights Reserved.

https://www.makeuseof.com/tag/build-4wd-arduino-robot-beginners/ 18/18

Das könnte Ihnen auch gefallen