The Arduino Mothbot

The Arduino Mothbot
The purpose of this project is to design and build a simple light-following robot using an Arduino Duemilanove microcontroller board. I really wanted to share a robot project that was cheap, simple to build, and had a complete set of instructions for all of the different steps. I hope I've succeeded and I'd love to get comments about making this instructable even better.

The design of this robot focused around using the book "Getting Started with Arduino" by Massimo Banzi and published with [makezine.com Make]. I also employed code for running the servos from a project titled: How to Make an Arduino Controlled Servo Robot (SERB).

The Arduino Mothbot is in total a pretty quick robot to build. Assuming you start with all of the parts and don't have to improvise, the project in total should take maybe an hour to build. That is if you follow the instructions and copy the code. However, if you build only one feature at a time and test along the way then this project could take significantly longer. The advantage of the longer track is that you'll probably learn a lot more and have some fun along the way.
 
Remove these adsRemove these ads by Signing Up
 

Step 1Gather your Parts and Tools

Building this robot is going to cost you roughly $80 in parts if you've never done anything like this before. The cost for me was significantly less since I've got a lot of electronics lying around to work from. However, I know how frustrating it can be to try and follow an instructable without knowing which parts to get, where to order from, and how much everything will cost up front so I've done all that work for you. Once you've got the parts all squared away it should be a snap to do this project. Follow the following link to my project wiki to get a complete parts list.

Arduino Mothbot Parts List



Now you may want to get some tools. Since this project employs a solderless breadboard you can do without a lot of fancy electronics equipment. Hopefully you can find the rest of the things you need in a garage:

1. Needle nose pliers
2. Wire Cutters
3. Flat head screw driver
4. Small Phillips (4-sided) screw driver
5. Adjustable wrench or 11/32" hex wrench
6. Drill
7. 1/16", 5/32" and 7/32" drill bits
8. Saw (optional)
9. Safety Goggles

Please use safe practices when using any power tools.
« Previous StepDownload PDFView All StepsNext Step »
19 comments
Jan 16, 2012. 3:08 AMkerryavance says:
would it be possible to use two motors instead of continuous rotation servos?
Jan 30, 2010. 10:42 PMcprocjr says:
 Where is the code? All I can see is the first line of it.
Dec 9, 2011. 1:28 AMDavid97 says:
/*
* Arduino Mothbot
*
* Digital Pin Wiring:
* pin 7 - Pushbutton Signal
* pin 11 - Right Servo Signal
* pin 3 - Left Servo Signal
*
* Analog Pin Wiring:
* pin 0 - Left Light Sensor Signal
* pin 1 - Right Light Sensor Signal
*
* License: This work is licenced under the Creative Commons
* Attribution-Share Alike 3.0 Unported License. To
* view a copy of this licence, visit
* http://creativecommons.org/licenses/by-sa/3.0/
* or send a letter to Creative Commons, 171 Second
* Street, Suite 300, San Francisco, California 94105,
* USA.
*/

//------------------------------------------------------------------------
// START OF ARDUINO MOTHBOT SETUP

//--- Library
#include

//--- Pin Definitions
#define LEFTSERVOPIN 3 //The digital pin that the left servo is connected to
#define RIGHTSERVOPIN 11 //The digital pin that the right servo is connected to
#define LEFTLIGHTSENSOR 0 //The analog pin the left light sensor is connected to
#define RIGHTLIGHTSENSOR 1 //The analog pin the right light sensor is connected to
#define PUSHBUTTON 7 //The digital pin that the pushbutton sensor is connected to
#define LED 13 //The LED on the arduino

//--- Servo Setup
Servo leftServo;
Servo rightServo;

//--- Speed Setup
int robotSpeed = 75; //set the speed of the robot
int rightSpeed = 50;
int leftSpeed = 50;

//--- Sensor Values
int leftLightSensorVal = 0;
int rightLightSensorVal = 0;
int lightSensorThreshold = 200;

//--- On/Off values
int pushButtonVal = 0;
int old_pushButtonVal = 0;
int pushButtonState = 0;

//--- Delay Threshold
int delayParam = 10; //Supported Times - 0 - 255 (0 to 25.5 Seconds) value * 100 milliseconds

// END OF ARDUINO MOTHBOT SETUP
//------------------------------------------------------------------------

//------------------------------------------------------------------------
//START OF ARDUINO MOTHBOT PROGRAM

//--- The program setup
void setup()
{
Serial.begin(9600); //Starts the serial port
robotSetup(); //sets the state of all neccesary
//pins and adds servos to your sketch
}

//--- The main program code
void loop()
{
//--- Get information from the light sensor
leftLightSensorVal = analogRead(LEFTLIGHTSENSOR);
rightLightSensorVal = analogRead(RIGHTLIGHTSENSOR);

//--- Get information from the pushbutton sensor
pushButtonVal = digitalRead(PUSHBUTTON);

//--- Set the state of the pushbutton
if((pushButtonVal == HIGH) && (old_pushButtonVal == LOW))
{
Serial.println("Button Pushed");
pushButtonState = 1 - pushButtonState;
delay(10); // Simple de-bouncing
}

//--- Reset the state of the push button
old_pushButtonVal = pushButtonVal;

//--- If pushbutton state HIGH then turn on the robot and
// listen to the light sensor information
if(pushButtonState == 1)
{
digitalWrite(LED, HIGH); // turn LED ON

if(leftLightSensorVal - rightLightSensorVal >= lightSensorThreshold)
{
Serial.println("Left");
goLeft();
delay(delayParam * 100);
goStop();
}
else if(rightLightSensorVal - leftLightSensorVal >= lightSensorThreshold)
{
Serial.println("Right");
goRight();
delay(delayParam * 100);
goStop();
}
else
{
Serial.println("Forward");
goForward();
delay(delayParam * 100);
goStop();
}
}
else
{
Serial.println("Stop");
digitalWrite(LED, LOW); // turn LED OFF
goStop();
}
}

