wikiHow is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, 80 people, some anonymous, worked to edit and improve it over time. Together, they cited 6 references. This article has been viewed 1,505,412 times.
Learn more...

Do you want to learn how to build your own robot? There are a lot different types of robots that you can make by yourself. Most of the people want to see a robot perform the simple tasks of moving from point A to point B. You can make a robot completely from analogue components or buy a starter kit from scratch! Building your own robot is a great way to teach yourself both electronics as well as computer programming.

Gather your components. In order to build a basic robot, you'll need several simple components. You can find most, if not all, of these components at your local electronics hobby shop, or at a number of online retailers. Some basic kits may include all of these components as well. This robot does not require any soldering:

Align the two servos on the end of the battery pack. This should be the end that the battery pack's wire is coming out of The servos should be touching bottoms, and the rotating mechanisms of each should be facing out the sides of the battery pack. It's important that the servos are properly aligned so that the wheels go straight. The wires for the servos should be coming off the back of the battery pack.

Affix the servos with your tape or glue.[2]XResearch source Make sure that they are solidly attached to the battery pack. The backs of the servos should be aligned flush with the back of the battery pack.

Affix the breadboard perpendicularly on the open space on the battery pack. It should hang over the front of the battery pack just a little bit, and will extend beyond each side. Make sure that it is securely fastened before proceeding. The "A" row should be closest to the servos.

Attach the Arduino microcontroller to the tops of the servos. If you attached the servos properly, there should be a flat space made by them touching. Stick the Arduino board onto this flat space so that the Arduino's USB and Power connectors are facing the back (away from the breadboard). The front of the Arduino should be just barely overlapping the breadboard.

Put the wheels on the servos. Firmly press the wheels onto the rotating mechanism of the servo. This may require a significant amount of force, as the wheels are designed to fit as tightly as possible for the best traction.

Attach the caster to the bottom of the breadboard. If you flip the chassis over, you should see a bit of breadboard extending past the battery pack. Attach the caster to this extended piece, using risers if necessary. The caster acts as the front wheel, allowing the robot to easily turn in any direction.[3]XResearch source

If you bought a kit, the caster may have come with a few risers that you can use to ensure the caster reaches the ground. i

Connect the servo cables to the headers, with the black cable on the left side (pins 1 and 6). This will connect the servos to the breadboard. Make sure the left servo is connected to the left header, and the right servo to the right header.

Attach the sensor to the front of the breadboard. It does not get plugged into the outer power rails on the breadboard, but instead into the first row of lettered pins (J). Make sure you place it in the exact center, with an equal number of pins available on each side.

Connect the red and black wires to the far-right red and blue pins on back of the breadboard. The black cable should be plugged into the blue rail pin at pin 30. The red cable should be plugged into the red rail pin at pin 30.

Download and extract the Arduino IDE. This is the Arduino development environment, and allows you to program instructions that you can then upload to your Arduino microcontroller. You can download it for free from arduino.cc/en/main/software. Unzip the downloaded file by double-clicking it and move the folder inside to an easy to access location. You won't be actually installing the program. Instead, you'll just run it from the extracted folder by double-clicking arduino.exe.[6]XResearch source

Right-click on the "Unknown device" in the "Other devices" section and select "Update Driver Software." If you don't see this option, click "Properties" instead, select the "Driver" tab, and then click "Update Driver."

Paste the following code to make your robot go straight. The code below will make your Arduino continuously move forward.

#include<Servo.h> // this adds the "Servo" library to the program// the following creates two servo objectsServoleftMotor;ServorightMotor;voidsetup(){leftMotor.attach(12);// if you accidentally switched up the pin numbers for your servos, you can swap the numbers hererightMotor.attach(13);}voidloop(){leftMotor.write(180);// with continuous rotation, 180 tells the servo to move at full speed "forward."rightMotor.write(0);// if both of these are at 180, the robot will go in a circle because the servos are flipped. "0" tells it to move full speed "backwards."}

Add the kill switch functionality. Add the following code to the "void loop()" section of your code to enable the kill switch, above the "write()" functions.

if(digitalRead(2)==HIGH)// this registers when the button is pressed on pin 2 of the Arduino{while(1){leftMotor.write(90);// "90" is neutral position for the servos, which tells them to stop turningrightMotor.write(90);}}

Upload and test your code. With the kill switch code added, you can upload and test the robot. It should continue to drive forward until you press the switch, at which point it will stop moving. The full code should look like this:

#include<Servo.h>// the following creates two servo objectsServoleftMotor;ServorightMotor;voidsetup(){leftMotor.attach(12);rightMotor.attach(13);}voidloop(){if(digitalRead(2)==HIGH){while(1){leftMotor.write(90);rightMotor.write(90);}}leftMotor.write(180);rightMotor.write(0);}

Advertisement

Part 6 of 6:

