Building the HUB-ee Buggy - SparkFun Learn

Building the HUB-ee Buggy a learn.sparkfun.com
tutorial
Available online at: http://sfe.io/t86
Contents
OMG Robots!
Gather the Bits
The Best Laid Plans
Rolling Chassis
If I Only Had a Brain
Moar Power
Basic Object Avoidance
HUB-ee Buggy Robot Boogie
Troubleshooting
Resources and Going Further
OMG Robots!
Everybody loves robots! (Well, except for Sarah Connor.) They come in all shapes and sizes: from
small to large; simple to complex. As hobbyists, we’re often drawn to more complex robots, but
sometimes a simple design is more fun.
Case in point: The HUB-ee Buggy
Page 1 of 13
The HUB-ee Buggy robot is a simple two-wheel, object-avoiding robot based on theHUB-ee wheel
system. HUB-ee wheels make it possible to build a wheeled robot chassis in a snap by combining
the wheel, motor, and driver into a single package. Just bolt a pair of HUB-ee wheels onto a
microcontroller, and you’ve basically made yourself a little robot. That’s essentially what we’re doing
here.
This is an awesome project for starting out with robotics, because it’s easy to go from a pile of parts
to a rolling robot which can easily be expanded with more sensors, wheels, or complex control code
as you learn and progress.
Let’s see what we need to get started…
Suggested Reading
Here are some topics you’ll need to be familiar with to make your Hub-ee Buggy.
How to Solder
Working with Wire
What is Arduino?
What is a Shield?
Pulse Width Modulation
Connector Basics
IR Communcation
Motor Basics
Page 2 of 13
Gather the Bits
There are only a handful of parts in the HUB-ee Buggy. Let’s start with a list:
Hub-ee Buggy SparkFun Wish List
(2) HUB-ee Wheel - 120:1 Metric
ROB-11666
The HUB-ee is a type of robot servo but designed for wheels, in fact it is a wheel, but it is also a motor, a senso…
HUB-ee - Cable (150mm)
ROB-11672
This 150mm ribbon cable has an AMP Micro-MaTch connector on either end. It's a quick and easy way to get y…
RedBoard - Programmed with Arduino
DEV-11575
At SparkFun we use many Arduinos and we're always looking for the simplest, most stable one. Each board is…
Arduino ProtoShield - Bare PCB
DEV-11665
By popular request, we're selling the always-handy ProtoShield PCB on its own! Originally spawned with colla…
Break Away Headers - Straight
PRT-00116
A row of headers - break to fit. 40 pins that can be cut to any size. Used with custom PCBs or general custom h…
Infrared Proximity Sensor - Sharp GP2Y0A21YK
SEN-00242
Infrared proximity sensor made by Sharp. Part # GP2Y0A21YK has an analog output that varies from 3.1V at 1…
There are a few parts you’ll need to pick up from your local hardware store:
Infrared Sensor Jumper Wire - 3-Pin JST
SEN-08733
4 x 10mm
M3JST
Bolts
Three pin
connector with red, black, and yellow colors. 5 inch wire outs. This cable comes fully assembled…
4 x 6mm M3 Bolts and Nuts
Battery Holder - 4xAA to Barrel Jack Connector
PRT-09835
You’ll also need
a couple of tools, but you won’t need a machine shop by any stretch:
This is a simple 4 cell AA battery holder. The 5 inch cable is terminated with a standard 5.5x2.1mm, center pos…
Small Screwdriver
(4) 1500 mAh Alkaline Battery - AA
Rotary Tool
or Small Drill
PRT-09100
Soldering
and
solder
These Iron
are your
standard
1.5V AA alkaline batteries from Duracell. Don't even think about trying to recharge the…
Wire Strippers
(2) Standoff - Nylon (4-40; 3/4"; 10 pack)
PRT-11796
Now that we’ve
gathered everything we need to get started, it’s time to do a little planning and
These nylon standoffs are 3/4" long and tapped for a 4-40 screw. These are great for mounting your board in a…
figure out what each of these bits are for…
Screw - Phillips Head (1/2", 4-40, 10 pack)
PRT-10452
The Best
Laid Plans
Standard Philips-head 4-40 screws. They are 1/2" long and come in packs of ten. This is the screw size we use…
To understand
how
the HUB-ee
Buggy works, it’s probably best to start by breaking it down one
(6) Nut
- Metal
(4-40)
PRT-10232
component at a time:
We could talk about how the Zinc used in these nuts is only from the finest old-growth Zinc trees from across th…
Angle Bracket - 4-40
Page 3 of 13
The wheels, motors and drivers are built into the HUB-ee wheels so it’s pretty obvious what function
those serve. Without wheels and motors, this robot isn’t going anywhere.
The wheels won’t know where to go if there’s no brain, and that’s where the Redboard comes in!
The Redboard is an Arduino-compatible development board based on the ATmega328
microcontroller. It’ll take input from the Sharp IR sensor and give output to the HUB-ee wheels.
Speaking of the IR sensor, the Sharp GP2Y0A21YK is one of my favorite sensors for simple object
detection and avoidance. It’s an infrared sensor that detects objects by shining an infrared source
and detecting the reflected light. Reading the output is really simple with a microcontroller like the
ATmega328 that has a built in ADC because it outputs a voltage. This is how the HUB-ee Buggy
will keep from bumping into things and getting stuck.
With only two wheels, the HUB-ee Buggy would tip over if there wasn’t some support under it to
keep it upright. The simple way to solve that problem is just to add a few stand-offs to prop it up as it
scoots around.
The Protoshield PCB is really nice for small projects like this which are basically built on top of a
development board. Not only will it allow us to wire things to the Redboard, but it will also serve as
a base to which we can attach the wheels.
Finally, we need power to get this whole production moving. Originally, I thought that theLiPo
Shield might be a good solution for this, but it just couldn’t drive the wheels to my satisfaction. Thus,
decided to keep it simple and pop on a AA battery holder.
Rolling Chassis
Alright! Build time!
The first step is to actually mount the HUB-ee wheels to the Protoshield PCB. This will create a little
robot base that we can sit the Redboard on top of. The HUB-ee wheels come with a plastic right
angle bracket. Go ahead and screw that to the wheel hubs using your M3 bolts. Once you have the
wheels connected to the brackets, find a good spot to mount them. About midway on either side of
the board worked well for me. Just line up the brackets where you want to put them, and mark the
bolt pattern with a pen.
Page 4 of 13
Now take your drill (or rotary tool with a drill bit), and drill holes to mount your wheels. Once you’ve
drilled your holes and you’re sure that they line up, go ahead and put the 4-40 bolts through, and
connect the wheels to the Protoshield.
At this point you’ll have a pretty wobbly little platform so we’ll have to add a few standoffs to keep it
stable. Drop one of your ½" 4-40 bolts through the mounting holes on two opposite corners of the
Protoshield. The standoffs aren’t quite long enough to keep the bot from falling over, so we need to
bolster them with a little bit of bolt length on either side. For the bolt on the “back” of the robot
(now’s a good time to decide which is the back, I chose the side without the FTDI header), add a
nut, and tighten it up the the board. Now put another nut on there, and snug a standoff against the
nut. By twisting the nut and the standoff against each other you can force them to stay put on the
bolt. Now take another 4-40 bolt, put a nut on it, and screw it into the other end of the standoff. Now
the head of that bolt should be touching the ground and keeping the bot upright.
Do the same thing on the opposite corner, but, instead of using a nut to secure the top bolt, use the
Page 5 of 13
threaded side of a 4-40 right angle bracket. This bracket will hold our Sharp IR sensor in place.
Make your life easier by putting a ½" 4-40 screw through the other hole on the angle bracket before
using it to secure the standoff bolt.
Pick up the Sharp IR Sensor, mount it onto the protruding 4-40 screw, and secure it using a nut.
Now that the chassis is complete, let’s concern ourselves with a brain…
If I Only Had a Brain
Two wheels stuck together do not, a robot, make. The Redboard will be our robot brain today. It will
read the output of the infrared sensor, and, if it seems too close to an object, will change the
direction of travel to avoid collision.
The first matter of business is to connect the “brain” to the robot base, which means that we’ll need
to solder some headers to the Protoshield. Break off a few headers and solder them to the I/O pins
on the Protoshield. They’ll act both as an electrical connection and a mechanical connection,
keeping the wheels stuck to the brain. The easiest way to make sure your headers are straight
when you solder them is to go ahead and stack the shield onto the Redboard. For more shield
assembly advice, check out our shield tutorial.
Page 6 of 13
Once you’ve soldered the headers in place, it’s time to make all of the electrical connections from
the shield to the wheels and IR sensor. Take your 150mm HUB-ee cable, and cut it in half. I know,
it’s a painful process for some people to cut through a nice clean cable, but we’ll get through this
together. Once the cable is bisected, it’ll serve as a pair of pigtail connectors. Strip the wire ends,
and plug one of the pigtails into each HUB-ee wheel. Go ahead and plug the Infrared Sensor
Jumper Wire into the Sharp IR sensor.
Now, let’s solder these jumpers in place. We won’t be using the encoders in the HUB-ee wheels for
this project, so we don’t need to worry about those wires. The HUB-ee Datasheet has a great pinout
for the HUB-ee connector that will help us figure out which wires are which.
Page 7 of 13
Looks like the wire with a red stripe is ground and the one next to it is power. Make those
connections to the GND and 5V pins, respectively, and solder them in place. The next two pins are
control inputs and can be connected to any of the Redboard’s GPIO pins. I chose D7/D8 for my left
wheel and D10/D11 for my right. The next connection to make is the PWM pin which controls the
speed of the HUB-ee. Make sure that these are connected to PWM-capable pins on the Redboard.
I chose D6 for my left wheel and D9 for my right.
Lastly, the sensor needs to be connected. Simply wire the red and black wires to the 5V and GND
pins, respectively, then solder the yellow wire to an analog input pin. I connected mine to A3.
Your HUB-ee Buggy is all wired up now and ready for a power supply…
Moar Power
As I mentioned earlier, I had originally planned on using theLiPower Shield as the power plant for
the HUB-ee Buggy, but it just didn’t have enough juice to run the motors so I kept it simple with a
4xAA battery holder. The 4xAA to Barrel Jack Connector is perfect for this purpose because we can
just plop it on top of the HUB-ee Buggy and plug the barrel connector into the Redboard.
Page 8 of 13
To keep the battery pack in place, I just took a piece of electrical tape and wrapped it around the
robot. Now that our ‘bot has brains, wheels, and power, we can start programming it!
Basic Object Avoidance
The HUB-ee Buggy is almost ready to go, but right now it doesn’t have any way of avoiding
obstacles. We need to tell it what to do when it encounters a wall or other large object. There are a
lot of really clever ways to do this, and you can make the code as complicated as you want. In this
case, though, we’ll keep it basic. The HUB-ee Buggy will keep an eye out for obstacles and turn
around when it gets too close to one. This will be achieved by reversing one of the wheels for a few
milliseconds.
Plug the Redboard into your computer, and load up this Arduino code:
language:c
// Define the pins to make the code more readable
// Our Sharp IR sensor is connected to A4
#define sharpIR A4
// Our left HUB-ee is connected to D6, D7 and D8
#define leftPWM 6
#define leftIN1 7
#define leftIN2 8
// Our right HUB-ee is connected to D9, D10 and D11
#define rightPWM 9
#define rightIN1 10
#define rightIN2 11
// This is the threshold for how close an object will
// be allowed to get before the HUB-ee Buggy changes
Page 9 of 13
// its direction of travel. You may need to adjust it.
int tooClose = 200;
// This stores the value read from the Sharp IR sensor
int irVal = 0;
void setup() {
// Set the infrared sensor as an input. This isn't
// strictly necessary because the ADC is an input
// by default but it's good practice.
pinMode(sharpIR, INPUT);
// Set all of the outputs accordingly
pinMode(leftPWM, OUTPUT);
pinMode(leftIN1, OUTPUT);
pinMode(leftIN2, OUTPUT);
pinMode(rightPWM, OUTPUT);
pinMode(rightIN1, OUTPUT);
pinMode(rightIN2, OUTPUT);
}
void loop() {
// Read the initial value of A4 (or whatever sharpIR is defined as):
irVal = analogRead(sharpIR);
// The Buggy will execute the code inside the 'while' brackets
// as long as the IR sensor input isn't higher than the
// 'tooClose' value
// This original statement compared the pin number (0) to 'tooClose,'
// always returning true. This causes the buggy to spin in circles
// ad infinitum...
// while(sharpIR<tooClose){
// ------------------------------------------------------------------// Instead, we should compare the value read from A4 to 'tooClose.'
// Also, the Sharp IR module specified in the bill of materials
// reads higher as the distance closes, so we should be watching for
// when the value on A4 is greater than, not less than, 'tooClose.'
// This works:
while(irVal > tooClose) {
// The wheel direction pins are set according to the
// HUB-ee datasheet and the speed output is set to
// 200, pretty quick.
digitalWrite(leftIN1, HIGH);
digitalWrite(leftIN2, LOW);
analogWrite(leftPWM, 200);
// The wheel direction pins are set opposite on
// the right wheel because it needs to turn the
// same direction as the left and its mirrored.
digitalWrite(rightIN1, LOW);
Page 10 of 13
digitalWrite(rightIN2, HIGH);
analogWrite(rightPWM, 200);
// IMPORTANT:
// It is crucially important that you do not change the direction
// of the HUB-ee wheels too rapidly. I learned this $55 lesson
// the hard way.
// Let's put in a delay, otherwise we're reading from
// A4 as fast as this while loop can execute
delay(100);
// Read the value of the Sharp IR sensor
irVal = analogRead(sharpIR);
}
// When the IR sensor detects a close object, the code
// outside the brackets above will execute. In this
// case, we're reversing one of the wheels to make
// the Buggy turn. We'll run it this way for half a
// second (500 milliseconds) before returning to
// normal mode.
digitalWrite(leftIN1, LOW);
digitalWrite(leftIN2, HIGH);
delay(500);
}
Note: It’s best to give the wheels a small delay before switching directions. Doing so too quickly
may result in damaging the wheels.
HUB-ee Buggy Robot Boogie
And there you have it! Once you’ve uploaded your code, the HUB-ee Buggy should be ready to
take off across the floor knocking things over skillfully avoiding all obstacles. Check out the video
below for a demonstration!
Page 11 of 13
I hope you enjoyed this project! If you make a super-beefy version of the HUB-ee Buggy,send us a
pic, because we’d love to see your improvements.
If you’re having trouble getting your HUB-ee Buggy working, check out the Troubleshooting page of
this tutorial.
Troubleshooting
If your HUB-ee Buggy isn’t acting like it’s supposed to, there are a couple of easily solved problems
that might be responsible:
“It’s just spinning in circles”
When you put your hand in front of the sensor, does it start going straight or backwards? If this is
the case, then one of your motors is either wired backwards or running backwards, depending on
how you wanna look at it. No worries! Just go into the code and change the direction values for one
of your motors.
If it isn’t responding at all to sensor input, you might have the sensor threshold set too high or low.
Play with the threshold variable in your code. Also, make sure that your sensor is wired to the right
analog input.
“It doesn’t turn on”
Check your batteries, first and foremost. If you have a multimeter, check the battery pack output by
placing the positive probe inside the barrel connector and placing the ground probe against the
outside of the connector. You should see about 6VDC. If it’s lower than that, the Redboard’s
voltage regulator may be “dropping out” too much of the voltage to power the processor and the
motors.
Page 12 of 13
If you’ve confirmed that your batteries are okay but your board still won’t turn on, check for short
circuits. Anywhere where a power connection might be connected directly to a ground is gonna
mess up your ‘bot.
“It’s on, but not moving”
Double check your motor wiring. If that looks okay, then look at your code, and make sure it’s able
to execute up to the point where the wheels are supposed to move. You can check by placing a
digitalWrite(13, HIGH) right below the motor commands. That way, if the code makes it to that point,
the on-board LED will light up.
If that works but the motors still aren’t moving, check that you’ve declared the right motor pins in
your code.
“It tips over when it sees an obstacle”
Whoa there, Turbo. Sounds like the motors are cranking too hard. Change the analogWrite()
statements in the code to something lower. Or, if you’re really clever, add a new analogWrite()
statement before changing the wheel direction to make your Buggy slow down after it encounters
an obstacle.
Resources and Going Further
Now that you’ve entered the world of robotics, there’s no end to the fun you can have designing and
building robots. If you’d like more, check out these other great tutorials.
Getting Started with the RedBot
You could use a robotic claw and a pan/tilt bracket to design a robot with an arm to fetch items
for you.
You could create your own remote control usingvarious SparkFun parts.
And, last but not least, you can always give your robot a new look withdifferent types of robot
chassis.
learn.sparkfun.com | CC BY-SA 3.0 | SparkFun Electronics | Niwot, Colorado
Page 13 of 13