//END OF ARDUINO MOTHBOT PROGRAM
//------------------------------------------------------------------------

//------------------------------------------------------------------------
//START OF ARDUINO MOTHBOT FUNCTIONS

//--- The setup for the robot
void robotSetup()
{
//--- Set the speed of the robot
setSpeed(robotSpeed);

//--- Set up the servos
pinMode(LEFTSERVOPIN, OUTPUT); //sets the left servo signal pin
//to output
pinMode(RIGHTSERVOPIN, OUTPUT); //sets the right servo signal pin
//to output
leftServo.attach(LEFTSERVOPIN); //attaches left servo
rightServo.attach(RIGHTSERVOPIN); //attaches right servo

//--- Set up the pushbutton
pinMode(PUSHBUTTON, INPUT); //sets the pushbutton sensor pin to input

//--- Set up the LED to see robot state
pinMode(LED, OUTPUT); //sets the led pin as output

//--- Tell the robot to stop the servos
goStop();
}

//--- Set the speed of the robot between 0-(stopped) and 100-(full speed)
void setSpeed(int newSpeed)
{
setSpeedLeft(newSpeed); //sets left speed
setSpeedRight(newSpeed); //sets right speed
}

//--- Set the speed of the left wheel
void setSpeedLeft(int newSpeed)
{
if(newSpeed >= 100) {newSpeed = 100;} //if speed is greater than 100
//make it 100
if(newSpeed <= 0) {newSpeed = 0;} //if speed is less than 0 make
//it 0
leftSpeed = newSpeed * 0.9; //between 0 and 90
}

//--- Set the speed of the right wheel
void setSpeedRight(int newSpeed)
{
if(newSpeed >= 100) {newSpeed = 100;} //if speed is greater than 100
//make it 100
if(newSpeed <= 0) {newSpeed = 0;} //if speed is less than 0 make
//it 0
rightSpeed = newSpeed * 0.9; //scales the speed to be
}

//--- Move the robot forward
void goForward()
{
leftServo.write(90 + leftSpeed);
rightServo.write(90 - rightSpeed);
}

//--- Move the robot backward
void goBackward()
{
leftServo.write(90 - leftSpeed);
rightServo.write(90 + rightSpeed);
}

//--- Move the robot right
void goRight()
{
leftServo.write(90 + leftSpeed);
rightServo.write(90 + rightSpeed);
}

//--- Move the robot left
void goLeft()
{
leftServo.write(90 - leftSpeed);
rightServo.write(90 - rightSpeed);
}

//--- Stop the robot
void goStop()
{
leftServo.write(90);
rightServo.write(90);
}

//END OF ARDUINO MOTHBOT FUNCTIONS
//------------------------------------------------------------------------

Jan 10, 2012. 1:41 PMCello62 says:
In the first line of library there is a missing type needs:
#include Servo.h
May 18, 2011. 1:37 PMdremeldude says:
You probably could use the 5 volt line for the servos if you don't mind the servos rotating a little slower
Jun 18, 2011. 2:14 PMChowmix12 says:
It is good practice to use a different power supply for the motors than the logic supply. This way, there is reduced feedback from the motors, and sometimes, motors can cause brownouts on the Arduino, meaning it will reset.
Jul 3, 2011. 8:12 AMdremeldude says:
good to know
Jun 20, 2011. 10:18 AMmy not in use says:
wow
Mar 13, 2011. 12:38 PMdoncrush says:
Thanks for providing my first foray into build it yourself robotics! Looking forward to improving it with additional sensors/capabilities.
Oct 11, 2010. 6:47 AMrobodevil says:
you didnt show the connected power wire from battery pack to the solderless board..
Jan 1, 2010. 1:09 AMkinglevi says:
 Very good project I would like if you could show me the curcit digram.Planning a similar project .The digram would be very helpful.THANKS
Jun 15, 2009. 3:40 PMkyle.marsh says:
Great intro robot! Mayhaps I'll actually build the aquatic mothbot I dreamed up for my pool now....

The jerky motion of a robot that only moves straight or pivots tends to bug me (although it is a "moth" bot), so one possible extension of the mothbot is to make it turn, rather than pivot. To smooth out your mothbot, instead of making both wheels turn the same speed, try varying the speed based on the relative readings from the light sensors. For example:
--If the reading from the two sensors is the same (within a reasonable threshold) put both motors full ahead.
--If the reading from the right sensor is twice that of the left, set the left motor twice as fast as the right motor.
--Fiddle with the parameters until you get something you like. You may want to turn down your delay so you get faster updates and smoother performance.

Another possible extension that can help make your robot more robust to different environments is to add a potentiometer (variable resistor) to change the threshold you use on your light sensors on-the-go without having to reprogram your bot.

Thanks for the good project!
Jun 15, 2009. 3:39 PMPaulys says:
Nice job! Do you have any video of the robot in action?
Jun 14, 2009. 5:03 PMBongmaster says:
nicely documented :) the explanation of the light sensor code may help me with my bot (pretty much same thing but with bumper switches and h-bridges as well as light sensing :)
Jun 15, 2009. 9:26 AMBongmaster says:
will do ;)
Jun 15, 2009. 6:39 AMoomlout says:
Love the DIY Arduino robot base, and great Instructable. (Otis (our office SERB) is wondering if a play date can be scheduled) .:the oomlout team:.

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
48
Followers
7
Author:natantus(Chris Gilmer Project)
I'm a software and aerospace engineer. When I've got free time I like to work on robot projects and love to play with my Makerbot Cupcake and Eggbot. I would love to be involved in DIY prosthetics a...
more »