Example

1

Follow an example. The following code will use the sensor attached to the robot to make it turn to the left whenever it encounters an obstacle. See the comments in the code for details about what each part does. The code below is the entire program.

#include<Servo.h>ServoleftMotor;ServorightMotor;constintserialPeriod=250;// this limits output to the console to once every 1/4 secondunsignedlongtimeSerialDelay=0;constintloopPeriod=20;// this sets how often the sensor takes a reading to 20ms, which is a frequency of 50HzunsignedlongtimeLoopDelay=0;// this assigns the TRIG and ECHO functions to the pins on the Arduino. Make adjustments to the numbers here if you connected differentlyconstintultrasonic2TrigPin=8;constintultrasonic2EchoPin=9;intultrasonic2Distance;intultrasonic2Duration;// this defines the two possible states for the robot: driving forward or turning left#define DRIVE_FORWARD 0#define TURN_LEFT 1intstate=DRIVE_FORWARD;// 0 = drive forward (DEFAULT), 1 = turn leftvoidsetup(){Serial.begin(9600);// these sensor pin configurationspinMode(ultrasonic2TrigPin,OUTPUT);pinMode(ultrasonic2EchoPin,INPUT);// this assigns the motors to the Arduino pinsleftMotor.attach(12);rightMotor.attach(13);}voidloop(){if(digitalRead(2)==HIGH)// this detects the kill switch{while(1){leftMotor.write(90);rightMotor.write(90);}}debugOutput();// this prints debugging messages to the serial consoleif(millis()-timeLoopDelay>=loopPeriod){readUltrasonicSensors();// this instructs the sensor to read and store the measured distancesstateMachine();timeLoopDelay=millis();}}voidstateMachine(){if(state==DRIVE_FORWARD)// if no obstacles detected{if(ultrasonic2Distance>6||ultrasonic2Distance<0)// if there's nothing in front of the robot. ultrasonicDistance will be negative for some ultrasonics if there is no obstacle{// drive forwardrightMotor.write(180);leftMotor.write(0);}else// if there's an object in front of us{state=TURN_LEFT;}}elseif(state==TURN_LEFT)// if an obstacle is detected, turn left{unsignedlongtimeToTurnLeft=500;// it takes around .5 seconds to turn 90 degrees. You may need to adjust this if your wheels are a different size than the exampleunsignedlongturnStartTime=millis();// save the time that we started turningwhile((millis()-turnStartTime)<timeToTurnLeft)// stay in this loop until timeToTurnLeft has elapsed{// turn left, remember that when both are set to "180" it will turn.rightMotor.write(180);leftMotor.write(180);}state=DRIVE_FORWARD;}}voidreadUltrasonicSensors(){// this is for ultrasonic 2. You may need to change these commands if you use a different sensor.digitalWrite(ultrasonic2TrigPin,HIGH);delayMicroseconds(10);// keeps the trig pin high for at least 10 microsecondsdigitalWrite(ultrasonic2TrigPin,LOW);ultrasonic2Duration=pulseIn(ultrasonic2EchoPin,HIGH);ultrasonic2Distance=(ultrasonic2Duration/2)/29;}// the following is for debugging errors in the console.voiddebugOutput(){if((millis()-timeSerialDelay)>serialPeriod){Serial.print("ultrasonic2Distance: ");Serial.print(ultrasonic2Distance);Serial.print("cm");Serial.println();timeSerialDelay=millis();}}

About This Article

wikiHow is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, 80 people, some anonymous, worked to edit and improve it over time. Together, they cited 6 references. This article has been viewed 1,505,412 times.

To build a simple robot that can move on its own, purchase a starter kit, or assemble the components you need from an electronics supplier. You'll need a microcontroller, the small computer that will serve as your robot's brain; a pair of continuous rotation servos to drive the wheels; wheels that fit the servos; a caster roller; a small solderless breadboard for building your circuits; a battery holder; a distance sensor; a push button switch, and jumper wires. Affix the servos to the end of the battery pack with double-sided tape or hot glue, making sure the the rotating ends of the servos are on the long sides of the battery pack. Attach the breadboard to the battery pack next to the servos with the tape or hot glue. Place the microcontroller on the flat space on top of the servos and affix firmly there. Press the wheels firmly onto the spindles of the servos. Attach the caster to the front of the breadboard. The caster spins freely, and acts as the front wheel of the robot, making it easy to turn and roll in any direction. Plug the distance sensor to the front of your breadboard. Wire up your robot, connecting the servos, microcontroller, switch and battery pack to your breadboard. Connect your microcontroller to a computer via a USB cable. Upload a basic control program from your computer to the microcontroller. This robot can go forward, backward, stop, and turn away from obstacles. Test your robot on a smooth flat surface, and experiment to see what you can make it do. For more tips, including how to use Arduino software, read on!